Refs #10238: Refactor Context Port Type

Task-Url: https://support.d4science.org/issues/10238

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158595 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-17 10:42:37 +00:00
parent 3794de9127
commit b9a5ebe072
10 changed files with 420 additions and 201 deletions

View File

@ -1,13 +1,20 @@
package org.gcube.informationsystem.resourceregistry.context; package org.gcube.informationsystem.resourceregistry.context;
import java.util.Iterator;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.entity.Context; import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,14 +29,36 @@ public class ContextManagement extends EntityManagement<Context> {
private static Logger logger = LoggerFactory.getLogger(ContextManagement.class); private static Logger logger = LoggerFactory.getLogger(ContextManagement.class);
private void init() {
forceAdmin = true;
this.ignoreStartWithKeys.add(Context.PARENT_PROPERTY);
this.ignoreStartWithKeys.add(Context.CHILDREN_PROPERTY);
}
public ContextManagement() { public ContextManagement() {
super(AccessType.CONTEXT); super(AccessType.CONTEXT);
forceAdmin = true; init();
} }
public ContextManagement(OrientGraph orientGraph) { public ContextManagement(OrientGraph orientGraph) {
super(AccessType.CONTEXT, orientGraph); super(AccessType.CONTEXT, orientGraph);
forceAdmin = true; init();
}
@Override
protected ContextNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new ContextNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected EntityAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
return new EntityAvailableInAnotherContextException(message);
}
@Override
protected ContextAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new ContextAlreadyPresentException(message);
} }
@Override @Override
@ -86,13 +115,17 @@ public class ContextManagement extends EntityManagement<Context> {
String propertyName = Context.PARENT_PROPERTY; String propertyName = Context.PARENT_PROPERTY;
if (jsonNode.has(propertyName)) { if (jsonNode.has(propertyName)) {
JsonNode parentJsonNode = jsonNode.get(propertyName); JsonNode isParentOfJsonNode = jsonNode.get(propertyName);
JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
ContextManagement contextManagement = new ContextManagement(orientGraph); ContextManagement contextManagement = new ContextManagement(orientGraph);
contextManagement.setJSON(parentJsonNode); contextManagement.setJSON(parentJsonNode);
Vertex parent = contextManagement.getElement();
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph); IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph);
isParentOfManagement.reallyCreate(parent, this.element); isParentOfManagement.setJSON(isParentOfJsonNode);
isParentOfManagement.setSourceEntityManagement(contextManagement);
isParentOfManagement.setTargetEntityManagement(this);
isParentOfManagement.internalCreate();
} }
@ -101,17 +134,28 @@ public class ContextManagement extends EntityManagement<Context> {
@Override @Override
protected Vertex reallyUpdate() throws ERNotFoundException, ResourceRegistryException { protected Vertex reallyUpdate() throws ERNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
return null; Vertex context = getElement();
// TODO check if parent changed
context = (Vertex) ERManagement.updateProperties(oClass, context, jsonNode, ignoreKeys, ignoreStartWithKeys);
return context;
} }
@Override @Override
protected boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException { protected boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub Iterable<Edge> iterable = getElement().getEdges(Direction.OUT);
return false; Iterator<Edge> iterator = iterable.iterator();
while (iterator.hasNext()) {
throw new ContextException("Cannot remove a " + Context.NAME + " having children");
} }
element.remove();
return true;
}
} }

View File

@ -8,10 +8,14 @@ import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsParentOf; import org.gcube.informationsystem.model.relation.IsParentOf;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isparentof.IsParentOfNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -19,7 +23,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public class IsParentOfManagement extends RelationManagement<IsParentOf> { public class IsParentOfManagement extends RelationManagement<IsParentOf, ContextManagement, ContextManagement> {
public IsParentOfManagement() { public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF); super(AccessType.IS_PARENT_OF);
@ -29,6 +33,22 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
super(AccessType.IS_PARENT_OF, orientGraph); super(AccessType.IS_PARENT_OF, orientGraph);
} }
@Override
protected IsParentOfNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new IsParentOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected RelationAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
String message) {
return new RelationAvailableInAnotherContextException(message);
}
@Override
protected IsParentOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new IsParentOfAlreadyPresentException(message);
}
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false, true); return serializeAsJson(false, true);
@ -41,14 +61,14 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
Vertex source = element.getVertex(Direction.OUT); Vertex source = element.getVertex(Direction.OUT);
ContextManagement sourceContextManagement = new ContextManagement(orientGraph); ContextManagement sourceContextManagement = new ContextManagement(orientGraph);
sourceContextManagement.setElement(source); sourceContextManagement.setElement(source);
if(includeSource) { if (includeSource) {
relation.put(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly()); relation.put(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly());
} }
Vertex target = element.getVertex(Direction.IN); Vertex target = element.getVertex(Direction.IN);
ContextManagement targetContextManagement = new ContextManagement(orientGraph); ContextManagement targetContextManagement = new ContextManagement(orientGraph);
targetContextManagement.setElement(target); targetContextManagement.setElement(target);
if(includeTarget) { if (includeTarget) {
relation.put(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly()); relation.put(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly());
} }
@ -63,8 +83,14 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
return relation; return relation;
} }
public Edge reallyCreate(Vertex source, Vertex target) throws ResourceRegistryException { @Override
return super.reallyCreate(source, target); public boolean addToContext() throws ERNotFoundException, ContextException {
throw new UnsupportedOperationException();
}
@Override
public boolean removeFromContext() throws ERNotFoundException, ContextException {
throw new UnsupportedOperationException();
} }
} }

