2019-11-06 12:13:19 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.instances.model.entities;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2020-07-07 17:15:22 +02:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
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.reference.entities.Facet;
|
2021-03-02 11:54:59 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2022-02-01 16:49:12 +01:00
|
|
|
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;
|
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;
|
2021-03-05 15:04:27 +01:00
|
|
|
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;
|
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;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2021-02-22 16:36:19 +01:00
|
|
|
public class FacetManagement extends EntityManagement<Facet, FacetType> {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-03-05 15:04:27 +01:00
|
|
|
protected ConsistsOfManagement consistsOfManagement;
|
2021-03-04 21:57:01 +01:00
|
|
|
protected ResourceManagement resourceManagement;
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public FacetManagement() {
|
2017-03-29 16:53:37 +02:00
|
|
|
super(AccessType.FACET);
|
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 FacetNotFoundException getSpecificNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new FacetNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
public FacetAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new FacetAvailableInAnotherContextException(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected FacetAlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new FacetAlreadyPresentException(message);
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-16 18:43:47 +01:00
|
|
|
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
|
2021-02-17 11:29:43 +01:00
|
|
|
return serializeSelfAsJsonNode();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
|
|
|
@Override
|
2023-05-11 18:35:56 +02:00
|
|
|
protected Vertex reallyCreate() throws FacetAlreadyPresentException, ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
return createVertex();
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@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);
|
2016-12-19 14:59:27 +01:00
|
|
|
return facet;
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:57:01 +01:00
|
|
|
@Override
|
2021-03-05 16:19:55 +01:00
|
|
|
protected void reallyRemoveFromContext()
|
2021-03-04 21:57:01 +01:00
|
|
|
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);
|
2021-09-15 16:22:48 +02:00
|
|
|
// 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();
|
2021-03-04 21:57:01 +01:00
|
|
|
resourceManagement = new ResourceManagement();
|
2023-05-11 18:35:56 +02:00
|
|
|
resourceManagement.setElement(vertex);
|
2021-09-15 16:22:48 +02:00
|
|
|
// Not needed resourceManagement.setSourceSecurityContext(sourceSecurityContext);
|
|
|
|
resourceManagement.setTargetSecurityContext(targetSecurityContext);
|
2021-03-05 18:14:07 +01:00
|
|
|
resourceManagement.addToRelationManagements(consistsOfManagement);
|
2021-03-04 21:57:01 +01:00
|
|
|
}
|
2021-03-05 16:19:55 +01:00
|
|
|
super.reallyRemoveFromContext();
|
2021-03-04 21:57:01 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2021-03-05 11:59:24 +01:00
|
|
|
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();
|
2021-03-05 15:04:27 +01:00
|
|
|
consistsOfManagement = new ConsistsOfManagement();
|
2023-05-11 18:35:56 +02:00
|
|
|
consistsOfManagement.setElement(edge);
|
2021-03-05 15:04:27 +01:00
|
|
|
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();
|
2021-03-04 21:57:01 +01:00
|
|
|
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-04 21:57:01 +01:00
|
|
|
}
|
2021-03-05 11:59:24 +01:00
|
|
|
|
2021-03-06 10:53:45 +01:00
|
|
|
affectedInstances.put(uuid, serializeAsAffectedInstance());
|
2021-03-05 11:59:24 +01:00
|
|
|
getElement().delete();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2021-02-22 11:19:11 +01:00
|
|
|
|
2021-03-02 10:58:23 +01:00
|
|
|
protected void checkResource() throws SchemaViolationException, ResourceRegistryException {
|
|
|
|
if(!entryPoint) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-16 15:23:04 +02:00
|
|
|
|
2021-03-02 10:58:23 +01:00
|
|
|
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) {
|
|
|
|
// 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;
|
2021-03-02 10:58:23 +01:00
|
|
|
try {
|
2021-03-04 21:57:01 +01:00
|
|
|
if(resourceManagement==null) {
|
2023-05-11 18:35:56 +02:00
|
|
|
Vertex vertex = getElement().getVertices(DIRECTION.IN).iterator().next();
|
2021-03-04 21:57:01 +01:00
|
|
|
ResourceManagement resourceManagement = new ResourceManagement();
|
2023-05-11 18:35:56 +02:00
|
|
|
resourceManagement.setElement(vertex);
|
2021-03-04 21:57:01 +01:00
|
|
|
}
|
2021-03-02 10:58:23 +01:00
|
|
|
switch (operation) {
|
|
|
|
case CREATE: case DELETE:
|
|
|
|
resourceManagement.setWorkingContext(getWorkingContext());
|
2023-05-11 18:35:56 +02:00
|
|
|
resourceManagement.setDatabase(database);
|
2021-03-02 10:58:23 +01:00
|
|
|
break;
|
|
|
|
|
2021-03-05 18:14:07 +01:00
|
|
|
case ADD_TO_CONTEXT:
|
2021-09-15 16:22:48 +02:00
|
|
|
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:
|
2021-09-15 16:22:48 +02:00
|
|
|
// Not needed 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 11:45:27 +01:00
|
|
|
resourceManagement.setOperation(operation);
|
2021-09-16 15:23:04 +02:00
|
|
|
|
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-03-02 10:25:58 +01:00
|
|
|
@Override
|
|
|
|
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
|
|
|
super.sanityCheck();
|
2021-03-02 10:58:23 +01:00
|
|
|
checkResource();
|
2021-03-02 10:25:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 11:54:59 +01:00
|
|
|
@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");
|
2021-03-02 11:54:59 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
public Vertex internalCreate() throws AlreadyPresentException, ResourceRegistryException {
|
2021-03-02 11:54:59 +01:00
|
|
|
if(entryPoint && operation == Operation.CREATE) {
|
2021-03-02 16:19:29 +01:00
|
|
|
throw new SchemaViolationException("You cannot create a stand alone Facet");
|
2021-03-02 11:54:59 +01:00
|
|
|
}
|
|
|
|
return super.internalCreate();
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|