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@158633 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-17 17:10:35 +00:00
parent 2d7805b132
commit 782e60ff13
4 changed files with 38 additions and 7 deletions

View File

@ -67,10 +67,27 @@ public abstract class EntityManagement<E extends Entity> extends
RelationManagement relationManagement = relationManagements.get(id); RelationManagement relationManagement = relationManagements.get(id);
if(relationManagement==null) { if(relationManagement==null) {
relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge); relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge);
relationManagements.put(id, relationManagement);
} }
return relationManagement; return relationManagement;
} }
protected void addToRelationManagement(@SuppressWarnings("rawtypes") RelationManagement relationManagement) throws ResourceRegistryException {
Element elem = relationManagement.getElement();
String id = elem.getId().toString();
if(relationManagements.get(id)!=relationManagement) {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Two different instance of ");
errorMessage.append(relationManagement.getClass().getSimpleName());
errorMessage.append(" point to the same ");
errorMessage.append(elem.getClass().getSimpleName());
errorMessage.append(". ");
errorMessage.append(Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(errorMessage.toString());
}
relationManagements.put(id, relationManagement);
}
protected EntityManagement(AccessType accessType, OrientGraph orientGraph) { protected EntityManagement(AccessType accessType, OrientGraph orientGraph) {
this(accessType); this(accessType);
this.orientGraph = orientGraph; this.orientGraph = orientGraph;

View File

@ -84,9 +84,19 @@ 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 = ERManagementUtility.getRelationManagement(orientGraph, edge); RelationManagement relationManagement = getRelationManagement(edge);
if(relationManagement.getSourceEntityManagement()==null) {
relationManagement.setSourceEntityManagement(this); relationManagement.setSourceEntityManagement(this);
}else if(relationManagement.getSourceEntityManagement()!=null) {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("SourceEntityManagement for ");
errorMessage.append(relationManagement.getClass().getSimpleName());
errorMessage.append(" is not the one expected. ");
errorMessage.append(Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(errorMessage.toString());
}
if (relationManagement instanceof ConsistsOfManagement) { if (relationManagement instanceof ConsistsOfManagement) {
try { try {
@ -106,8 +116,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
* by default as design choice and not because forget * by default as design choice and not because forget
* *
* else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ * else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){
* JSONObject isRelatedTo = relationManagement * JSONObject isRelatedTo = relationManagement.serializeAsJson(true, true);
.serializeAsJson();
* sourceResource = addIsRelatedTo(sourceResource, isRelatedTo); * sourceResource = addIsRelatedTo(sourceResource, isRelatedTo);
* } * }
*/ */
@ -140,6 +149,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
com.setJSON(consistOfJsonNode); com.setJSON(consistOfJsonNode);
com.setSourceEntityManagement(this); com.setSourceEntityManagement(this);
com.internalCreate(); com.internalCreate();
addToRelationManagement(com);
} }
} }
@ -152,6 +162,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
irtm.setJSON(relationJsonNode); irtm.setJSON(relationJsonNode);
irtm.setSourceEntityManagement(this); irtm.setSourceEntityManagement(this);
irtm.internalCreate(); irtm.internalCreate();
addToRelationManagement(irtm);
} }
} }
@ -170,6 +181,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph); ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
com.setJSON(relationJsonNode); com.setJSON(relationJsonNode);
com.internalCreateOrUdate(); com.internalCreateOrUdate();
addToRelationManagement(com);
} }
} }
@ -181,6 +193,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
orientGraph); orientGraph);
irtm.setJSON(relationJsonNode); irtm.setJSON(relationJsonNode);
irtm.internalUpdate(); irtm.internalUpdate();
addToRelationManagement(irtm);
} }
} }

View File

@ -3,7 +3,6 @@
*/ */
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;
@ -92,7 +91,7 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected S getSourceEntityManagement() throws ResourceRegistryException { public S getSourceEntityManagement() throws ResourceRegistryException {
if (sourceEntityManagement == null) { if (sourceEntityManagement == null) {
Vertex source = getElement().getVertex(Direction.OUT); Vertex source = getElement().getVertex(Direction.OUT);
sourceEntityManagement = newSourceEntityManagement(); sourceEntityManagement = newSourceEntityManagement();
@ -219,7 +218,7 @@ public abstract class RelationManagement<R extends Relation, S extends EntityMan
try { try {
targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY)); targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
} catch (SchemaException e) { } catch (SchemaException e) {
StringWriter errorMessage = new StringWriter(); StringBuilder errorMessage = new StringBuilder();
errorMessage.append("A "); errorMessage.append("A ");
errorMessage.append(erType); errorMessage.append(erType);
errorMessage.append(" can be only created beetween "); errorMessage.append(" can be only created beetween ");

View File

@ -41,6 +41,8 @@ public class Utility {
private static final Logger logger = LoggerFactory.getLogger(Utility.class); private static final Logger logger = LoggerFactory.getLogger(Utility.class);
public static final String SHOULD_NOT_OCCUR_ERROR_MESSAGE = "This is really strange and should not occur. Please contact the system administrator";
public static JSONObject toJsonObject(OrientElement element, boolean raw) public static JSONObject toJsonObject(OrientElement element, boolean raw)
throws ResourceRegistryException { throws ResourceRegistryException {
try { try {