204 lines
7.9 KiB
Java
204 lines
7.9 KiB
Java
package org.gcube.informationsystem.resourceregistry.instances.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.contexts.security.SecurityContext;
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement;
|
|
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.instances.model.relations.RelationManagement;
|
|
import org.gcube.informationsystem.resourceregistry.types.TypesCache;
|
|
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.arcadedb.database.Document;
|
|
import com.arcadedb.graph.Edge;
|
|
import com.arcadedb.graph.Vertex;
|
|
import com.arcadedb.remote.RemoteDatabase;
|
|
import com.arcadedb.schema.DocumentType;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
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(SecurityContext workingContext, RemoteDatabase database,
|
|
Document element) throws ResourceRegistryException {
|
|
if(element instanceof Vertex) {
|
|
return getEntityManagement(workingContext, database, (Vertex) element);
|
|
} else if(element instanceof Edge) {
|
|
return getRelationManagement(workingContext, database, (Edge) element);
|
|
}
|
|
throw new ResourceRegistryException(String.format("%s is not a %s nor a %s", element.getClass().getSimpleName(),
|
|
Entity.NAME, Relation.NAME));
|
|
}
|
|
|
|
public static Document getAnyElementByUUID(UUID uuid) throws NotFoundException, ResourceRegistryException {
|
|
try {
|
|
return DBUtility.getElementByUUIDAsAdmin(null, uuid, Vertex.class);
|
|
} catch(NotFoundException e) {
|
|
return DBUtility.getElementByUUIDAsAdmin(null, uuid, Edge.class);
|
|
} catch(ResourceRegistryException e) {
|
|
throw e;
|
|
} catch(Exception e) {
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
}
|
|
|
|
public static Document getAnyElementByUUID(RemoteDatabase database, UUID uuid)
|
|
throws NotFoundException, ResourceRegistryException {
|
|
try {
|
|
return DBUtility.getElementByUUID(database, null, uuid, Vertex.class);
|
|
} catch(NotFoundException e) {
|
|
return DBUtility.getElementByUUID(database, null, uuid, Edge.class);
|
|
} catch(ResourceRegistryException e) {
|
|
throw e;
|
|
} catch(Exception e) {
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
}
|
|
|
|
public static ElementManagement<?, ?> getERManagementFromUUID(SecurityContext workingContext, RemoteDatabase database,
|
|
UUID uuid) throws ResourceRegistryException {
|
|
try {
|
|
Document element = getAnyElementByUUID(database, uuid);
|
|
return getERManagement(workingContext, database, 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(SecurityContext workingContext, RemoteDatabase database,
|
|
Vertex vertex) throws ResourceRegistryException {
|
|
|
|
if(database == null) {
|
|
throw new ResourceRegistryException("Database instance is null. " + DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
|
|
if(vertex == null) {
|
|
throw new ResourceRegistryException(Vertex.class.getSimpleName() + " instance is null. " + DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
|
|
DocumentType documentType = null;
|
|
try {
|
|
documentType = ElementManagementUtility.getDocumentType(vertex);
|
|
} catch(Exception e) {
|
|
String error = String.format("Unable to detect type of %s. %s", vertex.toString(),
|
|
DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
logger.error(error, e);
|
|
throw new ResourceRegistryException(error);
|
|
}
|
|
|
|
EntityManagement<?, ?> entityManagement = null;
|
|
if(documentType.isSubTypeOf(Resource.NAME)) {
|
|
entityManagement = new ResourceManagement();
|
|
} else if(documentType.isSubTypeOf(Facet.NAME)) {
|
|
entityManagement = new FacetManagement();
|
|
} else {
|
|
String error = String.format("{%s is not a %s nor a %s. %s", vertex, Resource.NAME, Facet.NAME,
|
|
DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw new ResourceRegistryException(error);
|
|
}
|
|
entityManagement.setDatabase(database);
|
|
entityManagement.setWorkingContext(workingContext);
|
|
entityManagement.setElement(vertex);
|
|
return entityManagement;
|
|
}
|
|
|
|
public static RelationManagement<?,?> getRelationManagement(SecurityContext workingContext, RemoteDatabase database,
|
|
Edge edge) throws ResourceRegistryException {
|
|
|
|
if(database == null) {
|
|
throw new ResourceRegistryException("Database instance is null. " + DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
|
|
if(edge == null) {
|
|
throw new ResourceRegistryException(
|
|
Edge.class.getSimpleName() + " instance is null. " + DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
|
|
DocumentType documentType = ElementManagementUtility.getDocumentType(edge);
|
|
|
|
RelationManagement<?,?> relationManagement = null;
|
|
if(documentType.isSubTypeOf(ConsistsOf.NAME)) {
|
|
relationManagement = new ConsistsOfManagement();
|
|
} else if(documentType.isSubTypeOf(IsRelatedTo.NAME)) {
|
|
relationManagement = new IsRelatedToManagement();
|
|
} else {
|
|
String error = String.format("{%s is not a %s nor a %s. %s", edge, ConsistsOf.NAME, IsRelatedTo.NAME,
|
|
DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw new ResourceRegistryException(error);
|
|
}
|
|
|
|
relationManagement.setDatabase(database);
|
|
relationManagement.setWorkingContext(workingContext);
|
|
|
|
relationManagement.setElement(edge);
|
|
return relationManagement;
|
|
}
|
|
|
|
public static <E extends Document> E getElementFromOptional(Optional<E> optional) throws ResourceRegistryException {
|
|
if(optional.isPresent()) {
|
|
return optional.get();
|
|
}else {
|
|
throw new ResourceRegistryException("An element not belonging to any defined type should not exists. Please contact the administrator.");
|
|
}
|
|
}
|
|
|
|
public static DocumentType getDocumentType(Document document) throws ResourceRegistryException {
|
|
DocumentType documentType = document.getType();
|
|
if(documentType==null) {
|
|
throw new ResourceRegistryException("An element not belonging to any defined type should not exists. Please contact the administrator.");
|
|
}
|
|
return documentType;
|
|
}
|
|
|
|
}
|