Reorganizing code

This commit is contained in:
Luca Frosini 2021-03-05 10:46:59 +01:00
parent 5a94aadce5
commit 9770531b33
15 changed files with 155 additions and 59 deletions

View File

@ -111,12 +111,12 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
}
@Override
protected ContextNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected ContextNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new ContextNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected ContextAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected ContextAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new ContextAlreadyPresentException(message);
}

View File

@ -48,12 +48,12 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
}
@Override
protected IsParentOfNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected IsParentOfNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new IsParentOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected IsParentOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected IsParentOfAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new IsParentOfAlreadyPresentException(message);
}

View File

@ -94,14 +94,14 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
protected boolean reload;
/**
* An operation can affects multiple instances (e.g. addToContext)
* An operation can affects multiple instances (e.g. create, update)
* We need to know if the instance is the entry point of the operation
* or if it is just a subordinated operation.
* This is required for example in addTocontext to trigger sanity check
* in the Resource when the action is performed directly on the Facet,
* or the Resource sanity check is not required to be triggered
* because the entry point of the action already take care
* of triggering this action (e.g. the addToContext was invoked on the Resource and
* of triggering this action (e.g. the update was invoked on the Resource and
* each describing Facets must not trigger multiple time the sanityCheck).
*/
protected boolean entryPoint;
@ -463,9 +463,9 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
this.typeName = oClass.getName();
}
protected abstract NotFoundException getSpecificElementNotFoundException(NotFoundException e);
protected abstract NotFoundException getSpecificNotFoundException(NotFoundException e);
protected abstract AlreadyPresentException getSpecificERAlreadyPresentException(String message);
protected abstract AlreadyPresentException getSpecificAlreadyPresentException(String message);
public El getElement() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
if(element == null) {
@ -495,7 +495,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
return Utility.getElementByUUID(oDatabaseDocument, typeName == null ? accessType.getName() : typeName, uuid,
elementClass);
} catch(NotFoundException e) {
throw getSpecificElementNotFoundException(e);
throw getSpecificNotFoundException(e);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
@ -508,7 +508,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
return Utility.getElementByUUIDAsAdmin(typeName == null ? accessType.getName() : typeName, uuid,
elementClass);
} catch(NotFoundException e) {
throw getSpecificElementNotFoundException(e);
throw getSpecificNotFoundException(e);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
@ -1115,15 +1115,6 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
return superClasses;
}
*/
/*
public boolean isAvailableOnContext(SecurityContext securityContext) {
try {
return securityContext.isElementInContext(element);
} catch (ResourceRegistryException e) {
return false;
}
}
*/
protected String getNotNullErrorMessage(String fieldName) {
StringBuffer stringBuffer = new StringBuffer();

View File

@ -104,8 +104,7 @@ public class ElementManagementUtility {
}
}
@SuppressWarnings("rawtypes")
public static ElementManagement getERManagementFromUUID(SecurityContext workingContext, ODatabaseDocument orientGraph,
public static ElementManagement<?, ?> getERManagementFromUUID(SecurityContext workingContext, ODatabaseDocument orientGraph,
UUID uuid) throws ResourceRegistryException {
OElement element;
try {
@ -117,8 +116,7 @@ public class ElementManagementUtility {
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
public static EntityManagement getEntityManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument,
public static EntityManagement<?, ?> getEntityManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument,
OVertex vertex) throws ResourceRegistryException {
if(oDatabaseDocument == null) {
@ -141,7 +139,7 @@ public class ElementManagementUtility {
throw new ResourceRegistryException(error);
}
EntityManagement entityManagement = null;
EntityManagement<?, ?> entityManagement = null;
if(oClass.isSubClassOf(Resource.NAME)) {
entityManagement = new ResourceManagement();
} else if(oClass.isSubClassOf(Facet.NAME)) {
@ -157,8 +155,7 @@ public class ElementManagementUtility {
return entityManagement;
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static RelationManagement getRelationManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument,
public static RelationManagement<?,?> getRelationManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument,
OEdge edge) throws ResourceRegistryException {
if(oDatabaseDocument == null) {
@ -173,7 +170,7 @@ public class ElementManagementUtility {
OClass oClass = ElementManagementUtility.getOClass(edge);
RelationManagement relationManagement = null;
RelationManagement<?,?> relationManagement = null;
if(oClass.isSubClassOf(ConsistsOf.NAME)) {
relationManagement = new ConsistsOfManagement();
} else if(oClass.isSubClassOf(IsRelatedTo.NAME)) {

View File

@ -133,7 +133,7 @@ public abstract class EntityElementManagement<E extends EntityElement, ET extend
OVertex v = getElement();
if(v != null) {
String error = String.format("A %s with UUID %s already exist", typeName, uuid.toString());
throw getSpecificERAlreadyPresentException(error);
throw getSpecificAlreadyPresentException(error);
}
}

View File

@ -21,14 +21,32 @@ public interface ERManagement {
public void setHonourPropagationConstraintsInContextSharing(boolean honourPropagationConstraintsInContextSharing);
public Map<UUID,JsonNode> getAffectedInstances();
public void setAffectedInstances(Map<UUID,JsonNode> affectedInstances);
/**
* Set source security context to evaluate addToContext
* @param sourceSecurityContext the source security context
*/
public void setSourceSecurityContext(SecurityContext sourceSecurityContext);
public SecurityContext getSourceSecurityContext();
public Map<UUID, JsonNode> internalAddToContext()
throws ContextException, ResourceRegistryException;
public Map<UUID, JsonNode> addToContext(UUID contextUUID)
throws SchemaViolationException, NotFoundException, ContextException, ResourceRegistryException;
/**
* Set target security context of addToContext/removeFromContext
* @param targetSecurityContext
*/
public void setTargetSecurityContext(SecurityContext targetSecurityContext);
public SecurityContext getTargetSecurityContext();
public Map<UUID, JsonNode> internalRemoveFromContext()
throws ContextException, ResourceRegistryException;
@ -37,6 +55,6 @@ public interface ERManagement {
// public void sanityCheck() throws SchemaViolationException, ResourceRegistryException;
public AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message);
public AvailableInAnotherContextException getSpecificAvailableInAnotherContextException(String message);
}

View File

@ -59,6 +59,11 @@ import com.orientechnologies.orient.core.sql.executor.OResultSet;
*/
public abstract class EntityManagement<E extends Entity, ET extends EntityType> extends EntityElementManagement<E, ET> implements ERManagement {
/**
* The source context of an addToContex
*/
protected SecurityContext sourceSecurityContext;
/**
* The target context of an addToContex/RemoveFromContext
*/
@ -111,11 +116,41 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
*/
protected boolean honourPropagationConstraintsInContextSharing;
/**
* Instances affected by addToContext/removeFromContext
*/
protected Map<UUID,JsonNode> affectedInstances;
@Override
public Map<UUID,JsonNode> getAffectedInstances() {
return affectedInstances;
}
@Override
public void setAffectedInstances(Map<UUID,JsonNode> affectedInstances) {
this.affectedInstances = affectedInstances;
}
@Override
public void setSourceSecurityContext(SecurityContext sourceSecurityContext) {
this.sourceSecurityContext = sourceSecurityContext;
}
@Override
public SecurityContext getSourceSecurityContext() {
return sourceSecurityContext;
}
@Override
public void setTargetSecurityContext(SecurityContext targetSecurityContext) {
this.targetSecurityContext = targetSecurityContext;
}
@Override
public SecurityContext getTargetSecurityContext() {
return sourceSecurityContext;
}
public boolean isDryRunContextSharing() {
return dryRunContextSharing;
}
@ -136,8 +171,17 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
* Provide a cache edge-internal-id -> RelationManagement
* this avoid to recreate the relationManagement of already visited edges
*/
@SuppressWarnings("rawtypes")
protected Map<String,RelationManagement> relationManagements;
protected Map<String,RelationManagement<?, ?>> relationManagements;
/*
public boolean isAvailableOnContext(SecurityContext securityContext) {
try {
return securityContext.isElementInContext(element);
} catch (ResourceRegistryException e) {
return false;
}
}
*/
protected EntityManagement(AccessType accessType) {
super(accessType);
@ -171,7 +215,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
} catch(NotFoundException e) {
try {
retrieveElementFromAnyContext();
throw getSpecificERAvailableInAnotherContextException(typeName == null ? accessType.getName()
throw getSpecificAvailableInAnotherContextException(typeName == null ? accessType.getName()
: typeName + " with UUID " + uuid + " is available in another "
+ Context.class.getSimpleName());
} catch(AvailableInAnotherContextException e1) {
@ -258,7 +302,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
OVertex v = getElement();
if(v != null) {
String error = String.format("A %s with UUID %s already exist", typeName, uuid.toString());
throw getSpecificERAlreadyPresentException(error);
throw getSpecificAlreadyPresentException(error);
}
}
@ -267,7 +311,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
OElement el = ElementManagementUtility.getAnyElementByUUID(uuid);
String error = String.format("UUID %s is already used by another %s. This is not allowed.",
uuid.toString(), (el instanceof OVertex) ? Entity.NAME : Relation.NAME);
throw getSpecificERAvailableInAnotherContextException(error);
throw getSpecificAvailableInAnotherContextException(error);
} catch(NotFoundException e1) {
// OK the UUID is not already used.
@ -357,6 +401,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
oDatabaseDocument.begin();
setAsEntryPoint();
sourceSecurityContext = ContextUtility.getCurrentSecurityContext();
targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
Map<UUID,JsonNode> affectedInstances = internalAddToContext();
@ -543,7 +588,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
OElement element = ElementManagementUtility.getAnyElementByUUID(oDatabaseDocument, referenceUUID);
if(element instanceof OVertex) {
@SuppressWarnings("unchecked")
EntityManagement<Entity, EntityType> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(),
EntityManagement<Entity, EntityType> entityManagement = (EntityManagement<Entity, EntityType>) ElementManagementUtility.getEntityManagement(getWorkingContext(),
oDatabaseDocument, (OVertex) element);
String elementType = entityManagement.getTypeName();

View File

@ -34,17 +34,17 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
}
@Override
protected FacetNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected FacetNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new FacetNotFoundException(e.getMessage(), e.getCause());
}
@Override
public FacetAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
public FacetAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(String message) {
return new FacetAvailableInAnotherContextException(message);
}
@Override
protected FacetAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected FacetAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new FacetAlreadyPresentException(message);
}

View File

@ -51,18 +51,18 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
}
@Override
protected ResourceNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected ResourceNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new ResourceNotFoundException(e.getMessage(), e.getCause());
}
@Override
public ResourceAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
public ResourceAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
String message) {
return new ResourceAvailableInAnotherContextException(message);
}
@Override
protected ResourceAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected ResourceAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new ResourceAlreadyPresentException(message);
}

View File

@ -44,18 +44,18 @@ public class ConsistsOfManagement extends RelationManagement<FacetManagement, Fa
}
@Override
protected ConsistsOfNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected ConsistsOfNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new ConsistsOfNotFoundException(e.getMessage(), e.getCause());
}
@Override
public ConsistsOfAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
public ConsistsOfAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
String message) {
return new ConsistsOfAvailableInAnotherContextException(message);
}
@Override
protected ConsistsOfAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected ConsistsOfAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new ConsistsOfAlreadyPresentException(message);
}

View File

@ -33,18 +33,18 @@ public class IsRelatedToManagement extends RelationManagement<ResourceManagement
}
@Override
protected IsRelatedToNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected IsRelatedToNotFoundException getSpecificNotFoundException(NotFoundException e) {
return new IsRelatedToNotFoundException(e.getMessage(), e.getCause());
}
@Override
public IsRelatedToAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
public IsRelatedToAvailableInAnotherContextException getSpecificAvailableInAnotherContextException(
String message) {
return new IsRelatedToAvailableInAnotherContextException(message);
}
@Override
protected IsRelatedToAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected IsRelatedToAlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new IsRelatedToAlreadyPresentException(message);
}

