resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/ConsistsOfManagement.java

157 lines
5.9 KiB
Java
Raw Normal View History

2019-11-06 12:14:27 +01:00
package org.gcube.informationsystem.resourceregistry.instances.model.relations;
2021-03-03 17:18:36 +01:00
import java.util.Iterator;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.DeleteConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
2021-03-03 17:18:36 +01:00
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.consistsof.ConsistsOfNotFoundException;
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.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
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 ConsistsOfManagement extends RelationManagement<FacetManagement, FacetType> {
public static final PropagationConstraint DEFAULT_CONSISTS_OF_PC;
static {
DEFAULT_CONSISTS_OF_PC = new PropagationConstraintImpl();
DEFAULT_CONSISTS_OF_PC.setDeleteConstraint(DeleteConstraint.cascade);
DEFAULT_CONSISTS_OF_PC.setRemoveConstraint(RemoveConstraint.cascade);
DEFAULT_CONSISTS_OF_PC.setAddConstraint(AddConstraint.propagate);
}
public ConsistsOfManagement() {
super(AccessType.CONSISTS_OF, Facet.class, DEFAULT_CONSISTS_OF_PC);
}
@Override
2021-03-05 10:46:59 +01:00
protected ConsistsOfNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new ConsistsOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
2021-03-05 10:46:59 +01:00
public ConsistsOfAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
String message) {
return new ConsistsOfAvailableInAnotherContextException(message);
}
@Override
2021-03-05 10:46:59 +01:00
protected ConsistsOfAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new ConsistsOfAlreadyPresentException(message);
}
@Override
protected FacetManagement newTargetEntityManagement() throws ResourceRegistryException {
2021-02-10 15:45:48 +01:00
FacetManagement facetManagement = new FacetManagement();
2023-05-11 18:35:56 +02:00
facetManagement.setDatabase(database);
2021-02-10 15:45:48 +01:00
facetManagement.setWorkingContext(getWorkingContext());
return facetManagement;
}
2021-02-05 17:50:16 +01:00
2021-03-03 17:18:36 +01:00
@Override
2023-05-11 18:35:56 +02:00
protected Edge reallyCreate() throws ResourceRegistryException {
Edge thisEdge = super.reallyCreate();
2021-03-03 17:18:36 +01:00
2023-05-11 18:35:56 +02:00
Vertex target = (Vertex) getTargetEntityManagement().getElement();
2021-03-03 17:18:36 +01:00
int count = 0;
2023-05-11 18:35:56 +02:00
Iterable<Edge> iterable = target.getEdges(DIRECTION.IN);
Iterator<Edge> iterator = iterable.iterator();
2021-03-03 17:18:36 +01:00
while(iterator.hasNext()) {
iterator.next();
++count;
}
if(count > 1) {
throw new SchemaViolationException("It is not possible to create multiple " + ConsistsOf.NAME + " between the same " + Facet.NAME);
}
2023-05-11 18:35:56 +02:00
return thisEdge;
2021-03-03 17:18:36 +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) {
2021-03-03 17:18:36 +01:00
// an update to the Facet only modify the ConsistsOf properties, no need to check the source resource
return;
}
2023-05-11 18:35:56 +02:00
RemoteDatabase targetSecurityContextDatabase = null;
try {
ResourceManagement resourceManagement = getSourceEntityManagement();
switch (operation) {
case CREATE: case DELETE:
break;
case ADD_TO_CONTEXT: case REMOVE_FROM_CONTEXT:
resourceManagement = new ResourceManagement();
resourceManagement.setElementType(sourceEntityManagement.getTypeName());
resourceManagement.setUUID(sourceEntityManagement.getUUID());
if(operation == Operation.ADD_TO_CONTEXT) {
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;
}
resourceManagement.setOperation(operation);
2021-03-05 18:14:07 +01:00
resourceManagement.addToRelationManagements(this);
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();
}
}
}
2021-02-05 17:50:16 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
super.sanityCheck();
checkResource();
2021-02-05 17:50:16 +01:00
}
}