Skip to content

armgit / core

Public Imported from GitHub
0

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

Updated group assets to support adding sub types of the childAssetType

Rich Turnerfc751aa88f60
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 43492eb92..1c3548e13 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -669,7 +669,17 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
LOG.info(msg);
return new IllegalStateException(msg);
});
- if (!childAssetType.equals(asset.getType())) {
+
+ // Look through type hierarchy for a match - this allows sub types
+ Class<?> clazz = asset.getClass();
+ boolean typeMatch = childAssetType.equals(clazz.getSimpleName());
+
+ while (!typeMatch && clazz != Asset.class) {
+ clazz = clazz.getSuperclass();
+ typeMatch = childAssetType.equals(clazz.getSimpleName());
+ }
+
+ if (!typeMatch) {
String msg = "Asset type does not match parent GROUP asset's childAssetType attribute: asset=" + asset;
LOG.info(msg);
throw new IllegalStateException(msg);
diff --git a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
index 5c5bbc907..6e8272a49 100644
--- a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
@@ -164,7 +164,7 @@ public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleRe
);
if (consoleParent == null) {
- consoleParent = new GroupAsset(CONSOLE_PARENT_ASSET_NAME, ConsoleAsset.DESCRIPTOR);
+ consoleParent = new GroupAsset(CONSOLE_PARENT_ASSET_NAME, ConsoleAsset.class);
consoleParent.setChildAssetType(ConsoleAsset.DESCRIPTOR.getName());
consoleParent.setRealm(tenant.getRealm());
consoleParent = assetStorageService.merge(consoleParent);
diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
index bc15934aa..4b8b4b7fa 100644
--- a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
@@ -902,7 +902,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
// ### Parking ###
- GroupAsset parkingGroupAsset = new GroupAsset("Parking group", ParkingAsset.DESCRIPTOR);
+ GroupAsset parkingGroupAsset = new GroupAsset("Parking group", ParkingAsset.class);
parkingGroupAsset.setParent(mobilityAndSafety);
parkingGroupAsset.getAttributes().addOrReplace(
new Attribute<>("totalOccupancy", POSITIVE_INTEGER)
@@ -1276,7 +1276,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
// ### Ships ###
- GroupAsset shipGroupAsset = new GroupAsset("Ship group", ShipAsset.DESCRIPTOR);
+ GroupAsset shipGroupAsset = new GroupAsset("Ship group", ShipAsset.class);
shipGroupAsset.setParent(mobilityAndSafety);
shipGroupAsset.setId(UniqueIdentifierGenerator.generateId(shipGroupAsset.getName()));
shipGroupAsset = assetStorageService.merge(shipGroupAsset);
diff --git a/model/src/main/java/org/openremote/model/asset/impl/ElectricVehicleFleetGroupAsset.java b/model/src/main/java/org/openremote/model/asset/impl/ElectricVehicleFleetGroupAsset.java
index 47de7a2f1..8bc344d69 100644
--- a/model/src/main/java/org/openremote/model/asset/impl/ElectricVehicleFleetGroupAsset.java
+++ b/model/src/main/java/org/openremote/model/asset/impl/ElectricVehicleFleetGroupAsset.java
@@ -46,7 +46,7 @@ public class ElectricVehicleFleetGroupAsset extends GroupAsset {
}
public ElectricVehicleFleetGroupAsset(String name) {
- super(name, ElectricVehicleAsset.DESCRIPTOR);
+ super(name, ElectricVehicleAsset.class);
}
public Optional<String> getFleetCategory() {
diff --git a/model/src/main/java/org/openremote/model/asset/impl/GroupAsset.java b/model/src/main/java/org/openremote/model/asset/impl/GroupAsset.java
index b50923fcc..23247af05 100644
--- a/model/src/main/java/org/openremote/model/asset/impl/GroupAsset.java
+++ b/model/src/main/java/org/openremote/model/asset/impl/GroupAsset.java
@@ -29,8 +29,8 @@ import javax.persistence.Entity;
import java.util.Optional;
/**
- * An asset for grouping other assets together; if the {@link #CHILD_ASSET_TYPE} {@link Attribute} is specified then
- * all children must be of the specified type.
+ * An asset for grouping other assets together; The {@link #CHILD_ASSET_TYPE} {@link Attribute} must be specified and
+ * all children must be of the specified type or a subtype of the specified type.
*/
@Entity
public class GroupAsset extends Asset<GroupAsset> {
@@ -45,13 +45,8 @@ public class GroupAsset extends Asset<GroupAsset> {
super(null);
}
- protected GroupAsset(String name, AssetDescriptor<? extends GroupAsset> descriptor, String childAssetType) {
- super(name);
- setChildAssetType(childAssetType);
- }
-
- public GroupAsset(String name, AssetDescriptor<?> childAssetType) {
- this(name, childAssetType.getName());
+ public GroupAsset(String name, Class<? extends Asset<?>> childAssetType) {
+ this(name, childAssetType.getSimpleName());
}
public GroupAsset(String name, String childAssetType) {