View File

@ -32,20 +32,13 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.context.ContextManagement;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.IsParentOfManagement;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
@ -96,7 +89,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
protected final Set<String> ignoreStartWithKeys; protected final Set<String> ignoreStartWithKeys;
protected Class<El> elementClass; protected Class<El> elementClass;
protected AccessType accessType; protected final AccessType accessType;
protected OrientGraph orientGraph; protected OrientGraph orientGraph;
@ -108,10 +101,14 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
protected El element; protected El element;
/** /**
* Yhis boolean is used to force the use of ADMIN user instead of the user of the context * This boolean is used to force the use of ADMIN user instead of the user of the context
*/ */
protected boolean forceAdmin; protected boolean forceAdmin;
public AccessType getAccessType() {
return accessType;
}
public boolean isForceAdmin() { public boolean isForceAdmin() {
return forceAdmin; return forceAdmin;
} }
@ -346,15 +343,19 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public El internalCreate() throws ERAlreadyPresentException, ResourceRegistryException { public El internalCreate() throws ERAlreadyPresentException, ResourceRegistryException {
try { try {
reallyCreate(); reallyCreate();
Header entityHeader = HeaderUtility.getHeader(jsonNode, true); Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
if (entityHeader != null) { if (entityHeader != null) {
element.setProperty(Entity.HEADER_PROPERTY, entityHeader); element.setProperty(Entity.HEADER_PROPERTY, entityHeader);
} else { } else {
entityHeader = HeaderUtility.addHeader(element, null); entityHeader = HeaderUtility.addHeader(element, null);
} }
ContextUtility.addToActualContext(orientGraph, element);
((OrientVertex) element).save(); if(!(this instanceof ContextManagement || this instanceof IsParentOfManagement)){
ContextUtility.addToActualContext(orientGraph, element);
}
((OrientElement) element).save();
return element; return element;
}catch (ResourceRegistryException e) { }catch (ResourceRegistryException e) {
@ -438,51 +439,11 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
this.uuid = HeaderUtility.getHeader(element).getUUID(); this.uuid = HeaderUtility.getHeader(element).getUUID();
} }
protected ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) { protected abstract ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e);
switch (accessType) {
case RESOURCE:
return new ResourceNotFoundException(e.getMessage(), e.getCause());
case FACET:
return new FacetNotFoundException(e.getMessage(), e.getCause());
case IS_RELATED_TO:
return new RelationNotFoundException(e.getMessage(), e.getCause());
case CONSISTS_OF:
return new RelationNotFoundException(e.getMessage(), e.getCause());
default:
return e;
}
}
protected ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message){ protected abstract ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message);
switch (accessType) {
case RESOURCE:
return new ResourceAvailableInAnotherContextException(message);
case FACET:
return new FacetAvailableInAnotherContextException(message);
case IS_RELATED_TO:
return new RelationAvailableInAnotherContextException(message);
case CONSISTS_OF:
return new RelationAvailableInAnotherContextException(message);
default:
return new ERAvailableInAnotherContextException(message);
}
}
protected ERAlreadyPresentException getSpecificERAlreadyPresentException(String message){
switch (accessType) {
case RESOURCE:
return new ResourceAlreadyPresentException(message);
case FACET:
return new FacetAlreadyPresentException(message);
case IS_RELATED_TO:
return new RelationAlreadyPresentException(message);
case CONSISTS_OF:
return new RelationAlreadyPresentException(message);
default:
return new ERAlreadyPresentException(message);
}
}
protected abstract ERAlreadyPresentException getSpecificERAlreadyPresentException(String message);
public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException {
if (element == null) { if (element == null) {

View File

@ -8,7 +8,9 @@ import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
@ -28,6 +30,21 @@ public class FacetManagement extends EntityManagement<Facet> {
super(AccessType.FACET, orientGraph); super(AccessType.FACET, orientGraph);
} }
@Override
protected FacetNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new FacetNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected FacetAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
return new FacetAvailableInAnotherContextException(message);
}
@Override
protected FacetAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new FacetAlreadyPresentException(message);
}
@Override @Override
public String serialize() throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return serializeSelfOnly().toString(); return serializeSelfOnly().toString();

