Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Implemented more asset queries

Christian Bauerc0751a88f809
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
index bb9d1b56c..100b10272 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
@@ -119,7 +119,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
List<ServerAsset> things = assetStorageService.findAll(new AssetQuery()
.select(new AssetQuery.Select(true, false))
.type(THING)
- .parent(new AssetQuery.Parent(agent.getId()))
+ .parent(new AssetQuery.ParentPredicate(agent.getId()))
);
for (Asset thing : things) {
deployThing(thing, PersistenceEvent.Cause.UPDATE);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
index 777176368..43af33072 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
@@ -106,8 +106,8 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
List<ServerAsset> result = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(true))
- .realm(new AssetQuery.Realm(tenant.getId()))
+ .parent(new AssetQuery.ParentPredicate(true))
+ .tenant(new AssetQuery.TenantPredicate(tenant.getId()))
);
return result.toArray(new Asset[result.size()]);
@@ -125,7 +125,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
if (isSuperUser()) {
List<ServerAsset> result = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(parentId))
+ .parent(new AssetQuery.ParentPredicate(parentId))
);
return result.toArray(new Asset[result.size()]);
} else {
@@ -135,8 +135,8 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
}
List<ServerAsset> result = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(parentId))
- .realm(new AssetQuery.Realm(tenant.getId()))
+ .parent(new AssetQuery.ParentPredicate(parentId))
+ .tenant(new AssetQuery.TenantPredicate(tenant.getId()))
);
return result.toArray(new Asset[result.size()]);
}
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.java
index 3b62f1fe3..d393a9ebe 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
@@ -159,7 +159,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
Asset asset = em.find(ServerAsset.class, assetId);
if (asset != null) {
List<ServerAsset> children = findAll(em, new AssetQuery()
- .parent(new AssetQuery.Parent(asset.getId()))
+ .parent(new AssetQuery.ParentPredicate(asset.getId()))
);
if (children.size() > 0)
return false;
@@ -229,7 +229,6 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
public List<ServerAsset> execute(Connection connection) throws SQLException {
LOG.fine("Preparing asset query: " + sb.toString());
PreparedStatement st = connection.prepareStatement(sb.toString());
- LOG.fine("Query binders: " + whereClause.value.size());
for (ParameterBinder binder : whereClause.value) {
binder.accept(st);
}
@@ -250,12 +249,11 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
sb.append("select ");
sb.append("A.ID as ID, A.OBJ_VERSION as OBJ_VERSION, A.CREATED_ON as CREATED_ON, A.NAME as NAME, A.ASSET_TYPE as ASSET_TYPE, ");
sb.append("A.PARENT_ID as PARENT_ID, ");
- sb.append(query.parent != null && query.parent.noParent
+ sb.append(query.parentPredicate != null && query.parentPredicate.noParent
? " NULL PARENT_NAME, NULL as PARENT_TYPE, "
: " P.NAME as PARENT_NAME, P.ASSET_TYPE as PARENT_TYPE, "
);
- sb.append("A.REALM_ID as REALM_ID, R.NAME as TENANT_NAME, ");
- sb.append("(select RA.VALUE from REALM_ATTRIBUTE RA where RA.REALM_ID = R.ID and RA.NAME = 'displayName') as TENANT_DISPLAY_NAME, ");
+ sb.append("A.REALM_ID as REALM_ID, R.NAME as TENANT_NAME, RA.VALUE as TENANT_DISPLAY_NAME, ");
sb.append("A.LOCATION as LOCATION");
if (query.select != null && query.select.loadComplete) {
@@ -271,9 +269,11 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
protected String buildFromString(AssetQuery query) {
StringBuilder sb = new StringBuilder();
- sb.append(" from ASSET A cross join (select * from REALM) as R ");
+ sb.append(" from ASSET A ");
+ sb.append(" join REALM R on R.ID = A.REALM_ID ");
+ sb.append(" join REALM_ATTRIBUTE RA on RA.REALM_ID = R.ID and RA.NAME = 'displayName'");
- if (query.parent != null && !query.parent.noParent) {
+ if (query.parentPredicate != null && !query.parentPredicate.noParent) {
sb.append(" cross join ASSET P ");
} else {
sb.append(" left outer join ASSET P on A.PARENT_ID = P.ID ");
@@ -287,11 +287,6 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
sb.append(" cross join USER_ASSET ua ");
}
- if (query.hasAttributeRestrictions()) {
- sb.append(" cross join jsonb_each(A.ATTRIBUTES) as AX ");
- sb.append(" cross join jsonb_array_elements(jsonb_extract_path(AX.VALUE, 'meta')) as AM ");
- }
-
return sb.toString();
}
@@ -299,7 +294,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
StringBuilder sb = new StringBuilder();
List<ParameterBinder> binders = new ArrayList<>();
- sb.append(" where r.ID = a.REALM_ID ");
+ sb.append(" where true ");
if (query.id != null) {
sb.append(" and A.ID = ? ");
@@ -315,31 +310,30 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
binders.add(st -> st.setString(pos, query.name.prepareValue()));
}
- if (query.parent != null) {
- if (query.parent.id != null) {
+ if (query.parentPredicate != null) {
+ if (query.parentPredicate.id != null) {
sb.append(" and p.ID = a.PARENT_ID ");
sb.append(" and A.PARENT_ID = ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, query.parent.id));
- } else if (query.parent.type != null) {
+ binders.add(st -> st.setString(pos, query.parentPredicate.id));
+ } else if (query.parentPredicate.type != null) {
sb.append(" and p.ID = a.PARENT_ID ");
sb.append(" and P.ASSET_TYPE = ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, query.parent.type));
- } else if (query.parent.noParent) {
+ binders.add(st -> st.setString(pos, query.parentPredicate.type));
+ } else if (query.parentPredicate.noParent) {
sb.append(" and A.PARENT_ID is null ");
- } else {
}
}
- if (query.realm != null && query.realm.id != null) {
+ if (query.tenantPredicate != null && query.tenantPredicate.realmId != null) {
sb.append(" and R.ID = ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, query.realm.id));
- } else if (query.realm != null && query.realm.name != null) {
+ binders.add(st -> st.setString(pos, query.tenantPredicate.realmId));
+ } else if (query.tenantPredicate != null && query.tenantPredicate.realm != null) {
sb.append(" and R.NAME = ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, query.realm.id));
+ binders.add(st -> st.setString(pos, query.tenantPredicate.realm));
}
if (query.userId != null) {
@@ -355,34 +349,54 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
binders.add(st -> st.setString(pos, query.type.prepareValue()));
}
- if (query.attributeMeta != null) {
- if (query.attributeMeta.itemNameSearch != null) {
- sb.append(query.attributeMeta.itemNameSearch.caseSensitive
- ? " and jsonb_extract_path_text(AM.VALUE, 'name') "
- : " and upper(jsonb_extract_path_text(AM.VALUE, 'name')) "
+ if (query.attributeMetaPredicate != null) {
+ StringBuilder attributeMetaBuilder = new StringBuilder();
+
+ if (query.attributeMetaPredicate.itemNamePredicate != null) {
+ attributeMetaBuilder.append(query.attributeMetaPredicate.itemNamePredicate.caseSensitive
+ ? " and AM.VALUE #>> '{name}' "
+ : " and upper(AM.VALUE #>> '{name}') "
);
- sb.append(query.attributeMeta.itemNameSearch.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
+ attributeMetaBuilder.append(query.attributeMetaPredicate.itemNamePredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, query.attributeMeta.itemNameSearch.prepareValue()));
+ binders.add(st -> st.setString(pos, query.attributeMetaPredicate.itemNamePredicate.prepareValue()));
}
- if (query.attributeMeta.itemValueSearch != null) {
- if (query.attributeMeta.itemValueSearch instanceof AssetQuery.StringSearch) {
- AssetQuery.StringSearch stringSearch = (AssetQuery.StringSearch) query.attributeMeta.itemValueSearch;
- sb.append(stringSearch.caseSensitive
- ? " and jsonb_extract_path(AM.VALUE, 'value') "
- : " and upper(jsonb_extract_path(AM.VALUE, 'value')) "
+ if (query.attributeMetaPredicate.itemValuePredicate != null) {
+ if (query.attributeMetaPredicate.itemValuePredicate instanceof AssetQuery.StringPredicate) {
+ AssetQuery.StringPredicate stringPredicate = (AssetQuery.StringPredicate) query.attributeMetaPredicate.itemValuePredicate;
+ attributeMetaBuilder.append(stringPredicate.caseSensitive
+ ? " and AM.VALUE #>> '{value}' "
+ : " and upper(AM.VALUE #>> '{value}') "
);
- sb.append(stringSearch.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
+ attributeMetaBuilder.append(stringPredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
final int pos = binders.size() + 1;
- binders.add(st -> st.setString(pos, stringSearch.prepareValue()));
- } else if (query.attributeMeta.itemValueSearch instanceof AssetQuery.BooleanSearch) {
- AssetQuery.BooleanSearch booleanSearch = (AssetQuery.BooleanSearch) query.attributeMeta.itemValueSearch;
- sb.append(" and jsonb_extract_path(AM.VALUE, 'value') = to_jsonb(")
- .append(booleanSearch.predicate)
- .append(") ");
+ binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));
+ } else if (query.attributeMetaPredicate.itemValuePredicate instanceof AssetQuery.BooleanPredicate) {
+ AssetQuery.BooleanPredicate booleanPredicate = (AssetQuery.BooleanPredicate) query.attributeMetaPredicate.itemValuePredicate;
+ attributeMetaBuilder.append(" and AM.VALUE #> '{value}' = to_jsonb(").append(booleanPredicate.predicate).append(") ");
+ } else if (query.attributeMetaPredicate.itemValuePredicate instanceof AssetQuery.StringArrayPredicate) {
+ AssetQuery.StringArrayPredicate stringArrayPredicate = (AssetQuery.StringArrayPredicate) query.attributeMetaPredicate.itemValuePredicate;
+ for (int i = 0; i < stringArrayPredicate.predicates.length; i++) {
+ AssetQuery.StringPredicate stringPredicate = stringArrayPredicate.predicates[i];
+ attributeMetaBuilder.append(stringPredicate.caseSensitive
+ ? " and AM.VALUE #> '{value}' ->> " + i
+ : " and upper(AM.VALUE #> '{value}' ->> " + i + ") "
+ );
+ attributeMetaBuilder.append(stringPredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
+ final int pos = binders.size() + 1;
+ binders.add(st -> st.setString(pos, stringPredicate.prepareValue()));
+ }
}
}
- // TODO Implement AssetQuery.AttributeRef
+
+ if (attributeMetaBuilder.length() > 0) {
+ sb.append(" and A.ID in (select distinct 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(attributeMetaBuilder);
+ sb.append(")");
+ }
}
return new Pair<>(sb.toString(), binders);
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.groovy
index d46af87af..fd8d6fbc9 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
@@ -12,6 +12,7 @@ import spock.lang.Specification
import org.openremote.model.asset.AssetType
import static org.openremote.model.asset.AssetType.THING
+import static org.openremote.model.asset.AssetQuery.*
class AssetQueryTest extends Specification implements ManagerContainerTrait {
@@ -48,7 +49,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
asset = assetStorageService.find(
new AssetQuery()
- .select(new AssetQuery.Select(true, false))
+ .select(new Select(true, false))
.id(managerDemoSetup.smartOfficeId)
)
@@ -72,8 +73,8 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
def assets = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(true))
- .realm(new AssetQuery.Realm(keycloakDemoSetup.masterTenant.id))
+ .parent(new ParentPredicate(true))
+ .tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.id))
)
then: "result should match"
@@ -96,9 +97,9 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
new AssetQuery()
- .select(new AssetQuery.Select(true, false))
- .parent(new AssetQuery.Parent(true))
- .realm(new AssetQuery.Realm(keycloakDemoSetup.masterTenant.id))
+ .select(new Select(true, false))
+ .parent(new ParentPredicate(true))
+ .tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.id))
)
then: "result should match"
@@ -122,7 +123,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(managerDemoSetup.smartHomeId))
+ .parent(new ParentPredicate(managerDemoSetup.smartHomeId))
)
then: "result should match"
@@ -147,8 +148,8 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(managerDemoSetup.smartHomeId))
- .realm(new AssetQuery.Realm(keycloakDemoSetup.customerATenant.id))
+ .parent(new ParentPredicate(managerDemoSetup.smartHomeId))
+ .tenant(new TenantPredicate(keycloakDemoSetup.customerATenant.id))
)
then: "result should match"
@@ -160,8 +161,8 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
new AssetQuery()
- .parent(new AssetQuery.Parent(managerDemoSetup.smartHomeId))
- .realm(new AssetQuery.Realm(keycloakDemoSetup.masterTenant.id))
+ .parent(new ParentPredicate(managerDemoSetup.smartHomeId))
+ .tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.id))
)
then: "result should match"
@@ -169,7 +170,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
- new AssetQuery().select(new AssetQuery.Select(true, true)).userId(keycloakDemoSetup.testuser3Id)
+ new AssetQuery().select(new Select(true, true)).userId(keycloakDemoSetup.testuser3Id)
)
then: "result should match"
@@ -186,7 +187,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
- new AssetQuery().select(new AssetQuery.Select(true, true)).userId(keycloakDemoSetup.testuser2Id)
+ new AssetQuery().select(new Select(true, true)).userId(keycloakDemoSetup.testuser2Id)
)
then: "result should match"
@@ -212,7 +213,17 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(new AssetQuery()
.type(THING)
- .parent(new AssetQuery.Parent(managerDemoSetup.demoAgentId))
+ .parent(new ParentPredicate(managerDemoSetup.demoAgentId))
+ )
+
+ then: "result should match"
+ assets.size() == 1
+ assets.get(0).id == managerDemoSetup.thingId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(new AssetQuery()
+ .type(THING)
+ .parent(new ParentPredicate().type(AssetType.AGENT))
)
then: "result should match"
@@ -223,14 +234,101 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed"
assets = assetStorageService.findAll(
new AssetQuery()
- .realm(new AssetQuery.Realm(keycloakDemoSetup.masterTenant.id))
- .attributeMeta(new AssetQuery.AttributeMeta(AssetMeta.STORE_DATA_POINTS, new AssetQuery.BooleanSearch(true)))
+ .tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.id))
+ .attributeMeta(new AttributeMetaPredicate(AssetMeta.STORE_DATA_POINTS, new BooleanPredicate(true)))
+ )
+
+ then: "result should match"
+ assets.size() == 1
+ assets.get(0).id == managerDemoSetup.thingId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery()
+ .attributeMeta(new AttributeMetaPredicate().itemValue(new StringPredicate(Match.END, "kWh")))
+ )
+
+ then: "result should match"
+ assets.size() == 1
+ assets.get(0).id == managerDemoSetup.thingId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery().attributeMeta(
+ new AttributeRefPredicate(
+ AssetMeta.AGENT_LINK,
+ managerDemoSetup.demoAgentId,
+ managerDemoSetup.demoAgentProtocolConfigName
+ )
+ )
+ )
+
+ then: "result should match"
+ assets.size() == 2
+ assets.get(0).id == managerDemoSetup.thingId
+ assets.get(1).id == managerDemoSetup.apartment1LivingroomThermostatId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery().attributeMeta(
+ new AttributeRefPredicate(
+ AssetMeta.AGENT_LINK,
+ managerDemoSetup.demoAgentId,
+ managerDemoSetup.demoAgentProtocolConfigName
+ )
+ )
+ )
+
+ then: "result should match"
+ assets.size() == 2
+ assets.get(0).id == managerDemoSetup.thingId
+ assets.get(1).id == managerDemoSetup.apartment1LivingroomThermostatId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery().attributeMeta(
+ new AttributeRefPredicate(
+ AssetMeta.AGENT_LINK,
+ managerDemoSetup.demoAgentId,
+ managerDemoSetup.demoAgentProtocolConfigName
+ )
+ ).name(new StringPredicate(Match.CONTAINS, false, "thing"))
)
then: "result should match"
assets.size() == 1
assets.get(0).id == managerDemoSetup.thingId
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery().attributeMeta(
+ new AttributeRefPredicate(
+ AssetMeta.AGENT_LINK,
+ managerDemoSetup.demoAgentId,
+ managerDemoSetup.demoAgentProtocolConfigName
+ )
+ ).parent(new ParentPredicate(managerDemoSetup.apartment1LivingroomId))
+ )
+
+ then: "result should match"
+ assets.size() == 1
+ assets.get(0).id == managerDemoSetup.apartment1LivingroomThermostatId
+
+ when: "a query is executed"
+ assets = assetStorageService.findAll(
+ new AssetQuery().attributeMeta(
+ new AttributeRefPredicate(
+ AssetMeta.AGENT_LINK,
+ managerDemoSetup.demoAgentId,
+ managerDemoSetup.demoAgentProtocolConfigName
+ )
+ ).tenant(new TenantPredicate().realm(keycloakDemoSetup.customerATenant.realm))
+ )
+
+ then: "result should match"
+ assets.size() == 1
+ assets.get(0).id == managerDemoSetup.apartment1LivingroomThermostatId
+
cleanup: "the server should be stopped"
stopContainer(container)
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetQuery.java b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
index 9a5df2933..5c904d8ce 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetQuery.java
@@ -19,15 +19,42 @@
*/
package org.openremote.model.asset;
+import org.openremote.model.AttributeRef;
+
import java.util.Locale;
/**
- * Encapsulate asset query details. A query returns a collection of
- * {@link Asset} instances, unless if the {@link #id} is set, then only
- * a single instance can be found.
+ * Encapsulate asset query restriction, project, and ordering of results.
+ * <p>
+ * A query returns a collection of {@link Asset} instances, or when the
+ * {@link #id} predicate is set, a single instance.
*/
public class AssetQuery {
+ static public class Select {
+ public boolean loadComplete;
+ // TODO: Filtering attributes can be moved into database functions
+ public boolean filterProtected;
+
+ public Select() {
+ }
+
+ public Select(boolean loadComplete, boolean filterProtected) {
+ this.loadComplete = loadComplete;
+ this.filterProtected = filterProtected;
+ }
+
+ public Select loadComplete(boolean loadComplete) {
+ this.loadComplete = loadComplete;
+ return this;
+ }
+
+ public Select filterProtected(boolean filterProtected) {
+ this.filterProtected = filterProtected;
+ return this;
+ }
+ }
+
public enum Match {
EXACT,
BEGIN,
@@ -49,45 +76,44 @@ public class AssetQuery {
}
}
- public interface Search {
-
+ public interface ValuePredicate {
}
- static public class StringSearch {
+ static public class StringPredicate implements ValuePredicate {
public Match match = Match.EXACT;
public boolean caseSensitive = true;
public String value;
- public StringSearch() {
+ public StringPredicate() {
}
- public StringSearch(String value) {
+ public StringPredicate(String value) {
this.value = value;
}
- public StringSearch(Match match, String value) {
+ public StringPredicate(Match match, String value) {
this.match = match;
this.value = value;
}
- public StringSearch(Match match, boolean caseSensitive, String value) {
+ public StringPredicate(Match match, boolean caseSensitive, String value) {
this.match = match;
this.caseSensitive = caseSensitive;
this.value = value;
}
- public StringSearch match(Match match) {
+ public StringPredicate match(Match match) {
this.match = match;
return this;
}
- public StringSearch caseSensitive(boolean caseSensitive) {
+ public StringPredicate caseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
return this;
}
- public StringSearch search(String search) {
- this.value = search;
+ public StringPredicate value(String value) {
+ this.value = value;
return this;
}
@@ -99,167 +125,180 @@ public class AssetQuery {
}
}
- static public class BooleanSearch implements Search {
-
+ static public class BooleanPredicate implements ValuePredicate {
public boolean predicate;
- public BooleanSearch() {
+ public BooleanPredicate() {
}
- public BooleanSearch(boolean predicate) {
+ public BooleanPredicate(boolean predicate) {
this.predicate = predicate;
}
- public BooleanSearch predicate(boolean predicate) {
+ public BooleanPredicate predicate(boolean predicate) {
this.predicate = predicate;
return this;
}
}
- static public class Parent {
+ static public class StringArrayPredicate implements ValuePredicate {
+ public StringPredicate[] predicates = new StringPredicate[0];
+
+ public StringArrayPredicate() {
+ }
+
+ public StringArrayPredicate(StringPredicate... predicates) {
+ this.predicates = predicates;
+ }
+
+ public StringArrayPredicate predicates(StringPredicate... predicates) {
+ this.predicates = predicates;
+ return this;
+ }
+ }
+
+ static public class ParentPredicate {
public String id;
public String type;
public boolean noParent;
- public Parent() {
+ public ParentPredicate() {
}
- public Parent(String id) {
+ public ParentPredicate(String id) {
this.id = id;
}
- public Parent(boolean noParent) {
+ public ParentPredicate(boolean noParent) {
this.noParent = noParent;
}
- public Parent id(String id) {
+ public ParentPredicate id(String id) {
this.id = id;
return this;
}
- public Parent type(String type) {
+ public ParentPredicate type(String type) {
this.type = type;
return this;
}
- public Parent noParent(boolean noParent) {
+ public ParentPredicate type(AssetType type) {
+ return type(type.getValue());
+ }
+
+ public ParentPredicate noParent(boolean noParent) {
this.noParent = noParent;
return this;
}
}
- static public class Realm {
- public String id;
- public String name;
+ static public class TenantPredicate {
+ public String realmId;
+ public String realm;
- public Realm() {
+ public TenantPredicate() {
}
- public Realm(String id) {
- this.id = id;
+ public TenantPredicate(String realmId) {
+ this.realmId = realmId;
}
- public Realm id(String id) {
- this.id = id;
+ public TenantPredicate realmId(String id) {
+ this.realmId = id;
return this;
}
- public Realm name(String name) {
- this.name = name;
+ public TenantPredicate realm(String name) {
+ this.realm = name;
return this;
}
}
- static public class AttributeRef {
- public String entityId;
- public String attributeName;
+ static public class AttributeMetaPredicate {
+ public StringPredicate itemNamePredicate;
+ public ValuePredicate itemValuePredicate;
- public AttributeRef() {
+ public AttributeMetaPredicate() {
}
- public AttributeRef(String entityId) {
- this.entityId = entityId;
+ public AttributeMetaPredicate(StringPredicate itemNamePredicate) {
+ this.itemNamePredicate = itemNamePredicate;
}
- public AttributeRef(String entityId, String attributeName) {
- this.entityId = entityId;
- this.attributeName = attributeName;
+ public AttributeMetaPredicate(AssetMeta assetMeta) {
+ this.itemNamePredicate = new StringPredicate(assetMeta.getName());
}
- public AttributeRef entityId(String entityId) {
- this.entityId = entityId;
- return this;
+ public AttributeMetaPredicate(ValuePredicate itemValuePredicate) {
+ this.itemValuePredicate = itemValuePredicate;
}
- public AttributeRef attributeName(String attributeName) {
- this.attributeName = attributeName;
- return this;
+ public AttributeMetaPredicate(StringPredicate itemNamePredicate, ValuePredicate itemValuePredicate) {
+ this.itemNamePredicate = itemNamePredicate;
+ this.itemValuePredicate = itemValuePredicate;
}
- }
- static public class AttributeMeta {
- public StringSearch itemNameSearch;
- public Search itemValueSearch;
-
- public AttributeMeta() {
+ public AttributeMetaPredicate(AssetMeta assetMeta, ValuePredicate itemValuePredicate) {
+ this(new StringPredicate(assetMeta.getName()), itemValuePredicate);
}
- public AttributeMeta(StringSearch itemNameSearch) {
- this.itemNameSearch = itemNameSearch;
+ public AttributeMetaPredicate itemName(StringPredicate itemNamePredicate) {
+ this.itemNamePredicate = itemNamePredicate;
+ return this;
}
- public AttributeMeta(AssetMeta assetMeta) {
- this.itemNameSearch = new StringSearch(assetMeta.getName());
+ public AttributeMetaPredicate itemName(AssetMeta assetMeta) {
+ return itemName(new StringPredicate(assetMeta.getName()));
}
- public AttributeMeta(Search itemValueSearch) {
- this.itemValueSearch = itemValueSearch;
+ public AttributeMetaPredicate itemValue(ValuePredicate itemValuePredicate) {
+ this.itemValuePredicate = itemValuePredicate;
+ return this;
}
+ }
- public AttributeMeta(StringSearch itemNameSearch, Search itemValueSearch) {
- this.itemNameSearch = itemNameSearch;
- this.itemValueSearch = itemValueSearch;
- }
+ static public class AttributeRefPredicate extends AttributeMetaPredicate {
- public AttributeMeta(AssetMeta assetMeta, Search itemValueSearch) {
- this(new StringSearch(assetMeta.getName()), itemValueSearch);
+ public AttributeRefPredicate() {
}
- public AttributeMeta itemNameSearch(StringSearch itemNameSearch) {
- this.itemNameSearch = itemNameSearch;
- return this;
+ public AttributeRefPredicate(AttributeRef attributeRef) {
+ this(attributeRef.getEntityId(), attributeRef.getAttributeName());
}
- public AttributeMeta itemNameSearch(AssetMeta assetMeta) {
- return itemNameSearch(new StringSearch(assetMeta.getName()));
+ public AttributeRefPredicate(String entityId, String attributeName) {
+ super(
+ new StringArrayPredicate(
+ new StringPredicate(entityId),
+ new StringPredicate(attributeName)
+ ));
}
- public AttributeMeta itemValueSearch(Search itemValueSearch) {
- this.itemValueSearch = itemValueSearch;
- return this;
+ public AttributeRefPredicate(String name, String entityId, String attributeName) {
+ super(
+ new StringPredicate(name),
+ new StringArrayPredicate(
+ new StringPredicate(entityId),
+ new StringPredicate(attributeName)
+ ));
}
- }
-
- static public class Select {
- public boolean loadComplete;
- public boolean filterProtected;
- public Select() {
+ public AttributeRefPredicate(AssetMeta assetMeta, String entityId, String attributeName) {
+ this(assetMeta.getName(), entityId, attributeName);
}
- public Select(boolean loadComplete, boolean filterProtected) {
- this.loadComplete = loadComplete;
- this.filterProtected = filterProtected;
+ public AttributeRefPredicate(StringPredicate name, AttributeRef attributeRef) {
+ super(
+ name,
+ new StringArrayPredicate(
+ new StringPredicate(attributeRef.getEntityId()),
+ new StringPredicate(attributeRef.getAttributeName())
+ ));
}
- public Select loadComplete(boolean loadComplete) {
- this.loadComplete = loadComplete;
- return this;
- }
-
- public Select filterProtected(boolean filterProtected) {
- this.filterProtected = filterProtected;
- return this;
+ public AttributeRefPredicate(AssetMeta assetMeta, AttributeRef attributeRef) {
+ this(new StringPredicate(assetMeta.getName()), attributeRef);
}
}
@@ -290,21 +329,20 @@ public class AssetQuery {
}
}
+ // Projection
public Select select = new Select(false, false);
- /**
- * If this value is set, no other restrictions will be applied and only a single result should be returned.
- */
+ // Restriction predicates
public String id;
-
- public StringSearch name;
- public Parent parent;
+ public StringPredicate name;
+ public ParentPredicate parentPredicate;
// TODO Implement descendants restriction
- public Realm realm;
+ public TenantPredicate tenantPredicate;
public String userId;
- public StringSearch type;
- public AttributeRef attributeRef;
- public AttributeMeta attributeMeta;
+ public StringPredicate type;
+ public AttributeMetaPredicate attributeMetaPredicate;
+
+ // Ordering
public OrderBy orderBy = new OrderBy("createdOn");
public AssetQuery() {
@@ -315,23 +353,26 @@ public class AssetQuery {
return this;
}
- public AssetQuery name(StringSearch name) {
- this.name = name;
+ /**
+ * If this value is set, no other restrictions will be applied and only a single result should be returned.
+ */
+ public AssetQuery id(String id) {
+ this.id = id;
return this;
}
- public AssetQuery id(String id) {
- this.id = id;
+ public AssetQuery name(StringPredicate name) {
+ this.name = name;
return this;
}
- public AssetQuery parent(Parent parent) {
- this.parent = parent;
+ public AssetQuery parent(ParentPredicate parentPredicate) {
+ this.parentPredicate = parentPredicate;
return this;
}
- public AssetQuery realm(Realm realm) {
- this.realm = realm;
+ public AssetQuery tenant(TenantPredicate tenantPredicate) {
+ this.tenantPredicate = tenantPredicate;
return this;
}
@@ -340,22 +381,17 @@ public class AssetQuery {
return this;
}
- public AssetQuery type(StringSearch type) {
+ public AssetQuery type(StringPredicate type) {
this.type = type;
return this;
}
public AssetQuery type(AssetType assetType) {
- return type(new StringSearch(assetType.getValue()));
+ return type(new StringPredicate(assetType.getValue()));
}
- public AssetQuery attributeRef(AttributeRef attributeRef) {
- this.attributeRef = attributeRef;
- return this;
- }
-
- public AssetQuery attributeMeta(AttributeMeta attributeMeta) {
- this.attributeMeta = attributeMeta;
+ public AssetQuery attributeMeta(AttributeMetaPredicate attributeMetaPredicate) {
+ this.attributeMetaPredicate = attributeMetaPredicate;
return this;
}
@@ -363,8 +399,4 @@ public class AssetQuery {
this.orderBy = orderBy;
return this;
}
-
- public boolean hasAttributeRestrictions() {
- return attributeMeta != null;
- }
}