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
Implemented more asset queries
Christian Bauerf4ab021a9066
diff --git a/container/src/main/java/org/openremote/container/util/Util.java b/container/src/main/java/org/openremote/container/util/Util.javaindex 959c9a87c..db2c090ea 100644--- a/container/src/main/java/org/openremote/container/util/Util.java+++ b/container/src/main/java/org/openremote/container/util/Util.java@@ -5,6 +5,9 @@ import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;+/**+ * TODO Old functions for old rules, can be deleted?+ */public class Util {public static <T> T[] reverseArray(T[] array, Class<T> clazz) {if (array == null) {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 d393a9ebe..bc27cf9c8 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@@ -224,6 +224,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpdasb.append(buildFromString(query));Pair<String, List<ParameterBinder>> whereClause = buildWhereClause(query);sb.append(whereClause.key);+ sb.append(buildOrderByString(query));return em.unwrap(Session.class).doReturningWork(new AbstractReturningWork<List<ServerAsset>>() {@Overridepublic List<ServerAsset> execute(Connection connection) throws SQLException {@@ -290,6 +291,39 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpdareturn sb.toString();}+ protected String buildOrderByString(AssetQuery query) {+ StringBuilder sb = new StringBuilder();++ if (query.id != null) {+ return sb.toString();+ }++ if (query.orderBy != null && query.orderBy.property != null) {+ sb.append(" order by " ) ;++ switch (query.orderBy.property) {+ case CREATED_ON:+ sb.append(" A.CREATED_ON ");+ break;+ case ASSET_TYPE:+ sb.append(" A.ASSET_TYPE ");+ break;+ case NAME:+ sb.append(" A.NAME ");+ break;+ case PARENT_ID:+ sb.append(" A.PARENT_ID ");+ break;+ case REALM_ID:+ sb.append(" A.REALM_ID");+ break;+ }+ sb.append(query.orderBy.descending ? " desc" : " asc ");+ }++ return sb.toString();+ }+protected Pair<String, List<ParameterBinder>> buildWhereClause(AssetQuery query) {StringBuilder sb = new StringBuilder();List<ParameterBinder> binders = new ArrayList<>();@@ -326,6 +360,12 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda}}+ if (query.pathPredicate != null && query.pathPredicate.hasPath()) {+ sb.append(" and ? <@ get_asset_tree_path(A.ID)");+ final int pos = binders.size() + 1;+ binders.add(st -> st.setArray(pos, st.getConnection().createArrayOf("text", query.pathPredicate.path)));+ }+if (query.tenantPredicate != null && query.tenantPredicate.realmId != null) {sb.append(" and R.ID = ? ");final int pos = binders.size() + 1;@@ -390,7 +430,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda}if (attributeMetaBuilder.length() > 0) {- sb.append(" and A.ID in (select distinct A.ID from ");+ 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 ");diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovyindex fd8d6fbc9..f39669c2e 100644--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy@@ -13,6 +13,7 @@ import org.openremote.model.asset.AssetTypeimport static org.openremote.model.asset.AssetType.THINGimport static org.openremote.model.asset.AssetQuery.*+import static org.openremote.model.asset.AssetQuery.OrderBy.Property.*class AssetQueryTest extends Specification implements ManagerContainerTrait {@@ -158,6 +159,20 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {assets.get(1).id == managerDemoSetup.apartment2Idassets.get(2).id == managerDemoSetup.apartment3Id+ when: "a query is executed"+ assets = assetStorageService.findAll(+ new AssetQuery()+ .parent(new ParentPredicate(managerDemoSetup.smartHomeId))+ .tenant(new TenantPredicate(keycloakDemoSetup.customerATenant.id))+ .orderBy(new OrderBy(NAME, true))+ )++ then: "result should match"+ assets.size() == 3+ assets.get(0).id == managerDemoSetup.apartment3Id+ assets.get(1).id == managerDemoSetup.apartment2Id+ assets.get(2).id == managerDemoSetup.apartment1Id+when: "a query is executed"assets = assetStorageService.findAll(new AssetQuery()@@ -168,6 +183,29 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {then: "result should match"assets.size() == 0+ when: "a query is executed"+ assets = assetStorageService.findAll(+ new AssetQuery()+ .path(new PathPredicate(assetStorageService.find(managerDemoSetup.apartment1Id, true).getPath()))+ )++ then: "result should match"+ assets.size() == 3+ assets.get(0).id == managerDemoSetup.apartment1Id+ assets.get(1).id == managerDemoSetup.apartment1LivingroomId+ assets.get(2).id == managerDemoSetup.apartment1LivingroomThermostatId++ when: "a query is executed"+ assets = assetStorageService.findAll(+ new AssetQuery()+ .path(new PathPredicate(assetStorageService.find(managerDemoSetup.apartment1Id, true).getPath()))+ .type(AssetType.ROOM)+ )++ then: "result should match"+ assets.size() == 1+ assets.get(0).id == managerDemoSetup.apartment1LivingroomId+when: "a query is executed"assets = assetStorageService.findAll(new AssetQuery().select(new Select(true, true)).userId(keycloakDemoSetup.testuser3Id)diff --git a/model/src/main/java/org/openremote/model/asset/AssetQuery.java b/model/src/main/java/org/openremote/model/asset/AssetQuery.javaindex 5c904d8ce..80adc98b2 100644--- a/model/src/main/java/org/openremote/model/asset/AssetQuery.java+++ b/model/src/main/java/org/openremote/model/asset/AssetQuery.java@@ -193,6 +193,26 @@ public class AssetQuery {}}+ static public class PathPredicate {+ public String[] path;++ public PathPredicate() {+ }++ public PathPredicate(String[] path) {+ this.path = path;+ }++ public PathPredicate path(String[] path) {+ this.path = path;+ return this;+ }++ public boolean hasPath() {+ return path != null && path.length > 0;+ }+ }+static public class TenantPredicate {public String realmId;public String realm;@@ -270,18 +290,18 @@ public class AssetQuery {public AttributeRefPredicate(String entityId, String attributeName) {super(new StringArrayPredicate(- new StringPredicate(entityId),- new StringPredicate(attributeName)- ));+ new StringPredicate(entityId),+ new StringPredicate(attributeName)+ ));}public AttributeRefPredicate(String name, String entityId, String attributeName) {super(new StringPredicate(name),new StringArrayPredicate(- new StringPredicate(entityId),- new StringPredicate(attributeName)- ));+ new StringPredicate(entityId),+ new StringPredicate(attributeName)+ ));}public AttributeRefPredicate(AssetMeta assetMeta, String entityId, String attributeName) {@@ -292,9 +312,9 @@ public class AssetQuery {super(name,new StringArrayPredicate(- new StringPredicate(attributeRef.getEntityId()),- new StringPredicate(attributeRef.getAttributeName())- ));+ new StringPredicate(attributeRef.getEntityId()),+ new StringPredicate(attributeRef.getAttributeName())+ ));}public AttributeRefPredicate(AssetMeta assetMeta, AttributeRef attributeRef) {@@ -303,22 +323,31 @@ public class AssetQuery {}static public class OrderBy {- public String property;++ public enum Property {+ CREATED_ON,+ NAME,+ ASSET_TYPE,+ PARENT_ID,+ REALM_ID+ }++ public Property property;public boolean descending;public OrderBy() {}- public OrderBy(String property) {+ public OrderBy(Property property) {this.property = property;}- public OrderBy(String property, boolean descending) {+ public OrderBy(Property property, boolean descending) {this.property = property;this.descending = descending;}- public OrderBy property(String property) {+ public OrderBy property(Property property) {this.property = property;return this;}@@ -336,14 +365,14 @@ public class AssetQuery {public String id;public StringPredicate name;public ParentPredicate parentPredicate;- // TODO Implement descendants restriction+ public PathPredicate pathPredicate;public TenantPredicate tenantPredicate;public String userId;public StringPredicate type;public AttributeMetaPredicate attributeMetaPredicate;// Ordering- public OrderBy orderBy = new OrderBy("createdOn");+ public OrderBy orderBy = new OrderBy(OrderBy.Property.CREATED_ON);public AssetQuery() {}@@ -371,6 +400,11 @@ public class AssetQuery {return this;}+ public AssetQuery path(PathPredicate pathPredicate) {+ this.pathPredicate = pathPredicate;+ return this;+ }+public AssetQuery tenant(TenantPredicate tenantPredicate) {this.tenantPredicate = tenantPredicate;return this;