2019-11-06 12:13:19 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.instances.model.entities;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2021-02-08 16:26:58 +01:00
|
|
|
import java.util.ArrayList;
|
2021-01-27 21:57:06 +01:00
|
|
|
import java.util.HashMap;
|
2016-12-19 14:59:27 +01:00
|
|
|
import java.util.Iterator;
|
2021-02-08 16:26:58 +01:00
|
|
|
import java.util.List;
|
2021-01-27 21:57:06 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2020-07-07 17:15:22 +02:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
2021-02-05 17:50:16 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
2019-10-25 17:45:59 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2021-02-05 17:50:16 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
|
|
|
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
2021-01-27 21:57:06 +01:00
|
|
|
import org.gcube.informationsystem.context.reference.entities.Context;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
2021-02-05 17:50:16 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.properties.Header;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
|
2021-02-05 17:50:16 +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.NotFoundException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2021-01-27 21:57:06 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
2017-04-28 17:43:30 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
|
2017-11-17 11:42:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
2020-01-27 17:07:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
|
2019-11-06 12:14:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement;
|
2021-02-08 16:26:58 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.types.entities.ResourceTypeDefinitionManagement;
|
2016-12-22 17:27:26 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
2021-02-08 16:26:58 +01:00
|
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
|
|
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
|
|
|
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
|
|
|
import com.orientechnologies.orient.core.metadata.schema.OClass;
|
|
|
|
import com.orientechnologies.orient.core.record.ODirection;
|
|
|
|
import com.orientechnologies.orient.core.record.OEdge;
|
|
|
|
import com.orientechnologies.orient.core.record.OVertex;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceManagement extends EntityManagement<Resource> {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public ResourceManagement() {
|
2017-03-29 16:53:37 +02:00
|
|
|
super(AccessType.RESOURCE);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
public ResourceManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument) {
|
|
|
|
super(AccessType.RESOURCE, workingContext, oDatabaseDocument);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-17 11:42:37 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-04 15:31:13 +02:00
|
|
|
protected ResourceNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ResourceNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-05 17:50:16 +01:00
|
|
|
public ResourceAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
|
2017-11-30 18:06:08 +01:00
|
|
|
String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ResourceAvailableInAnotherContextException(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ResourceAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
|
|
|
|
return new ResourceAlreadyPresentException(message);
|
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2019-02-13 12:33:29 +01:00
|
|
|
public JsonNode serializeAsJson() throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
JsonNode sourceResource = serializeSelfOnly();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-11-30 18:06:08 +01:00
|
|
|
* Cannot get ConsistsOf edge only because is not polymorphic for a
|
|
|
|
* com.tinkerpop.blueprints.Vertex vertex.getEdges(Direction.OUT,
|
|
|
|
* ConsistsOf.NAME); TODO Looks for a different query
|
2016-12-19 15:56:16 +01:00
|
|
|
*/
|
2017-04-14 14:37:57 +02:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
Iterable<OEdge> edges = getElement().getEdges(ODirection.OUT);
|
|
|
|
for(OEdge edge : edges) {
|
2017-11-17 18:10:35 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
2017-11-17 18:10:35 +01:00
|
|
|
RelationManagement relationManagement = getRelationManagement(edge);
|
2017-11-21 18:18:11 +01:00
|
|
|
relationManagement.setReload(reload);
|
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement.giveMeSourceEntityManagementAsIs() == null) {
|
2017-11-17 18:10:35 +01:00
|
|
|
relationManagement.setSourceEntityManagement(this);
|
2017-11-17 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement.giveMeSourceEntityManagementAsIs() != this) {
|
2017-11-17 18:10:35 +01:00
|
|
|
StringBuilder errorMessage = new StringBuilder();
|
|
|
|
errorMessage.append("SourceEntityManagement for ");
|
|
|
|
errorMessage.append(relationManagement.getClass().getSimpleName());
|
|
|
|
errorMessage.append(" is not the one expected. ");
|
|
|
|
errorMessage.append(Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
|
|
throw new ResourceRegistryException(errorMessage.toString());
|
|
|
|
}
|
2017-11-17 11:42:37 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement instanceof ConsistsOfManagement) {
|
2017-03-28 16:02:23 +02:00
|
|
|
try {
|
2019-02-13 12:33:29 +01:00
|
|
|
JsonNode consistsOf = relationManagement.serializeAsJson(true, true);
|
2017-03-28 16:02:23 +02:00
|
|
|
sourceResource = addConsistsOf(sourceResource, consistsOf);
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw new ResourceRegistryException(e);
|
2017-03-28 16:02:23 +02:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 15:37:15 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-11-30 18:06:08 +01:00
|
|
|
* This comment is just to show that IsRelatedTo is not serialized by default as
|
|
|
|
* design choice and not because forget
|
2017-01-17 16:53:24 +01:00
|
|
|
*
|
2019-02-13 12:33:29 +01:00
|
|
|
* else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ JsonNode
|
2017-11-30 18:06:08 +01:00
|
|
|
* isRelatedTo = relationManagement.serializeAsJson(true, true); sourceResource
|
|
|
|
* = addIsRelatedTo(sourceResource, isRelatedTo); }
|
2016-12-19 14:59:27 +01:00
|
|
|
*/
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return sourceResource;
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
public static JsonNode addConsistsOf(JsonNode sourceResource, JsonNode consistsOf)
|
2017-11-30 18:06:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2017-11-15 19:31:49 +01:00
|
|
|
return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF.lowerCaseFirstCharacter());
|
2017-01-17 16:53:24 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
public static JsonNode addIsRelatedTo(JsonNode sourceResource, JsonNode isRelatedTo)
|
2017-11-30 18:06:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2017-11-15 19:31:49 +01:00
|
|
|
return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO.lowerCaseFirstCharacter());
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2019-11-05 18:36:44 +01:00
|
|
|
protected OVertex reallyCreate() throws ResourceAlreadyPresentException, ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
createVertex();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode consistOfJsonNode : jsonNodeArray) {
|
2019-11-05 18:36:44 +01:00
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument);
|
2019-02-13 12:33:29 +01:00
|
|
|
com.setJsonNode(consistOfJsonNode);
|
2017-11-17 11:42:37 +01:00
|
|
|
com.setSourceEntityManagement(this);
|
|
|
|
com.internalCreate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(com);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2019-11-05 18:36:44 +01:00
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument);
|
2019-02-13 12:33:29 +01:00
|
|
|
irtm.setJsonNode(relationJsonNode);
|
2017-11-17 11:42:37 +01:00
|
|
|
irtm.setSourceEntityManagement(this);
|
|
|
|
irtm.internalCreate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(irtm);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2019-11-05 18:36:44 +01:00
|
|
|
protected OVertex reallyUpdate() throws ResourceNotFoundException, ResourceRegistryException {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2019-11-05 18:36:44 +01:00
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument);
|
2019-02-13 12:33:29 +01:00
|
|
|
com.setJsonNode(relationJsonNode);
|
2017-10-26 17:09:07 +02:00
|
|
|
com.internalCreateOrUdate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(com);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2019-11-05 18:36:44 +01:00
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument);
|
2019-02-13 12:33:29 +01:00
|
|
|
irtm.setJsonNode(relationJsonNode);
|
2017-10-26 17:09:07 +02:00
|
|
|
irtm.internalUpdate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(irtm);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2017-11-30 18:06:08 +01:00
|
|
|
protected boolean reallyDelete() throws ResourceNotFoundException, ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
// internalDeleteResource(orientGraph, uuid, null);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
Iterable<OEdge> iterable = element.getEdges(ODirection.OUT);
|
|
|
|
Iterator<OEdge> iterator = iterable.iterator();
|
2017-11-30 18:06:08 +01:00
|
|
|
while(iterator.hasNext()) {
|
2017-11-17 11:42:37 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
OEdge edge = iterator.next();
|
|
|
|
OClass oClass = getOClass(edge);
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
RelationManagement relationManagement = null;
|
2019-11-05 18:36:44 +01:00
|
|
|
if(oClass.isSubClassOf(IsRelatedTo.NAME)) {
|
|
|
|
relationManagement = new IsRelatedToManagement(getWorkingContext(), oDatabaseDocument);
|
|
|
|
} else if(oClass.isSubClassOf(ConsistsOf.NAME)) {
|
|
|
|
relationManagement = new ConsistsOfManagement(getWorkingContext(), oDatabaseDocument);
|
2017-01-17 16:53:24 +01:00
|
|
|
} else {
|
2017-11-30 18:06:08 +01:00
|
|
|
logger.warn("{} is not a {} nor a {}. {}", Utility.toJsonString(edge, true), IsRelatedTo.NAME,
|
2017-11-23 09:39:21 +01:00
|
|
|
ConsistsOf.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement != null) {
|
2016-12-30 17:31:12 +01:00
|
|
|
relationManagement.setElement(edge);
|
2017-10-26 17:09:07 +02:00
|
|
|
relationManagement.internalDelete();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-11-05 18:36:44 +01:00
|
|
|
element.delete();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-27 12:59:00 +02:00
|
|
|
|
2021-01-27 21:57:06 +01:00
|
|
|
@Override
|
|
|
|
protected Map<UUID,JsonNode> reallyAddToContext(SecurityContext targetSecurityContext)
|
|
|
|
throws ContextException, ResourceRegistryException {
|
|
|
|
|
|
|
|
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
|
|
|
|
|
|
|
|
if(!dryRunContextSharing) {
|
|
|
|
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DO NOT UNCOMMENT
|
|
|
|
* // affectedInstances.put(uuid, serializeSelfOnly());
|
|
|
|
* the instance is added in internalAddToContext() function after
|
|
|
|
* the update of Header metadata i.e. modifiedBy, lastUpdateTime
|
|
|
|
*/
|
|
|
|
|
2021-02-05 17:50:16 +01:00
|
|
|
if(honourPropagationConstraintsInContextSharing) {
|
|
|
|
Iterable<OEdge> edges = getElement().getEdges(ODirection.OUT);
|
|
|
|
|
|
|
|
int facetCounter = 0;
|
|
|
|
|
|
|
|
for(OEdge edge : edges) {
|
|
|
|
RelationManagement<?> relationManagement = getRelationManagement(edge);
|
|
|
|
relationManagement.setDryRunContextSharing(dryRunContextSharing);
|
|
|
|
relationManagement.setHonourPropagationConstraintsInContextSharing(honourPropagationConstraintsInContextSharing);
|
|
|
|
Map<UUID,JsonNode> resourceCharacterisationInstances = relationManagement.internalAddToContext(targetSecurityContext);
|
|
|
|
if(relationManagement instanceof ConsistsOfManagement) {
|
|
|
|
facetCounter = facetCounter + resourceCharacterisationInstances.size();
|
|
|
|
}
|
|
|
|
affectedInstances.putAll(resourceCharacterisationInstances);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(facetCounter == 0) {
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("Any ");
|
|
|
|
stringBuffer.append(Resource.NAME);
|
|
|
|
stringBuffer.append(" must ");
|
|
|
|
stringBuffer.append(ConsistsOf.NAME);
|
|
|
|
stringBuffer.append(" at least of one ");
|
|
|
|
stringBuffer.append(Facet.NAME);
|
|
|
|
stringBuffer.append(" in any ");
|
|
|
|
stringBuffer.append(Context.NAME);
|
|
|
|
throw new ResourceRegistryException(stringBuffer.toString());
|
2021-01-27 21:57:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return affectedInstances;
|
|
|
|
}
|
|
|
|
|
2018-06-08 15:05:47 +02:00
|
|
|
public String all(boolean polymorphic) throws ResourceRegistryException {
|
2017-09-27 12:59:00 +02:00
|
|
|
try {
|
2019-11-05 18:36:44 +01:00
|
|
|
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-08 15:05:47 +02:00
|
|
|
return reallyGetAll(polymorphic);
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-09-27 12:59:00 +02:00
|
|
|
throw e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2017-09-27 12:59:00 +02:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
2019-11-05 18:36:44 +01:00
|
|
|
if(oDatabaseDocument != null) {
|
|
|
|
oDatabaseDocument.close();
|
2017-09-27 12:59:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2021-02-08 16:26:58 +01:00
|
|
|
@Override
|
2021-02-05 17:50:16 +01:00
|
|
|
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws ResourceRegistryException {
|
2021-02-08 16:26:58 +01:00
|
|
|
/*
|
|
|
|
* Actually this function only check the Resource constraint, i.e., resource has at least a Facet
|
|
|
|
* in any context.
|
|
|
|
*
|
|
|
|
* We need to load the Resource definition and validate it.
|
|
|
|
* This task is not very easy because we cannot check just the type but
|
|
|
|
* me must take in account that a specialisation instance is valid.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
read();
|
|
|
|
|
|
|
|
ResourceTypeDefinitionManagement resourceTypeDefinitionManagement = new ResourceTypeDefinitionManagement();
|
|
|
|
resourceTypeDefinitionManagement.setName(elementType);
|
|
|
|
String stringType = resourceTypeDefinitionManagement.read().toString();
|
|
|
|
ResourceType resourceType = null;
|
|
|
|
try {
|
|
|
|
resourceType = (ResourceType) TypeMapper.deserializeTypeDefinition(stringType);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Create something like SchemaDefinitionNotRespected o ConstaintNotRespected Exception
|
|
|
|
|
|
|
|
List<LinkedEntity> constraint = new ArrayList<>();
|
|
|
|
List<LinkedEntity> linkedEntities = resourceType.getFacets();
|
|
|
|
for(LinkedEntity linkedEntity : linkedEntities) {
|
|
|
|
if(linkedEntity.getMin()>0 || linkedEntity.getMax()>0) {
|
|
|
|
constraint.add(linkedEntity);
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 17:50:16 +01:00
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
ArrayNode consistsOfArrayNode = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY);
|
|
|
|
for(JsonNode consistsOfJsonNode : consistsOfArrayNode) {
|
|
|
|
String consistsOfUUIDString = consistsOfJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText();
|
|
|
|
UUID consistsOfUUID = UUID.fromString(consistsOfUUIDString);
|
|
|
|
if(expectedInstances.containsKey(consistsOfUUID)) {
|
2021-02-08 16:26:58 +01:00
|
|
|
// we need to check that also the facets is present
|
2021-02-05 17:50:16 +01:00
|
|
|
JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY);
|
|
|
|
String facetUUIDString = facetJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText();
|
|
|
|
UUID facetUUID = UUID.fromString(facetUUIDString);
|
|
|
|
if(!expectedInstances.containsKey(facetUUID)) {
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a ");
|
|
|
|
stringBuffer.append(ConsistsOf.NAME);
|
|
|
|
stringBuffer.append(" relation (i.e. ");
|
|
|
|
stringBuffer.append(consistsOfJsonNode.get(Element.CLASS_PROPERTY).asText());
|
|
|
|
stringBuffer.append(" with UUID ");
|
|
|
|
stringBuffer.append(consistsOfUUIDString);
|
|
|
|
stringBuffer.append(") without indicating the target ");
|
|
|
|
stringBuffer.append(Facet.NAME);
|
|
|
|
stringBuffer.append(" (i.e. ");
|
|
|
|
stringBuffer.append(facetJsonNode.get(Element.CLASS_PROPERTY).asText());
|
|
|
|
stringBuffer.append(" with UUID ");
|
|
|
|
stringBuffer.append(facetUUIDString);
|
|
|
|
stringBuffer.append(").");
|
|
|
|
logger.debug(stringBuffer.toString());
|
|
|
|
throw new ResourceRegistryException(stringBuffer.toString());
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check Resource definition for mandatory ConsistsOf/Facet
|
|
|
|
|
|
|
|
if(!found) {
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a ");
|
|
|
|
stringBuffer.append(Resource.NAME);
|
|
|
|
stringBuffer.append(" (i.e. ");
|
|
|
|
stringBuffer.append(elementType);
|
|
|
|
stringBuffer.append(" with UUID ");
|
|
|
|
stringBuffer.append(uuid.toString());
|
|
|
|
stringBuffer.append(") without adding at least a ");
|
|
|
|
stringBuffer.append(Facet.NAME);
|
|
|
|
logger.debug(stringBuffer.toString());
|
|
|
|
throw new ResourceRegistryException(stringBuffer.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|