resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java

471 lines
17 KiB
Java
Raw Normal View History

2020-01-27 17:07:37 +01:00
package org.gcube.informationsystem.resourceregistry.contexts.entities;
import java.util.HashMap;
import java.util.Iterator;
2023-04-20 13:46:46 +02:00
import java.util.List;
import java.util.Map;
import java.util.UUID;
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
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.NullNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
2023-04-18 17:52:07 +02:00
import org.gcube.informationsystem.base.reference.IdentifiableElement;
2021-10-25 11:40:47 +02:00
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
2020-11-05 12:12:19 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
2023-05-10 17:21:00 +02:00
import org.gcube.informationsystem.resourceregistry.contexts.ServerContextCache;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
2021-10-21 16:32:05 +02:00
import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
2021-10-25 12:13:18 +02:00
import org.gcube.informationsystem.resourceregistry.queries.operators.QueryConditionalOperator;
import org.gcube.informationsystem.resourceregistry.queries.operators.QueryLogicalOperator;
2023-05-11 18:35:56 +02:00
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
2023-02-07 16:27:10 +01:00
import org.gcube.informationsystem.serialization.ElementMapper;
2021-02-22 16:36:19 +01:00
import org.gcube.informationsystem.types.reference.entities.EntityType;
2023-04-19 13:14:11 +02:00
import org.gcube.informationsystem.utils.UUIDManager;
2023-04-21 15:56:52 +02:00
import org.gcube.informationsystem.utils.UUIDUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-05-11 18:35:56 +02:00
import com.arcadedb.database.Document;
import com.arcadedb.graph.Edge;
import com.arcadedb.graph.Vertex;
import com.arcadedb.graph.Vertex.DIRECTION;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.remote.RemoteDatabase;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-02-22 16:36:19 +01:00
public class ContextManagement extends EntityElementManagement<Context, EntityType> {
2021-02-19 19:32:23 +01:00
private static Logger logger = LoggerFactory.getLogger(ContextManagement.class);
2021-02-19 19:32:23 +01:00
protected String name;
2021-02-19 19:32:23 +01:00
private void init() {
this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY);
this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY);
2021-02-18 09:42:51 +01:00
this.typeName = Context.NAME;
2023-05-10 17:21:00 +02:00
this.forceIncludeMeta = true;
this.forceIncludeAllMeta = true;
}
2021-02-19 19:32:23 +01:00
public ContextManagement() {
super(AccessType.CONTEXT);
init();
}
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
public ContextManagement(RemoteDatabase database) throws ResourceRegistryException {
this();
2023-05-11 18:35:56 +02:00
this.database = database;
getWorkingContext();
}
2021-02-19 19:32:23 +01:00
@Override
public Map<UUID,JsonNode> getAffectedInstances() {
throw new UnsupportedOperationException();
}
public String getName() {
2021-02-19 19:32:23 +01:00
if (name == null) {
if (element == null) {
if (jsonNode != null) {
name = jsonNode.get(Context.NAME_PROPERTY).asText();
}
} else {
2023-05-11 18:35:56 +02:00
name = element.getString(Context.NAME_PROPERTY);
}
}
return name;
}
2021-02-19 19:32:23 +01:00
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
2021-02-19 19:32:23 +01:00
if (workingContext == null) {
workingContext = ContextSecurityContext.getInstance();
}
return workingContext;
}
2021-02-19 19:32:23 +01:00
@Override
2021-03-05 10:46:59 +01:00
protected ContextNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new ContextNotFoundException(e.getMessage(), e.getCause());
}
2021-02-19 19:32:23 +01:00
@Override
2021-03-05 10:46:59 +01:00
protected ContextAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new ContextAlreadyPresentException(message);
}
2021-02-19 19:32:23 +01:00
protected void checkContext(ContextManagement parentContext)
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
StringBuffer select = new StringBuffer();
StringBuffer errorMessage = new StringBuffer();
2021-02-19 19:32:23 +01:00
if (parentContext != null) {
String parentId = parentContext.getElement().getIdentity().toString();
2021-02-19 19:32:23 +01:00
select.append("SELECT FROM (TRAVERSE out(");
select.append(IsParentOf.NAME);
select.append(") FROM ");
select.append(parentId);
select.append(" MAXDEPTH 1) WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(QueryConditionalOperator.EQ.getConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append(IdentifiableElement.ID_PROPERTY);
select.append(QueryConditionalOperator.NE.getConditionalOperator());
select.append("\"");
select.append(parentContext.uuid);
select.append("\"");
errorMessage.append("A ");
errorMessage.append(Context.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());
errorMessage.append(" has been already created as child of ");
errorMessage.append(parentContext.getElement().toString());
} else {
select.append("SELECT FROM ");
select.append(Context.NAME);
select.append(" WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(QueryConditionalOperator.EQ.getConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append("in(\"");
select.append(IsParentOf.NAME);
select.append("\").size() = 0");
errorMessage.append("A root ");
errorMessage.append(Context.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());
errorMessage.append(" already exist");
}
logger.trace("Checking if {} -> {}", errorMessage, select);
2023-05-11 18:35:56 +02:00
ResultSet resultSet = database.command("sql", select.toString(), new HashMap<>());
2021-02-19 19:32:23 +01:00
if (resultSet != null && resultSet.hasNext()) {
throw new ContextAlreadyPresentException(errorMessage.toString());
}
2021-02-19 19:32:23 +01:00
}
2021-02-19 19:32:23 +01:00
@Override
2021-02-17 11:29:43 +01:00
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
2021-02-19 19:32:23 +01:00
2021-02-17 11:29:43 +01:00
JsonNode context = serializeSelfAsJsonNode();
2021-02-19 19:32:23 +01:00
int count = 0;
2023-05-11 18:35:56 +02:00
Iterable<Edge> parents = getElement().getEdges(DIRECTION.IN);
for (Edge edge : parents) {
2021-02-19 19:32:23 +01:00
if (++count > 1) {
throw new ContextException("A " + Context.NAME + " can not have more than one parent");
}
try {
2023-05-11 18:35:56 +02:00
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
isParentOfManagement.setElement(edge);
isParentOfManagement.includeSource(true);
isParentOfManagement.includeTarget(false);
JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode();
2021-02-19 19:32:23 +01:00
if (isParentOf != null) {
((ObjectNode) context).replace(Context.PARENT_PROPERTY, isParentOf);
}
2021-02-19 19:32:23 +01:00
} catch (Exception e) {
2023-05-11 18:35:56 +02:00
logger.error("Unable to correctly serialize {}. {}", edge, DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ContextException("");
}
}
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
Iterable<Edge> childrenEdges = getElement().getEdges(DIRECTION.OUT);
for (Edge edge : childrenEdges) {
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
isParentOfManagement.setElement(edge);
try {
2021-02-17 11:29:43 +01:00
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY);
2021-02-19 19:32:23 +01:00
} catch (ResourceRegistryException e) {
2023-05-11 18:35:56 +02:00
logger.error("Unable to correctly serialize {}. {}", edge, DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw e;
2021-02-19 19:32:23 +01:00
} catch (Exception e) {
2023-05-11 18:35:56 +02:00
logger.error("Unable to correctly serialize {}. {}", edge, DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(e);
}
}
2021-02-19 19:32:23 +01:00
return context;
}
2021-02-19 19:32:23 +01:00
@Override
2023-05-11 18:35:56 +02:00
protected Vertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
SecurityContext securityContext = null;
SecurityContext parentSecurityContext = null;
2021-02-19 19:32:23 +01:00
try {
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
2021-02-19 19:32:23 +01:00
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
2023-05-11 18:35:56 +02:00
ContextManagement parentContextManagement = new ContextManagement(database);
parentContextManagement.setJsonNode(parentJsonNode);
UUID parentUUID = parentContextManagement.uuid;
parentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(parentUUID);
2021-02-19 19:32:23 +01:00
checkContext(parentContextManagement);
2021-02-19 19:32:23 +01:00
if (uuid == null) {
2023-04-21 15:56:52 +02:00
uuid = UUIDManager.getInstance().generateValidUUID();
}
2021-02-19 19:32:23 +01:00
createVertex();
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
isParentOfManagement.setJsonNode(isParentOfJsonNode);
isParentOfManagement.setSourceEntityManagement(parentContextManagement);
isParentOfManagement.setTargetEntityManagement(this);
2021-02-19 19:32:23 +01:00
isParentOfManagement.internalCreate();
2021-02-19 19:32:23 +01:00
} else {
checkContext(null);
createVertex();
}
2021-02-19 19:32:23 +01:00
securityContext = new SecurityContext(uuid);
securityContext.setParentSecurityContext(parentSecurityContext);
2023-05-11 18:35:56 +02:00
securityContext.create(database);
2021-02-19 19:32:23 +01:00
ContextUtility.getInstance().addSecurityContext(securityContext);
2021-02-19 19:32:23 +01:00
return getElement();
2021-02-19 19:32:23 +01:00
} catch (Exception e) {
2023-05-11 18:35:56 +02:00
database.rollback();
2021-02-19 19:32:23 +01:00
if (securityContext != null) {
2023-05-11 18:35:56 +02:00
securityContext.delete(database);
2021-02-19 19:32:23 +01:00
if (parentSecurityContext != null && securityContext != null) {
parentSecurityContext.getChildren().remove(securityContext);
}
2023-05-10 17:21:00 +02:00
ServerContextCache.getInstance().cleanCache();
}
throw e;
}
}
2021-02-19 19:32:23 +01:00
@Override
2023-05-11 18:35:56 +02:00
protected Vertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
2021-02-19 19:32:23 +01:00
boolean parentChanged = false;
boolean nameChanged = false;
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
Vertex parent = null;
boolean found = false;
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
Iterable<Vertex> iterable = getElement().getVertices(DIRECTION.IN, IsParentOf.NAME);
for (Vertex p : iterable) {
2021-02-19 19:32:23 +01:00
if (found) {
String message = String.format("{} has more than one parent. {}", Context.NAME,
2023-05-11 18:35:56 +02:00
DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(message.toString());
}
parent = p;
found = true;
}
2021-02-19 19:32:23 +01:00
ContextManagement actualParentContextManagement = null;
2021-02-19 19:32:23 +01:00
if (parent != null) {
2023-05-11 18:35:56 +02:00
actualParentContextManagement = new ContextManagement(database);
actualParentContextManagement.setElement(parent);
}
2021-02-19 19:32:23 +01:00
ContextManagement newParentContextManagement = actualParentContextManagement;
2021-02-19 19:32:23 +01:00
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
JsonNode parentContextJsonNode = null;
2021-02-19 19:32:23 +01:00
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
parentContextJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
}
2021-02-19 19:32:23 +01:00
if (parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) {
2023-04-21 15:56:52 +02:00
UUID parentUUID = UUIDUtility.getUUID(parentContextJsonNode);
2021-02-19 19:32:23 +01:00
if (actualParentContextManagement != null) {
if (parentUUID.compareTo(actualParentContextManagement.uuid) != 0) {
parentChanged = true;
}
} else {
parentChanged = true;
}
2021-02-19 19:32:23 +01:00
if (parentChanged) {
2023-05-11 18:35:56 +02:00
newParentContextManagement = new ContextManagement(database);
newParentContextManagement.setJsonNode(parentContextJsonNode);
}
} else {
2021-02-19 19:32:23 +01:00
if (actualParentContextManagement != null) {
parentChanged = true;
newParentContextManagement = null;
}
2021-02-19 19:32:23 +01:00
}
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
String oldName = getElement().getString(Context.NAME_PROPERTY);
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
2021-02-19 19:32:23 +01:00
if (oldName.compareTo(newName) != 0) {
nameChanged = true;
name = newName;
}
2021-02-19 19:32:23 +01:00
if (parentChanged || nameChanged) {
checkContext(newParentContextManagement);
}
2021-02-19 19:32:23 +01:00
if (parentChanged) {
move(newParentContextManagement, false);
}
2021-02-19 19:32:23 +01:00
2023-05-11 18:35:56 +02:00
element = (Vertex) updateProperties(documentType, getElement(), jsonNode, ignoreKeys, ignoreStartWithKeys);
2021-02-19 19:32:23 +01:00
2023-05-10 17:21:00 +02:00
ServerContextCache.getInstance().cleanCache();
2021-02-19 19:32:23 +01:00
return element;
}
2021-02-19 19:32:23 +01:00
private void move(ContextManagement newParentContextManagement, boolean check)
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
2021-02-19 19:32:23 +01:00
if (check) {
checkContext(newParentContextManagement);
}
2021-02-19 19:32:23 +01:00
SecurityContext newParentSecurityContext = null;
2021-02-19 19:32:23 +01:00
// Removing the old parent relationship if any
2023-05-11 18:35:56 +02:00
Iterable<Edge> edges = getElement().getEdges(DIRECTION.IN, IsParentOf.NAME);
2021-02-19 19:32:23 +01:00
if (edges != null && edges.iterator().hasNext()) {
2023-05-11 18:35:56 +02:00
Iterator<Edge> edgeIterator = edges.iterator();
Edge edge = edgeIterator.next();
IsParentOfManagement isParentOfManagement = new IsParentOfManagement();
isParentOfManagement.setElement(edge);
isParentOfManagement.internalDelete();
2021-02-19 19:32:23 +01:00
if (edgeIterator.hasNext()) {
throw new ContextException(
2023-05-11 18:35:56 +02:00
"Seems that the Context has more than one Parent. " + DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
2021-02-19 19:32:23 +01:00
if (newParentContextManagement != null) {
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
2023-05-11 18:35:56 +02:00
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
isParentOfManagement.setJsonNode(isParentOfJsonNode);
isParentOfManagement.setSourceEntityManagement(newParentContextManagement);
isParentOfManagement.setTargetEntityManagement(this);
isParentOfManagement.internalCreate();
2021-02-19 19:32:23 +01:00
newParentSecurityContext = ContextUtility.getInstance()
.getSecurityContextByUUID(newParentContextManagement.uuid);
}
2021-02-19 19:32:23 +01:00
SecurityContext thisSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(uuid);
2023-05-11 18:35:56 +02:00
thisSecurityContext.changeParentSecurityContext(newParentSecurityContext, database);
}
2021-02-19 19:32:23 +01:00
@Override
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
2023-05-11 18:35:56 +02:00
Iterable<Edge> iterable = getElement().getEdges(DIRECTION.OUT);
Iterator<Edge> iterator = iterable.iterator();
2021-02-19 19:32:23 +01:00
while (iterator.hasNext()) {
throw new ContextException("Cannot remove a " + Context.NAME + " having children");
}
2021-02-19 19:32:23 +01:00
element.delete();
2021-02-19 19:32:23 +01:00
ContextUtility contextUtility = ContextUtility.getInstance();
SecurityContext securityContext = contextUtility.getSecurityContextByUUID(uuid);
2023-05-11 18:35:56 +02:00
securityContext.delete(database);
2021-02-19 19:32:23 +01:00
2023-05-10 17:21:00 +02:00
ServerContextCache.getInstance().cleanCache();
}
2021-02-19 19:32:23 +01:00
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode();
2023-05-11 18:35:56 +02:00
Iterable<Document> iterable = database.browseClass(typeName, polymorphic);
for (Document vertex : iterable) {
ContextManagement contextManagement = new ContextManagement();
2023-05-10 17:21:00 +02:00
contextManagement.setForceIncludeMeta(forceIncludeMeta);
contextManagement.setForceIncludeAllMeta(forceIncludeAllMeta);
2023-05-11 18:35:56 +02:00
contextManagement.setElement((Vertex) vertex);
try {
2021-02-17 15:16:25 +01:00
JsonNode jsonObject = contextManagement.serializeAsJsonNode();
arrayNode.add(jsonObject);
2021-02-19 19:32:23 +01:00
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
2023-05-11 18:35:56 +02:00
vertex.toString(), DBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
try {
return objectMapper.writeValueAsString(arrayNode);
2021-02-19 19:32:23 +01:00
} catch (JsonProcessingException e) {
throw new ResourceRegistryException(e);
}
}
2020-11-05 12:12:19 +01:00
public String allFromServer(boolean polymorphic) throws ResourceRegistryException {
return super.all(polymorphic);
}
2021-02-19 19:32:23 +01:00
2020-11-05 12:12:19 +01:00
@Override
public String all(boolean polymorphic) throws ResourceRegistryException {
try {
2023-05-10 17:21:00 +02:00
ServerContextCache contextCache = ServerContextCache.getInstance();
2023-04-20 13:46:46 +02:00
List<Context> contexts = contextCache.getContexts();
return ElementMapper.marshal(contexts);
2020-11-05 12:12:19 +01:00
} catch (JsonProcessingException | ResourceRegistryException e) {
return allFromServer(polymorphic);
}
}
2021-02-19 19:32:23 +01:00
public String readFromServer()
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
2021-02-05 17:50:16 +01:00
return super.read().toString();
2020-11-05 12:12:19 +01:00
}
2021-02-19 19:32:23 +01:00
public String readAsString()
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
2020-11-05 12:12:19 +01:00
try {
2023-05-10 17:21:00 +02:00
ServerContextCache contextCache = ServerContextCache.getInstance();
2020-11-05 12:12:19 +01:00
return ElementMapper.marshal(contextCache.getContextByUUID(uuid));
} catch (JsonProcessingException | ResourceRegistryException e) {
return readFromServer();
}
}
2021-02-19 19:32:23 +01:00
2021-02-18 18:22:39 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
2021-02-18 18:22:39 +01:00
// Nothing to do
}
}