View File

@ -14,7 +14,9 @@ import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
@ -49,11 +51,27 @@ public class ResourceManagement extends EntityManagement<Resource> {
super(AccessType.RESOURCE, orientGraph); super(AccessType.RESOURCE, orientGraph);
} }
@Override
protected ResourceNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new ResourceNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected ResourceAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
return new ResourceAvailableInAnotherContextException(message);
}
@Override
protected ResourceAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new ResourceAlreadyPresentException(message);
}
@Override @Override
public String serialize() throws ResourceRegistryException { public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString(); return serializeAsJson().toString();
} }
@SuppressWarnings("unchecked")
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
@ -69,12 +87,12 @@ public class ResourceManagement extends EntityManagement<Resource> {
Iterable<Edge> edges = getElement().getEdges(Direction.OUT); Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
for (Edge edge : edges) { for (Edge edge : edges) {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge);
.getRelationManagement(orientGraph, edge); relationManagement.setSourceEntityManagement(this);
if (relationManagement instanceof ConsistsOfManagement) { if (relationManagement instanceof ConsistsOfManagement) {
try { try {
JSONObject consistsOf = relationManagement JSONObject consistsOf = relationManagement.serializeAsJson(true, true);
.serializeAsJson();
sourceResource = addConsistsOf(sourceResource, consistsOf); sourceResource = addConsistsOf(sourceResource, consistsOf);
}catch (ResourceRegistryException e) { }catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge); logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge);
@ -122,7 +140,8 @@ public class ResourceManagement extends EntityManagement<Resource> {
for (JsonNode consistOfJsonNode : jsonNodeArray) { for (JsonNode consistOfJsonNode : jsonNodeArray) {
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph); ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
com.setJSON(consistOfJsonNode); com.setJSON(consistOfJsonNode);
com.reallyCreate(element); com.setSourceEntityManagement(this);
com.internalCreate();
} }
} }
@ -133,7 +152,8 @@ public class ResourceManagement extends EntityManagement<Resource> {
IsRelatedToManagement irtm = new IsRelatedToManagement( IsRelatedToManagement irtm = new IsRelatedToManagement(
orientGraph); orientGraph);
irtm.setJSON(relationJsonNode); irtm.setJSON(relationJsonNode);
irtm.reallyCreate(element); irtm.setSourceEntityManagement(this);
irtm.internalCreate();
} }
} }
@ -180,6 +200,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
Iterable<Edge> iterable = element.getEdges(Direction.OUT); Iterable<Edge> iterable = element.getEdges(Direction.OUT);
Iterator<Edge> iterator = iterable.iterator(); Iterator<Edge> iterator = iterable.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Edge edge = iterator.next(); Edge edge = iterator.next();
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType(); OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")

View File

@ -5,6 +5,12 @@ package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.consistsOf.ConsistsOfNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -12,7 +18,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public class ConsistsOfManagement extends RelationManagement<ConsistsOf> { public class ConsistsOfManagement extends RelationManagement<ConsistsOf, ResourceManagement, FacetManagement> {
public ConsistsOfManagement() { public ConsistsOfManagement() {
super(AccessType.CONSISTS_OF); super(AccessType.CONSISTS_OF);
@ -22,4 +28,20 @@ public class ConsistsOfManagement extends RelationManagement<ConsistsOf> {
super(AccessType.CONSISTS_OF, orientGraph); super(AccessType.CONSISTS_OF, orientGraph);
} }
@Override
protected ConsistsOfNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new ConsistsOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected ConsistsOfAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
String message) {
return new ConsistsOfAvailableInAnotherContextException(message);
}
@Override
protected ConsistsOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new ConsistsOfAlreadyPresentException(message);
}
} }

View File

