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;
import java.util.Iterator;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
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.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.entity.EntityAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,14 +29,36 @@ public class ContextManagement extends EntityManagement<Context> {
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() {
super(AccessType.CONTEXT);
forceAdmin = true;
init();
}
public ContextManagement(OrientGraph 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
@ -86,13 +115,17 @@ public class ContextManagement extends EntityManagement<Context> {
String propertyName = Context.PARENT_PROPERTY;
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.setJSON(parentJsonNode);
Vertex parent = contextManagement.getElement();
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
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
protected boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
return false;
Iterable<Edge> iterable = getElement().getEdges(Direction.OUT);
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.Relation;
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 com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -19,8 +23,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("rawtypes")
public class IsParentOfManagement extends RelationManagement<IsParentOf> {
public class IsParentOfManagement extends RelationManagement<IsParentOf, ContextManagement, ContextManagement> {
public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF);
}
@ -28,12 +32,28 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
public IsParentOfManagement(OrientGraph 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
public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false, true);
}
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JSONObject relation = serializeSelfOnly();
@ -41,17 +61,17 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
Vertex source = element.getVertex(Direction.OUT);
ContextManagement sourceContextManagement = new ContextManagement(orientGraph);
sourceContextManagement.setElement(source);
if(includeSource) {
if (includeSource) {
relation.put(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly());
}
Vertex target = element.getVertex(Direction.IN);
ContextManagement targetContextManagement = new ContextManagement(orientGraph);
targetContextManagement.setElement(target);
if(includeTarget) {
if (includeTarget) {
relation.put(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly());
}
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
throw e;
@ -62,9 +82,15 @@ public class IsParentOfManagement extends RelationManagement<IsParentOf> {
return relation;
}
public Edge reallyCreate(Vertex source, Vertex target) throws ResourceRegistryException {
return super.reallyCreate(source, target);
@Override
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.resourceregistry.api.exceptions.ResourceRegistryException;
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.ERAvailableInAnotherContextException;
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.context.ContextManagement;
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.dbinitialization.DatabaseIntializator;
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 Class<El> elementClass;
protected AccessType accessType;
protected final AccessType accessType;
protected OrientGraph orientGraph;
@ -108,10 +101,14 @@ public abstract class ERManagement<ERType extends ER, El extends 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;
public AccessType getAccessType() {
return accessType;
}
public boolean isForceAdmin() {
return forceAdmin;
}
@ -346,15 +343,19 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public El internalCreate() throws ERAlreadyPresentException, ResourceRegistryException {
try {
reallyCreate();
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
if (entityHeader != null) {
element.setProperty(Entity.HEADER_PROPERTY, entityHeader);
} else {
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;
}catch (ResourceRegistryException e) {
@ -438,51 +439,11 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
this.uuid = HeaderUtility.getHeader(element).getUUID();
}
protected 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 abstract ERNotFoundException getSpecificElementNotFoundException(ERNotFoundException e);
protected 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 ERAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message);
protected abstract ERAlreadyPresentException getSpecificERAlreadyPresentException(String message);
public El getElement() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException {
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.resourceregistry.api.exceptions.ResourceRegistryException;
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.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import com.tinkerpop.blueprints.Vertex;
@ -27,7 +29,22 @@ public class FacetManagement extends EntityManagement<Facet> {
public FacetManagement(OrientGraph 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
public String serialize() throws ResourceRegistryException {
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.resourceregistry.api.exceptions.ResourceRegistryException;
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.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
@ -48,12 +50,28 @@ public class ResourceManagement extends EntityManagement<Resource> {
public ResourceManagement(OrientGraph 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
public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
@ -69,12 +87,12 @@ public class ResourceManagement extends EntityManagement<Resource> {
Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
for (Edge edge : edges) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = RelationManagement
.getRelationManagement(orientGraph, edge);
RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge);
relationManagement.setSourceEntityManagement(this);
if (relationManagement instanceof ConsistsOfManagement) {
try {
JSONObject consistsOf = relationManagement
.serializeAsJson();
JSONObject consistsOf = relationManagement.serializeAsJson(true, true);
sourceResource = addConsistsOf(sourceResource, consistsOf);
}catch (ResourceRegistryException e) {
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) {
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
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(
orientGraph);
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);
Iterator<Edge> iterator = iterable.iterator();
while (iterator.hasNext()) {
Edge edge = iterator.next();
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
@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.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;
@ -12,7 +18,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("rawtypes")
public class ConsistsOfManagement extends RelationManagement<ConsistsOf> {
public class ConsistsOfManagement extends RelationManagement<ConsistsOf, ResourceManagement, FacetManagement> {
public ConsistsOfManagement() {
super(AccessType.CONSISTS_OF);
@ -21,5 +27,21 @@ public class ConsistsOfManagement extends RelationManagement<ConsistsOf> {
public ConsistsOfManagement(OrientGraph 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.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;
@ -12,14 +17,30 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("rawtypes")
public class IsRelatedToManagement extends RelationManagement<IsRelatedTo> {
public class IsRelatedToManagement extends RelationManagement<IsRelatedTo, ResourceManagement, ResourceManagement> {
public IsRelatedToManagement() {
super(AccessType.IS_RELATED_TO);
}
public IsRelatedToManagement(OrientGraph 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;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
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.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.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
@ -45,13 +48,15 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
@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> {
protected final Class<? extends Entity> targetEntityClass;
protected S sourceEntityManagement;
protected T targetEntityManagement;
protected RelationManagement(AccessType accessType) {
super(accessType);
@ -77,6 +82,9 @@ public abstract class RelationManagement<R extends Relation> extends
this.targetEntityClass = Resource.class;
break;
}
sourceEntityManagement = null;
targetEntityManagement = null;
}
@ -84,7 +92,7 @@ public abstract class RelationManagement<R extends Relation> extends
this(accessType);
this.orientGraph = orientGraph;
}
@Override
public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
@ -92,19 +100,30 @@ public abstract class RelationManagement<R extends Relation> extends
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(true, true);
}
@SuppressWarnings("unchecked")
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JSONObject relation = serializeSelfOnly();
try {
Vertex source = element.getVertex(Direction.OUT);
EntityManagement sourceEntityManagement = EntityManagement
.getEntityManagement(orientGraph, source);
relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
if(includeSource) {
if(sourceEntityManagement == null) {
Vertex source = element.getVertex(Direction.OUT);
sourceEntityManagement = (S) EntityManagement.getEntityManagement(orientGraph, source);
}
relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
}
Vertex target = element.getVertex(Direction.IN);
EntityManagement targetEntityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
relation.put(Relation.TARGET_PROPERTY,
targetEntityManagement.serializeAsJson());
if(includeTarget) {
if(targetEntityManagement == null) {
Vertex target = element.getVertex(Direction.IN);
targetEntityManagement = (T) EntityManagement
.getEntityManagement(orientGraph, target);
}
relation.put(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson());
}
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
@ -161,69 +180,112 @@ public abstract class RelationManagement<R extends Relation> extends
return visitedSourceResources;
}
private Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
throws ResourceRegistryException {
ResourceManagement srmSource = new ResourceManagement(orientGraph);
srmSource.setUUID(sourceUUID);
Vertex source = srmSource.getElement();
EntityManagement entityManagement = getEntityManagement();
entityManagement.setUUID(targetUUID);
Vertex target = (Vertex) entityManagement.getElement();
return reallyCreate(source, target);
}
protected Edge reallyCreate(Vertex source, Vertex target)
throws ResourceRegistryException {
@Override
protected Edge reallyCreate() throws ResourceRegistryException {
EntityManagement sourceEntityManagement = EntityManagement
.getEntityManagement(orientGraph, source);
EntityManagement targetEntityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
if (!(sourceEntityManagement instanceof ResourceManagement)) {
String error = String.format(
"Any type of %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ",
Relation.NAME, Resource.NAME, Relation.SOURCE_PROPERTY,
erType, sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
if (this instanceof IsRelatedToManagement) {
if (!(targetEntityManagement instanceof ResourceManagement)) {
String error = String.format("A %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Resource.NAME, Relation.TARGET_PROPERTY, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
if(sourceEntityManagement==null) {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)){
throw new ResourceRegistryException(
"Error while creating relation. No source definition found");
}
} else if (this instanceof ConsistsOfManagement) {
if (!(targetEntityManagement instanceof FacetManagement)) {
String error = String.format("A %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Facet.NAME, Relation.TARGET_PROPERTY, erType,
UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
setSourceUUID(sourceUUID);
}
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 {
String error = String.format("{%s is not a %s nor a %s. "
+ "This is really strange and should not occur. "
+ "Please Investigate it.", this,
IsRelatedToManagement.class.getSimpleName(),
ConsistsOfManagement.class.getSimpleName());
throw new ResourceRegistryException(error);
if (!(sourceEntityManagement instanceof ResourceManagement)) {
String error = String.format(
"Any type of %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ",
Relation.NAME, Resource.NAME, Relation.SOURCE_PROPERTY,
erType, sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
if (this instanceof IsRelatedToManagement) {
if (!(targetEntityManagement instanceof ResourceManagement)) {
String error = String.format("A %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Resource.NAME, Relation.TARGET_PROPERTY, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
} else if (this instanceof ConsistsOfManagement) {
if (!(targetEntityManagement instanceof FacetManagement)) {
String error = String.format("A %s can have only a %s as %s. "
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Facet.NAME, Relation.TARGET_PROPERTY, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
throw new ResourceRegistryException(error);
}
} else {
String error = String.format("{%s is not a %s nor a %s. "
+ "This is really strange and should not occur. "
+ "Please Investigate it.", this,
IsRelatedToManagement.class.getSimpleName(),
ConsistsOfManagement.class.getSimpleName());
throw new ResourceRegistryException(error);
}
}
*/
// end of if-else to be revisited
logger.trace("Creating {} beetween {} -> {}", erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
Vertex source = (Vertex) sourceEntityManagement.getElement();
Vertex target = (Vertex) targetEntityManagement.getElement();
element = orientGraph.addEdge(null, source, target, erType);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys,
@ -241,43 +303,24 @@ public abstract class RelationManagement<R extends Relation> extends
return element;
}
public Edge reallyCreate(Vertex source) throws ResourceRegistryException {
Vertex target = null;
EntityManagement entityManagement = getEntityManagement();
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
throw new ResourceRegistryException(
"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 {
ResourceManagement srmSource = new ResourceManagement(orientGraph);
srmSource.setUUID(sourceUUID);
Vertex source = srmSource.getElement();
return reallyCreate(source);
}
@Override
protected Edge reallyCreate() throws ResourceRegistryException {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)){
throw new ResourceRegistryException(
"Error while creating relation. No source definition found");
}
UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
return reallyCreate(sourceUUID);
public void setSourceEntityManagement(S sourceEntityManagement) {
this.sourceEntityManagement = sourceEntityManagement;
}
public void setTargetEntityManagement(T targetEntityManagement) {
this.targetEntityManagement = targetEntityManagement;
}
@SuppressWarnings("unchecked")
public void setSourceUUID(UUID sourceUUID) throws ResourceRegistryException {
this.sourceEntityManagement = (S) ERManagement.getERManagementFromUUID(orientGraph, sourceUUID);
}
@SuppressWarnings("unchecked")
public void setTargetUUID(UUID targetUUID) throws ResourceRegistryException {
this.targetEntityManagement = (T) ERManagement.getERManagementFromUUID(orientGraph, targetUUID);
}
@Override
protected Edge reallyUpdate() throws ResourceRegistryException {
@ -483,17 +526,20 @@ public abstract class RelationManagement<R extends Relation> extends
return true;
}
protected EntityManagement getEntityManagement()
@SuppressWarnings("unchecked")
protected T getTargetEntityManagement()
throws ResourceRegistryException {
EntityManagement entityManagement;
T entityManagement;
switch (accessType) {
case CONSISTS_OF:
entityManagement = new FacetManagement(orientGraph);
entityManagement = (T) new FacetManagement(orientGraph);
break;
case IS_RELATED_TO:
entityManagement = new ResourceManagement(orientGraph);
entityManagement = (T) new ResourceManagement(orientGraph);
break;
default:
String error = String.format("{%s is not a %s nor a %s. "
+ "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)
throws ResourceRegistryException {
EntityManagement entityManagement = EntityManagement
.getEntityManagement(orientGraph, target);
EntityManagement entityManagement = EntityManagement.getEntityManagement(orientGraph, target);
if (entityManagement != null) {
return entityManagement.internalDelete();
} else {
@ -617,8 +662,11 @@ public abstract class RelationManagement<R extends Relation> extends
try {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = reallyCreate(sourceUUID, targetUUID);
setSourceUUID(sourceUUID);
setTargetUUID(targetUUID);
element = reallyCreate();
orientGraph.commit();
@ -713,7 +761,7 @@ public abstract class RelationManagement<R extends Relation> extends
}
@Override
public boolean addToContext() throws ContextException {
public boolean addToContext() throws ERNotFoundException, ContextException {
logger.debug("Going to add {} with UUID {} to actual Context",
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
public void testJava() throws Exception {

View File

@ -16,6 +16,7 @@ import java.util.Map;
import java.util.UUID;
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.entity.facet.AccessPointFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
@ -164,7 +165,7 @@ public class ERManagementTest extends ScopedTest {
}
@Test(expected=ResourceRegistryException.class)
public void testCreateAbstarctEntity() throws Exception {
public void testCreateAbstractEntity() throws Exception {
StateFacet stateFacet = new StateFacetImpl();
stateFacet.setValue("READY");
@ -383,6 +384,49 @@ public class ERManagementTest extends ScopedTest {
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
public void testCreateHostingNodeAndEServiceWithSharedFacet() throws Exception {
Map<String, Resource> map = createHostingNodeAndEService();