2020-01-27 17:07:37 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.contexts.entities;
|
2016-12-22 17:27:26 +01:00
|
|
|
|
2019-11-06 12:13:19 +01:00
|
|
|
import java.util.HashMap;
|
2017-11-17 11:42:37 +01:00
|
|
|
import java.util.Iterator;
|
2023-04-20 13:46:46 +02:00
|
|
|
import java.util.List;
|
2021-03-05 11:59:24 +01:00
|
|
|
import java.util.Map;
|
2017-11-23 09:39:21 +01:00
|
|
|
import java.util.UUID;
|
2017-11-17 11:42:37 +01:00
|
|
|
|
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;
|
2019-10-25 17:45:59 +02:00
|
|
|
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;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.relations.Relation;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
|
2020-11-05 12:12:19 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2016-12-22 17:27:26 +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.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;
|
2020-01-30 11:53:15 +01:00
|
|
|
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;
|
2017-11-15 19:31:49 +01:00
|
|
|
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;
|
2016-12-22 17:27:26 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(ContextManagement.class);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +01:00
|
|
|
protected String name;
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-17 11:42:37 +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;
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
public ContextManagement() {
|
|
|
|
super(AccessType.CONTEXT);
|
2017-11-17 11:42:37 +01:00
|
|
|
init();
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
public ContextManagement(RemoteDatabase database) throws ResourceRegistryException {
|
2017-11-28 17:34:43 +01:00
|
|
|
this();
|
2023-05-11 18:35:56 +02:00
|
|
|
this.database = database;
|
2017-11-28 17:34:43 +01:00
|
|
|
getWorkingContext();
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2021-03-05 11:59:24 +01:00
|
|
|
@Override
|
|
|
|
public Map<UUID,JsonNode> getAffectedInstances() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
public String getName() {
|
2021-02-19 19:32:23 +01:00
|
|
|
if (name == null) {
|
|
|
|
if (element == null) {
|
|
|
|
if (jsonNode != null) {
|
2017-11-23 09:39:21 +01:00
|
|
|
name = jsonNode.get(Context.NAME_PROPERTY).asText();
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
} else {
|
2023-05-11 18:35:56 +02:00
|
|
|
name = element.getString(Context.NAME_PROPERTY);
|
2017-11-23 09:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
@Override
|
2017-11-28 17:34:43 +01:00
|
|
|
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
|
2021-02-19 19:32:23 +01:00
|
|
|
if (workingContext == null) {
|
2021-10-22 19:26:36 +02:00
|
|
|
workingContext = ContextSecurityContext.getInstance();
|
2017-11-28 17:34:43 +01:00
|
|
|
}
|
|
|
|
return workingContext;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected ContextNotFoundException getSpecificNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ContextNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
2021-03-05 10:46:59 +01:00
|
|
|
protected ContextAlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ContextAlreadyPresentException(message);
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
protected void checkContext(ContextManagement parentContext)
|
|
|
|
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
|
2021-10-22 11:11:01 +02:00
|
|
|
|
|
|
|
StringBuffer select = new StringBuffer();
|
|
|
|
StringBuffer errorMessage = new StringBuffer();
|
|
|
|
|
2021-02-19 19:32:23 +01:00
|
|
|
if (parentContext != null) {
|
2019-11-05 18:36:44 +01:00
|
|
|
String parentId = parentContext.getElement().getIdentity().toString();
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2021-10-22 11:11:01 +02: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());
|
2023-04-28 11:43:48 +02:00
|
|
|
select.append(IdentifiableElement.ID_PROPERTY);
|
2021-10-22 11:11:01 +02:00
|
|
|
select.append(QueryConditionalOperator.NE.getConditionalOperator());
|
|
|
|
select.append("\"");
|
|
|
|
select.append(parentContext.uuid);
|
|
|
|
select.append("\"");
|
|
|
|
|
2021-10-22 19:26:36 +02:00
|
|
|
errorMessage.append("A ");
|
2021-10-22 11:11:01 +02:00
|
|
|
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());
|
2017-11-17 15:59:25 +01:00
|
|
|
} else {
|
2021-10-22 11:11:01 +02:00
|
|
|
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
|
|
|
|
2021-10-22 11:11:01 +02:00
|
|
|
if (resultSet != null && resultSet.hasNext()) {
|
|
|
|
throw new ContextAlreadyPresentException(errorMessage.toString());
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +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
|
|
|
|
2017-11-30 18:06:08 +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) {
|
2017-11-15 19:31:49 +01:00
|
|
|
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);
|
2017-11-15 19:31:49 +01:00
|
|
|
isParentOfManagement.setElement(edge);
|
2021-09-13 16:48:29 +02:00
|
|
|
isParentOfManagement.includeSource(true);
|
|
|
|
isParentOfManagement.includeTarget(false);
|
|
|
|
JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode();
|
2021-02-19 19:32:23 +01:00
|
|
|
if (isParentOf != null) {
|
2019-02-13 12:33:29 +01:00
|
|
|
((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);
|
2017-11-15 19:31:49 +01:00
|
|
|
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);
|
2017-11-15 19:31:49 +01:00
|
|
|
isParentOfManagement.setElement(edge);
|
|
|
|
try {
|
2021-02-17 11:29:43 +01:00
|
|
|
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
|
2017-11-15 19:31:49 +01:00
|
|
|
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);
|
2017-11-15 19:31:49 +01:00
|
|
|
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);
|
2017-11-15 19:31:49 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
return context;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
@Override
|
2023-05-11 18:35:56 +02:00
|
|
|
protected Vertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
|
2017-11-28 17:34:43 +01:00
|
|
|
SecurityContext securityContext = null;
|
2017-12-05 10:10:34 +01:00
|
|
|
SecurityContext parentSecurityContext = null;
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 16:37:53 +01:00
|
|
|
try {
|
|
|
|
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
|
|
|
|
|
2017-11-23 16:37:53 +01:00
|
|
|
JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
|
2023-05-11 18:35:56 +02:00
|
|
|
ContextManagement parentContextManagement = new ContextManagement(database);
|
2019-02-13 12:33:29 +01:00
|
|
|
parentContextManagement.setJsonNode(parentJsonNode);
|
2017-12-05 10:10:34 +01:00
|
|
|
UUID parentUUID = parentContextManagement.uuid;
|
|
|
|
parentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(parentUUID);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-12-05 10:10:34 +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();
|
2017-11-23 17:09:19 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 17:09:19 +01:00
|
|
|
createVertex();
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2023-05-11 18:35:56 +02:00
|
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
|
2019-02-13 12:33:29 +01:00
|
|
|
isParentOfManagement.setJsonNode(isParentOfJsonNode);
|
2017-12-05 10:10:34 +01:00
|
|
|
isParentOfManagement.setSourceEntityManagement(parentContextManagement);
|
2017-11-23 16:37:53 +01:00
|
|
|
isParentOfManagement.setTargetEntityManagement(this);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 16:37:53 +01:00
|
|
|
isParentOfManagement.internalCreate();
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
} else {
|
2017-11-23 16:37:53 +01:00
|
|
|
checkContext(null);
|
|
|
|
createVertex();
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
securityContext = new SecurityContext(uuid);
|
2017-12-05 10:10:34 +01:00
|
|
|
securityContext.setParentSecurityContext(parentSecurityContext);
|
2023-05-11 18:35:56 +02:00
|
|
|
securityContext.create(database);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
ContextUtility.getInstance().addSecurityContext(securityContext);
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 16:37:53 +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) {
|
2017-12-05 10:10:34 +01:00
|
|
|
parentSecurityContext.getChildren().remove(securityContext);
|
|
|
|
}
|
2023-05-10 17:21:00 +02:00
|
|
|
ServerContextCache.getInstance().cleanCache();
|
2017-11-28 17:34:43 +01:00
|
|
|
}
|
2017-11-23 16:37:53 +01:00
|
|
|
throw e;
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +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
|
|
|
|
2017-11-23 09:39:21 +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;
|
2017-11-23 09:39:21 +01:00
|
|
|
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) {
|
2017-11-30 18:06:08 +01:00
|
|
|
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);
|
2017-11-23 09:39:21 +01:00
|
|
|
throw new ResourceRegistryException(message.toString());
|
|
|
|
}
|
|
|
|
parent = p;
|
|
|
|
found = true;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +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);
|
2017-11-23 09:39:21 +01:00
|
|
|
actualParentContextManagement.setElement(parent);
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +01:00
|
|
|
ContextManagement newParentContextManagement = actualParentContextManagement;
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +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)) {
|
2017-11-23 09:39:21 +01:00
|
|
|
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) {
|
2017-11-23 09:39:21 +01:00
|
|
|
parentChanged = true;
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
} else {
|
2017-11-23 09:39:21 +01:00
|
|
|
parentChanged = true;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (parentChanged) {
|
2023-05-11 18:35:56 +02:00
|
|
|
newParentContextManagement = new ContextManagement(database);
|
2019-02-13 12:33:29 +01:00
|
|
|
newParentContextManagement.setJsonNode(parentContextJsonNode);
|
2017-11-23 09:39:21 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
} else {
|
2021-02-19 19:32:23 +01:00
|
|
|
if (actualParentContextManagement != null) {
|
2017-11-23 09:39:21 +01:00
|
|
|
parentChanged = true;
|
|
|
|
newParentContextManagement = null;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +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);
|
2017-11-23 09:39:21 +01:00
|
|
|
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
|
2021-02-19 19:32:23 +01:00
|
|
|
if (oldName.compareTo(newName) != 0) {
|
2017-11-23 09:39:21 +01:00
|
|
|
nameChanged = true;
|
|
|
|
name = newName;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (parentChanged || nameChanged) {
|
2017-11-23 09:39:21 +01:00
|
|
|
checkContext(newParentContextManagement);
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (parentChanged) {
|
2017-11-23 09:39:21 +01:00
|
|
|
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
|
|
|
|
2017-11-23 09:39:21 +01:00
|
|
|
return element;
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
private void move(ContextManagement newParentContextManagement, boolean check)
|
2017-11-23 09:39:21 +01:00
|
|
|
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
|
2021-02-19 19:32:23 +01:00
|
|
|
if (check) {
|
2017-11-23 09:39:21 +01:00
|
|
|
checkContext(newParentContextManagement);
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
SecurityContext newParentSecurityContext = null;
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 09:39:21 +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();
|
2017-11-23 09:39:21 +01:00
|
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement();
|
|
|
|
isParentOfManagement.setElement(edge);
|
|
|
|
isParentOfManagement.internalDelete();
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (edgeIterator.hasNext()) {
|
2017-11-30 18:06:08 +01:00
|
|
|
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);
|
2017-11-23 09:39:21 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
|
|
|
if (newParentContextManagement != null) {
|
2017-11-23 09:39:21 +01:00
|
|
|
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
|
2023-05-11 18:35:56 +02:00
|
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(database);
|
2019-02-13 12:33:29 +01:00
|
|
|
isParentOfManagement.setJsonNode(isParentOfJsonNode);
|
2017-11-23 09:39:21 +01:00
|
|
|
isParentOfManagement.setSourceEntityManagement(newParentContextManagement);
|
|
|
|
isParentOfManagement.setTargetEntityManagement(this);
|
|
|
|
isParentOfManagement.internalCreate();
|
2021-02-19 19:32:23 +01:00
|
|
|
newParentSecurityContext = ContextUtility.getInstance()
|
|
|
|
.getSecurityContextByUUID(newParentContextManagement.uuid);
|
2017-11-23 09:39:21 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
SecurityContext thisSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(uuid);
|
2023-05-11 18:35:56 +02:00
|
|
|
thisSecurityContext.changeParentSecurityContext(newParentSecurityContext, database);
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-15 19:31:49 +01:00
|
|
|
@Override
|
2021-03-05 11:59:24 +01:00
|
|
|
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()) {
|
2017-11-17 11:42:37 +01:00
|
|
|
throw new ContextException("Cannot remove a " + Context.NAME + " having children");
|
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
element.delete();
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-12-05 10:10:34 +01:00
|
|
|
ContextUtility contextUtility = ContextUtility.getInstance();
|
2017-11-28 17:34:43 +01:00
|
|
|
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();
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|
2021-02-19 19:32:23 +01:00
|
|
|
|
2017-11-23 16:25:16 +01:00
|
|
|
@Override
|
|
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
2019-02-13 12:33:29 +01:00
|
|
|
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) {
|
2017-11-23 16:25:16 +01:00
|
|
|
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);
|
2017-11-23 16:25:16 +01:00
|
|
|
try {
|
2021-02-17 15:16:25 +01:00
|
|
|
JsonNode jsonObject = contextManagement.serializeAsJsonNode();
|
2019-02-13 12:33:29 +01:00
|
|
|
arrayNode.add(jsonObject);
|
2021-02-19 19:32:23 +01:00
|
|
|
} catch (ResourceRegistryException e) {
|
2017-11-30 18:06:08 +01:00
|
|
|
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);
|
2017-11-23 16:25:16 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 12:33:29 +01:00
|
|
|
try {
|
|
|
|
return objectMapper.writeValueAsString(arrayNode);
|
2021-02-19 19:32:23 +01:00
|
|
|
} catch (JsonProcessingException e) {
|
2019-02-13 12:33:29 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
2017-11-23 16:25:16 +01:00
|
|
|
}
|
2019-11-04 18:01:20 +01:00
|
|
|
|
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
|
|
|
|
}
|
2017-11-15 19:31:49 +01:00
|
|
|
}
|