@ -5,6 +5,11 @@ package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.isrelatedto.IsRelatedToNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -12,7 +17,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public class IsRelatedToManagement extends RelationManagement<IsRelatedTo> { public class IsRelatedToManagement extends RelationManagement<IsRelatedTo, ResourceManagement, ResourceManagement> {
public IsRelatedToManagement() { public IsRelatedToManagement() {
super(AccessType.IS_RELATED_TO); super(AccessType.IS_RELATED_TO);
@ -22,4 +27,20 @@ public class IsRelatedToManagement extends RelationManagement<IsRelatedTo> {
super(AccessType.IS_RELATED_TO, orientGraph); super(AccessType.IS_RELATED_TO, orientGraph);
} }
@Override
protected IsRelatedToNotFoundException getSpecificElementNotFoundException(ERNotFoundException e) {
return new IsRelatedToNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected IsRelatedToAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
String message) {
return new IsRelatedToAvailableInAnotherContextException(message);
}
@Override
protected IsRelatedToAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new IsRelatedToAlreadyPresentException(message);
}
} }

View File

@ -3,6 +3,7 @@
*/ */
package org.gcube.informationsystem.resourceregistry.er.relation; package org.gcube.informationsystem.resourceregistry.er.relation;
import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -25,7 +26,9 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
@ -45,14 +48,16 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public abstract class RelationManagement<R extends Relation> extends public abstract class RelationManagement<R extends Relation, S extends EntityManagement, T extends EntityManagement> extends
ERManagement<R, Edge> { ERManagement<R, Edge> {
protected final Class<? extends Entity> targetEntityClass; protected final Class<? extends Entity> targetEntityClass;
protected S sourceEntityManagement;
protected T targetEntityManagement;
protected RelationManagement(AccessType accessType) { protected RelationManagement(AccessType accessType) {
super(accessType); super(accessType);
@ -78,6 +83,9 @@ public abstract class RelationManagement<R extends Relation> extends
break; break;
} }
sourceEntityManagement = null;
targetEntityManagement = null;
} }
protected RelationManagement(AccessType accessType, OrientGraph orientGraph) { protected RelationManagement(AccessType accessType, OrientGraph orientGraph) {
@ -92,19 +100,30 @@ public abstract class RelationManagement<R extends Relation> extends
@Override @Override
public JSONObject serializeAsJson() throws ResourceRegistryException { public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(true, true);
}
@SuppressWarnings("unchecked")
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JSONObject relation = serializeSelfOnly(); JSONObject relation = serializeSelfOnly();
try { try {
if(includeSource) {
if(sourceEntityManagement == null) {
Vertex source = element.getVertex(Direction.OUT); Vertex source = element.getVertex(Direction.OUT);
EntityManagement sourceEntityManagement = EntityManagement sourceEntityManagement = (S) EntityManagement.getEntityManagement(orientGraph, source);
.getEntityManagement(orientGraph, source); }
relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly()); relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
}
if(includeTarget) {
if(targetEntityManagement == null) {
Vertex target = element.getVertex(Direction.IN); Vertex target = element.getVertex(Direction.IN);
EntityManagement targetEntityManagement = EntityManagement targetEntityManagement = (T) EntityManagement
.getEntityManagement(orientGraph, target); .getEntityManagement(orientGraph, target);
relation.put(Relation.TARGET_PROPERTY, }
targetEntityManagement.serializeAsJson()); relation.put(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson());
}
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e); logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
@ -162,27 +181,63 @@ public abstract class RelationManagement<R extends Relation> extends
return visitedSourceResources; return visitedSourceResources;
} }
private Edge reallyCreate(UUID sourceUUID, UUID targetUUID) @Override
throws ResourceRegistryException { protected Edge reallyCreate() throws ResourceRegistryException {
ResourceManagement srmSource = new ResourceManagement(orientGraph);
srmSource.setUUID(sourceUUID);
Vertex source = srmSource.getElement();
EntityManagement entityManagement = getEntityManagement(); if(sourceEntityManagement==null) {
entityManagement.setUUID(targetUUID);
Vertex target = (Vertex) entityManagement.getElement();
return reallyCreate(source, target);
if(!jsonNode.has(Relation.SOURCE_PROPERTY)){
throw new ResourceRegistryException(
"Error while creating relation. No source definition found");
} }
protected Edge reallyCreate(Vertex source, Vertex target) UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility
throws ResourceRegistryException { .getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
EntityManagement sourceEntityManagement = EntityManagement setSourceUUID(sourceUUID);
.getEntityManagement(orientGraph, source); }
EntityManagement targetEntityManagement = EntityManagement
.getEntityManagement(orientGraph, target); if(targetEntityManagement == null) {
targetEntityManagement = getTargetEntityManagement();
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
throw new ResourceRegistryException(
"Error while creating " + erType + ". No target definition found");
}
try {
targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
}catch (SchemaException e) {
StringWriter errorMessage = new StringWriter();
errorMessage.append("A ");
errorMessage.append(erType);
errorMessage.append(" can be only created beetween ");
errorMessage.append(sourceEntityManagement.getAccessType().getName());
errorMessage.append(" and ");
errorMessage.append(targetEntityManagement.getAccessType().getName());
throw new ResourceRegistryException(errorMessage.toString(), e);
}
try {
targetEntityManagement.getElement();
} catch (Exception e) {
targetEntityManagement.internalCreate();
}
}
// Revisit this if-else because should be not needed anymore.
/*
if(this instanceof IsParentOfManagement) {
if (!(sourceEntityManagement instanceof ContextManagement && targetEntityManagement instanceof ContextManagement)) {
String error = String.format(
"A %s can be only created beetween two %s. "
+ "Cannot instatiate %s beetween %s -> %s ",
IsParentOf.NAME, Context.NAME, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
} else {
if (!(sourceEntityManagement instanceof ResourceManagement)) { if (!(sourceEntityManagement instanceof ResourceManagement)) {
String error = String.format( String error = String.format(
"Any type of %s can have only a %s as %s. " "Any type of %s can have only a %s as %s. "
@ -220,10 +275,17 @@ public abstract class RelationManagement<R extends Relation> extends
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }
}
*/
// end of if-else to be revisited
logger.trace("Creating {} beetween {} -> {}", erType, logger.trace("Creating {} beetween {} -> {}", erType,
sourceEntityManagement.serialize(), sourceEntityManagement.serialize(),
targetEntityManagement.serialize()); targetEntityManagement.serialize());
Vertex source = (Vertex) sourceEntityManagement.getElement();
Vertex target = (Vertex) targetEntityManagement.getElement();
element = orientGraph.addEdge(null, source, target, erType); element = orientGraph.addEdge(null, source, target, erType);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys,
@ -241,44 +303,25 @@ public abstract class RelationManagement<R extends Relation> extends
return element; return element;
} }
public Edge reallyCreate(Vertex source) throws ResourceRegistryException {
Vertex target = null;
EntityManagement entityManagement = getEntityManagement();
if (!jsonNode.has(Relation.TARGET_PROPERTY)) { public void setSourceEntityManagement(S sourceEntityManagement) {
throw new ResourceRegistryException( this.sourceEntityManagement = sourceEntityManagement;
"Error while creating relation. No target definition found");
}
entityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
try {
target = (Vertex) entityManagement.getElement();
} catch (Exception e) {
target = (Vertex) entityManagement.internalCreate();
}
return reallyCreate(source, target);
} }
public Edge reallyCreate(UUID sourceUUID) throws ResourceRegistryException { public void setTargetEntityManagement(T targetEntityManagement) {
ResourceManagement srmSource = new ResourceManagement(orientGraph); this.targetEntityManagement = targetEntityManagement;
srmSource.setUUID(sourceUUID);
Vertex source = srmSource.getElement();
return reallyCreate(source);
} }
@Override @SuppressWarnings("unchecked")
protected Edge reallyCreate() throws ResourceRegistryException { public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)){ this.sourceEntityManagement = (S) ERManagement.getERManagementFromUUID(orientGraph, sourceUUID);
throw new ResourceRegistryException(
"Error while creating relation. No source definition found");
} }
UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility @SuppressWarnings("unchecked")
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY)); public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException {
this.targetEntityManagement = (T) ERManagement.getERManagementFromUUID(orientGraph, targetUUID);
return reallyCreate(sourceUUID);
} }
@Override @Override
protected Edge reallyUpdate() throws ResourceRegistryException { protected Edge reallyUpdate() throws ResourceRegistryException {
@ -483,17 +526,20 @@ public abstract class RelationManagement<R extends Relation> extends
return true; return true;
} }
protected EntityManagement getEntityManagement()
@SuppressWarnings("unchecked")
protected T getTargetEntityManagement()
throws ResourceRegistryException { throws ResourceRegistryException {
EntityManagement entityManagement; T entityManagement;
switch (accessType) { switch (accessType) {
case CONSISTS_OF: case CONSISTS_OF:
entityManagement = new FacetManagement(orientGraph); entityManagement = (T) new FacetManagement(orientGraph);
break; break;
case IS_RELATED_TO: case IS_RELATED_TO:
entityManagement = new ResourceManagement(orientGraph); entityManagement = (T) new ResourceManagement(orientGraph);
break; break;
default: default:
String error = String.format("{%s is not a %s nor a %s. " String error = String.format("{%s is not a %s nor a %s. "
+ "This is really strange ad should not occur. " + "This is really strange ad should not occur. "
@ -537,8 +583,7 @@ public abstract class RelationManagement<R extends Relation> extends
protected boolean deleteTargetVertex(Vertex target) protected boolean deleteTargetVertex(Vertex target)
throws ResourceRegistryException { throws ResourceRegistryException {
EntityManagement entityManagement = EntityManagement EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target);
.getEntityManagement(orientGraph, target);
if (entityManagement != null) { if (entityManagement != null) {
return entityManagement.internalDelete(); return entityManagement.internalDelete();
} else { } else {
@ -618,7 +663,10 @@ public abstract class RelationManagement<R extends Relation> extends
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = reallyCreate(sourceUUID, targetUUID); setSourceUUID(sourceUUID);
setTargetUUID(targetUUID);
element = reallyCreate();
orientGraph.commit(); orientGraph.commit();
@ -713,7 +761,7 @@ public abstract class RelationManagement<R extends Relation> extends
} }
@Override @Override
public boolean addToContext() throws ContextException { public boolean addToContext() throws ERNotFoundException, ContextException {
logger.debug("Going to add {} with UUID {} to actual Context", logger.debug("Going to add {} with UUID {} to actual Context",
accessType.getName(), uuid); accessType.getName(), uuid);

View File

@ -53,6 +53,21 @@ public class ContextManagementTest {
} }
@Test
public void test() throws Exception {
UUID uuid = UUID.fromString("34395f1c-ecb3-4d21-8abe-986ba1650919");
Context myTest = new ContextImpl("myTest");
myTest.setParent(uuid);
String contextJsonString = ISMapper.marshal(myTest);
logger.debug("myTest : {}", contextJsonString);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setJSON(contextJsonString);
contextManagement.create();
}
@Test @Test
public void testJava() throws Exception { public void testJava() throws Exception {

View File

@ -16,6 +16,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.informationsystem.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl; import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl;
import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl; import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl; import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
@ -164,7 +165,7 @@ public class ERManagementTest extends ScopedTest {
} }
@Test(expected=ResourceRegistryException.class) @Test(expected=ResourceRegistryException.class)
public void testCreateAbstarctEntity() throws Exception { public void testCreateAbstractEntity() throws Exception {
StateFacet stateFacet = new StateFacetImpl(); StateFacet stateFacet = new StateFacetImpl();
stateFacet.setValue("READY"); stateFacet.setValue("READY");
@ -383,6 +384,49 @@ public class ERManagementTest extends ScopedTest {
Assert.assertTrue(deleted); Assert.assertTrue(deleted);
} }
@Test
public void testCreateConsistsOfBeetweenResources() throws Exception {
Map<String, Resource> map = createHostingNodeAndEService();
UUID hostingNodeUUID = map.get(HostingNode.NAME).getHeader().getUUID();
UUID eServiceUUID = map.get(EService.NAME).getHeader().getUUID();
HostingNode hostingNode = new HostingNodeImpl();
hostingNode.setHeader(new HeaderImpl(hostingNodeUUID));
SimpleFacet fakeEServiceAsSimpleFacet = new SimpleFacetImpl();
fakeEServiceAsSimpleFacet.setHeader(new HeaderImpl(eServiceUUID));
ConsistsOf<Resource, Facet> consistsOf = new ConsistsOfImpl<Resource, Facet>(hostingNode, fakeEServiceAsSimpleFacet, null);
try {
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
String json = ISMapper.marshal(consistsOf);
json = json.replaceAll(SimpleFacet.NAME, EService.NAME);
consistsOfManagement.setJSON(json);
consistsOfManagement.create();
logger.debug("The creation terminated correctly. This should not happen");
} catch (ResourceRegistryException e) {
logger.error("Sounds good. A {} cannot be created between two resources", ConsistsOf.NAME, e);
} finally {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
boolean deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNodeUUID);
deleted = resourceManagement.delete();
Assert.assertTrue(deleted);
}
}
@Test @Test
public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception { public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception {
Map<String, Resource> map = createHostingNodeAndEService(); Map<String, Resource> map = createHostingNodeAndEService();