resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java

197 lines
7.3 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.instances.model.entities;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
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;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.FacetType;
2023-05-11 18:35:56 +02:00
import com.arcadedb.graph.Edge;
import com.arcadedb.graph.Vertex;
import com.arcadedb.graph.Vertex.DIRECTION;
import com.arcadedb.remote.RemoteDatabase;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-02-22 16:36:19 +01:00
public class FacetManagement extends EntityManagement<Facet, FacetType> {
protected ConsistsOfManagement consistsOfManagement;
protected ResourceManagement resourceManagement;
public FacetManagement() {
super(AccessType.FACET);
}
@Override
2021-03-05 10:46:59 +01:00
protected FacetNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new FacetNotFoundException(e.getMessage(), e.getCause());
}
@Override
2021-03-05 10:46:59 +01:00
public FacetAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(String message) {
return new FacetAvailableInAnotherContextException(message);
}
@Override
2021-03-05 10:46:59 +01:00
protected FacetAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new FacetAlreadyPresentException(message);
}
@Override
2021-02-16 18:43:47 +01:00
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
2021-02-17 11:29:43 +01:00
return serializeSelfAsJsonNode();
}
@Override
2023-05-11 18:35:56 +02:00
protected Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException {
return createVertex();
}
@Override
2023-05-11 18:35:56 +02:00
protected Vertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException {
Vertex facet = getElement();
facet = (Vertex) updateProperties(documentType, facet, jsonNode, ignoreKeys, ignoreStartWithKeys);
return facet;
}
@Override
protected void reallyRemoveFromContext()
throws ContextException, ResourceRegistryException {
if(entryPoint) {
2023-05-11 18:35:56 +02:00
Edge edge = getElement().getEdges(DIRECTION.IN).iterator().next();
2021-03-05 18:14:07 +01:00
consistsOfManagement = new ConsistsOfManagement();
2023-05-11 18:35:56 +02:00
consistsOfManagement.setElement(edge);
2021-03-05 18:14:07 +01:00
consistsOfManagement.setTargetEntityManagement(this);
// Not needed consistsOfManagement.setSourceSecurityContext(sourceSecurityContext);
consistsOfManagement.setTargetSecurityContext(targetSecurityContext);
2021-03-06 10:53:45 +01:00
affectedInstances.put(uuid, consistsOfManagement.serializeAsAffectedInstance());
2021-03-05 18:14:07 +01:00
2023-05-11 18:35:56 +02:00
Vertex vertex = getElement().getVertices(DIRECTION.IN).iterator().next();
resourceManagement = new ResourceManagement();
2023-05-11 18:35:56 +02:00
resourceManagement.setElement(vertex);
// Not needed resourceManagement.setSourceSecurityContext(sourceSecurityContext);
resourceManagement.setTargetSecurityContext(targetSecurityContext);
2021-03-05 18:14:07 +01:00
resourceManagement.addToRelationManagements(consistsOfManagement);
}
super.reallyRemoveFromContext();
}
@Override
protected void reallyDelete() throws FacetNotFoundException, ResourceRegistryException {
2021-03-05 15:44:04 +01:00
if(entryPoint) {
2023-05-11 18:35:56 +02:00
Edge edge = getElement().getEdges(DIRECTION.IN).iterator().next();
consistsOfManagement = new ConsistsOfManagement();
2023-05-11 18:35:56 +02:00
consistsOfManagement.setElement(edge);
consistsOfManagement.setTargetEntityManagement(this);
2021-03-06 10:53:45 +01:00
affectedInstances.put(uuid, consistsOfManagement.serializeAsAffectedInstance());
2021-03-05 15:44:04 +01:00
2023-05-11 18:35:56 +02:00
Vertex vertex = getElement().getVertices(DIRECTION.IN).iterator().next();
resourceManagement = new ResourceManagement();
2023-05-11 18:35:56 +02:00
resourceManagement.setElement(vertex);
2021-03-05 18:14:07 +01:00
resourceManagement.addToRelationManagements(consistsOfManagement);
}
2021-03-06 10:53:45 +01:00
affectedInstances.put(uuid, serializeAsAffectedInstance());
getElement().delete();
}
2021-02-22 11:19:11 +01:00
protected void checkResource() throws SchemaViolationException, ResourceRegistryException {
if(!entryPoint) {
return;
}
if(operation.isSafe()) {
/*
* The sanity check is not required for a safe operation.
*/
return;
}
if(operation == Operation.UPDATE) {
// an update to the Facet only modify the Facet properties, no need to check the source resource
return;
}
2023-05-11 18:35:56 +02:00
RemoteDatabase targetSecurityContextDatabase = null;
try {
if(resourceManagement==null) {
2023-05-11 18:35:56 +02:00
Vertex vertex = getElement().getVertices(DIRECTION.IN).iterator().next();
ResourceManagement resourceManagement = new ResourceManagement();
2023-05-11 18:35:56 +02:00
resourceManagement.setElement(vertex);
}
switch (operation) {
case CREATE: case DELETE:
resourceManagement.setWorkingContext(getWorkingContext());
2023-05-11 18:35:56 +02:00
resourceManagement.setDatabase(database);
break;
2021-03-05 18:14:07 +01:00
case ADD_TO_CONTEXT:
resourceManagement.setSourceSecurityContext(sourceSecurityContext);
resourceManagement.setTargetSecurityContext(targetSecurityContext);
2021-03-05 18:14:07 +01:00
resourceManagement.setWorkingContext(targetSecurityContext);
2023-05-11 18:35:56 +02:00
targetSecurityContextDatabase = targetSecurityContext.getRemoteDatabase(PermissionMode.READER);
resourceManagement.setDatabase(targetSecurityContextDatabase);
2021-03-05 18:14:07 +01:00
break;
case REMOVE_FROM_CONTEXT:
// Not needed resourceManagement.setSourceSecurityContext(sourceSecurityContext);
resourceManagement.setTargetSecurityContext(targetSecurityContext);
resourceManagement.setWorkingContext(targetSecurityContext);
2023-05-11 18:35:56 +02:00
targetSecurityContextDatabase = targetSecurityContext.getRemoteDatabase(PermissionMode.READER);
resourceManagement.setDatabase(targetSecurityContextDatabase );
break;
default:
// You should not be here
return;
}
2021-03-04 11:45:27 +01:00
resourceManagement.setOperation(operation);
resourceManagement.sanityCheck();
}catch (ResourceRegistryException e) {
throw e;
}catch (Exception e) {
throw new ResourceRegistryException(e);
}finally {
2023-05-11 18:35:56 +02:00
if(targetSecurityContextDatabase !=null) {
targetSecurityContextDatabase.close();
// database.activateOnCurrentThread();
}
}
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
super.sanityCheck();
checkResource();
}
@Override
public String create() throws AlreadyPresentException, ResourceRegistryException {
2021-03-02 16:19:29 +01:00
throw new SchemaViolationException("You cannot create a stand alone Facet");
}
2023-05-11 18:35:56 +02:00
public Vertex internalCreate() throws AlreadyPresentException, ResourceRegistryException {
if(entryPoint && operation == Operation.CREATE) {
2021-03-02 16:19:29 +01:00
throw new SchemaViolationException("You cannot create a stand alone Facet");
}
return super.internalCreate();
}
}