Refactored EntityManagement
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@141258 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
963afbe0fb
commit
4718080044
|
@ -97,13 +97,14 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
}
|
||||
|
||||
public void setVertex(Vertex vertex) throws ResourceRegistryException {
|
||||
if(vertex==null){
|
||||
throw new ResourceRegistryException("Trying to set null Vertex in " + this);
|
||||
if (vertex == null) {
|
||||
throw new ResourceRegistryException("Trying to set null Vertex in "
|
||||
+ this);
|
||||
}
|
||||
this.vertex = vertex;
|
||||
this.uuid = HeaderUtility.getHeader(vertex).getUUID();
|
||||
}
|
||||
|
||||
|
||||
public void setUUID(UUID uuid) throws ResourceRegistryException {
|
||||
this.uuid = uuid;
|
||||
if (jsonNode != null) {
|
||||
|
@ -202,20 +203,21 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
}
|
||||
}
|
||||
|
||||
public Vertex getVertex() throws EntityNotFoundException, ResourceRegistryException {
|
||||
public Vertex getVertex() throws EntityNotFoundException,
|
||||
ResourceRegistryException {
|
||||
try {
|
||||
if(vertex == null){
|
||||
if (vertex == null) {
|
||||
vertex = Utility.getElementByUUID(orientGraph,
|
||||
entityType == null ? baseType : entityType, uuid,
|
||||
Vertex.class);
|
||||
entityType == null ? baseType : entityType, uuid,
|
||||
Vertex.class);
|
||||
}
|
||||
return vertex;
|
||||
} catch (ResourceRegistryException e) {
|
||||
if(Resource.class.isAssignableFrom(entityClass)){
|
||||
if (Resource.class.isAssignableFrom(entityClass)) {
|
||||
throw new ResourceNotFoundException(e);
|
||||
}else if(Facet.class.isAssignableFrom(entityClass)){
|
||||
} else if (Facet.class.isAssignableFrom(entityClass)) {
|
||||
throw new FacetNotFoundException(e);
|
||||
}else {
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
@ -249,7 +251,7 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
}
|
||||
|
||||
this.vertex = vertexEntity;
|
||||
|
||||
|
||||
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
|
||||
if (entityHeader != null) {
|
||||
vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader);
|
||||
|
@ -284,8 +286,9 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
}
|
||||
|
||||
public abstract String serialize() throws ResourceRegistryException;
|
||||
|
||||
public abstract JSONObject serializeAsJson() throws ResourceRegistryException;
|
||||
|
||||
public abstract JSONObject serializeAsJson()
|
||||
throws ResourceRegistryException;
|
||||
|
||||
public abstract Vertex reallyCreate() throws EntityAlreadyPresentException,
|
||||
ResourceRegistryException;
|
||||
|
@ -296,34 +299,36 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
public abstract boolean reallyDelete() throws EntityNotFoundException,
|
||||
ResourceRegistryException;
|
||||
|
||||
|
||||
public boolean reallyAddToContext() throws ContextException, ResourceRegistryException {
|
||||
public boolean reallyAddToContext() throws ContextException,
|
||||
ResourceRegistryException {
|
||||
getVertex();
|
||||
|
||||
ContextUtility.addToActualContext(orientGraph, vertex);
|
||||
|
||||
Iterable<Edge> edges = vertex.getEdges(Direction.OUT, ConsistsOf.NAME);
|
||||
|
||||
|
||||
/*
|
||||
* Use this when the add integrity directive are inserted
|
||||
* Iterable<Edge> edges = vertex.getEdges(Direction.OUT);
|
||||
*
|
||||
* Use this when the add integrity directive are inserted Iterable<Edge>
|
||||
* edges = vertex.getEdges(Direction.OUT);
|
||||
*/
|
||||
for (Edge edge : edges) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge);
|
||||
RelationManagement relationManagement = RelationManagement
|
||||
.getRelationManagement(orientGraph, edge);
|
||||
relationManagement.reallyAddToContext();
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException {
|
||||
public boolean reallyRemoveFromContext() throws ContextException,
|
||||
ResourceRegistryException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static EntityManagement getEntityManagement(OrientGraph orientGraph, Vertex vertex) throws ResourceRegistryException{
|
||||
public static EntityManagement getEntityManagement(OrientGraph orientGraph,
|
||||
Vertex vertex) throws ResourceRegistryException {
|
||||
OrientVertexType orientVertexType = ((OrientVertex) vertex).getType();
|
||||
EntityManagement entityManagement = null;
|
||||
if (orientVertexType.isSubClassOf(Resource.NAME)) {
|
||||
|
@ -333,23 +338,20 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
} else {
|
||||
String error = String.format("{%s is not a %s nor a %s. "
|
||||
+ "This is really strange ad should not occur. "
|
||||
+ "Please Investigate it.", vertex,
|
||||
Resource.NAME, Facet.NAME);
|
||||
+ "Please Investigate it.", vertex, Resource.NAME,
|
||||
Facet.NAME);
|
||||
throw new ResourceRegistryException(error);
|
||||
}
|
||||
entityManagement.setVertex(vertex);
|
||||
return entityManagement;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String create() throws EntityAlreadyPresentException,
|
||||
ResourceRegistryException {
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
reallyCreate();
|
||||
|
||||
|
@ -377,18 +379,16 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
public String read() throws EntityNotFoundException,
|
||||
ResourceRegistryException {
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.READER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.READER);
|
||||
|
||||
getVertex();
|
||||
|
||||
return serialize();
|
||||
} catch (ResourceRegistryException fnfe) {
|
||||
throw fnfe;
|
||||
} catch (ResourceRegistryException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ResourceRegistryException(e.getMessage());
|
||||
throw new ResourceRegistryException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
|
@ -399,10 +399,8 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
public String update() throws EntityNotFoundException,
|
||||
ResourceRegistryException {
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
reallyUpdate();
|
||||
|
||||
|
@ -421,8 +419,7 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw new ResourceRegistryException("Error Updating " + baseType,
|
||||
e.getCause());
|
||||
throw new ResourceRegistryException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
|
@ -436,10 +433,8 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
logger.debug("Going to delete {} with UUID {}", baseType, uuid);
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
boolean deleted = reallyDelete();
|
||||
|
||||
|
@ -450,13 +445,12 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
|
||||
return deleted;
|
||||
|
||||
} catch (FacetNotFoundException fnfe) {
|
||||
logger.error("Unable to delete {} with UUID {}", baseType, uuid,
|
||||
fnfe);
|
||||
} catch (ResourceRegistryException e) {
|
||||
logger.error("Unable to delete {} with UUID {}", baseType, uuid, e);
|
||||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw fnfe;
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("Unable to delete {} with UUID {}", baseType, uuid, e);
|
||||
if (orientGraph != null) {
|
||||
|
@ -475,11 +469,9 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
baseType, uuid);
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
}
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
|
||||
boolean added = reallyAddToContext();
|
||||
|
||||
|
@ -489,13 +481,12 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
|
||||
return added;
|
||||
} catch (Exception e) {
|
||||
logger.error(
|
||||
"Unable to add {} with UUID {} to actual Context",
|
||||
logger.error("Unable to add {} with UUID {} to actual Context",
|
||||
baseType, uuid, e);
|
||||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw new ContextException(e.getMessage());
|
||||
throw new ContextException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
|
@ -508,16 +499,15 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
baseType, uuid);
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
}
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
|
||||
boolean removed = reallyRemoveFromContext();
|
||||
|
||||
orientGraph.commit();
|
||||
logger.info("{} with UUID {} successfully removed from actual Context",
|
||||
logger.info(
|
||||
"{} with UUID {} successfully removed from actual Context",
|
||||
baseType, uuid);
|
||||
|
||||
return removed;
|
||||
|
@ -528,12 +518,12 @@ public abstract class EntityManagement<E extends Entity> {
|
|||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw new ContextException(e.getMessage());
|
||||
throw new ContextException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.Iterator;
|
|||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
import org.gcube.informationsystem.model.embedded.Header;
|
||||
import org.gcube.informationsystem.model.embedded.RelationProperty;
|
||||
|
@ -220,11 +221,24 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
}
|
||||
|
||||
public String serialize() throws ResourceRegistryException {
|
||||
return Utility.toJsonString((OrientEdge) getEdge(), false);
|
||||
return serializeAsJson().toString();
|
||||
}
|
||||
|
||||
public JSONObject serializeAsJson() throws ResourceRegistryException {
|
||||
return Utility.toJsonObject((OrientEdge) getEdge(), false);
|
||||
JSONObject ret = Utility.toJsonObject((OrientEdge) getEdge(), false);
|
||||
|
||||
Vertex vertex = edge.getVertex(Direction.IN);
|
||||
EntityManagement entityManagement = EntityManagement
|
||||
.getEntityManagement(orientGraph, vertex);
|
||||
|
||||
try {
|
||||
ret.put(Relation.TARGET_PROPERTY,
|
||||
entityManagement.serializeAsJson());
|
||||
} catch (JSONException e) {
|
||||
new ResourceRegistryException(e);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Edge reallyCreate(UUID sourceUUID, UUID targetUUID)
|
||||
|
@ -233,16 +247,9 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
srmSource.setUUID(sourceUUID);
|
||||
Vertex source = srmSource.getVertex();
|
||||
|
||||
Vertex target = null;
|
||||
if (ConsistsOf.class.isAssignableFrom(relationClass)) {
|
||||
FacetManagement fmTarget = new FacetManagement(orientGraph);
|
||||
fmTarget.setUUID(targetUUID);
|
||||
target = fmTarget.getVertex();
|
||||
} else if (IsRelatedTo.class.isAssignableFrom(relationClass)) {
|
||||
ResourceManagement srmTarget = new ResourceManagement(orientGraph);
|
||||
srmTarget.setUUID(targetUUID);
|
||||
target = srmTarget.getVertex();
|
||||
}
|
||||
EntityManagement entityManagement = getEntityManagement();
|
||||
entityManagement.setUUID(targetUUID);
|
||||
Vertex target = entityManagement.getVertex();
|
||||
|
||||
return reallyCreate(source, target);
|
||||
|
||||
|
@ -441,10 +448,8 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
public String create(UUID sourceUUID, UUID targetUUID)
|
||||
throws ResourceRegistryException {
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
edge = reallyCreate(sourceUUID, targetUUID);
|
||||
|
||||
|
@ -472,10 +477,9 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
public String read() throws RelationNotFoundException,
|
||||
ResourceRegistryException {
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
return serialize();
|
||||
|
||||
|
@ -499,10 +503,9 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
public String update() throws RelationNotFoundException,
|
||||
ResourceRegistryException {
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
edge = reallyUpdate();
|
||||
|
||||
|
@ -533,10 +536,8 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
baseType, uuid, targetEntityClass.getSimpleName());
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
}
|
||||
orientGraph = ContextUtility
|
||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||
|
||||
boolean deleted = reallyDelete();
|
||||
|
||||
|
@ -547,12 +548,12 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
|
||||
return deleted;
|
||||
|
||||
} catch (ResourceRegistryException rre) {
|
||||
} catch (ResourceRegistryException e) {
|
||||
logger.error("Unable to remove {} with UUID.", baseType, uuid);
|
||||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw rre;
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("Unable to remove {} with UUID {}.", baseType, uuid);
|
||||
if (orientGraph != null) {
|
||||
|
@ -572,11 +573,9 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
baseType, uuid);
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
}
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
|
||||
boolean added = reallyAddToContext();
|
||||
|
||||
|
@ -591,7 +590,7 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw new ContextException(e.getMessage());
|
||||
throw new ContextException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
|
@ -604,11 +603,9 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
baseType, uuid);
|
||||
|
||||
try {
|
||||
if (orientGraph == null) {
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
}
|
||||
orientGraph = SecurityContextMapper.getSecurityContextFactory(
|
||||
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
|
||||
PermissionMode.WRITER).getTx();
|
||||
|
||||
boolean removed = reallyRemoveFromContext();
|
||||
|
||||
|
@ -625,7 +622,7 @@ public abstract class RelationManagement<R extends Relation> {
|
|||
if (orientGraph != null) {
|
||||
orientGraph.rollback();
|
||||
}
|
||||
throw new ContextException(e.getMessage());
|
||||
throw new ContextException(e);
|
||||
} finally {
|
||||
if (orientGraph != null) {
|
||||
orientGraph.shutdown();
|
||||
|
|
Loading…
Reference in New Issue