Refs #11288: Made resource-registry more RESTful

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

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@168995 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-06-11 09:01:10 +00:00
parent f8480f0c43
commit d5d79c14cc
7 changed files with 161 additions and 28 deletions

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
@ -31,8 +32,16 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@SuppressWarnings("rawtypes")
public class IsParentOfManagement extends RelationManagement<IsParentOf,ContextManagement,ContextManagement> {
public static final PropagationConstraint DEFAULT_IS_PARENT_OF_PC;
static {
DEFAULT_IS_PARENT_OF_PC = new PropagationConstraintImpl();
DEFAULT_IS_PARENT_OF_PC.setRemoveConstraint(RemoveConstraint.keep);
DEFAULT_IS_PARENT_OF_PC.setAddConstraint(AddConstraint.unpropagate);
}
public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF);
super(AccessType.IS_PARENT_OF, DEFAULT_IS_PARENT_OF_PC);
}
public IsParentOfManagement(OrientGraph orientGraph) throws ResourceRegistryException {

View File

@ -21,7 +21,6 @@ import org.gcube.informationsystem.model.ER;
import org.gcube.informationsystem.model.ISManageable;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
@ -251,7 +250,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
if(entityHeader != null) {
element.setProperty(Entity.HEADER_PROPERTY, entityHeader);
element.setProperty(ER.HEADER_PROPERTY, entityHeader);
} else {
entityHeader = HeaderUtility.addHeader(element, null);
}

View File

@ -1,6 +1,10 @@
package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -19,12 +23,20 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@SuppressWarnings("rawtypes")
public class ConsistsOfManagement extends RelationManagement<ConsistsOf,ResourceManagement,FacetManagement> {
public static final PropagationConstraint DEFAULT_CONSISTS_OF_PC;
static {
DEFAULT_CONSISTS_OF_PC = new PropagationConstraintImpl();
DEFAULT_CONSISTS_OF_PC.setRemoveConstraint(RemoveConstraint.cascadeWhenOrphan);
DEFAULT_CONSISTS_OF_PC.setAddConstraint(AddConstraint.propagate);
}
public ConsistsOfManagement() {
super(AccessType.CONSISTS_OF);
super(AccessType.CONSISTS_OF, DEFAULT_CONSISTS_OF_PC);
}
public ConsistsOfManagement(SecurityContext workingContext, OrientGraph orientGraph) {
super(AccessType.CONSISTS_OF, workingContext, orientGraph);
super(AccessType.CONSISTS_OF, workingContext, orientGraph, DEFAULT_CONSISTS_OF_PC);
}
@Override

View File

@ -1,6 +1,10 @@
package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.impl.embedded.PropagationConstraintImpl;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -18,12 +22,20 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@SuppressWarnings("rawtypes")
public class IsRelatedToManagement extends RelationManagement<IsRelatedTo,ResourceManagement,ResourceManagement> {
public static final PropagationConstraint DEFAULT_IS_RELATED_TO_PC;
static {
DEFAULT_IS_RELATED_TO_PC = new PropagationConstraintImpl();
DEFAULT_IS_RELATED_TO_PC.setRemoveConstraint(RemoveConstraint.keep);
DEFAULT_IS_RELATED_TO_PC.setAddConstraint(AddConstraint.unpropagate);
}
public IsRelatedToManagement() {
super(AccessType.IS_RELATED_TO);
super(AccessType.IS_RELATED_TO,DEFAULT_IS_RELATED_TO_PC);
}
public IsRelatedToManagement(SecurityContext workingContext, OrientGraph orientGraph) {
super(AccessType.IS_RELATED_TO, workingContext, orientGraph);
super(AccessType.IS_RELATED_TO, workingContext, orientGraph, DEFAULT_IS_RELATED_TO_PC);
}
@Override

View File

@ -8,6 +8,7 @@ import java.util.UUID;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
@ -29,14 +30,18 @@ import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.utils.PropagationConstraintOrient;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import com.fasterxml.jackson.databind.JsonNode;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientElement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
/**
@ -51,7 +56,9 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
protected S sourceEntityManagement;
protected T targetEntityManagement;
protected RelationManagement(AccessType accessType) {
public final PropagationConstraint defaultPropagationConstraint;
protected RelationManagement(AccessType accessType, PropagationConstraint defaultPropagationConstraint) {
super(accessType);
this.ignoreKeys.add(Relation.HEADER_PROPERTY);
@ -76,13 +83,14 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
break;
}
sourceEntityManagement = null;
targetEntityManagement = null;
this.sourceEntityManagement = null;
this.targetEntityManagement = null;
this.defaultPropagationConstraint = defaultPropagationConstraint;
}
protected RelationManagement(AccessType accessType, SecurityContext workingContext, OrientGraph orientGraph) {
this(accessType);
protected RelationManagement(AccessType accessType, SecurityContext workingContext, OrientGraph orientGraph,
PropagationConstraint defaultPropagationConstraint) {
this(accessType, defaultPropagationConstraint);
this.orientGraph = orientGraph;
setWorkingContext(workingContext);
}
@ -202,6 +210,52 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return visitedSourceResources;
}
protected PropagationConstraintOrient getPropagationConstraint(ODocument oDocument)
throws ResourceRegistryException {
PropagationConstraintOrient propagationConstraintOrient = new PropagationConstraintOrient();
PropagationConstraint propagationConstraint = null;
if(oDocument == null) {
propagationConstraint = defaultPropagationConstraint;
} else if(oDocument instanceof PropagationConstraintOrient) {
propagationConstraint = (PropagationConstraint) oDocument;
} else {
try {
propagationConstraint = ISMapper.unmarshal(PropagationConstraint.class, oDocument.toJSON());
} catch(Exception e) {
logger.warn("Unable to recreate {}. {}", PropagationConstraint.NAME,
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
AddConstraint addConstraint = propagationConstraint.getAddConstraint();
if(addConstraint == null) {
addConstraint = defaultPropagationConstraint.getAddConstraint();
logger.warn("Unable to get {}. Default value ({}) will be used", AddConstraint.class.getSimpleName(),
addConstraint);
}
propagationConstraintOrient.setAddConstraint(addConstraint);
RemoveConstraint removeConstraint = propagationConstraint.getRemoveConstraint();
if(removeConstraint == null) {
removeConstraint = defaultPropagationConstraint.getRemoveConstraint();
logger.warn("Unable to get {}. Default value ({}) will be used", RemoveConstraint.class.getSimpleName(),
removeConstraint);
}
propagationConstraintOrient.setRemoveConstraint(removeConstraint);
return propagationConstraintOrient;
}
protected void checkPropagationConstraint() throws ResourceRegistryException {
OrientElement orientElement = (OrientElement) element;
Object object = orientElement.getProperty(Relation.PROPAGATION_CONSTRAINT);
PropagationConstraintOrient pc = getPropagationConstraint((ODocument) object);
orientElement.setProperty(Relation.PROPAGATION_CONSTRAINT, pc, OType.EMBEDDED);
}
@Override
protected Edge reallyCreate() throws ResourceRegistryException {
@ -222,7 +276,8 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
targetEntityManagement = newTargetEntityManagement();
if(!jsonNode.has(Relation.TARGET_PROPERTY)) {
throw new ResourceRegistryException("Error while creating " + elementType + ". No target definition found");
throw new ResourceRegistryException(
"Error while creating " + elementType + ". No target definition found");
}
try {
@ -255,6 +310,8 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
checkPropagationConstraint();
logger.info("{} successfully created", elementType);
return element;
@ -288,7 +345,8 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException {
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
AddConstraint addConstraint = AddConstraint.unpropagate;
@ -335,7 +393,8 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
return true;
}
public boolean forcedAddToContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException {
public boolean forcedAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
@ -351,7 +410,8 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext) throws ContextException, ResourceRegistryException {
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
getElement();
RemoveConstraint removeConstraint = RemoveConstraint.keep;
@ -564,11 +624,13 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
boolean added = forcedAddToContext(targetSecurityContext);
orientGraph.commit();
logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid, contextUUID);
logger.info("{} with UUID {} successfully added to Context with UUID {}", accessType.getName(), uuid,
contextUUID);
return added;
} catch(Exception e) {
logger.error("Unable to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID, e);
logger.error("Unable to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid,
contextUUID, e);
if(orientGraph != null) {
orientGraph.rollback();
}

View File

@ -10,9 +10,8 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.ER;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -75,8 +74,8 @@ public class HeaderUtility {
public static Header getHeader(JsonNode jsonNode, boolean creation)
throws JsonParseException, JsonMappingException, IOException {
if(jsonNode.has(Resource.HEADER_PROPERTY)) {
JsonNode headerNode = jsonNode.get(Resource.HEADER_PROPERTY);
if(jsonNode.has(ER.HEADER_PROPERTY)) {
JsonNode headerNode = jsonNode.get(ER.HEADER_PROPERTY);
if(headerNode.isNull()) {
return null;
}
@ -117,27 +116,27 @@ public class HeaderUtility {
public static Header addHeader(Element element, UUID uuid) {
Header header = createHeader(uuid);
element.setProperty(Entity.HEADER_PROPERTY, header);
element.setProperty(ER.HEADER_PROPERTY, header);
return header;
}
public static Header addHeader(Edge edge, UUID uuid) {
Header header = createHeader(uuid);
edge.setProperty(Entity.HEADER_PROPERTY, header);
edge.setProperty(ER.HEADER_PROPERTY, header);
return header;
}
public static Header getHeader(Element element) throws ResourceRegistryException {
return Utility.getEmbedded(Header.class, element, Entity.HEADER_PROPERTY);
return Utility.getEmbedded(Header.class, element, ER.HEADER_PROPERTY);
}
public static void updateModifiedByAndLastUpdate(Element element) throws ResourceRegistryException {
ODocument oDocument = element.getProperty(Entity.HEADER_PROPERTY);
ODocument oDocument = element.getProperty(ER.HEADER_PROPERTY);
String modifiedBy = getUser();
oDocument.field(Header.MODIFIED_BY_PROPERTY, modifiedBy);
Date lastUpdateTime = Calendar.getInstance().getTime();
oDocument.field(Header.LAST_UPDATE_TIME_PROPERTY, lastUpdateTime);
element.setProperty(Entity.HEADER_PROPERTY, oDocument);
element.setProperty(ER.HEADER_PROPERTY, oDocument);
}
}

View File

@ -0,0 +1,40 @@
package org.gcube.informationsystem.resourceregistry.utils;
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class PropagationConstraintOrient extends ODocument implements org.gcube.informationsystem.model.embedded.PropagationConstraint {
public PropagationConstraintOrient() {
super(PropagationConstraint.NAME);
}
protected PropagationConstraintOrient(String iClassName) {
super(iClassName);
}
@Override
public RemoveConstraint getRemoveConstraint() {
return RemoveConstraint.valueOf((String) this.field(PropagationConstraint.REMOVE_PROPERTY));
}
@Override
public void setRemoveConstraint(RemoveConstraint removeConstraint) {
this.field(PropagationConstraint.REMOVE_PROPERTY, removeConstraint.name());
}
@Override
public AddConstraint getAddConstraint() {
return AddConstraint.valueOf((String) this.field(PropagationConstraint.ADD_PROPERTY));
}
@Override
public void setAddConstraint(AddConstraint addConstraint) {
this.field(PropagationConstraint.ADD_PROPERTY, addConstraint.name());
}
}