From fb230cee943e9d30571ff615aa8f440fab02cb0e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 31 Oct 2024 17:39:48 +0100 Subject: [PATCH] Reorganizing code --- .../base/ElementManagementUtility.java | 58 +-------------- .../instances/model/ERManagementUtility.java | 70 ++++++++++++++++++- .../resourceregistry/queries/QueryImpl.java | 3 +- .../queries/json/JsonQuery.java | 3 +- .../resourceregistry/rest/Access.java | 12 ++-- .../rest/InstancesManager.java | 14 ++-- .../resourceregistry/rest/SharingManager.java | 16 ++--- 7 files changed, 94 insertions(+), 82 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/base/ElementManagementUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/base/ElementManagementUtility.java index 2947364..8599118 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/base/ElementManagementUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/base/ElementManagementUtility.java @@ -3,13 +3,10 @@ package org.gcube.informationsystem.resourceregistry.base; import java.util.Optional; import java.util.UUID; -import org.gcube.informationsystem.base.reference.AccessType; -import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; -import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.environments.Environment; @@ -19,7 +16,6 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Res import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; -import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,48 +33,6 @@ public class ElementManagementUtility { private static Logger logger = LoggerFactory.getLogger(ElementManagementUtility.class); - public static ElementManagement getERManagement(String type) throws ResourceRegistryException { - - AccessType accessType = TypesCache.getInstance().getCachedType(type).getAccessType(); - - ElementManagement erManagement = null; - - switch (accessType) { - case RESOURCE: - erManagement = new ResourceManagement(); - break; - - case FACET: - erManagement = new FacetManagement(); - break; - - case IS_RELATED_TO: - erManagement = new IsRelatedToManagement(); - break; - - case CONSISTS_OF: - erManagement = new ConsistsOfManagement(); - break; - - default: - throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); - } - erManagement.setElementType(type); - - return erManagement; - } - - public static ElementManagement getERManagement(Environment workingContext, ODatabaseDocument orientGraph, - OElement element) throws ResourceRegistryException { - if(element instanceof OVertex) { - return getEntityManagement(workingContext, orientGraph, (OVertex) element); - } else if(element instanceof OEdge) { - return getRelationManagement(workingContext, orientGraph, (OEdge) element); - } - throw new ResourceRegistryException(String.format("%s is not a %s nor a %s", element.getClass().getSimpleName(), - Entity.NAME, Relation.NAME)); - } - public static OElement getAnyElementByUUID(UUID uuid) throws NotFoundException, ResourceRegistryException { try { return OrientDBUtility.getElementByUUIDAsAdmin(null, uuid, OVertex.class); @@ -104,17 +58,7 @@ public class ElementManagementUtility { } } - public static ElementManagement getERManagementFromUUID(Environment workingContext, ODatabaseDocument orientGraph, - UUID uuid) throws ResourceRegistryException { - OElement element; - try { - element = getAnyElementByUUID(orientGraph, uuid); - return getERManagement(workingContext, orientGraph, element); - } catch(Exception e) { - throw new ResourceRegistryException(String.format("%s does not belong to an %s nor to a %s", - uuid.toString(), Entity.NAME, Relation.NAME)); - } - } + public static EntityManagement getEntityManagement(Environment workingContext, ODatabaseDocument oDatabaseDocument, OVertex vertex) throws ResourceRegistryException { 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 f8f9989..818410a 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 @@ -6,25 +6,91 @@ import java.util.Set; import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.model.reference.entities.Entity; +import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException; import org.gcube.informationsystem.resourceregistry.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; +import org.gcube.informationsystem.resourceregistry.environments.Environment; import org.gcube.informationsystem.resourceregistry.environments.Environment.PermissionMode; import org.gcube.informationsystem.resourceregistry.environments.administration.AdminEnvironment; import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment; +import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; +import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; +import com.orientechnologies.orient.core.record.OEdge; +import com.orientechnologies.orient.core.record.OElement; +import com.orientechnologies.orient.core.record.OVertex; public class ERManagementUtility { private static Logger staticLogger = LoggerFactory.getLogger(ERManagementUtility.class); +public static ElementManagement getERManagement(String type) throws ResourceRegistryException { + + AccessType accessType = TypesCache.getInstance().getCachedType(type).getAccessType(); + + ElementManagement erManagement = null; + + switch (accessType) { + case RESOURCE: + erManagement = new ResourceManagement(); + break; + + case FACET: + erManagement = new FacetManagement(); + break; + + case IS_RELATED_TO: + erManagement = new IsRelatedToManagement(); + break; + + case CONSISTS_OF: + erManagement = new ConsistsOfManagement(); + break; + + default: + throw new ResourceRegistryException(String.format("%s is not querable", type.toString())); + } + erManagement.setElementType(type); + + return erManagement; + } + + public static ElementManagement getERManagementFromUUID(Environment workingContext, ODatabaseDocument orientGraph, + UUID uuid) throws ResourceRegistryException { + OElement element; + try { + element = ElementManagementUtility.getAnyElementByUUID(orientGraph, uuid); + return getERManagement(workingContext, orientGraph, element); + } catch(Exception e) { + throw new ResourceRegistryException(String.format("%s does not belong to an %s nor to a %s", + uuid.toString(), Entity.NAME, Relation.NAME)); + } + } + + public static ElementManagement getERManagement(Environment workingContext, ODatabaseDocument orientGraph, + OElement element) throws ResourceRegistryException { + if(element instanceof OVertex) { + return ElementManagementUtility.getEntityManagement(workingContext, orientGraph, (OVertex) element); + } else if(element instanceof OEdge) { + return ElementManagementUtility.getRelationManagement(workingContext, orientGraph, (OEdge) element); + } + throw new ResourceRegistryException(String.format("%s is not a %s nor a %s", element.getClass().getSimpleName(), + Entity.NAME, Relation.NAME)); + } + public static Map addToContextNoPropagationConstraint(Map expectedInstances, UUID contextUUID, boolean dryRun) throws NotFoundException, ContextException, ResourceRegistryException { Set instances = expectedInstances.keySet(); @@ -44,7 +110,7 @@ public class ERManagementUtility { Set uuids = expectedInstances.keySet(); for(UUID uuid : uuids) { String type = TypeUtility.getTypeName(expectedInstances.get(uuid)); - ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + ElementManagement elementManagement = ERManagementUtility.getERManagement(type); elementManagement.setWorkingEnvironment(adminEnvironment); elementManagement.setODatabaseDocument(oDatabaseDocument); elementManagement.setUUID(uuid); @@ -124,7 +190,7 @@ public class ERManagementUtility { Map> instancesManagement = new HashMap<>(); for(UUID uuid : expectedInstances.keySet()) { String type = TypeUtility.getTypeName(expectedInstances.get(uuid)); - ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); + ElementManagement elementManagement = ERManagementUtility.getERManagement(type); elementManagement.setWorkingEnvironment(adminEnvironment); elementManagement.setODatabaseDocument(oDatabaseDocument); elementManagement.setUUID(uuid); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java index e8ef276..62ce2a1 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/queries/QueryImpl.java @@ -10,6 +10,7 @@ import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtilit import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.environments.Environment.PermissionMode; import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +60,7 @@ public class QueryImpl implements Query { } } else { OElement element = ElementManagementUtility.getElementFromOptional(oResult.getElement()); - ElementManagement erManagement = ElementManagementUtility.getERManagement(instanceEnvironment, oDatabaseDocument, + ElementManagement erManagement = ERManagementUtility.getERManagement(instanceEnvironment, oDatabaseDocument, element); erManagement.setAsEntryPoint(); jsonNode = erManagement.serializeAsJsonNode(); 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 0538a60..715a06d 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 @@ -21,6 +21,7 @@ import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtilit import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.environments.Environment.PermissionMode; import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.queries.json.base.JsonQueryERElement; import org.gcube.informationsystem.resourceregistry.queries.json.base.entities.JsonQueryFacet; import org.gcube.informationsystem.resourceregistry.queries.json.base.entities.JsonQueryResource; @@ -185,7 +186,7 @@ public class JsonQuery { try { JsonNode jsonNodeResult = null; - ElementManagement erManagement = ElementManagementUtility.getERManagement(instanceEnvironment, oDatabaseDocument, + ElementManagement erManagement = ERManagementUtility.getERManagement(instanceEnvironment, oDatabaseDocument, element); erManagement.setAsEntryPoint(); jsonNodeResult = erManagement.serializeAsJsonNode(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java index 8e55723..d54b448 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java @@ -36,9 +36,9 @@ import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; import org.gcube.informationsystem.resourceregistry.base.ElementManagement; -import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.queries.Query; import org.gcube.informationsystem.resourceregistry.queries.QueryImpl; @@ -240,7 +240,7 @@ public class Access extends BaseRest { serverRequestInfo.checkAllBooleanQueryParameters(); serverRequestInfo.checkLimitOffset(); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); return erManagement.all(polymorphic); } @@ -260,7 +260,7 @@ public class Access extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); try { erManagement.setUUID(UUID.fromString(uuid)); @@ -298,7 +298,7 @@ public class Access extends BaseRest { serverRequestInfo.checkAllBooleanQueryParameters(); @SuppressWarnings("rawtypes") - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setElementType(type); erManagement.setUUID(UUID.fromString(uuid)); @@ -318,7 +318,7 @@ public class Access extends BaseRest { logger.info("Requested to get contexts of {} with UUID {}", type, instanceId); setAccountingMethod(InstancesManager.GET_INSTANCE_CONTEXTS_METHOD); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setUUID(UUID.fromString(instanceId)); return erManagement.getContexts(); } @@ -472,7 +472,7 @@ public class Access extends BaseRest { serverRequestInfo.checkAllBooleanQueryParameters(); serverRequestInfo.checkLimitOffset(); - ElementManagement erManagement = ElementManagementUtility.getERManagement(resourcetype); + ElementManagement erManagement = ERManagementUtility.getERManagement(resourcetype); if(erManagement instanceof ResourceManagement) { UUID refereceUUID = null; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java index ff2a025..e1daf0b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/InstancesManager.java @@ -25,7 +25,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.reso import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.base.ElementManagement; -import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtility; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.rest.requests.ServerRequestInfo; /** @@ -62,7 +62,7 @@ public class InstancesManager extends BaseRest { serverRequestInfo.checkAllBooleanQueryParameters(); serverRequestInfo.checkLimitOffset(); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); return erManagement.all(polymorphic); } @@ -84,7 +84,7 @@ public class InstancesManager extends BaseRest { serverRequestInfo.checkBooleanQueryParameter(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER); @SuppressWarnings("rawtypes") - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); try { erManagement.setUUID(UUID.fromString(uuid)); boolean found = erManagement.exists(); @@ -121,7 +121,7 @@ public class InstancesManager extends BaseRest { ServerRequestInfo serverRequestInfo = initRequestInfo(); serverRequestInfo.checkAllBooleanQueryParameters(); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setElementType(type); erManagement.setUUID(UUID.fromString(uuid)); return erManagement.read().toString(); @@ -148,7 +148,7 @@ public class InstancesManager extends BaseRest { serverRequestInfo.checkAllIncludeQueryParameters(); @SuppressWarnings("rawtypes") - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setUUID(UUID.fromString(uuid)); erManagement.setElementType(type); erManagement.setJson(json); @@ -167,7 +167,7 @@ public class InstancesManager extends BaseRest { logger.info("Requested to delete {} with id {}", type, uuid); setAccountingMethod(Method.DELETE, InstancesManager.INSTANCE); - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setUUID(UUID.fromString(uuid)); erManagement.delete(); @@ -195,7 +195,7 @@ public class InstancesManager extends BaseRest { setAccountingMethod(InstancesManager.GET_INSTANCE_CONTEXTS_METHOD); @SuppressWarnings("rawtypes") - ElementManagement erManagement = ElementManagementUtility.getERManagement(type); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); erManagement.setUUID(UUID.fromString(instanceId)); return erManagement.getContexts(); } 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 91955ac..821a26d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/SharingManager.java @@ -24,8 +24,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaV import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath; import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation; import org.gcube.informationsystem.resourceregistry.base.ElementManagement; -import org.gcube.informationsystem.resourceregistry.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; +import org.gcube.informationsystem.resourceregistry.instances.model.ERManagementUtility; import org.gcube.informationsystem.resourceregistry.rest.requests.ServerRequestInfo; import org.gcube.informationsystem.resourceregistry.utils.UUIDUtility; import org.gcube.informationsystem.utils.TypeUtility; @@ -192,22 +192,22 @@ public class SharingManager extends BaseRest { setAccountingMethod(calledMethod.toString()); - ElementManagement elementManagement = ElementManagementUtility.getERManagement(type); - elementManagement.setUUID(UUID.fromString(instanceId)); - elementManagement.setDryRun(dryRun); + ElementManagement erManagement = ERManagementUtility.getERManagement(type); + erManagement.setUUID(UUID.fromString(instanceId)); + erManagement.setDryRun(dryRun); UUID contextUUID = UUID.fromString(contextId); if(operation == SharingOperation.ADD) { - ((ERManagement) elementManagement).setForceAddToContext(forceAddToContext); - ((ERManagement) elementManagement).addToContext(contextUUID); + ((ERManagement) erManagement).setForceAddToContext(forceAddToContext); + ((ERManagement) erManagement).addToContext(contextUUID); }else { - ((ERManagement) elementManagement).removeFromContext(contextUUID); + ((ERManagement) erManagement).removeFromContext(contextUUID); } try { ObjectMapper objectMapper = new ObjectMapper(); - return serializeAffectedInstaces(objectMapper, elementManagement.getAffectedInstances()); + return serializeAffectedInstaces(objectMapper, erManagement.getAffectedInstances()); } catch (Exception e) { throw new ResourceRegistryException(e); }