2019-11-06 12:14:27 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.instances.model.relations;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2021-03-03 17:18:36 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
2019-10-25 17:45:59 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
|
2019-11-04 18:01:20 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
|
|
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
|
2023-02-02 21:18:28 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.DeleteConstraint;
|
2019-02-13 12:33:29 +01:00
|
|
|
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;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2017-11-17 12:31:22 +01:00
|
|
|
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;
|
2021-03-02 10:58:23 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
|
2019-11-06 12:13:19 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
|
2021-03-02 10:58:23 +01:00
|
|
|
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;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
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;
|
2021-03-02 10:58:23 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2021-02-22 16:36:19 +01:00
|
|
|
public class ConsistsOfManagement extends RelationManagement<FacetManagement, FacetType> {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-11 11:01:10 +02:00
|
|
|
public static final PropagationConstraint DEFAULT_CONSISTS_OF_PC;
|
|
|
|
|
|
|
|
static {
|
|
|
|
DEFAULT_CONSISTS_OF_PC = new PropagationConstraintImpl();
|
2023-02-02 21:18:28 +01:00
|
|
|
DEFAULT_CONSISTS_OF_PC.setDeleteConstraint(DeleteConstraint.cascade);
|
2021-03-09 12:46:56 +01:00
|
|
|
DEFAULT_CONSISTS_OF_PC.setRemoveConstraint(RemoveConstraint.cascade);
|
2018-06-11 11:01:10 +02:00
|
|
|
DEFAULT_CONSISTS_OF_PC.setAddConstraint(AddConstraint.propagate);
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public ConsistsOfManagement() {
|
2019-11-04 18:01:20 +01:00
|
|
|
super(AccessType.CONSISTS_OF, Facet.class, DEFAULT_CONSISTS_OF_PC);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected ConsistsOfNotFoundException getSpecificNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ConsistsOfNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
public ConsistsOfAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
|
2017-11-17 11:42:37 +01:00
|
|
|
String message) {
|
|
|
|
return new ConsistsOfAvailableInAnotherContextException(message);
|
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected ConsistsOfAlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ConsistsOfAlreadyPresentException(message);
|
|
|
|
}
|
2017-11-17 12:31:22 +01:00
|
|
|
|
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
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;
|
2017-11-17 12:31:22 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-03-02 10:58:23 +01:00
|
|
|
protected void checkResource() throws SchemaViolationException, ResourceRegistryException {
|
|
|
|
if(!entryPoint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(operation.isSafe()) {
|
2021-09-16 15:23:04 +02:00
|
|
|
/*
|
|
|
|
* The sanity check is not required for a safe operation.
|
2021-03-02 10:58:23 +01:00
|
|
|
*/
|
|
|
|
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
|
2021-03-02 10:58:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
RemoteDatabase targetSecurityContextDatabase = null;
|
2021-03-02 10:58:23 +01:00
|
|
|
try {
|
2021-03-04 21:57:01 +01:00
|
|
|
ResourceManagement resourceManagement = getSourceEntityManagement();
|
|
|
|
|
2021-03-02 10:58:23 +01:00
|
|
|
switch (operation) {
|
|
|
|
case CREATE: case DELETE:
|
|
|
|
break;
|
2021-03-04 21:57:01 +01:00
|
|
|
|
2021-03-02 10:58:23 +01:00
|
|
|
case ADD_TO_CONTEXT: case REMOVE_FROM_CONTEXT:
|
2021-03-04 21:57:01 +01:00
|
|
|
resourceManagement = new ResourceManagement();
|
|
|
|
resourceManagement.setElementType(sourceEntityManagement.getTypeName());
|
|
|
|
resourceManagement.setUUID(sourceEntityManagement.getUUID());
|
2021-09-15 16:22:48 +02:00
|
|
|
if(operation == Operation.ADD_TO_CONTEXT) {
|
|
|
|
resourceManagement.setSourceSecurityContext(sourceSecurityContext);
|
|
|
|
}
|
|
|
|
resourceManagement.setTargetSecurityContext(targetSecurityContext);
|
2021-03-02 10:58:23 +01:00
|
|
|
resourceManagement.setWorkingContext(targetSecurityContext);
|
2023-05-11 18:35:56 +02:00
|
|
|
targetSecurityContextDatabase = targetSecurityContext.getRemoteDatabase(PermissionMode.READER);
|
|
|
|
resourceManagement.setDatabase(targetSecurityContextDatabase);
|
2021-03-02 10:58:23 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// You should not be here
|
|
|
|
return;
|
|
|
|
}
|
2021-03-04 21:57:01 +01:00
|
|
|
resourceManagement.setOperation(operation);
|
2021-03-05 18:14:07 +01:00
|
|
|
resourceManagement.addToRelationManagements(this);
|
2021-03-02 10:58:23 +01:00
|
|
|
resourceManagement.sanityCheck();
|
2021-03-04 21:57:01 +01:00
|
|
|
}catch (ResourceRegistryException e) {
|
|
|
|
throw e;
|
2021-03-02 10:58:23 +01:00
|
|
|
}catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}finally {
|
2023-05-11 18:35:56 +02:00
|
|
|
if(targetSecurityContextDatabase!=null) {
|
|
|
|
targetSecurityContextDatabase.close();
|
|
|
|
// database.activateOnCurrentThread();
|
2021-03-02 10:58:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:50:16 +01:00
|
|
|
@Override
|
2021-02-19 19:32:23 +01:00
|
|
|
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
2021-03-02 10:58:23 +01:00
|
|
|
super.sanityCheck();
|
|
|
|
checkResource();
|
2021-02-05 17:50:16 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|