Skip to content
Permalink
Browse files

Added some null checks to AssetStorageService and bug fixes for DateT…

…imePredicate
  • Loading branch information...
richturner committed May 11, 2019
1 parent a811f85 commit ff4104048a8779c2bca953dfb9a725659925def2
@@ -488,6 +488,9 @@ public boolean isUserAsset(String assetId) {
}

public boolean isUserAsset(String userId, String assetId) {
if (TextUtil.isNullOrEmpty(userId) || TextUtil.isNullOrEmpty(assetId)) {
return false;
}
return persistenceService.doReturningTransaction(entityManager -> {
try {
String queryStr = TextUtil.isNullOrEmpty(userId) ?
@@ -510,6 +513,9 @@ public boolean isUserAsset(String userId, String assetId) {
}

public boolean isUserAsset(List<String> userIds, String assetId) {
if (userIds == null || userIds.isEmpty() || TextUtil.isNullOrEmpty(assetId)) {
return false;
}
return persistenceService.doReturningTransaction(entityManager -> {
try {
return entityManager.createQuery(
@@ -525,6 +531,9 @@ public boolean isUserAsset(List<String> userIds, String assetId) {
}

public boolean isUserAssets(String userId, List<String> assetIds) {
if (TextUtil.isNullOrEmpty(userId) || assetIds == null || assetIds.isEmpty()) {
return false;
}
return persistenceService.doReturningTransaction(entityManager -> {
try {
return entityManager.createQuery(
@@ -550,6 +559,9 @@ public boolean isRealmAsset(String realm, String assetId) {
* Indicates if the specified assets belong to the specified realm
*/
public boolean isRealmAssets(String realm, List<String> assetIds) {
if (TextUtil.isNullOrEmpty(realm) || assetIds == null || assetIds.isEmpty()) {
return false;
}
return persistenceService.doReturningTransaction(entityManager -> {
try {
return entityManager.createQuery(
@@ -157,7 +157,7 @@ public void configure() throws Exception {
throw new NotificationProcessingException(MISSING_MESSAGE, "Notification message must be set");
}

if (notification.getTargets() == null || notification.getTargets().getType() == null || notification.getTargets().getIds() == null || Arrays.stream(notification.getTargets().getIds()).anyMatch(TextUtil::isNullOrEmpty)) {
if (notification.getTargets() == null || notification.getTargets().getType() == null || notification.getTargets().getIds() == null || notification.getTargets().getIds().length == 0 || Arrays.stream(notification.getTargets().getIds()).anyMatch(TextUtil::isNullOrEmpty)) {
throw new NotificationProcessingException(MISSING_TARGETS, "Notification targets must be set");
}

@@ -41,6 +41,7 @@
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
@@ -57,7 +58,6 @@
final protected NewAssetQuery query;
final protected TimerService timerService;
final protected AssetStorageService assetStorageService;
static final protected DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTimeNoMillis();

// TODO: Remove this ctor once asset queries merged
@SuppressWarnings("unchecked")
@@ -498,14 +498,14 @@ protected static boolean conditionIsEmpty(RuleCondition condition) {
if (TimeUtil.isTimeDuration(dateTimePredicate.value)) {
from = currentMillis + TimeUtil.parseTimeDuration(dateTimePredicate.value);
} else {
from = dateFormatter.parseMillis(dateTimePredicate.value);
from = ZonedDateTime.parse(dateTimePredicate.value).toInstant().toEpochMilli();
}

if (dateTimePredicate.operator == BaseAssetQuery.Operator.BETWEEN) {
if (TimeUtil.isTimeDuration(dateTimePredicate.rangeValue)) {
to = currentMillis + TimeUtil.parseTimeDuration(dateTimePredicate.rangeValue);
} else {
to = dateFormatter.parseMillis(dateTimePredicate.rangeValue);
to = ZonedDateTime.parse(dateTimePredicate.rangeValue).toInstant().toEpochMilli();
}
}
} catch (IllegalArgumentException e) {
@@ -7,7 +7,10 @@ import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
import org.openremote.model.asset.*
import org.openremote.model.asset.Asset
import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.asset.CalendarEventConfiguration
import org.openremote.model.attribute.MetaItemType
import org.openremote.model.calendar.CalendarEvent
import org.openremote.model.calendar.RecurrenceRule
@@ -22,11 +25,11 @@ import spock.util.concurrent.PollingConditions

import javax.persistence.EntityManager
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.function.Function

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME
import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME
import static org.openremote.model.asset.AssetType.THING
import static org.openremote.model.attribute.AttributeValueType.DATETIME
import static org.openremote.model.query.BaseAssetQuery.*
@@ -861,15 +864,15 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
)
lobby = assetStorageService.merge(lobby)

def rangeStart = LocalDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC).minusHours(2) // 28/01/2018 @ 1:00pm (UTC)
def rangeEnd = LocalDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC).plusHours(3) // 28/01/2018 @ 6:00pm (UTC)
def rangeStart = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC).minusHours(2) // 28/01/2018 @ 1:00pm (UTC)
def rangeEnd = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC).plusHours(3) // 28/01/2018 @ 6:00pm (UTC)

def assets = assetStorageService.findAll(
new AssetQuery()
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
.attributeValue(
"openingDate",
new DateTimePredicate(rangeStart.format(ISO_LOCAL_DATE_TIME), rangeEnd.format(ISO_LOCAL_DATE_TIME))
new DateTimePredicate(rangeStart.format(ISO_ZONED_DATE_TIME), rangeEnd.format(ISO_ZONED_DATE_TIME))
.operator(Operator.BETWEEN))
)

@@ -884,7 +887,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
.attributeValue(
"openingDate",
new DateTimePredicate(Operator.GREATER_THAN, rangeStart.format(ISO_LOCAL_DATE_TIME)))
new DateTimePredicate(Operator.GREATER_THAN, rangeStart.format(ISO_ZONED_DATE_TIME)))
)


@@ -898,7 +901,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
.attributeValue(
"openingDate",
new DateTimePredicate(Operator.LESS_THAN, rangeEnd.format(ISO_LOCAL_DATE_TIME)))
new DateTimePredicate(Operator.LESS_THAN, rangeEnd.format(ISO_ZONED_DATE_TIME)))
)


@@ -907,14 +910,14 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets[0].id == lobby.id

when: "the lobby has an opening date and the date is equal to the filtering date"
rangeStart = LocalDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC) // 28/01/2018 @ 2:00pm (UTC)
rangeStart = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1517151600000), ZoneOffset.UTC) // 28/01/2018 @ 2:00pm (UTC)

assets = assetStorageService.findAll(
new AssetQuery()
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
.attributeValue(
"openingDate",
new DateTimePredicate(Operator.EQUALS, rangeStart.format(ISO_LOCAL_DATE_TIME)))
new DateTimePredicate(Operator.EQUALS, rangeStart.format(ISO_ZONED_DATE_TIME)))
)


0 comments on commit ff41040

Please sign in to comment.
You can’t perform that action at this time.