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
Cleaned up asset query predicate arrays, fixed CCME in RulesEngine
Christian Bauer1cfe23070a69
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.javaindex 40dab9215..15d7f9ceb 100644--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java@@ -842,21 +842,23 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbinders.add(st -> st.setString(pos, query.type.prepareValue()));}- if (query.attributeMetaPredicate != null) {- String attributeMetaFilter = buildAttributeMetaFilter(query.attributeMetaPredicate, binders);-- if (attributeMetaFilter.length() > 0) {- sb.append(" and A.ID in (select A.ID from");- sb.append(" jsonb_each(A.ATTRIBUTES) as AX,");- sb.append(" jsonb_array_elements(AX.VALUE #> '{meta}') as AM");- sb.append(" where true");- sb.append(attributeMetaFilter);- sb.append(")");+ if (query.attributeMetaPredicates != null) {+ for (AbstractAssetQuery.AttributeMetaPredicate attributeMetaPredicate : query.attributeMetaPredicates) {+ String attributeMetaFilter = buildAttributeMetaFilter(attributeMetaPredicate, binders);++ if (attributeMetaFilter.length() > 0) {+ sb.append(" and A.ID in (select A.ID from");+ sb.append(" jsonb_each(A.ATTRIBUTES) as AX,");+ sb.append(" jsonb_array_elements(AX.VALUE #> '{meta}') as AM");+ sb.append(" where true");+ sb.append(attributeMetaFilter);+ sb.append(")");+ }}}- if (query.attributePredicateArray != null && query.attributePredicateArray.predicates != null) {- for (AssetQuery.AttributePredicate attributePredicate : query.attributePredicateArray.predicates) {+ if (query.attributePredicates != null) {+ for (AssetQuery.AttributePredicate attributePredicate : query.attributePredicates) {StringBuilder attributeFilterBuilder = new StringBuilder();attributeFilterBuilder.append(buildAttributeFilter(attributePredicate, binders));@@ -921,18 +923,18 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicprotected String buildAttributeFilter(AssetQuery.AttributePredicate attributePredicate, List<ParameterBinder> binders) {StringBuilder attributeBuilder = new StringBuilder();- if (attributePredicate.itemNamePredicate != null) {- attributeBuilder.append(attributePredicate.itemNamePredicate.caseSensitive+ if (attributePredicate.name != null) {+ attributeBuilder.append(attributePredicate.name.caseSensitive? " and AX.key": " and upper(AX.key)");- attributeBuilder.append(attributePredicate.itemNamePredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");+ attributeBuilder.append(attributePredicate.name.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");final int pos = binders.size() + 1;- binders.add(st -> st.setString(pos, attributePredicate.itemNamePredicate.prepareValue()));+ binders.add(st -> st.setString(pos, attributePredicate.name.prepareValue()));}- if (attributePredicate.itemValuePredicate != null) {- if (attributePredicate.itemValuePredicate instanceof AssetQuery.StringPredicate) {- AssetQuery.StringPredicate stringPredicate = (AssetQuery.StringPredicate) attributePredicate.itemValuePredicate;+ if (attributePredicate.value != null) {+ if (attributePredicate.value instanceof AssetQuery.StringPredicate) {+ AssetQuery.StringPredicate stringPredicate = (AssetQuery.StringPredicate) attributePredicate.value;attributeBuilder.append(stringPredicate.caseSensitive? " and AX.VALUE #>> '{value}'": " and upper(AX.VALUE #>> '{value}')"@@ -940,13 +942,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicattributeBuilder.append(stringPredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");final int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));- } else if (attributePredicate.itemValuePredicate instanceof AssetQuery.BooleanPredicate) {- AssetQuery.BooleanPredicate booleanPredicate = (AssetQuery.BooleanPredicate) attributePredicate.itemValuePredicate;+ } else if (attributePredicate.value instanceof AssetQuery.BooleanPredicate) {+ AssetQuery.BooleanPredicate booleanPredicate = (AssetQuery.BooleanPredicate) attributePredicate.value;attributeBuilder.append(" and AX.VALUE #> '{value}' = to_jsonb(").append(booleanPredicate.predicate).append(")");- } else if (attributePredicate.itemValuePredicate instanceof AssetQuery.StringArrayPredicate) {- AssetQuery.StringArrayPredicate stringArrayPredicate = (AssetQuery.StringArrayPredicate) attributePredicate.itemValuePredicate;+ } else if (attributePredicate.value instanceof AssetQuery.StringArrayPredicate) {+ AssetQuery.StringArrayPredicate stringArrayPredicate = (AssetQuery.StringArrayPredicate) attributePredicate.value;for (int i = 0; i < stringArrayPredicate.predicates.length; i++) {AssetQuery.StringPredicate stringPredicate = stringArrayPredicate.predicates[i];attributeBuilder.append(stringPredicate.caseSensitive@@ -957,8 +959,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicfinal int pos = binders.size() + 1;binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));}- } else if (attributePredicate.itemValuePredicate instanceof AssetQuery.DateTimePredicate) {- AssetQuery.DateTimePredicate dateTimePredicate = (AssetQuery.DateTimePredicate) attributePredicate.itemValuePredicate;+ } else if (attributePredicate.value instanceof AssetQuery.DateTimePredicate) {+ AssetQuery.DateTimePredicate dateTimePredicate = (AssetQuery.DateTimePredicate) attributePredicate.value;attributeBuilder.append(" and to_timestamp(AX.VALUE #>> '{value}', ?)");final int keyFormatPos = binders.size() + 1;binders.add(st -> st.setString(keyFormatPos, dateTimePredicate.dateFormat));@@ -992,8 +994,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbinders.add(st -> st.setString(formatPos2, dateTimePredicate.dateFormat));break;}- } else if (attributePredicate.itemValuePredicate instanceof AssetQuery.NumberPredicate) {- AssetQuery.NumberPredicate numberPredicate = (AssetQuery.NumberPredicate) attributePredicate.itemValuePredicate;+ } else if (attributePredicate.value instanceof AssetQuery.NumberPredicate) {+ AssetQuery.NumberPredicate numberPredicate = (AssetQuery.NumberPredicate) attributePredicate.value;attributeBuilder.append(" and (AX.VALUE #>> '{value}')::numeric");switch (numberPredicate.operatorMatch) {case EXACT:diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.javaindex 974e5532a..e70ae46f4 100644--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesEngine.java@@ -437,7 +437,7 @@ public class RulesEngine<T extends Ruleset> {// Insert initial asset statestry {- Set<AssetState> initialState = assetStates.keySet();+ Set<AssetState> initialState = new HashSet<>(assetStates.keySet());LOG.info("On " + this + ", inserting initial asset states: " + initialState.size());for (AssetState assetState : initialState) {insertAssetState(assetState);diff --git a/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java b/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.javaindex fc793219e..db70dc827 100644--- a/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java+++ b/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java@@ -475,6 +475,45 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {}}+ public static class AttributePredicate {+ public StringPredicate name;+ public ValuePredicate value;++ public AttributePredicate() {+ }++ public AttributePredicate(StringPredicate name) {+ this.name = name;+ }++ public AttributePredicate(ValuePredicate value) {+ this.value = value;+ }++ public AttributePredicate(StringPredicate name, ValuePredicate value) {+ this.name = name;+ this.value = value;+ }++ public AttributePredicate name(StringPredicate name) {+ this.name = name;+ return this;+ }++ public AttributePredicate value(ValuePredicate value) {+ this.value = value;+ return this;+ }++ @Override+ public String toString() {+ return getClass().getSimpleName() + "{" ++ "name=" + name ++ ", value=" + value ++ '}';+ }+ }+public static class AttributeMetaPredicate {public StringPredicate itemNamePredicate;public ValuePredicate itemValuePredicate;@@ -570,80 +609,6 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {}}- public static class AttributePredicate {- public StringPredicate itemNamePredicate;- public ValuePredicate itemValuePredicate;-- public AttributePredicate() {- }-- public AttributePredicate(StringPredicate itemNamePredicate) {- this.itemNamePredicate = itemNamePredicate;- }-- public AttributePredicate(MetaItemDescriptor metaItemDescriptor) {- this.itemNamePredicate = new StringPredicate(metaItemDescriptor.getUrn());- }-- public AttributePredicate(ValuePredicate itemValuePredicate) {- this.itemValuePredicate = itemValuePredicate;- }-- public AttributePredicate(StringPredicate itemNamePredicate, ValuePredicate itemValuePredicate) {- this.itemNamePredicate = itemNamePredicate;- this.itemValuePredicate = itemValuePredicate;- }-- public AttributePredicate(MetaItemDescriptor metaItemDescriptor, ValuePredicate itemValuePredicate) {- this(new StringPredicate(metaItemDescriptor.getUrn()), itemValuePredicate);- }-- public AttributePredicate itemName(StringPredicate itemNamePredicate) {- this.itemNamePredicate = itemNamePredicate;- return this;- }-- public AttributePredicate itemName(MetaItemDescriptor metaItemDescriptor) {- return itemName(new StringPredicate(metaItemDescriptor.getUrn()));- }-- public AttributePredicate itemValue(ValuePredicate itemValuePredicate) {- this.itemValuePredicate = itemValuePredicate;- return this;- }-- @Override- public String toString() {- return getClass().getSimpleName() + "{" +- "itemNamePredicate=" + itemNamePredicate +- ", itemValuePredicate=" + itemValuePredicate +- '}';- }- }-- public static class AttributePredicateArray {- public AttributePredicate[] predicates = new AttributePredicate[0];-- public AttributePredicateArray() {- }-- public AttributePredicateArray(AttributePredicate... predicates) {- this.predicates = predicates;- }-- public AttributePredicateArray predicates(AttributePredicate... predicates) {- this.predicates = predicates;- return this;- }-- @Override- public String toString() {- return getClass().getSimpleName() + "{" +- "predicates=" + Arrays.toString(predicates) +- '}';- }- }-public static class OrderBy {public enum Property {@@ -699,8 +664,8 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {public TenantPredicate tenantPredicate;public String userId;public StringPredicate type;- public AttributeMetaPredicate attributeMetaPredicate;- public AttributePredicateArray attributePredicateArray;+ public AttributePredicate[] attributePredicates;+ public AttributeMetaPredicate[] attributeMetaPredicates;// Orderingpublic OrderBy orderBy;@@ -768,13 +733,13 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {return type(new StringPredicate(assetType.getValue()));}- public CHILD attributeMeta(AttributeMetaPredicate attributeMetaPredicate) {- this.attributeMetaPredicate = attributeMetaPredicate;+ public CHILD attributes(AttributePredicate... attributePredicates) {+ this.attributePredicates = attributePredicates;return (CHILD) this;}- public CHILD attributes(AttributePredicateArray attributePredicateArray) {- this.attributePredicateArray = attributePredicateArray;+ public CHILD attributeMeta(AttributeMetaPredicate... attributeMetaPredicates) {+ this.attributeMetaPredicates = attributeMetaPredicates;return (CHILD) this;}@@ -794,8 +759,8 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {", tenantPredicate=" + tenantPredicate +", userId='" + userId + '\'' +", type=" + type +- ", attributeMetaPredicate=" + attributeMetaPredicate +- ", attributePredicateArray=" + attributePredicateArray ++ ", attributePredicates=" + Arrays.toString(attributePredicates) ++ ", attributeMetaPredicates=" + Arrays.toString(attributeMetaPredicates) +", orderBy=" + orderBy +'}';}diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovyindex 8d1994b12..fc5870f46 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy@@ -98,9 +98,9 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {when: "a user filtering query is executed that returns only IDs, names and attribute names and limits to protected attributes and meta"assets = assetStorageService.findAll(- new AssetQuery()- .select(new Select(Include.ONLY_ID_AND_NAME_AND_ATTRIBUTE_NAMES, RESTRICTED_READ))- .userId(keycloakDemoSetup.testuser3Id)+ new AssetQuery()+ .select(new Select(Include.ONLY_ID_AND_NAME_AND_ATTRIBUTE_NAMES, RESTRICTED_READ))+ .userId(keycloakDemoSetup.testuser3Id))then: "only the users assets should be retrieved"@@ -118,9 +118,9 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {when: "a query is executed that returns a protected attribute without any other meta items"assets = assetStorageService.findAll(- new AssetQuery()- .select(new Select(Include.ALL_EXCEPT_PATH, RESTRICTED_READ))- .id(managerDemoSetup.apartment2LivingroomId)+ new AssetQuery()+ .select(new Select(Include.ALL_EXCEPT_PATH, RESTRICTED_READ))+ .id(managerDemoSetup.apartment2LivingroomId))then: "only one asset should be retrieved"@@ -617,14 +617,14 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {!asset.getAttribute("motionSensor").isPresent()when: "a query is executed to select an asset with an attribute of a certain value"- AttributePredicate[] filter = [- [new StringPredicate("windowOpen"), new BooleanPredicate(false)],- [new StringPredicate("co2Level"), new NumberPredicate(340, OperatorMatch.GREATER_THEN, NumberType.INTEGER)]- ]asset = assetStorageService.find(- new AssetQuery()- .select(new Select(Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES))- .attributes(new AttributePredicateArray(filter)+ new AssetQuery().select(new Select(Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES)).attributes(+ new AttributePredicate(+ new StringPredicate("windowOpen"), new BooleanPredicate(false)+ ),+ new AttributePredicate(+ new StringPredicate("co2Level"), new NumberPredicate(340, OperatorMatch.GREATER_THEN, NumberType.INTEGER)+ )))