Using AccessType instead of class and its name which is much more clear and the code is cleaner

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@146435 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-03-29 14:53:37 +00:00
parent 5e7bb046c3
commit 290ce6ac90
10 changed files with 124 additions and 115 deletions

View File

@ -89,17 +89,19 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
protected final Set<String> ignoreKeys;
protected final Set<String> ignoreStartWithKeys;
protected OClass oClass;
protected Class<ERType> erTypeClass;
protected String baseType;
protected Class<El> elementClass;
protected AccessType accessType;
//protected Class<ERType> erTypeClass;
//protected String baseType;
protected OrientGraph orientGraph;
protected UUID uuid;
protected JsonNode jsonNode;
protected OClass oClass;
protected String erType;
protected Class<El> elementClass;
protected El element;
@SuppressWarnings("rawtypes")
@ -188,8 +190,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
}
protected ERManagement(Class<ERType> erClass) {
this.erTypeClass = erClass;
protected ERManagement(AccessType accessType) {
this.accessType = accessType;
this.ignoreKeys = new HashSet<String>();
@ -200,8 +202,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
}
protected ERManagement(Class<ERType> erClass, OrientGraph orientGraph) {
this(erClass);
protected ERManagement(AccessType accessType, OrientGraph orientGraph) {
this(accessType);
this.orientGraph = orientGraph;
}
@ -237,9 +239,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
String type = orientElement.getRecord().getClassName();
oClass = oSchema.getClass(type);
}else{
oClass = SchemaManagementImpl.getTypeSchema(erType, baseType);
oClass = SchemaManagementImpl.getTypeSchema(erType, accessType);
}
}
return oClass;
}
@ -247,22 +248,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public void setElementType(String erType) throws ResourceRegistryException {
this.erType = erType;
if (erType == null || erType.compareTo("") == 0) {
if (Facet.class.isAssignableFrom(erTypeClass)) {
this.erType = Facet.NAME;
erType = accessType.getName();
}
if (Resource.class.isAssignableFrom(erTypeClass)) {
this.erType = Resource.NAME;
}
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
this.erType = ConsistsOf.NAME;
}
if (IsRelatedTo.class.isAssignableFrom(erTypeClass)) {
this.erType = IsRelatedTo.NAME;
}
throw new ResourceRegistryException("Invalid type " + erType
+ " provided");
}
if (jsonNode != null) {
checkERMatch();
}
@ -358,25 +345,39 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
protected void throwElementNotFoundException(ERNotFoundException e)
throws ERNotFoundException {
if (Resource.class.isAssignableFrom(erTypeClass)) {
switch (accessType) {
case RESOURCE:
throw new ResourceNotFoundException(e);
} else if (Facet.class.isAssignableFrom(erTypeClass)) {
case FACET:
throw new FacetNotFoundException(e);
} else if (Relation.class.isAssignableFrom(erTypeClass)) {
case RELATION:
throw new RelationNotFoundException(e);
default:
break;
}
throw e;
}
protected void throwERAvailableInAnotherContextException(ERNotFoundException e)
throws ERAvailableInAnotherContextException {
if (Resource.class.isAssignableFrom(erTypeClass)) {
switch (accessType) {
case RESOURCE:
throw new ResourceAvailableInAnotherContextException(e);
} else if (Facet.class.isAssignableFrom(erTypeClass)) {
case FACET:
throw new FacetAvailableInAnotherContextException(e);
} else if (Relation.class.isAssignableFrom(erTypeClass)) {
case RELATION:
throw new RelationAvailableInAnotherContextException(e);
default:
break;
}
throw new ERAvailableInAnotherContextException(e);
@ -386,15 +387,16 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try {
if (element == null) {
element = Utility.getElementByUUID(orientGraph,
erType == null ? baseType : erType, uuid, elementClass);
erType == null ? accessType.getName() : erType, uuid, elementClass);
}
} catch (ERNotFoundException e) {
try{
Utility.getElementByUUIDAsAdmin(erType == null ? baseType : erType, uuid, elementClass);
Utility.getElementByUUIDAsAdmin(erType == null ? accessType.getName() : erType, uuid, elementClass);
throwERAvailableInAnotherContextException(e);
}catch (ERNotFoundException e1) {
// Using e not e1
throwElementNotFoundException(e);
// TODO Check if another ER has the same UUID
} catch (ResourceRegistryException e1) {
throw e1;
} catch (Exception e1) {
@ -502,7 +504,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public boolean delete() throws ERNotFoundException,
ResourceRegistryException {
logger.debug("Going to delete {} with UUID {}", baseType, uuid);
logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid);
try {
orientGraph = ContextUtility
@ -512,19 +514,19 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
orientGraph.commit();
logger.info("{} with UUID {} was successfully deleted.", baseType,
logger.info("{} with UUID {} was successfully deleted.", accessType.getName(),
uuid);
return deleted;
} catch (ResourceRegistryException e) {
logger.error("Unable to delete {} with UUID {}", baseType, uuid, e);
logger.error("Unable to delete {} with UUID {}", accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}
throw e;
} catch (Exception e) {
logger.error("Unable to delete {} with UUID {}", baseType, uuid, e);
logger.error("Unable to delete {} with UUID {}", accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}
@ -538,7 +540,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public boolean addToContext() throws ERNotFoundException, ContextException {
logger.debug("Going to add {} with UUID {} to actual Context",
baseType, uuid);
accessType.getName(), uuid);
try {
orientGraph = SecurityContextMapper.getSecurityContextFactory(
@ -549,12 +551,12 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
orientGraph.commit();
logger.info("{} with UUID {} successfully added to actual Context",
baseType, uuid);
accessType.getName(), uuid);
return added;
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to actual Context",
baseType, uuid, e);
accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}
@ -568,7 +570,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public boolean removeFromContext() throws ERNotFoundException, ContextException {
logger.debug("Going to remove {} with UUID {} from actual Context",
baseType, uuid);
accessType.getName(), uuid);
try {
orientGraph = SecurityContextMapper.getSecurityContextFactory(
@ -580,13 +582,13 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
orientGraph.commit();
logger.info(
"{} with UUID {} successfully removed from actual Context",
baseType, uuid);
accessType.getName(), uuid);
return removed;
} catch (Exception e) {
logger.error(
"Unable to remove {} with UUID {} from actual Context",
baseType, uuid, e);
accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}

View File

@ -6,8 +6,8 @@ package org.gcube.informationsystem.resourceregistry.er;
import java.util.HashSet;
import java.util.Set;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.ISManageable;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
@ -50,7 +50,7 @@ public class EmbeddedMangement {
String type = ERManagement.getClassProperty(jsonNode);
try {
SchemaManagementImpl.getTypeSchema(type, Embedded.NAME);
SchemaManagementImpl.getTypeSchema(type, AccessType.EMBEDDED);
} catch (SchemaNotFoundException e) {
throw e;
}

View File

@ -5,6 +5,7 @@ package org.gcube.informationsystem.resourceregistry.er.entity;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.entity.Facet;
@ -37,8 +38,8 @@ public abstract class EntityManagement<E extends Entity> extends
private static Logger logger = LoggerFactory
.getLogger(EntityManagement.class);
protected EntityManagement(Class<E> entityClass) {
super(entityClass);
protected EntityManagement(AccessType accessType) {
super(accessType);
this.ignoreKeys.add(Entity.HEADER_PROPERTY);
@ -51,17 +52,10 @@ public abstract class EntityManagement<E extends Entity> extends
this.ignoreStartWithKeys.add(OrientVertex.CONNECTION_OUT_PREFIX
.toUpperCase());
if (Facet.class.isAssignableFrom(entityClass)) {
this.baseType = Facet.NAME;
} else if (Resource.class.isAssignableFrom(entityClass)) {
this.baseType = Resource.NAME;
} else {
this.baseType = Entity.NAME;
}
}
protected EntityManagement(Class<E> entityClass, OrientGraph orientGraph) {
this(entityClass);
protected EntityManagement(AccessType accessType, OrientGraph orientGraph) {
this(accessType);
this.orientGraph = orientGraph;
}
@ -69,13 +63,13 @@ public abstract class EntityManagement<E extends Entity> extends
ResourceRegistryException {
logger.trace("Going to create {} for {} ({}) using {}",
Vertex.class.getSimpleName(), baseType, erType, jsonNode);
Vertex.class.getSimpleName(), accessType.getName(), erType, jsonNode);
try {
if(oClass.isAbstract()){
String error = String.format("Trying to create an instance of %s of type %s which is abstract. The operation will be aborted.",
baseType, erType);
accessType.getName(), erType);
throw new ResourceRegistryException(error);
}
@ -106,7 +100,8 @@ public abstract class EntityManagement<E extends Entity> extends
entityHeader = HeaderUtility.addHeader(element, null);
}
if (Resource.class.isAssignableFrom(erTypeClass)) {
if (accessType.compareTo(AccessType.RESOURCE)==0) {
// Facet and relation are created in calling method
} else {
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys,
@ -125,7 +120,7 @@ public abstract class EntityManagement<E extends Entity> extends
throw e;
} catch (Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}",
Vertex.class.getSimpleName(), baseType, erType, jsonNode, e);
Vertex.class.getSimpleName(), accessType.getName(), erType, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + erType
+ " with " + jsonNode, e.getCause());
}

View File

@ -4,6 +4,7 @@
package org.gcube.informationsystem.resourceregistry.er.entity;
import org.codehaus.jettison.json.JSONObject;
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;
@ -21,11 +22,11 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex;
public class FacetManagement extends EntityManagement<Facet> {
public FacetManagement() {
super(Facet.class);
super(AccessType.FACET);
}
public FacetManagement(OrientGraph orientGraph) {
super(Facet.class, orientGraph);
super(AccessType.FACET, orientGraph);
}
@Override

View File

@ -39,11 +39,11 @@ public class ResourceManagement extends EntityManagement<Resource> {
.getLogger(ResourceManagement.class);
public ResourceManagement() {
super(Resource.class);
super(AccessType.RESOURCE);
}
public ResourceManagement(OrientGraph orientGraph) {
super(Resource.class, orientGraph);
super(AccessType.RESOURCE, orientGraph);
}
@Override

View File

@ -3,6 +3,7 @@
*/
package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -14,11 +15,11 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public class ConsistsOfManagement extends RelationManagement<ConsistsOf> {
public ConsistsOfManagement() {
super(ConsistsOf.class);
super(AccessType.CONSISTS_OF);
}
public ConsistsOfManagement(OrientGraph orientGraph) {
super(ConsistsOf.class, orientGraph);
super(AccessType.CONSISTS_OF, orientGraph);
}
}

View File

@ -3,6 +3,7 @@
*/
package org.gcube.informationsystem.resourceregistry.er.relation;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -14,11 +15,11 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public class IsRelatedToManagement extends RelationManagement<IsRelatedTo> {
public IsRelatedToManagement() {
super(IsRelatedTo.class);
super(AccessType.IS_RELATED_TO);
}
public IsRelatedToManagement(OrientGraph orientGraph) {
super(IsRelatedTo.class, orientGraph);
super(AccessType.IS_RELATED_TO, orientGraph);
}
}

View File

@ -13,6 +13,7 @@ import java.util.UUID;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
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;
@ -59,8 +60,8 @@ public abstract class RelationManagement<R extends Relation> extends
protected final Class<? extends Entity> targetEntityClass;
protected RelationManagement(Class<R> relationClass) {
super(relationClass);
protected RelationManagement(AccessType accessType) {
super(accessType);
this.ignoreKeys.add(Relation.HEADER_PROPERTY);
this.ignoreKeys.add(Relation.TARGET_PROPERTY);
@ -70,20 +71,24 @@ public abstract class RelationManagement<R extends Relation> extends
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_IN.toUpperCase());
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
if (ConsistsOf.class.isAssignableFrom(relationClass)) {
this.baseType = ConsistsOf.NAME;
switch (accessType) {
case CONSISTS_OF:
this.targetEntityClass = Facet.class;
} else if (IsRelatedTo.class.isAssignableFrom(relationClass)) {
this.baseType = IsRelatedTo.NAME;
break;
case IS_RELATED_TO:
this.targetEntityClass = Resource.class;
} else {
this.baseType = Relation.NAME;
break;
default:
this.targetEntityClass = Resource.class;
}
break;
}
protected RelationManagement(Class<R> relationClass, OrientGraph orientGraph) {
this(relationClass);
}
protected RelationManagement(AccessType accessType, OrientGraph orientGraph) {
this(accessType);
this.orientGraph = orientGraph;
}
@ -188,7 +193,7 @@ public abstract class RelationManagement<R extends Relation> extends
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 ", baseType,
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Resource.NAME, Relation.TARGET_PROPERTY, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
@ -197,7 +202,7 @@ public abstract class RelationManagement<R extends Relation> extends
} 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 ", baseType,
+ "Cannot instatiate %s beetween %s -> %s ", accessType.getName(),
Facet.NAME, Relation.TARGET_PROPERTY, erType,
sourceEntityManagement.serialize(),
targetEntityManagement.serialize());
@ -264,7 +269,7 @@ public abstract class RelationManagement<R extends Relation> extends
ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys,
ignoreStartWithKeys);
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
if (accessType.compareTo(AccessType.CONSISTS_OF)==0) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
if (target != null) {
FacetManagement fm = new FacetManagement(orientGraph);
@ -435,17 +440,22 @@ public abstract class RelationManagement<R extends Relation> extends
protected EntityManagement getEntityManagement()
throws ResourceRegistryException {
EntityManagement entityManagement;
if (ConsistsOf.class.isAssignableFrom(erTypeClass)) {
switch (accessType) {
case CONSISTS_OF:
entityManagement = new FacetManagement(orientGraph);
} else if (IsRelatedTo.class.isAssignableFrom(erTypeClass)) {
break;
case IS_RELATED_TO:
entityManagement = new ResourceManagement(orientGraph);
} else {
break;
default:
String error = String.format("{%s is not a %s nor a %s. "
+ "This is really strange ad should not occur. "
+ "Please Investigate it.", erTypeClass, ConsistsOf.NAME,
+ "Please Investigate it.", accessType.getName(), ConsistsOf.NAME,
IsRelatedTo.NAME);
throw new ResourceRegistryException(error);
}
return entityManagement;
}
@ -487,7 +497,7 @@ public abstract class RelationManagement<R extends Relation> extends
ResourceRegistryException {
logger.debug(
"Going to remove {} with UUID {}. Related {}s will be detached.",
baseType, uuid, targetEntityClass.getSimpleName());
accessType.getName(), uuid, targetEntityClass.getSimpleName());
getElement();
@ -641,7 +651,7 @@ public abstract class RelationManagement<R extends Relation> extends
@Override
public boolean addToContext() throws ContextException {
logger.debug("Going to add {} with UUID {} to actual Context",
baseType, uuid);
accessType.getName(), uuid);
try {
orientGraph = SecurityContextMapper.getSecurityContextFactory(
@ -652,12 +662,12 @@ public abstract class RelationManagement<R extends Relation> extends
orientGraph.commit();
logger.info("{} with UUID {} successfully added to actual Context",
baseType, uuid);
accessType.getName(), uuid);
return added;
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to actual Context",
baseType, uuid, e);
accessType.getName(), uuid, e);
if (orientGraph != null) {
orientGraph.rollback();
}

View File

@ -56,22 +56,22 @@ public class SchemaManagementImpl implements SchemaManagement {
}
public static OClass getTypeSchema(OrientBaseGraph orientBaseGraph,
String type, String baseType) throws SchemaException {
String type, AccessType accessType) throws SchemaException {
OMetadata oMetadata = orientBaseGraph.getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema();
return getTypeSchema(oSchema, type, baseType);
return getTypeSchema(oSchema, type, accessType);
}
public static OClass getTypeSchema(OSchema oSchema, String type,
String baseType) throws SchemaException {
AccessType accessType) throws SchemaException {
try {
OClass oClass = oSchema.getClass(type);
if (oClass == null) {
throw new SchemaNotFoundException(type + " was not registered");
}
if (baseType != null && type.compareTo(baseType) != 0) {
if (!oClass.isSubClassOf(baseType)) {
throw new SchemaException(type + " is not a " + baseType);
if(accessType!=null && type.compareTo(accessType.getName())!= 0) {
if (!oClass.isSubClassOf(accessType.getName())) {
throw new SchemaException(type + " is not a " + accessType.getName());
}
}
return oClass;
@ -82,7 +82,7 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
public static OClass getTypeSchema(String type, String baseType)
public static OClass getTypeSchema(String type, AccessType accessType)
throws SchemaException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(
@ -92,11 +92,11 @@ public class SchemaManagementImpl implements SchemaManagement {
OrientGraphNoTx orientGraphNoTx = null;
try {
logger.debug("Getting {} Type {} schema",
baseType != null ? baseType : "", type);
accessType != null ? accessType.getName() : "", type);
orientGraphNoTx = orientGraphFactory.getNoTx();
return getTypeSchema(orientGraphNoTx, type, baseType);
return getTypeSchema(orientGraphNoTx, type, accessType);
} finally {
if (orientGraphNoTx != null) {

View File

@ -53,7 +53,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.
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.relation.RelationAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.SmartgearResourcesTest;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;