resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/entities/EntityElementManagement.java

174 lines
6.9 KiB
Java
Raw Normal View History

package org.gcube.informationsystem.resourceregistry.instances.base.entities;
import java.util.HashMap;
import java.util.Map;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.entities.EntityElement;
2021-02-05 17:50:16 +01:00
import org.gcube.informationsystem.base.reference.relations.RelationElement;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
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.entities.EntityAlreadyPresentException;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
import org.gcube.informationsystem.resourceregistry.instances.base.relations.RelationElementManagement;
2023-05-11 18:35:56 +02:00
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.EntityType;
2023-05-11 18:35:56 +02:00
import com.arcadedb.database.Document;
import com.arcadedb.graph.Edge;
import com.arcadedb.graph.MutableVertex;
import com.arcadedb.graph.Vertex;
import com.arcadedb.remote.RemoteDatabase;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2023-05-11 18:35:56 +02:00
public abstract class EntityElementManagement<E extends EntityElement, ET extends EntityType> extends ElementManagement<Vertex, ET> {
2021-02-26 18:46:00 +01:00
public final static String IN_PREFIX = "in_";
public final static String OUT_PREFIX = "out_";
/**
* Provide a cache edge-internal-id -> RelationManagement
* this avoid to recreate the relationManagement of already visited edges
*/
protected Map<String,RelationElementManagement<?,?,?,?>> relationManagements;
protected EntityElementManagement(AccessType accessType) {
super(accessType);
2021-02-26 18:41:55 +01:00
this.ignoreStartWithKeys.add(IN_PREFIX.toLowerCase());
this.ignoreStartWithKeys.add(OUT_PREFIX.toLowerCase());
this.ignoreStartWithKeys.add(IN_PREFIX.toUpperCase());
this.ignoreStartWithKeys.add(OUT_PREFIX.toUpperCase());
this.relationManagements = new HashMap<>();
}
2023-05-11 18:35:56 +02:00
protected EntityElementManagement(AccessType accessType, SecurityContext workingContext, RemoteDatabase database) {
this(accessType);
2023-05-11 18:35:56 +02:00
this.database = database;
setWorkingContext(workingContext);
}
/*
* It works perfectly in case of any kind of update. In case of use from create
* the cache does not work by using the ID because until commit the edge has a
* fake id starting with - (minus) sign. This not imply any collateral effect
* but a better solution is a desiderata.
*/
2023-05-11 18:35:56 +02:00
protected RelationElementManagement<?,?,?,?> getBaseRelationManagement(Edge edge) throws ResourceRegistryException {
String id = edge.getIdentity().toString();
RelationElementManagement<?,?,?,?> relationManagement = relationManagements.get(id);
if(relationManagement == null) {
2023-05-11 18:35:56 +02:00
relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(), database, edge);
relationManagements.put(id, relationManagement);
}
return relationManagement;
}
protected void addToRelationManagement(RelationElementManagement<?,?,?,?> baseRelationManagement)
throws ResourceRegistryException {
2023-05-11 18:35:56 +02:00
Document elem = baseRelationManagement.getElement();
String id = elem.getIdentity().toString();
if(relationManagements.get(id) != null && relationManagements.get(id) != baseRelationManagement) {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Two different instance of ");
errorMessage.append(baseRelationManagement.getClass().getSimpleName());
errorMessage.append(" point to the same ");
errorMessage.append(elem.getClass().getSimpleName());
errorMessage.append(". ");
2023-05-11 18:35:56 +02:00
errorMessage.append(DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(errorMessage.toString());
}
relationManagements.put(id, baseRelationManagement);
}
protected static JsonNode addRelation(JsonNode sourceResource, JsonNode relation, String arrayKey)
throws ResourceRegistryException {
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode relationArray = objectMapper.createArrayNode();
try {
if(sourceResource.has(arrayKey)) {
relationArray = (ArrayNode) sourceResource.get(arrayKey);
}
relationArray.add(relation);
((ObjectNode) sourceResource).replace(arrayKey, relationArray);
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
return sourceResource;
}
2023-05-11 18:35:56 +02:00
protected Vertex createVertex() throws EntityAlreadyPresentException, ResourceRegistryException {
2023-05-11 18:35:56 +02:00
logger.trace("Going to create {} for {} ({}) using {}", Vertex.class.getSimpleName(), accessType.getName(),
2021-02-18 09:42:51 +01:00
typeName, jsonNode);
try {
2023-05-11 18:35:56 +02:00
// TODO
// if(documentType.isAbstract()) {
// String error = String.format(
// "Trying to create an instance of %s of type %s which is abstract. The operation will be aborted.",
// accessType.getName(), typeName);
// throw new ResourceRegistryException(error);
// }
try {
if(uuid != null) {
2023-05-11 18:35:56 +02:00
Vertex v = getElement();
if(v != null) {
2021-02-18 09:42:51 +01:00
String error = String.format("A %s with UUID %s already exist", typeName, uuid.toString());
2021-03-05 10:46:59 +01:00
throw getSpecificAlreadyPresentException(error);
}
}
} catch(NotFoundException e) {
try {
2023-05-11 18:35:56 +02:00
Document el = ElementManagementUtility.getAnyElementByUUID(uuid);
2021-02-05 17:50:16 +01:00
String error = String.format("UUID %s is already used by another Element. This is not allowed.",
uuid.toString(),
2023-05-11 18:35:56 +02:00
(el instanceof Vertex) ? EntityElement.NAME : RelationElement.NAME);
2021-02-05 17:50:16 +01:00
throw new ResourceRegistryException(error);
} catch(NotFoundException e1) {
// OK the UUID is not already used.
}
} catch(AvailableInAnotherContextException e) {
throw e;
}
2023-05-11 18:35:56 +02:00
MutableVertex vertexEntity = database.newVertex(typeName);
2023-04-20 12:09:07 +02:00
this.element = vertexEntity;
if(accessType == AccessType.RESOURCE) {
// Facet and relation are created in calling method
} else {
2023-05-11 18:35:56 +02:00
updateProperties(documentType, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
}
2023-05-11 18:35:56 +02:00
logger.info("Created {} is {}", Vertex.class.getSimpleName(),
DBUtility.getAsStringForLogging(element));
return element;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
2023-05-11 18:35:56 +02:00
logger.trace("Error while creating {} for {} ({}) using {}", Vertex.class.getSimpleName(),
2021-02-18 09:42:51 +01:00
accessType.getName(), typeName, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
}
}
}