View File

@ -58,6 +58,11 @@ import com.orientechnologies.orient.core.record.impl.ODocument;
public abstract class RelationManagement<T extends EntityManagement<? extends Entity, TET>, TET extends EntityType>
extends RelationElementManagement<ResourceManagement, T, ResourceType, TET> implements ERManagement {
/**
* The source context of an addToContex
*/
protected SecurityContext sourceSecurityContext;
/**
* The target context of an addToContex/RemoveFromContext
*/
@ -110,11 +115,41 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
*/
protected boolean honourPropagationConstraintsInContextSharing;
/**
* Instances affected by addToContext/removeFromContext
*/
protected Map<UUID,JsonNode> affectedInstances;
@Override
public Map<UUID,JsonNode> getAffectedInstances() {
return affectedInstances;
}
@Override
public void setAffectedInstances(Map<UUID,JsonNode> affectedInstances) {
this.affectedInstances = affectedInstances;
}
@Override
public void setSourceSecurityContext(SecurityContext sourceSecurityContext) {
this.sourceSecurityContext = sourceSecurityContext;
}
@Override
public SecurityContext getSourceSecurityContext() {
return sourceSecurityContext;
}
@Override
public void setTargetSecurityContext(SecurityContext targetSecurityContext) {
this.targetSecurityContext = targetSecurityContext;
}
@Override
public SecurityContext getTargetSecurityContext() {
return sourceSecurityContext;
}
public boolean isDryRunContextSharing() {
return dryRunContextSharing;
}
@ -133,6 +168,16 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
public final PropagationConstraint defaultPropagationConstraint;
/*
public boolean isAvailableOnContext(SecurityContext securityContext) {
try {
return securityContext.isElementInContext(element);
} catch (ResourceRegistryException e) {
return false;
}
}
*/
protected RelationManagement(AccessType accessType, Class<? extends Entity> targetEntityClass, PropagationConstraint defaultPropagationConstraint) {
super(accessType, Resource.class, targetEntityClass);
this.defaultPropagationConstraint = defaultPropagationConstraint;
@ -159,7 +204,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
} catch(NotFoundException e) {
try {
retrieveElementFromAnyContext();
throw getSpecificERAvailableInAnotherContextException(typeName == null ? accessType.getName()
throw getSpecificAvailableInAnotherContextException(typeName == null ? accessType.getName()
: typeName + " with UUID " + uuid + " is available in another "
+ Context.class.getSimpleName());
} catch(AvailableInAnotherContextException e1) {
@ -797,7 +842,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
continue;
}
RelationManagement<T, TET> relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(),
RelationManagement<T, TET> relationManagement = (RelationManagement<T, TET>) ElementManagementUtility.getRelationManagement(getWorkingContext(),
oDatabaseDocument, edge);
visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources);
}

View File

@ -144,7 +144,7 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
return element;
} catch (NotFoundException e) {
throw getSpecificElementNotFoundException(e);
throw getSpecificNotFoundException(e);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
@ -182,12 +182,12 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
}
@Override
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message);
}

View File

@ -151,7 +151,7 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
return element;
} catch(NotFoundException e) {
throw getSpecificElementNotFoundException(e);
throw getSpecificNotFoundException(e);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
@ -183,12 +183,12 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
}
@Override
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message);
}

View File

@ -180,7 +180,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
return element;
} catch (NotFoundException e) {
throw getSpecificElementNotFoundException(e);
throw getSpecificNotFoundException(e);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
@ -194,12 +194,12 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
}
@Override
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message);
}