Skip to content

armgit / core

Public Imported from GitHub
0

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

Added group asset type and associated AssetStorageService merge logic

Rich Turner4c010f6be646
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index c2c9aea50..354abb43b 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -36,6 +36,7 @@ import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.rules.AssetQueryPredicate;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
+import org.openremote.model.AbstractValueHolder;
import org.openremote.model.Constants;
import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.*;
@@ -424,9 +425,11 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
public Asset merge(Asset asset, boolean overrideVersion, boolean skipGatewayCheck, String userName) {
return persistenceService.doReturningTransaction(em -> {
+ Asset existing = null;
+
if (asset.getId() != null) {
- Asset existing = em.find(Asset.class, asset.getId());
+ existing = em.find(Asset.class, asset.getId());
// Verify type has not been changed
if (existing != null && !existing.getType().equals(asset.getType())) {
@@ -449,9 +452,11 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
if (asset.getParentId() != null) {
// If this is a not a root asset...
Asset parent = find(em, asset.getParentId(), true);
+
// .. the parent must exist
if (parent == null)
throw new IllegalStateException("Parent not found: " + asset.getParentId());
+
// ... the parent can not be a child of the asset
if (parent.pathContains(asset.getId()))
throw new IllegalStateException("Invalid parent");
@@ -463,6 +468,16 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// ... and if we don't have a realm identifier, use the parent's
asset.setRealm(parent.getRealm());
}
+
+ // if parent is of type group then this child asset must have the correct type
+ if (parent.getWellKnownType() == AssetType.GROUP) {
+ String childAssetType = parent.getAttribute("childAssetType")
+ .flatMap(AbstractValueHolder::getValueAsString)
+ .orElseThrow(() -> new IllegalStateException("Parent group asset doesn't have a valid child asset type attribute"));
+ if (!childAssetType.equals(asset.getType())) {
+ throw new IllegalStateException("Asset type does not match parent group's child asset type attribute");
+ }
+ }
}
// Validate realm
@@ -483,6 +498,24 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
throw new IllegalStateException("Storing asset failed, invalid attributes: " + invalid);
}
+ // Validate group child asset type attribute
+ if (asset.getWellKnownType() == AssetType.GROUP) {
+ String childAssetType = asset.getAttribute("childAssetType")
+ .flatMap(AssetAttribute::getValueAsString)
+ .map(childAssetTypeString -> TextUtil.isNullOrEmpty(childAssetTypeString) ? null : childAssetTypeString)
+ .orElseThrow(() -> new IllegalStateException("Asset type group childAssetType attribute must be a valid string"));
+
+ String existingChildAssetType = existing != null ? existing
+ .getAttribute("childAssetType")
+ .flatMap(AssetAttribute::getValueAsString)
+ .orElseThrow(() ->
+ new IllegalStateException("Asset type group childAssetType attribute must be a valid string")) : childAssetType;
+
+ if (!childAssetType.equals(existingChildAssetType)) {
+ throw new IllegalStateException("Asset type group childAssetType attribute cannot be changed");
+ }
+ }
+
// Update all empty attribute timestamps with server-time (a caller which doesn't have a
// reliable time source such as a browser should clear the timestamp when setting an attribute
// value).
diff --git a/model/src/main/java/org/openremote/model/asset/AssetType.java b/model/src/main/java/org/openremote/model/asset/AssetType.java
index e410045af..29c13f600 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetType.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetType.java
@@ -283,6 +283,10 @@ public enum AssetType implements AssetDescriptor {
new AttributeDescriptorImpl("disabled", BOOLEAN, Values.create(false))
),
+ GROUP(ASSET_NAMESPACE + ":group", "folder", "B3B3B3",
+ new AttributeDescriptorImpl("childAssetType", STRING, null, READ_ONLY)
+ ),
+
THING(ASSET_NAMESPACE + ":thing", "cube-outline", null);
final protected String type;