From b1153f7283124d1b387d08a11b5a9f2d4665e742 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 21 Apr 2023 16:41:35 +0200 Subject: [PATCH] Migrated code to use utility --- .../instances/base/ElementManagement.java | 14 ++++---------- .../base/properties/PropertyElementManagement.java | 5 +++-- .../instances/model/ERManagementUtility.java | 5 +++-- .../instances/model/SharingOperationValidator.java | 5 +++-- .../resourceregistry/queries/json/JsonQuery.java | 4 ++-- .../queries/json/base/JsonQueryERElement.java | 7 ++++--- .../resourceregistry/rest/SharingManager.java | 4 ++-- .../instances/multicontext/MultiContextTest.java | 3 ++- 8 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java index 53ed7b0..cbba744 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/ElementManagement.java @@ -54,6 +54,7 @@ import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.entities.ResourceType; import org.gcube.informationsystem.types.reference.properties.PropertyDefinition; import org.gcube.informationsystem.types.reference.properties.PropertyType; +import org.gcube.informationsystem.utils.TypeUtility; import org.gcube.informationsystem.utils.UUIDManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -313,7 +314,7 @@ public abstract class ElementManagement { } if(typeName==null && jsonNode!=null) { - this.typeName = getClassProperty(jsonNode); + this.typeName = TypeUtility.getTypeName(jsonNode); } } @@ -339,7 +340,7 @@ public abstract class ElementManagement { } if(this.typeName == null) { - this.typeName = getClassProperty(jsonNode); + this.typeName = TypeUtility.getTypeName(jsonNode); getOClass(); } else { checkERMatch(); @@ -348,7 +349,7 @@ public abstract class ElementManagement { protected void checkERMatch() throws ResourceRegistryException { if(jsonNode != null) { - String type = getClassProperty(jsonNode); + String type = TypeUtility.getTypeName(jsonNode); if(type != null && type.compareTo(typeName) != 0) { String error = String.format("Requested type does not match with json representation %s!=%s", typeName, type); @@ -887,13 +888,6 @@ public abstract class ElementManagement { return getContextsAsObjectNode(objectMapper); } - public static String getClassProperty(JsonNode jsonNode) { - if(jsonNode.has(Element.CLASS_PROPERTY)) { - return jsonNode.get(Element.CLASS_PROPERTY).asText(); - } - return null; - } - public static Object getObjectFromJsonNode(JsonNode value) throws UnsupportedDataTypeException, ResourceRegistryException { JsonNodeType jsonNodeType = value.getNodeType(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java index f42421a..6e86a39 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/properties/PropertyElementManagement.java @@ -28,6 +28,7 @@ import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility; import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility; import org.gcube.informationsystem.types.reference.properties.PropertyType; +import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,9 +66,9 @@ public class PropertyElementManagement { ((ObjectNode) jsonNode).remove(Element.SUPERCLASSES_PROPERTY); } - if(jsonNode.has(Element.CLASS_PROPERTY)) { + String type = TypeUtility.getTypeName(jsonNode); + if(type!=null) { // Complex type - String type = ElementManagement.getClassProperty(jsonNode); OClass oClass = null; try { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java index 9c45d3d..cee17de 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/ERManagementUtility.java @@ -16,6 +16,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; +import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,7 +44,7 @@ public class ERManagementUtility { Map> instancesManagement = new HashMap<>(); Set uuids = expectedInstances.keySet(); for(UUID uuid : uuids) { - String type = expectedInstances.get(uuid).get(Element.CLASS_PROPERTY).asText(); + String type = TypeUtility.getTypeName(expectedInstances.get(uuid)); ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); elementManagement.setWorkingContext(adminSecurityContext); elementManagement.setODatabaseDocument(oDatabaseDocument); @@ -123,7 +124,7 @@ public class ERManagementUtility { Map> instancesManagement = new HashMap<>(); for(UUID uuid : expectedInstances.keySet()) { - String type = expectedInstances.get(uuid).get(Element.CLASS_PROPERTY).asText(); + String type = TypeUtility.getTypeName(expectedInstances.get(uuid)); ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); elementManagement.setWorkingContext(adminSecurityContext); elementManagement.setODatabaseDocument(oDatabaseDocument); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java index b59fd8a..f80df31 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/SharingOperationValidator.java @@ -9,6 +9,7 @@ import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; +import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,8 +60,8 @@ public class SharingOperationValidator implements OperationValidator { throw new ResourceRegistryException(errorMessage); } - String expectedType = expectedJsonNode.get(Element.CLASS_PROPERTY).asText(); - String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + String expectedType = TypeUtility.getTypeName(expectedJsonNode); + String affectedType = TypeUtility.getTypeName(affectedJsonNode); if(expectedType.compareTo(affectedType)!=0) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("The expected type of the instance with UUID "); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java index d1f5c29..1cb9782 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/JsonQuery.java @@ -9,7 +9,6 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Direction; -import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; @@ -27,6 +26,7 @@ import org.gcube.informationsystem.resourceregistry.queries.json.base.relations. import org.gcube.informationsystem.resourceregistry.types.CachedType; import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.Utility; +import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +67,7 @@ public class JsonQuery { } public static JsonQueryERElement getJsonQueryERElement(JsonNode jsonQuery) throws SchemaNotFoundException, SchemaException, ResourceRegistryException { - String type = jsonQuery.get(Element.CLASS_PROPERTY).asText(); + String type = TypeUtility.getTypeName(jsonQuery); AccessType accessType = TypesCache.getInstance().getCachedType(type).getAccessType(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/JsonQueryERElement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/JsonQueryERElement.java index e1a521d..8930090 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/JsonQueryERElement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/json/base/JsonQueryERElement.java @@ -18,6 +18,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaN import org.gcube.informationsystem.resourceregistry.queries.operators.QueryConditionalOperator; import org.gcube.informationsystem.resourceregistry.queries.operators.QueryLogicalOperator; import org.gcube.informationsystem.resourceregistry.types.TypesCache; +import org.gcube.informationsystem.utils.TypeUtility; public abstract class JsonQueryERElement { @@ -42,14 +43,14 @@ public abstract class JsonQueryERElement { public JsonQueryERElement(JsonNode jsonQuery, AccessType accessType) throws SchemaException, ResourceRegistryException { this.objectMapper = new ObjectMapper(); - this.type = jsonQuery.get(Element.CLASS_PROPERTY).asText(); + this.type = TypeUtility.getTypeName(jsonQuery); this.jsonNode = jsonQuery; this.accessType = accessType; this.entryPoint = false; this.fieldNamesToRemove = new HashSet<>(); - fieldNamesToRemove.add(Element.CLASS_PROPERTY); - fieldNamesToRemove.add(Element.SUPERCLASSES_PROPERTY); + this.fieldNamesToRemove.add(Element.CLASS_PROPERTY); + this.fieldNamesToRemove.add(Element.SUPERCLASSES_PROPERTY); validateType(this.type, this.accessType); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java index c9c04eb..e46f3d7 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java @@ -16,7 +16,6 @@ import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.common.authorization.library.provider.CalledMethodProvider; -import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.resourceregistry.ResourceInitializer; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -30,6 +29,7 @@ import org.gcube.informationsystem.resourceregistry.instances.base.ElementManage import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility; +import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -121,7 +121,7 @@ public class SharingManager { for(JsonNode node : arrayNode) { @SuppressWarnings("unused") - String type = node.get(Element.CLASS_PROPERTY).asText(); + String type = TypeUtility.getTypeName(node); UUID uuid = UUIDUtility.getUUID(node); expectedInstances.put(uuid, node); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java index 96548f4..f4728fe 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/MultiContextTest.java @@ -29,6 +29,7 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Res import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.types.TypeMapper; +import org.gcube.informationsystem.utils.TypeUtility; import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -260,7 +261,7 @@ public class MultiContextTest extends ERManagementTest { String expectedType = TypeMapper.getType(element.getClass()); JsonNode affectedJsonNode = affectedInstances.get(uuid); - String affectedType = affectedJsonNode.get(Element.CLASS_PROPERTY).asText(); + String affectedType = TypeUtility.getTypeName(affectedJsonNode); Assert.assertTrue(affectedType.compareTo(expectedType) == 0);