Skip to content

armgit / core

Public Imported from GitHub
0

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

Added AttributePredicate and AttributePredicateArray to filter on values of an Assets Attribute. Also introduced DateTimePredicate to do specific filtering on DateTime Attributes

Michael Rademaker0d2e28341f03
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 d70849838..9c7ab58d1 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
@@ -768,8 +768,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
sb.append(")");
}
}
- }
+ if (query.attributePredicateArray != null && query.attributePredicateArray.predicates != null) {
+ StringBuilder attributeFilterBuilder = new StringBuilder();
+ for (AssetQuery.AttributePredicate attributePredicate : query.attributePredicateArray.predicates) {
+ attributeFilterBuilder.append(buildAttributeFilter(attributePredicate, binders));
+ }
+ if (attributeFilterBuilder.length() > 0) {
+ sb.append(" and A.ID in (select A.ID from");
+ sb.append(" jsonb_each(A.ATTRIBUTES) as AX");
+ sb.append(" where true");
+ sb.append(attributeFilterBuilder.toString());
+ sb.append(")");
+ }
+ }
+ }
return sb.toString();
}
@@ -818,6 +831,86 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
return attributeMetaBuilder.toString();
}
+ protected String buildAttributeFilter(AssetQuery.AttributePredicate attributePredicate, List<ParameterBinder> binders) {
+ StringBuilder attributeBuilder = new StringBuilder();
+
+ if (attributePredicate.itemNamePredicate != null) {
+ attributeBuilder.append(attributePredicate.itemNamePredicate.caseSensitive
+ ? " and AX.key"
+ : " and upper(AX.key)"
+ );
+ attributeBuilder.append(attributePredicate.itemNamePredicate.match == AssetQuery.Match.EXACT ? " = ? " : " like ? ");
+ final int pos = binders.size() + 1;
+ binders.add(st -> st.setString(pos, attributePredicate.itemNamePredicate.prepareValue()));
+ }
+ if (attributePredicate.itemValuePredicate != null) {
+ if (attributePredicate.itemValuePredicate instanceof AssetQuery.StringPredicate) {
+ AssetQuery.StringPredicate stringPredicate = (AssetQuery.StringPredicate) attributePredicate.itemValuePredicate;
+ attributeBuilder.append(stringPredicate.caseSensitive
+ ? " and AX.VALUE #>> '{value}'"
+ : " and upper(AX.VALUE #>> '{value}')"
+ );
+ attributeBuilder.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;
+ 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;
+ for (int i = 0; i < stringArrayPredicate.predicates.length; i++) {
+ AssetQuery.StringPredicate stringPredicate = stringArrayPredicate.predicates[i];
+ attributeBuilder.append(stringPredicate.caseSensitive
+ ? " and AX.VALUE #> '{value}' ->> " + i
+ : " and upper(AX.VALUE #> '{value}' ->> " + i + ")"
+ );
+ attributeBuilder.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.DateTimePredicate) {
+ AssetQuery.DateTimePredicate dateTimePredicate = (AssetQuery.DateTimePredicate) attributePredicate.itemValuePredicate;
+ attributeBuilder.append(" and to_timestamp(AX.VALUE #>> '{value}', ?)");
+ final int keyFormatPos = binders.size() + 1;
+ binders.add(st -> st.setString(keyFormatPos, dateTimePredicate.dateFormat));
+
+ final int pos = binders.size() + 1;
+ binders.add(st -> st.setString(pos, dateTimePredicate.value));
+ final int formatPos = binders.size() + 1;
+ binders.add(st -> st.setString(formatPos, dateTimePredicate.dateFormat));
+
+ switch (dateTimePredicate.dateMatch) {
+ case EXACT:
+ attributeBuilder.append(" = to_timestamp(?, ?)");
+ break;
+ case AFTER:
+ attributeBuilder.append(" > to_timestamp(?, ?)");
+ break;
+ case AFTER_INCLUSIVE:
+ attributeBuilder.append(" >= to_timestamp(?, ?)");
+ break;
+ case BEFORE:
+ attributeBuilder.append(" < to_timestamp(?, ?)");
+ break;
+ case BEFORE_INCLUSIVE:
+ attributeBuilder.append(" <= to_timestamp(?, ?)");
+ break;
+ case BETWEEN:
+ attributeBuilder.append(" BETWEEN to_timestamp(?, ?) AND to_timestamp(?, ?)");
+ final int pos2 = binders.size() + 1;
+ binders.add(st -> st.setString(pos2, dateTimePredicate.rangeValue));
+ final int formatPos2 = binders.size() + 1;
+ binders.add(st -> st.setString(formatPos2, dateTimePredicate.dateFormat));
+ break;
+ }
+ }
+ }
+
+ return attributeBuilder.toString();
+ }
+
protected ServerAsset mapResultTuple(AbstractAssetQuery query, ResultSet rs) throws SQLException {
switch (query.select.include) {
case ONLY_ID_AND_NAME:
diff --git a/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java b/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java
index 663b99b18..f5811e5cd 100644
--- a/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java
+++ b/model/src/main/java/org/openremote/model/asset/AbstractAssetQuery.java
@@ -19,6 +19,8 @@
*/
package org.openremote.model.asset;
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.attribute.AttributeRef;
import java.util.Locale;
@@ -106,6 +108,26 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {
}
}
+ public enum DateMatch {
+ EXACT,
+ BEFORE,
+ BEFORE_INCLUSIVE,
+ AFTER,
+ AFTER_INCLUSIVE,
+ BETWEEN
+ }
+
+ @JsonSubTypes({
+ @JsonSubTypes.Type(value = StringPredicate.class, name = "string"),
+ @JsonSubTypes.Type(value = BooleanPredicate.class, name = "boolean"),
+ @JsonSubTypes.Type(value = StringArrayPredicate.class, name = "string-array"),
+ @JsonSubTypes.Type(value = DateTimePredicate.class, name = "datetime")
+ })
+ @JsonTypeInfo(
+ use = JsonTypeInfo.Id.NAME,
+ include = JsonTypeInfo.As.PROPERTY,
+ property = "predicateType"
+ )
public interface ValuePredicate {
}
@@ -187,46 +209,69 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {
}
}
- public static class AssetPredicate {
- public String id;
- public String type;
+ public static class DateTimePredicate implements ValuePredicate {
+ public DateMatch dateMatch = DateMatch.EXACT;
+ public String dateFormat = "YYYY-MM-DDTHH24:MI:SS";//postgres dateformat
+ public String value;
+ public String rangeValue;//used when dateMatch is Between as end date
- public AssetPredicate() {
+ public DateTimePredicate() {
}
- public AssetPredicate(String id) {
- this.id = id;
+ public DateTimePredicate(DateMatch dateMatch, String value) {
+ this.dateMatch = dateMatch;
+ this.value = value;
}
- public AssetPredicate id(String id) {
- this.id = id;
+ public DateTimePredicate(String afterValue, String beforeValue) {
+ this.dateMatch = DateMatch.BETWEEN;
+ this.value = afterValue;
+ this.rangeValue = beforeValue;
+ }
+
+ public DateTimePredicate dateMatch(DateMatch dateMatch) {
+ this.dateMatch = dateMatch;
return this;
}
- public AssetPredicate type(String type) {
- this.type = type;
+ public DateTimePredicate dateFormat(String dateFormat) {
+ this.dateFormat = dateFormat;
return this;
}
- public AssetPredicate type(AssetType type) {
- return type(type.getValue());
+ public DateTimePredicate value(String value) {
+ this.value = value;
+ return this;
+ }
+
+ public DateTimePredicate rangeValue(String beforeValue) {
+ this.dateMatch = DateMatch.BETWEEN;
+ this.rangeValue = beforeValue;
+ return this;
}
}
- public static class ParentPredicate extends AssetPredicate {
+ public static class ParentPredicate {
+ public String id;
+ public String type;
public boolean noParent;
public ParentPredicate() {
}
public ParentPredicate(String id) {
- super(id);
+ this.id = id;
}
public ParentPredicate(boolean noParent) {
this.noParent = noParent;
}
+ public ParentPredicate id(String id) {
+ this.id = id;
+ return this;
+ }
+
public ParentPredicate type(String type) {
this.type = type;
return this;
@@ -370,6 +415,28 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {
this(new StringPredicate(assetMeta.getUrn()), attributeRef);
}
}
+
+ public static class AttributePredicate {
+ public StringPredicate itemNamePredicate;
+ public ValuePredicate 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;
+ }
+ }
+
public static class OrderBy {
public enum Property {
@@ -418,6 +485,7 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {
public String userId;
public StringPredicate type;
public AttributeMetaPredicate attributeMetaPredicate;
+ public AttributePredicateArray attributePredicateArray;
// Ordering
public OrderBy orderBy = new OrderBy(OrderBy.Property.CREATED_ON);
@@ -490,6 +558,11 @@ public class AbstractAssetQuery<CHILD extends AbstractAssetQuery<CHILD>> {
return (CHILD) this;
}
+ public CHILD attributes(AttributePredicateArray attributePredicateArray) {
+ this.attributePredicateArray = attributePredicateArray;
+ return (CHILD) this;
+ }
+
public CHILD orderBy(OrderBy orderBy) {
this.orderBy = orderBy;
return (CHILD) this;