461 lines
17 KiB
Java
461 lines
17 KiB
Java
package org.gcube.informationsystem.resourceregistry.contexts.entities;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
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;
|
|
import org.gcube.informationsystem.context.reference.entities.Context;
|
|
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
|
|
import org.gcube.informationsystem.model.reference.properties.Header;
|
|
import org.gcube.informationsystem.model.reference.relations.Relation;
|
|
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
|
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
|
|
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.context.ContextAlreadyPresentException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
|
import org.gcube.informationsystem.types.reference.entities.EntityType;
|
|
import org.gcube.informationsystem.utils.ElementMapper;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
|
import com.orientechnologies.orient.core.record.ODirection;
|
|
import com.orientechnologies.orient.core.record.OEdge;
|
|
import com.orientechnologies.orient.core.record.OVertex;
|
|
import com.orientechnologies.orient.core.record.impl.ODocument;
|
|
import com.orientechnologies.orient.core.sql.executor.OResultSet;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class ContextManagement extends EntityElementManagement<Context, EntityType> {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ContextManagement.class);
|
|
|
|
protected String name;
|
|
|
|
private void init() {
|
|
this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY);
|
|
this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY);
|
|
this.typeName = Context.NAME;
|
|
}
|
|
|
|
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
|
|
|
|
@Override
|
|
public List<Context> renew() throws ResourceRegistryException {
|
|
String contextsJsonString = allFromServer(false);
|
|
List<Context> contexts = null;
|
|
try {
|
|
contexts = ElementMapper.unmarshalList(contextsJsonString);
|
|
} catch (IOException e) {
|
|
logger.error("Unable to read contexts from DB", e);
|
|
}
|
|
return contexts;
|
|
}
|
|
};
|
|
|
|
public ContextManagement() {
|
|
super(AccessType.CONTEXT);
|
|
init();
|
|
ContextCache contextCache = ContextCache.getInstance();
|
|
contextCache.setContextCacheRenewal(contextCacheRenewal);
|
|
}
|
|
|
|
public ContextManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
|
|
this();
|
|
this.oDatabaseDocument = oDatabaseDocument;
|
|
getWorkingContext();
|
|
}
|
|
|
|
@Override
|
|
public Map<UUID,JsonNode> getAffectedInstances() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
public String getName() {
|
|
if (name == null) {
|
|
if (element == null) {
|
|
if (jsonNode != null) {
|
|
name = jsonNode.get(Context.NAME_PROPERTY).asText();
|
|
}
|
|
} else {
|
|
name = element.getProperty(Context.NAME_PROPERTY);
|
|
}
|
|
}
|
|
return name;
|
|
}
|
|
|
|
@Override
|
|
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
|
|
if (workingContext == null) {
|
|
workingContext = ContextUtility.getInstance()
|
|
.getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID);
|
|
}
|
|
return workingContext;
|
|
}
|
|
|
|
@Override
|
|
protected ContextNotFoundException getSpecificNotFoundException(NotFoundException e) {
|
|
return new ContextNotFoundException(e.getMessage(), e.getCause());
|
|
}
|
|
|
|
@Override
|
|
protected ContextAlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
|
return new ContextAlreadyPresentException(message);
|
|
}
|
|
|
|
protected void checkContext(ContextManagement parentContext)
|
|
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
|
|
|
|
if (parentContext != null) {
|
|
String parentId = parentContext.getElement().getIdentity().toString();
|
|
|
|
String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + ") FROM " + parentId
|
|
+ " MAXDEPTH 1) WHERE " + Context.NAME_PROPERTY + "=\"" + getName() + "\" AND "
|
|
+ Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + "<>\"" + parentContext.uuid + "\"";
|
|
|
|
logger.trace(select);
|
|
|
|
StringBuilder message = new StringBuilder();
|
|
message.append("A context with name (");
|
|
message.append(getName());
|
|
message.append(") has been already created as child of ");
|
|
message.append(parentContext.getElement().toString());
|
|
|
|
logger.trace("Checking if {} -> {}", message, select);
|
|
|
|
OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>());
|
|
|
|
if (resultSet != null && resultSet.hasNext()) {
|
|
throw new ContextAlreadyPresentException(message.toString());
|
|
}
|
|
|
|
} else {
|
|
String select = "SELECT FROM " + Context.NAME + " WHERE " + Context.NAME_PROPERTY + " = \"" + getName()
|
|
+ "\"" + " AND in(\"" + IsParentOf.NAME + "\").size() = 0";
|
|
|
|
OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>());
|
|
|
|
if (resultSet != null && resultSet.hasNext()) {
|
|
throw new ContextAlreadyPresentException(
|
|
"A root context with the same name (" + this.getName() + ") already exist");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
|
|
|
|
JsonNode context = serializeSelfAsJsonNode();
|
|
|
|
int count = 0;
|
|
Iterable<OEdge> parents = getElement().getEdges(ODirection.IN);
|
|
for (OEdge edge : parents) {
|
|
if (++count > 1) {
|
|
throw new ContextException("A " + Context.NAME + " can not have more than one parent");
|
|
}
|
|
try {
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
|
|
isParentOfManagement.setElement(edge);
|
|
isParentOfManagement.includeSource(true);
|
|
isParentOfManagement.includeTarget(false);
|
|
JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode();
|
|
if (isParentOf != null) {
|
|
((ObjectNode) context).replace(Context.PARENT_PROPERTY, isParentOf);
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw new ContextException("");
|
|
}
|
|
}
|
|
|
|
Iterable<OEdge> childrenEdges = getElement().getEdges(ODirection.OUT);
|
|
for (OEdge edge : childrenEdges) {
|
|
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
|
|
isParentOfManagement.setElement(edge);
|
|
try {
|
|
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
|
|
context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY);
|
|
} catch (ResourceRegistryException e) {
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw e;
|
|
} catch (Exception e) {
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
}
|
|
|
|
return context;
|
|
}
|
|
|
|
@Override
|
|
protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
|
|
SecurityContext securityContext = null;
|
|
SecurityContext parentSecurityContext = null;
|
|
|
|
try {
|
|
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
|
|
|
|
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
|
|
|
|
JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
|
|
ContextManagement parentContextManagement = new ContextManagement(oDatabaseDocument);
|
|
parentContextManagement.setJsonNode(parentJsonNode);
|
|
UUID parentUUID = parentContextManagement.uuid;
|
|
parentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(parentUUID);
|
|
|
|
checkContext(parentContextManagement);
|
|
if (uuid == null) {
|
|
uuid = UUID.randomUUID();
|
|
}
|
|
|
|
createVertex();
|
|
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
|
|
isParentOfManagement.setJsonNode(isParentOfJsonNode);
|
|
isParentOfManagement.setSourceEntityManagement(parentContextManagement);
|
|
isParentOfManagement.setTargetEntityManagement(this);
|
|
|
|
isParentOfManagement.internalCreate();
|
|
|
|
} else {
|
|
checkContext(null);
|
|
createVertex();
|
|
}
|
|
|
|
securityContext = new SecurityContext(uuid);
|
|
securityContext.setParentSecurityContext(parentSecurityContext);
|
|
securityContext.create(oDatabaseDocument);
|
|
|
|
ContextUtility.getInstance().addSecurityContext(securityContext);
|
|
|
|
return getElement();
|
|
} catch (Exception e) {
|
|
oDatabaseDocument.rollback();
|
|
if (securityContext != null) {
|
|
securityContext.delete(oDatabaseDocument);
|
|
if (parentSecurityContext != null && securityContext != null) {
|
|
parentSecurityContext.getChildren().remove(securityContext);
|
|
}
|
|
ContextCache.getInstance().cleanCache();
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
|
|
|
|
boolean parentChanged = false;
|
|
boolean nameChanged = false;
|
|
|
|
OVertex parent = null;
|
|
boolean found = false;
|
|
|
|
Iterable<OVertex> iterable = getElement().getVertices(ODirection.IN, IsParentOf.NAME);
|
|
for (OVertex p : iterable) {
|
|
if (found) {
|
|
String message = String.format("{} has more than one parent. {}", Context.NAME,
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
throw new ResourceRegistryException(message.toString());
|
|
}
|
|
parent = p;
|
|
found = true;
|
|
}
|
|
|
|
ContextManagement actualParentContextManagement = null;
|
|
if (parent != null) {
|
|
actualParentContextManagement = new ContextManagement(oDatabaseDocument);
|
|
actualParentContextManagement.setElement(parent);
|
|
}
|
|
|
|
ContextManagement newParentContextManagement = actualParentContextManagement;
|
|
|
|
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
|
|
JsonNode parentContextJsonNode = null;
|
|
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
|
|
parentContextJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
|
|
}
|
|
|
|
if (parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) {
|
|
UUID parentUUID = org.gcube.informationsystem.utils.Utility.getUUIDFromJsonNode(parentContextJsonNode);
|
|
if (actualParentContextManagement != null) {
|
|
if (parentUUID.compareTo(actualParentContextManagement.uuid) != 0) {
|
|
parentChanged = true;
|
|
}
|
|
} else {
|
|
parentChanged = true;
|
|
}
|
|
|
|
if (parentChanged) {
|
|
newParentContextManagement = new ContextManagement(oDatabaseDocument);
|
|
newParentContextManagement.setJsonNode(parentContextJsonNode);
|
|
}
|
|
} else {
|
|
if (actualParentContextManagement != null) {
|
|
parentChanged = true;
|
|
newParentContextManagement = null;
|
|
}
|
|
|
|
}
|
|
|
|
String oldName = getElement().getProperty(Context.NAME_PROPERTY);
|
|
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
|
|
if (oldName.compareTo(newName) != 0) {
|
|
nameChanged = true;
|
|
name = newName;
|
|
}
|
|
|
|
if (parentChanged || nameChanged) {
|
|
checkContext(newParentContextManagement);
|
|
}
|
|
|
|
if (parentChanged) {
|
|
move(newParentContextManagement, false);
|
|
}
|
|
|
|
element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys, ignoreStartWithKeys);
|
|
|
|
ContextCache.getInstance().cleanCache();
|
|
|
|
return element;
|
|
}
|
|
|
|
private void move(ContextManagement newParentContextManagement, boolean check)
|
|
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
|
|
if (check) {
|
|
checkContext(newParentContextManagement);
|
|
}
|
|
|
|
SecurityContext newParentSecurityContext = null;
|
|
|
|
// Removing the old parent relationship if any
|
|
Iterable<OEdge> edges = getElement().getEdges(ODirection.IN, IsParentOf.NAME);
|
|
if (edges != null && edges.iterator().hasNext()) {
|
|
Iterator<OEdge> edgeIterator = edges.iterator();
|
|
OEdge edge = edgeIterator.next();
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement();
|
|
isParentOfManagement.setElement(edge);
|
|
isParentOfManagement.internalDelete();
|
|
|
|
if (edgeIterator.hasNext()) {
|
|
throw new ContextException(
|
|
"Seems that the Context has more than one Parent. " + Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
if (newParentContextManagement != null) {
|
|
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
|
|
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
|
|
isParentOfManagement.setJsonNode(isParentOfJsonNode);
|
|
isParentOfManagement.setSourceEntityManagement(newParentContextManagement);
|
|
isParentOfManagement.setTargetEntityManagement(this);
|
|
isParentOfManagement.internalCreate();
|
|
newParentSecurityContext = ContextUtility.getInstance()
|
|
.getSecurityContextByUUID(newParentContextManagement.uuid);
|
|
}
|
|
|
|
SecurityContext thisSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(uuid);
|
|
thisSecurityContext.changeParentSecurityContext(newParentSecurityContext, oDatabaseDocument);
|
|
}
|
|
|
|
@Override
|
|
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
|
|
Iterable<OEdge> iterable = getElement().getEdges(ODirection.OUT);
|
|
Iterator<OEdge> iterator = iterable.iterator();
|
|
while (iterator.hasNext()) {
|
|
throw new ContextException("Cannot remove a " + Context.NAME + " having children");
|
|
}
|
|
|
|
element.delete();
|
|
|
|
ContextUtility contextUtility = ContextUtility.getInstance();
|
|
SecurityContext securityContext = contextUtility.getSecurityContextByUUID(uuid);
|
|
securityContext.delete(oDatabaseDocument);
|
|
|
|
ContextCache.getInstance().cleanCache();
|
|
}
|
|
|
|
@Override
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
|
Iterable<ODocument> iterable = oDatabaseDocument.browseClass(typeName, polymorphic);
|
|
for (ODocument vertex : iterable) {
|
|
ContextManagement contextManagement = new ContextManagement();
|
|
contextManagement.setElement((OVertex) vertex);
|
|
try {
|
|
JsonNode jsonObject = contextManagement.serializeAsJsonNode();
|
|
arrayNode.add(jsonObject);
|
|
} catch (ResourceRegistryException e) {
|
|
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
|
|
vertex.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
}
|
|
}
|
|
try {
|
|
return objectMapper.writeValueAsString(arrayNode);
|
|
} catch (JsonProcessingException e) {
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
}
|
|
|
|
public String allFromServer(boolean polymorphic) throws ResourceRegistryException {
|
|
return super.all(polymorphic);
|
|
}
|
|
|
|
@Override
|
|
public String all(boolean polymorphic) throws ResourceRegistryException {
|
|
try {
|
|
ContextCache contextCache = ContextCache.getInstance();
|
|
return ElementMapper.marshal(contextCache.getContexts());
|
|
} catch (JsonProcessingException | ResourceRegistryException e) {
|
|
return allFromServer(polymorphic);
|
|
}
|
|
}
|
|
|
|
public String readFromServer()
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
|
return super.read().toString();
|
|
}
|
|
|
|
public String readAsString()
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
|
try {
|
|
ContextCache contextCache = ContextCache.getInstance();
|
|
return ElementMapper.marshal(contextCache.getContextByUUID(uuid));
|
|
} catch (JsonProcessingException | ResourceRegistryException e) {
|
|
return readFromServer();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
|
// Nothing to do
|
|
}
|
|
}
|