100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Asset query enhancements
Rich Turnerc069fc8beaea
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 78d01a31e..0dcc88a8a 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -952,7 +952,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (level == 1 && query.name != null) {sb.append(query.name.caseSensitive ? " and A.NAME " : " and upper(A.NAME)");- sb.append(buildMatchFilter(query.name.match));+ sb.append(buildMatchFilter(query.name));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, query.name.prepareValue()));}@@ -1007,7 +1007,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (query.type != null) {sb.append(query.type.caseSensitive ? " and A.ASSET_TYPE" : " and upper(A.ASSET_TYPE)");- sb.append(buildMatchFilter(query.type.match));+ sb.append(buildMatchFilter(query.type));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, query.type.prepareValue()));}@@ -1053,19 +1053,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AM.VALUE #>> '{name}'": " and upper(AM.VALUE #>> '{name}')");- switch (attributeMetaPredicate.itemNamePredicate.match) {- case EXACT:- attributeMetaBuilder.append(" = ? ");- break;- case NOT_EXACT:- attributeMetaBuilder.append(" <> ? ");- break;- case BEGIN:- case END:- case CONTAINS:- attributeMetaBuilder.append(" like ? ");- break;- }+ attributeMetaBuilder.append(buildMatchFilter(attributeMetaPredicate.itemNamePredicate));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, attributeMetaPredicate.itemNamePredicate.prepareValue()));}@@ -1076,7 +1064,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AM.VALUE #>> '{value}'": " and upper(AM.VALUE #>> '{value}')");- attributeMetaBuilder.append(buildMatchFilter(stringPredicate.match));+ attributeMetaBuilder.append(buildMatchFilter(stringPredicate));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));@@ -1093,7 +1081,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AM.VALUE #> '{value}' ->> " + i: " and upper(AM.VALUE #> '{value}' ->> " + i + ")");- attributeMetaBuilder.append(buildMatchFilter(stringPredicate.match));+ attributeMetaBuilder.append(buildMatchFilter(stringPredicate));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));}@@ -1111,7 +1099,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AX.key": " and upper(AX.key)");- attributeBuilder.append(buildMatchFilter(attributePredicate.name.match));+ attributeBuilder.append(buildMatchFilter(attributePredicate.name));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, attributePredicate.name.prepareValue()));@@ -1123,7 +1111,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AX.VALUE #>> '{value}'": " and upper(AX.VALUE #>> '{value}')");- attributeBuilder.append(buildMatchFilter(stringPredicate.match));+ attributeBuilder.append(buildMatchFilter(stringPredicate));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));} else if (attributePredicate.value instanceof BooleanPredicate) {@@ -1139,7 +1127,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic? " and AX.VALUE #> '{value}' ->> " + i: " and upper(AX.VALUE #> '{value}' ->> " + i + ")");- attributeBuilder.append(buildMatchFilter(stringPredicate.match));+ attributeBuilder.append(buildMatchFilter(stringPredicate));final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));}@@ -1151,59 +1139,16 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicfinal int pos = binders.size() + 1;binders.add(st -> st.setObject(pos, new java.sql.Timestamp(fromAndTo.key).toLocalDateTime()));+ attributeBuilder.append(buildOperatorFilter(dateTimePredicate.operator, dateTimePredicate.negate));- switch (dateTimePredicate.operator) {- case EQUALS:- attributeBuilder.append(" = ?");- break;- case NOT_EQUALS:- attributeBuilder.append(" <> ?");- break;- case GREATER_THAN:- attributeBuilder.append(" > ?");- break;- case GREATER_EQUALS:- attributeBuilder.append(" >= ?");- break;- case LESS_THAN:- attributeBuilder.append(" < ?");- break;- case LESS_EQUALS:- attributeBuilder.append(" <= ?");- break;- case BETWEEN:- final int pos2 = binders.size() + 1;- binders.add(st -> st.setObject(pos2, new java.sql.Timestamp(fromAndTo.value).toLocalDateTime()));- attributeBuilder.append(" BETWEEN ? AND ?");- break;+ if (dateTimePredicate.operator == Operator.BETWEEN) {+ final int pos2 = binders.size() + 1;+ binders.add(st -> st.setObject(pos2, new java.sql.Timestamp(fromAndTo.value).toLocalDateTime()));}} else if (attributePredicate.value instanceof NumberPredicate) {NumberPredicate numberPredicate = (NumberPredicate) attributePredicate.value;attributeBuilder.append(" and (AX.VALUE #>> '{value}')::numeric");- switch (numberPredicate.operator) {- case EQUALS:- default:- attributeBuilder.append(" = ?");- break;- case NOT_EQUALS:- attributeBuilder.append(" <> ?");- break;- case GREATER_THAN:- attributeBuilder.append(" > ?");- break;- case GREATER_EQUALS:- attributeBuilder.append(" >= ?");- break;- case LESS_THAN:- attributeBuilder.append(" < ?");- break;- case LESS_EQUALS:- attributeBuilder.append(" <= ?");- break;- case BETWEEN:- attributeBuilder.append(" BETWEEN ? AND ?");- break;- }+ attributeBuilder.append(buildOperatorFilter(numberPredicate.operator, numberPredicate.negate));final int pos = binders.size() + 1;switch (numberPredicate.numberType) {@@ -1232,6 +1177,46 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, keyPredicate.key));+ } else if (attributePredicate.value instanceof ArrayPredicate) {+ ArrayPredicate arrayPredicate = (ArrayPredicate) attributePredicate.value;+ attributeBuilder.append(" and 1=1");+ if (arrayPredicate.negated) {+ attributeBuilder.append(" and NOT(");+ }+ if (arrayPredicate.value != null) {+ if (arrayPredicate.index != null) {+ attributeBuilder.append(" and AX.VALUE -> ");+ attributeBuilder.append(arrayPredicate.index);+ } else {+ attributeBuilder.append(" and AX.VALUE");+ }+ attributeBuilder.append(" @> ?");+ final int pos = binders.size() + 1;+ PGobject pgJsonValue = new PGobject();+ pgJsonValue.setType("jsonb");+ try {+ pgJsonValue.setValue(arrayPredicate.value.toJson());+ } catch (SQLException e) {+ LOG.log(Level.SEVERE, "Failed to build SQL statement for array predicate", e);+ return "";+ }+ binders.add(st -> st.setObject(pos, pgJsonValue));+ }+ if (arrayPredicate.lengthEquals != null) {+ attributeBuilder.append("json_array_length(AX.VALUE) = ");+ attributeBuilder.append(arrayPredicate.lengthEquals);+ }+ if (arrayPredicate.lengthGreaterThan != null) {+ attributeBuilder.append("json_array_length(AX.VALUE) > ");+ attributeBuilder.append(arrayPredicate.lengthGreaterThan);+ }+ if (arrayPredicate.lengthLessThan != null) {+ attributeBuilder.append("json_array_length(AX.VALUE) < ");+ attributeBuilder.append(arrayPredicate.lengthLessThan);+ }+ if (arrayPredicate.negated) {+ attributeBuilder.append(")");+ }} else if (attributePredicate.value instanceof GeofencePredicate) {if (attributePredicate.value instanceof RadialGeofencePredicate) {RadialGeofencePredicate location = (RadialGeofencePredicate) attributePredicate.value;@@ -1267,17 +1252,56 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn attributeBuilder.toString();}- protected String buildMatchFilter(Match match) {- switch (match) {- case EXACT:+ protected String buildOperatorFilter(BaseAssetQuery.Operator operator, boolean negate) {+ switch (operator) {+ case EQUALS:+ if (negate) {+ return " <> ? ";+ }return " = ? ";- case NOT_EXACT:- return " <> ? ";+ case GREATER_THAN:+ if (negate) {+ return " <= ? ";+ }+ return " > ? ";+ case GREATER_EQUALS:+ if (negate) {+ return " < ? ";+ }+ return " >= ? ";+ case LESS_THAN:+ if (negate) {+ return " >= ? ";+ }+ return " < ? ";+ case LESS_EQUALS:+ if (negate) {+ return " > ? ";+ }+ return " <= ? ";+ case BETWEEN:+ if (negate) {+ return " NOT BETWEEN ? AND ? ";+ }+ return " BETWEEN ? AND ? ";+ }++ throw new IllegalArgumentException("Unsupported operator: " + operator);+ }++ protected String buildMatchFilter(StringPredicate predicate) {+ switch (predicate.match) {case BEGIN:case END:case CONTAINS:+ if (predicate.negate) {+ return " not like ? ";+ }return " like ? ";default:+ if (predicate.negate) {+ return " <> ? ";+ }return " = ? ";}}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex fa40dac79..0ce712c8b 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -22,8 +22,6 @@ package org.openremote.manager.rules;import com.vividsolutions.jts.geom.Coordinate;import com.vividsolutions.jts.geom.Envelope;import org.geotools.referencing.GeodeticCalculator;-import org.joda.time.format.DateTimeFormatter;-import org.joda.time.format.ISODateTimeFormat;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetStorageService;import org.openremote.model.attribute.Meta;@@ -34,10 +32,12 @@ import org.openremote.model.query.BaseAssetQuery.NumberType;import org.openremote.model.query.NewAssetQuery;import org.openremote.model.query.filter.*;import org.openremote.model.rules.AssetState;-import org.openremote.model.rules.json.RuleCondition;+import org.openremote.model.rules.json.LogicGroup;import org.openremote.model.rules.json.RuleOperator;import org.openremote.model.util.Pair;import org.openremote.model.util.TimeUtil;+import org.openremote.model.value.ArrayValue;+import org.openremote.model.value.ObjectValue;import org.openremote.model.value.Value;import org.openremote.model.value.Values;@@ -47,9 +47,6 @@ import java.util.function.Predicate;import java.util.function.Supplier;import java.util.stream.Collectors;-import static org.openremote.model.query.BaseAssetQuery.Operator.LESS_EQUALS;-import static org.openremote.model.query.BaseAssetQuery.Operator.LESS_THAN;-/*** Test an {@link AssetState} with a {@link BaseAssetQuery}.*/@@ -69,7 +66,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {this.query.names = query.name != null ? new StringPredicate[] {query.name} : null;this.query.parents = query.parent != null ? new ParentPredicate[] {query.parent} : null;this.query.paths = query.path != null ? new PathPredicate[] {query.path} : null;- this.query.attributes = query.attribute != null ? new RuleCondition<>(RuleOperator.AND, query.attribute, null) : null;+ this.query.attributes = query.attribute != null ? new LogicGroup<>(RuleOperator.AND, query.attribute, null) : null;this.query.types = query.type != null ? new StringPredicate[] {query.type} : null;this.query.userIds = query.userId != null ? new String[] {query.userId} : null;this.query.tenant = query.tenant;@@ -160,13 +157,59 @@ public class AssetQueryPredicate implements Predicate<AssetState> {switch (predicate.match) {case BEGIN:- return have.startsWith(shouldMatch);+ return predicate.negate != have.startsWith(shouldMatch);case END:- return have.endsWith(shouldMatch);+ return predicate.negate != have.endsWith(shouldMatch);case CONTAINS:- return have.contains(shouldMatch);+ return predicate.negate != have.contains(shouldMatch);+ }+ return predicate.negate != have.equals(shouldMatch);+ };+ }++ public static Predicate<ArrayValue> asPredicate(ArrayPredicate predicate) {+ return arrayValue -> {+ if (arrayValue == null) {+ return false;+ }++ boolean result = true;++ if (predicate.value != null) {+ if (predicate.index != null) {+ result = arrayValue.length() >= predicate.index && Objects.equals(arrayValue.get(predicate.index).orElse(null), predicate.value);+ } else {+ result = arrayValue.stream().anyMatch(av -> Objects.equals(av, predicate.value));+ }}- return have.equals(shouldMatch);++ if (result && predicate.lengthEquals != null) {+ result = arrayValue.length() == predicate.lengthEquals;+ }+ if (result && predicate.lengthGreaterThan != null) {+ result = arrayValue.length() > predicate.lengthGreaterThan;+ }+ if (result && predicate.lengthEquals != null) {+ result = arrayValue.length() < predicate.lengthLessThan;+ }+ if (predicate.negated) {+ return !result;+ }+ return result;+ };+ }++ public static Predicate<ObjectValue> asPredicate(ObjectValueKeyPredicate predicate) {+ return objectValue -> {+ if (objectValue == null) {+ return false;+ }++ boolean result = objectValue.hasKey(predicate.key);+ if (predicate.negated) {+ return !result;+ }+ return result;};}@@ -209,21 +252,37 @@ public class AssetQueryPredicate implements Predicate<AssetState> {switch (predicate.operator) {case EQUALS:+ if (predicate.negate) {+ return timestamp != fromAndTo.key.longValue();+ }return timestamp == fromAndTo.key.longValue();- case NOT_EQUALS:- return timestamp != fromAndTo.key.longValue();case GREATER_THAN:+ if (predicate.negate) {+ return timestamp <= fromAndTo.key;+ }return timestamp > fromAndTo.key;case GREATER_EQUALS:+ if (predicate.negate) {+ return timestamp < fromAndTo.key;+ }return timestamp >= fromAndTo.key;case LESS_THAN:+ if (predicate.negate) {+ return timestamp >= fromAndTo.key;+ }return timestamp < fromAndTo.key;case LESS_EQUALS:+ if (predicate.negate) {+ return timestamp > fromAndTo.key;+ }return timestamp <= fromAndTo.key;case BETWEEN:if (fromAndTo.value == null) {throw new IllegalArgumentException("Date time predicate 'rangeValue' is not valid: " + predicate);}+ if (predicate.negate) {+ return !(timestamp > fromAndTo.key && timestamp < fromAndTo.value);+ }return timestamp > fromAndTo.key && timestamp < fromAndTo.value;}@@ -235,31 +294,49 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return d -> {if (d == null) {- // If given a null and we want to know if it's "less than x", it's always less than x- // TODO Should be consistent with BETWEEN behavior?- if (predicate.operator == LESS_THAN || predicate.operator == LESS_EQUALS) {- return true;- }+ // RT: Commented this out as shouldn't treat null like a value all operators should fail+// // If given a null and we want to know if it's "less than x", it's always less than x+// // TODO Should be consistent with BETWEEN behavior?+// if (predicate.operator == LESS_THAN || predicate.operator == LESS_EQUALS) {+// return true;+// }return false;}Number leftOperand = predicate.numberType == NumberType.DOUBLE ? d : d.intValue();Number rightOperand = predicate.numberType == NumberType.DOUBLE ? predicate.value : (int) predicate.value;+switch (predicate.operator) {case EQUALS:+ if (predicate.negate) {+ return !leftOperand.equals(rightOperand);+ }return leftOperand.equals(rightOperand);- case NOT_EQUALS:- return !leftOperand.equals(rightOperand);case BETWEEN:+ if (predicate.negate) {+ return !(leftOperand.doubleValue() >= rightOperand.doubleValue() && leftOperand.doubleValue() <= predicate.rangeValue);+ }return leftOperand.doubleValue() >= rightOperand.doubleValue() && leftOperand.doubleValue() <= predicate.rangeValue;case LESS_THAN:+ if (predicate.negate) {+ return leftOperand.doubleValue() >= rightOperand.doubleValue();+ }return leftOperand.doubleValue() < rightOperand.doubleValue();case LESS_EQUALS:+ if (predicate.negate) {+ return leftOperand.doubleValue() > rightOperand.doubleValue();+ }return leftOperand.doubleValue() <= rightOperand.doubleValue();case GREATER_THAN:+ if (predicate.negate) {+ return leftOperand.doubleValue() <= rightOperand.doubleValue();+ }return leftOperand.doubleValue() > rightOperand.doubleValue();case GREATER_EQUALS:+ if (predicate.negate) {+ return leftOperand.doubleValue() < rightOperand.doubleValue();+ }return leftOperand.doubleValue() >= rightOperand.doubleValue();}return false;@@ -400,6 +477,18 @@ public class AssetQueryPredicate implements Predicate<AssetState> {.flatMap(GeoJSONPoint::fromValue).map(point -> new Coordinate(point.getY(), point.getX())).orElse(null));+ } else if (predicate instanceof ObjectValueKeyPredicate) {++ ObjectValueKeyPredicate p = (ObjectValueKeyPredicate) predicate;+ return asPredicate(p).test(Optional.ofNullable(value)+ .flatMap(Values::getObject)+ .orElse(null));+ } else if (predicate instanceof ArrayPredicate) {++ ArrayPredicate p = (ArrayPredicate) predicate;+ return asPredicate(p).test(Optional.ofNullable(value)+ .flatMap(Values::getArray)+ .orElse(null));} else {// TODO Implement morethrow new UnsupportedOperationException(@@ -428,7 +517,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return meta -> meta.stream().anyMatch(metaItemPredicate);}- public static Predicate<AssetState> asPredicate(Supplier<Long> currentMillisProducer, RuleCondition<AttributePredicate> condition) {+ public static Predicate<AssetState> asPredicate(Supplier<Long> currentMillisProducer, LogicGroup<AttributePredicate> condition) {if (conditionIsEmpty(condition)) {return as -> true;}@@ -437,9 +526,9 @@ public class AssetQueryPredicate implements Predicate<AssetState> {List<Predicate<AssetState>> assetStatePredicates = new ArrayList<>();- if (condition.predicates != null && condition.predicates.length > 0) {+ if (condition.items != null && condition.items.length > 0) {assetStatePredicates.addAll(- Arrays.stream(condition.predicates)+ Arrays.stream(condition.items).map(p -> {if (p instanceof NewAttributePredicate) {return asPredicate(currentMillisProducer, (NewAttributePredicate)p);@@ -449,9 +538,9 @@ public class AssetQueryPredicate implements Predicate<AssetState> {);}- if (condition.conditions != null && condition.conditions.length > 0) {+ if (condition.groups != null && condition.groups.length > 0) {assetStatePredicates.addAll(- Arrays.stream(condition.conditions)+ Arrays.stream(condition.groups).map(c -> asPredicate(currentMillisProducer, c)).collect(Collectors.toList()));}@@ -459,9 +548,9 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return asPredicate(assetStatePredicates, operator);}- protected static boolean conditionIsEmpty(RuleCondition condition) {- return (condition.predicates == null || condition.predicates.length == 0)- && (condition.conditions == null || condition.conditions.length == 0);+ protected static boolean conditionIsEmpty(LogicGroup condition) {+ return (condition.items == null || condition.items.length == 0)+ && (condition.groups == null || condition.groups.length == 0);}protected static <T> Predicate<T> asPredicate(Collection<Predicate<T>> predicates, RuleOperator operator) {diff --git a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.javaindex d7ef6cf1d..d61dc8319 100644--- a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java@@ -55,7 +55,6 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {*/public enum Match {EXACT,- NOT_EXACT,BEGIN,END,CONTAINS;@@ -80,7 +79,6 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {*/public enum Operator {EQUALS,- NOT_EQUALS,GREATER_THAN,GREATER_EQUALS,LESS_THAN,diff --git a/model/src/main/java/org/openremote/model/query/NewAssetQuery.java b/model/src/main/java/org/openremote/model/query/NewAssetQuery.javaindex d6fda5fb8..00ad4f149 100644--- a/model/src/main/java/org/openremote/model/query/NewAssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/NewAssetQuery.java@@ -21,7 +21,8 @@ package org.openremote.model.query;import org.openremote.model.query.filter.*;import org.openremote.model.rules.json.RuleActionTarget;-import org.openremote.model.rules.json.RuleCondition;+import org.openremote.model.rules.json.LogicGroup;+import org.openremote.model.rules.json.RuleConditionReset;/*** <p>@@ -29,7 +30,7 @@ import org.openremote.model.rules.json.RuleCondition;* if after applying the filtering one or more {@link org.openremote.model.rules.AssetState}s are returned.* <p>* The {@link org.openremote.model.asset.Asset}s associated with the returned {@link org.openremote.model.rules.AssetState}s- * are then used as the context for any {@link org.openremote.model.rules.json.RuleTriggerReset} to control when the+ * are then used as the context for any {@link RuleConditionReset} to control when the* {@link NewAssetQuery} is allowed to be applied again to the same asset. They can also optionally be used as context* in the {@link RuleActionTarget}, to apply an action to the matched* {@link org.openremote.model.asset.Asset}s.@@ -47,7 +48,7 @@ public class NewAssetQuery {public TenantPredicate tenant;public String[] userIds;public StringPredicate[] types;- public RuleCondition<AttributePredicate> attributes;+ public LogicGroup<AttributePredicate> attributes;/*** To be used in combination with {@link #limit} to control the ordering of {@link org.openremote.model.asset.Asset}sdiff --git a/model/src/main/java/org/openremote/model/query/filter/ArrayPredicate.java b/model/src/main/java/org/openremote/model/query/filter/ArrayPredicate.javanew file mode 100644index 000000000..20d8d9755--- /dev/null+++ b/model/src/main/java/org/openremote/model/query/filter/ArrayPredicate.java@@ -0,0 +1,115 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.query.filter;++import com.fasterxml.jackson.annotation.JsonCreator;+import com.fasterxml.jackson.annotation.JsonProperty;+import org.openremote.model.value.ObjectValue;+import org.openremote.model.value.Value;+import org.openremote.model.value.Values;++/**+ * Determines if the value is an array and meets the following:+ * <ul>+ * <li>{@link #value} - Does array contain this value</li>+ * <li>{@link #index} - If specified will check that the value at this index equals the specified {@link #value}</li>+ * <li>{@link #lengthEquals} - Is length of array equal to the specified value</li>+ * <li>{@link #lengthGreaterThan} - Is length of array greater than the specified value</li>+ * <li>{@link #lengthLessThan} - Is length of array less than the specified value</li>+ * </ul>+ * There is an implicit 'and' between each specified condition; the negation is applied once all the criteria are+ * evaluated.+ */+public class ArrayPredicate implements ValuePredicate {++ public static final String name = "array";+ public boolean negated;+ public Value value;+ public Integer index;+ public Integer lengthEquals;+ public Integer lengthGreaterThan;+ public Integer lengthLessThan;++ @JsonCreator+ public ArrayPredicate(@JsonProperty("value") Value value,+ @JsonProperty("index") Integer index,+ @JsonProperty("lengthEquals") Integer lengthEquals,+ @JsonProperty("lengthGreaterThan") Integer lengthGreaterThan,+ @JsonProperty("lengthLessThan") Integer lengthLessThan,+ @JsonProperty("negated") boolean negate) {+ this.value = value;+ this.index = index;+ this.lengthEquals = lengthEquals;+ this.lengthGreaterThan = lengthGreaterThan;+ this.lengthLessThan = lengthLessThan;+ negated = negate;+ }++ public ArrayPredicate negate() {+ negated = !negated;+ return this;+ }++ public ArrayPredicate value(Value value) {+ this.value = value;+ return this;+ }++ public ArrayPredicate index(int index) {+ this.index = index;+ return this;+ }++ public ArrayPredicate lengthEquals(int lengthEquals) {+ this.lengthEquals = lengthEquals;+ return this;+ }++ public ArrayPredicate lengthGreaterThan(int lengthGreaterThan) {+ this.lengthGreaterThan = lengthGreaterThan;+ return this;+ }++ public ArrayPredicate lengthLessThan(int lengthLessThan) {+ this.lengthLessThan = lengthLessThan;+ return this;+ }++ @Override+ public ObjectValue toModelValue() {+ ObjectValue objectValue = Values.createObject();+ objectValue.put("predicateType", name);+ objectValue.put("negated", Values.create(negated));+ objectValue.put("value", value);+ if (index != null) {+ objectValue.put("index", Values.create(index));+ }+ if (lengthEquals != null) {+ objectValue.put("lengthEquals", Values.create(lengthEquals));+ }+ if (lengthGreaterThan != null) {+ objectValue.put("lengthGreaterThan", Values.create(lengthGreaterThan));+ }+ if (lengthLessThan != null) {+ objectValue.put("lengthLessTHan", Values.create(lengthLessThan));+ }+ return objectValue;+ }+}diff --git a/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java b/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.javaindex 0ea2d04bf..8429c71da 100644--- a/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java@@ -34,6 +34,7 @@ public class DateTimePredicate implements ValuePredicate {public String value; // Sliding window value e.g. 1h or fixed date timepublic String rangeValue; // Sliding window value e.g. 1h or fixed date time (used as upper bound when Operator.BETWEEN)public BaseAssetQuery.Operator operator = BaseAssetQuery.Operator.EQUALS;+ public boolean negate;public DateTimePredicate() {}@@ -61,6 +62,9 @@ public class DateTimePredicate implements ValuePredicate {objectValue.getString("operator").ifPresent(operator -> {dateTimePredicate.operator = BaseAssetQuery.Operator.valueOf(operator);});+ objectValue.getBoolean("negate").ifPresent(negate -> {+ dateTimePredicate.negate = negate;+ });return dateTimePredicate;}@@ -80,11 +84,17 @@ public class DateTimePredicate implements ValuePredicate {return this;}+ public DateTimePredicate negate(boolean negate) {+ this.negate = negate;+ return this;+ }+public ObjectValue toModelValue() {ObjectValue objectValue = Values.createObject();objectValue.put("predicateType", name);objectValue.put("value", Values.create(value));objectValue.put("rangeValue", Values.create(rangeValue));+ objectValue.put("negate", Values.create(negate));objectValue.put("operator", Values.create(operator.toString()));return objectValue;}@@ -95,6 +105,7 @@ public class DateTimePredicate implements ValuePredicate {"value='" + value + '\'' +", rangeValue='" + rangeValue + '\'' +", operator =" + operator ++ ", negate =" + negate +'}';}}diff --git a/model/src/main/java/org/openremote/model/query/filter/GeofencePredicate.java b/model/src/main/java/org/openremote/model/query/filter/GeofencePredicate.javaindex cd04b4fc1..720bca82f 100644--- a/model/src/main/java/org/openremote/model/query/filter/GeofencePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/GeofencePredicate.java@@ -19,11 +19,14 @@*/package org.openremote.model.query.filter;+import com.fasterxml.jackson.annotation.JsonIgnore;+public abstract class GeofencePredicate implements ValuePredicate {public boolean negated;// TODO: switch return type to location object+ @JsonIgnorepublic abstract double[] getCentrePoint();public GeofencePredicate negate() {diff --git a/model/src/main/java/org/openremote/model/query/filter/NumberPredicate.java b/model/src/main/java/org/openremote/model/query/filter/NumberPredicate.javaindex 55990e580..aea3d9025 100644--- a/model/src/main/java/org/openremote/model/query/filter/NumberPredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/NumberPredicate.java@@ -30,6 +30,7 @@ public class NumberPredicate implements ValuePredicate {public double rangeValue; // Used as upper bound when Operator.BETWEENpublic BaseAssetQuery.Operator operator = BaseAssetQuery.Operator.EQUALS;public BaseAssetQuery.NumberType numberType = BaseAssetQuery.NumberType.DOUBLE;+ public boolean negate;public NumberPredicate() {}@@ -62,6 +63,9 @@ public class NumberPredicate implements ValuePredicate {objectValue.getNumber("rangeValue").ifPresent(rangeValue -> {numberPredicate.rangeValue = rangeValue;});+ objectValue.getBoolean("negate").ifPresent(negate -> {+ numberPredicate.negate = negate;+ });objectValue.getString("operator").ifPresent(operator -> {numberPredicate.operator = BaseAssetQuery.Operator.valueOf(operator);});@@ -89,11 +93,17 @@ public class NumberPredicate implements ValuePredicate {return this;}+ public NumberPredicate negate(boolean negate) {+ this.negate = negate;+ return this;+ }+public ObjectValue toModelValue() {ObjectValue objectValue = Values.createObject();objectValue.put("predicateType", name);objectValue.put("value", Values.create(value));objectValue.put("rangeValue", Values.create(rangeValue));+ objectValue.put("negate", Values.create(negate));objectValue.put("operator", Values.create(operator.toString()));return objectValue;}@@ -105,6 +115,7 @@ public class NumberPredicate implements ValuePredicate {", rangeValue=" + rangeValue +", operator=" + operator +", numberType=" + numberType ++ ", negate=" + negate +'}';}}diff --git a/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java b/model/src/main/java/org/openremote/model/query/filter/StringPredicate.javaindex d1791b005..08c0db348 100644--- a/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java@@ -31,6 +31,7 @@ public class StringPredicate implements ValuePredicate {public BaseAssetQuery.Match match = BaseAssetQuery.Match.EXACT;public boolean caseSensitive = true;public String value;+ public boolean negate;public StringPredicate() {}@@ -58,6 +59,9 @@ public class StringPredicate implements ValuePredicate {objectValue.getBoolean("caseSensitive").ifPresent(caseSensitive -> {stringPredicate.caseSensitive = caseSensitive;});+ objectValue.getBoolean("negate").ifPresent(negate -> {+ stringPredicate.negate = negate;+ });objectValue.getString("value").ifPresent(value -> {stringPredicate.value = value;});@@ -79,6 +83,11 @@ public class StringPredicate implements ValuePredicate {return this;}+ public StringPredicate negate(boolean negate) {+ this.negate = negate;+ return this;+ }+public String prepareValue() {String s = match.prepare(this.value);if (!caseSensitive)@@ -91,6 +100,7 @@ public class StringPredicate implements ValuePredicate {objectValue.put("predicateType", name);objectValue.put("match", Values.create(match.toString()));objectValue.put("caseSensitive", Values.create(caseSensitive));+ objectValue.put("negate", Values.create(negate));objectValue.put("value", Values.create(value));return objectValue;}@@ -100,6 +110,7 @@ public class StringPredicate implements ValuePredicate {return getClass().getSimpleName() + "{" +"match=" + match +", caseSensitive=" + caseSensitive ++ ", negate=" + negate +", value='" + value + '\'' +'}';}diff --git a/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java b/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.javaindex 922086fda..d921d707d 100644--- a/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java@@ -32,6 +32,7 @@ import org.openremote.model.value.ObjectValue;@JsonSubTypes.Type(value = RadialGeofencePredicate.class, name = RadialGeofencePredicate.name),@JsonSubTypes.Type(value = RectangularGeofencePredicate.class, name = RectangularGeofencePredicate.name),@JsonSubTypes.Type(value = ObjectValueKeyPredicate.class, name = ObjectValueKeyPredicate.name),+ @JsonSubTypes.Type(value = ArrayPredicate.class, name = ArrayPredicate.name),@JsonSubTypes.Type(value = ValueEmptyPredicate.class, name = ValueEmptyPredicate.name),@JsonSubTypes.Type(value = ValueNotEmptyPredicate.class, name = ValueNotEmptyPredicate.name)})