Reorganizing code

This commit is contained in:
Luca Frosini 2024-10-31 17:39:48 +01:00
parent e29a78cd2d
commit fb230cee94
7 changed files with 94 additions and 82 deletions

View File

@ -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 {

View File

@ -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<UUID,JsonNode> addToContextNoPropagationConstraint(Map<UUID, JsonNode> expectedInstances, UUID contextUUID, boolean dryRun)
throws NotFoundException, ContextException, ResourceRegistryException {
Set<UUID> instances = expectedInstances.keySet();
@ -44,7 +110,7 @@ public class ERManagementUtility {
Set<UUID> 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<UUID, ElementManagement<?,?>> 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);

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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();
}

View File

@ -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);
}