Permalink
Browse files

Various rules updates - updated DateTimePredicate support and added s…

…upport for it in rules and improved code reuse for asset query predicates
  • Loading branch information...
richturner committed Dec 23, 2018
1 parent 6ac3b92 commit 8aa3d82804e1eeaee7e503c159083f7ef03e6900
Showing with 286 additions and 103 deletions.
  1. +1 −1 client/src/main/java/org/openremote/app/client/notifications/NotificationsActivity.java
  2. +43 −14 manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
  3. +1 −1 manager/src/main/java/org/openremote/manager/notification/NotificationService.java
  4. +134 −34 manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
  5. +1 −1 manager/src/main/java/org/openremote/manager/rules/RulesClock.java
  6. +2 −2 manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
  7. +9 −6 manager/src/main/java/org/openremote/manager/rules/RulesFacts.java
  8. +7 −1 manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
  9. +1 −2 manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
  10. +8 −1 manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
  11. +3 −3 manager/src/main/resources/demo/rules/DemoConsoleLocation.groovy
  12. +8 −8 manager/src/main/resources/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy
  13. +3 −1 model/src/main/java/org/openremote/model/notification/Notification.java
  14. +5 −0 model/src/main/java/org/openremote/model/query/BaseAssetQuery.java
  15. +7 −0 model/src/main/java/org/openremote/model/query/UserQuery.java
  16. +0 −1 model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java
  17. +8 −8 model/src/main/java/org/openremote/model/query/filter/DateTimePredicate.java
  18. +9 −0 model/src/main/java/org/openremote/model/rules/AssetState.java
  19. +1 −4 model/src/main/java/org/openremote/model/rules/Ruleset.java
  20. +22 −4 model/src/main/java/org/openremote/model/util/TimeUtil.java
  21. +9 −10 test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
  22. +4 −1 test/src/test/groovy/org/openremote/test/rules/BasicRulesFactsTest.groovy
@@ -444,6 +444,6 @@ protected void refreshNotifications(Consumer<SentNotification[]> notificationCon
}

protected Notification buildNotification() {
return new Notification("Manager Message", sendOptions.getMessage(), new Notification.Targets(sendOptions.selectedTargetType, sendOptions.selectedTarget));
return new Notification("Manager Message", sendOptions.getMessage(), new Notification.Targets(sendOptions.selectedTargetType, sendOptions.selectedTarget), null, null);
}
}
@@ -40,6 +40,7 @@
import org.openremote.container.web.WebService;
import org.openremote.manager.asset.console.ConsoleResourceImpl;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.rules.AssetQueryPredicate;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.model.Constants;
import org.openremote.model.ValidationFailure;
@@ -54,7 +55,9 @@
import org.openremote.model.query.filter.*;
import org.openremote.model.security.ClientRole;
import org.openremote.model.security.User;
import org.openremote.model.util.Pair;
import org.openremote.model.util.TextUtil;
import org.openremote.model.util.TimeUtil;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
@@ -64,8 +67,13 @@
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -411,7 +419,7 @@ public boolean delete(String assetId) {
}

public boolean isUserAsset(String assetId) {
return isUserAsset(null, assetId);
return isUserAsset((String)null, assetId);
}

public boolean isUserAsset(String userId, String assetId) {
@@ -436,6 +444,21 @@ public boolean isUserAsset(String userId, String assetId) {
});
}

public boolean isUserAsset(List<String> userIds, String assetId) {
return persistenceService.doReturningTransaction(entityManager -> {
try {
return entityManager.createQuery(
"select count(ua) from UserAsset ua where ua.id.userId in :userIds and ua.id.assetId = :assetId",
Long.class)
.setParameter("userIds", userIds)
.setParameter("assetId", assetId)
.getSingleResult() > 0;
} catch (NoResultException ex) {
return false;
}
});
}

public boolean isUserAssets(String userId, List<String> assetIds) {
return persistenceService.doReturningTransaction(entityManager -> {
try {
@@ -642,6 +665,7 @@ protected PreparedAssetQuery buildQuery(BaseAssetQuery query) {
}

sb.append(buildOrderByString(query));
sb.append(buildLimitString(query));
return new PreparedAssetQuery(sb.toString(), binders);
}

@@ -830,6 +854,13 @@ protected String buildOrderByString(BaseAssetQuery query) {
return sb.toString();
}

protected String buildLimitString(BaseAssetQuery query) {
if (query.limit > 0) {
return " LIMIT " + query.limit;
}
return "";
}

protected String buildWhereClause(BaseAssetQuery query, int level, List<ParameterBinder> binders) {
// level = 1 is main query
// level = 2 is union
@@ -1082,36 +1113,34 @@ protected String buildAttributeFilter(AttributePredicate attributePredicate, Lis
final int keyFormatPos = binders.size() + 1;
binders.add(st -> st.setString(keyFormatPos, dateTimePredicate.dateFormat));

Pair<Long, Long> fromAndTo = AssetQueryPredicate.asFromAndTo(timerService.getCurrentTimeMillis(), dateTimePredicate);

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));
binders.add(st -> st.setDate(pos, new java.sql.Date(fromAndTo.key)));

switch (dateTimePredicate.operator) {
case EQUALS:
attributeBuilder.append(" = to_timestamp(?, ?)");
attributeBuilder.append(" = ?");
break;
case NOT_EQUALS:
attributeBuilder.append(" <> to_timestamp(?, ?)");
attributeBuilder.append(" <> ?");
break;
case GREATER_THAN:
attributeBuilder.append(" > to_timestamp(?, ?)");
attributeBuilder.append(" > ?");
break;
case GREATER_EQUALS:
attributeBuilder.append(" >= to_timestamp(?, ?)");
attributeBuilder.append(" >= ?");
break;
case LESS_THAN:
attributeBuilder.append(" < to_timestamp(?, ?)");
attributeBuilder.append(" < ?");
break;
case LESS_EQUALS:
attributeBuilder.append(" <= to_timestamp(?, ?)");
attributeBuilder.append(" <= ?");
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));
binders.add(st -> st.setDate(pos2, new java.sql.Date(fromAndTo.value)));
attributeBuilder.append(" BETWEEN ? AND ?");
break;
}
} else if (attributePredicate.value instanceof NumberPredicate) {
@@ -526,7 +526,7 @@ protected Instant getRepeatAfterTimestamp(Notification notification, Instant las
break;
}
} else if (!TextUtil.isNullOrEmpty(notification.getRepeatInterval())) {
timestamp = lastSend.plus(TimeUtil.parseTimeString(notification.getRepeatInterval()), ChronoUnit.MILLIS);
timestamp = lastSend.plus(TimeUtil.parseTimeDuration(notification.getRepeatInterval()), ChronoUnit.MILLIS);
}

return timestamp;
Oops, something went wrong.

0 comments on commit 8aa3d82

Please sign in to comment.