Reorganizing code

This commit is contained in:
Luca Frosini 2021-03-04 11:45:27 +01:00
parent 35be405f8e
commit dc481fbba6
6 changed files with 59 additions and 50 deletions

View File

@ -127,8 +127,11 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
this.operation = null;
}
protected void setAsEntryPoint(Operation operation) {
protected void setAsEntryPoint() {
this.entryPoint = true;
}
public void setOperation(Operation operation) {
this.operation = operation;
}
@ -383,6 +386,8 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
public El internalCreate() throws AlreadyPresentException, ResourceRegistryException {
try {
setOperation(Operation.CREATE);
reallyCreate();
Header entityHeader = HeaderUtility.getHeader(jsonNode, true);
@ -411,6 +416,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
public El internalUpdate() throws NotFoundException, ResourceRegistryException {
try {
setOperation(Operation.UPDATE);
reallyUpdate();
@ -439,7 +445,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
protected abstract boolean reallyDelete() throws NotFoundException, ResourceRegistryException;
public boolean internalDelete() throws NotFoundException, ResourceRegistryException {
// Added for consistency with create and update addToContext removeFromContext.
setOperation(Operation.DELETE);
return reallyDelete();
}
@ -509,10 +515,11 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException;
public String all(boolean polymorphic) throws ResourceRegistryException {
setAsEntryPoint(Operation.QUERY);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
setAsEntryPoint();
setOperation(Operation.QUERY);
return reallyGetAll(polymorphic);
} catch(ResourceRegistryException e) {
throw e;
@ -530,10 +537,11 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
setAsEntryPoint(Operation.EXISTS);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
setAsEntryPoint();
setOperation(Operation.EXISTS);
getElement();
@ -563,12 +571,12 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
oDatabaseDocument.begin();
boolean update = false;
try {
setAsEntryPoint(Operation.UPDATE);
setAsEntryPoint();
getElement();
update = true;
internalUpdate();
} catch(NotFoundException e) {
setAsEntryPoint(Operation.CREATE);
setAsEntryPoint();
String calledMethod = CalledMethodProvider.instance.get();
calledMethod = calledMethod.replace("update", "create");
CalledMethodProvider.instance.set(calledMethod);
@ -609,11 +617,12 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
public String create() throws AlreadyPresentException, ResourceRegistryException {
setAsEntryPoint(Operation.CREATE);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
internalCreate();
@ -647,11 +656,14 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
setAsEntryPoint(Operation.READ);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
setAsEntryPoint();
setOperation(Operation.READ);
getElement();
return serializeAsJsonNode().toString();
@ -673,12 +685,13 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
public String update() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
setAsEntryPoint(Operation.UPDATE);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
internalUpdate();
oDatabaseDocument.commit();
@ -713,14 +726,14 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
public boolean delete() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
setAsEntryPoint(Operation.DELETE);
logger.debug("Going to delete {} with UUID {}", accessType.getName(), uuid);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
oDatabaseDocument = ContextUtility.getAdminSecurityContext().getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
boolean deleted = reallyDelete();
boolean deleted = internalDelete();
if(deleted) {
oDatabaseDocument.commit();
@ -757,13 +770,15 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
public Set<String> getContextsSet() throws NotFoundException, ContextException, ResourceRegistryException {
setAsEntryPoint(Operation.GET_METADATA);
logger.debug("Going to get contexts for {} with UUID", typeName, uuid);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
setAsEntryPoint();
setOperation(Operation.GET_METADATA);
Set<String> contexts = SecurityContext.getContexts(getElement());
return contexts;
} catch(ResourceRegistryException e) {

View File

@ -187,16 +187,15 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
return element;
}
@SuppressWarnings("rawtypes")
/*
* It works perfectly in case of any kind of update. In case of use from create
* the cache does not work by using the ID because until commit the edge has a
* fake id starting with - (minus) sign. This not imply any collateral effect
* but a better solution is a desiderata.
*/
protected RelationManagement getRelationManagement(OEdge edge) throws ResourceRegistryException {
protected RelationManagement<?,?> getRelationManagement(OEdge edge) throws ResourceRegistryException {
String id = edge.getIdentity().toString();
RelationManagement relationManagement = relationManagements.get(id);
RelationManagement<?,?> relationManagement = relationManagements.get(id);
if(relationManagement == null) {
relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(), oDatabaseDocument, edge);
relationManagements.put(id, relationManagement);
@ -332,6 +331,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
public Map<UUID,JsonNode> internalAddToContext()
throws ContextException, ResourceRegistryException {
try {
setOperation(Operation.ADD_TO_CONTEXT);
Map<UUID,JsonNode> affectedInstances = reallyAddToContext();
if(!dryRunContextSharing) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
@ -348,13 +348,13 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
}
public Map<UUID,JsonNode> addToContext(UUID contextUUID) throws SchemaViolationException, NotFoundException, ContextException, ResourceRegistryException {
setAsEntryPoint(Operation.ADD_TO_CONTEXT);
logger.info("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
workingContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = workingContext.getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
@ -393,6 +393,7 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
public Map<UUID,JsonNode> internalRemoveFromContext()
throws ContextException, ResourceRegistryException {
try {
setOperation(Operation.REMOVE_FROM_CONTEXT);
Map<UUID,JsonNode> affectedInstances = reallyRemoveFromContext();
if(!dryRunContextSharing) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
@ -440,13 +441,14 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
public Map<UUID,JsonNode> removeFromContext(UUID contextUUID)
throws SchemaViolationException, NotFoundException, ContextException, ResourceRegistryException {
setAsEntryPoint(Operation.REMOVE_FROM_CONTEXT);
logger.debug("Going to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
workingContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = workingContext.getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
@ -796,9 +798,14 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
public String query(String relationType, String referenceType, UUID referenceUUID, ODirection direction,
boolean polymorphic, Map<String,String> constraint, boolean includeRelationInResult) throws ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
setAsEntryPoint(Operation.QUERY);
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
workingContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = workingContext.getDatabaseDocument(PermissionMode.READER);
setAsEntryPoint();
setOperation(Operation.QUERY);
TypesCache typesCache = TypesCache.getInstance();
AccessType relationAccessType = typesCache.getCachedType(relationType).getAccessType();
@ -861,6 +868,9 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
}

View File

@ -105,6 +105,7 @@ public class FacetManagement extends EntityManagement<Facet, FacetType> {
return;
}
resourceManagement.setOperation(operation);
resourceManagement.sanityCheck();
}catch (Exception e) {

View File

@ -267,10 +267,11 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
relationManagement.setHonourPropagationConstraintsInContextSharing(honourPropagationConstraintsInContextSharing);
relationManagement.setTargetSecurityContext(targetSecurityContext);
Map<UUID,JsonNode> resourceCharacterisationInstances = relationManagement.internalAddToContext();
affectedInstances.putAll(resourceCharacterisationInstances);
if(relationManagement instanceof ConsistsOfManagement) {
facetCounter = facetCounter + resourceCharacterisationInstances.size();
}
affectedInstances.putAll(resourceCharacterisationInstances);
relationManagement.sanityCheck();
}
if(facetCounter == 0) {
@ -390,11 +391,6 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
*/
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
/*
cleanCachedSerialization();
JsonNode resourceInstance = createCompleteJsonNode();
*/
TypesCache typesCache = TypesCache.getInstance();
Set<LinkedEntity> consistsOfFacetConstraints = getResourceTypeConstraint();
@ -403,8 +399,6 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
Iterable<OEdge> edges = getElement().getEdges(ODirection.OUT);
//ArrayNode consistsOfArrayNode = (ArrayNode) resourceInstance.get(Resource.CONSISTS_OF_PROPERTY);
//for(JsonNode consistsOfJsonNode : consistsOfArrayNode) {
for(OEdge edge : edges) {
RelationManagement<?,?> relationManagement = getRelationManagement(edge);
if(!(relationManagement instanceof ConsistsOfManagement)) {
@ -413,10 +407,6 @@ public class ResourceManagement extends EntityManagement<Resource, ResourceType>
String consistsOfType = relationManagement.getTypeName();
String facetType = relationManagement.getTargetEntityManagement().getTypeName();
// String consistsOfType = consistsOfJsonNode.get(Element.CLASS_PROPERTY).asText();
// JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY);
// String facetType = facetJsonNode.get(Element.CLASS_PROPERTY).asText();
for(LinkedEntity constraint : consistsOfFacetConstraints) {
if(constraintSatisfied(typesCache, constraint, consistsOfType, facetType)) {
Integer integer = satisfiedConsistsOfFacet.get(constraint);

View File

@ -491,6 +491,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
public Map<UUID,JsonNode> internalAddToContext()
throws ContextException, ResourceRegistryException {
try {
operation = Operation.ADD_TO_CONTEXT;
Map<UUID,JsonNode> affectedInstances = reallyAddToContext();
if(propagationConstraint.getAddConstraint()==PropagationConstraint.AddConstraint.propagate) {
if(!dryRunContextSharing) {
@ -512,6 +513,8 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
throws ContextException, ResourceRegistryException {
getElement();
setOperation(Operation.ADD_TO_CONTEXT);
Map<UUID,JsonNode> affectedInstances = new HashMap<>();
/* Adding source to Context */
@ -537,12 +540,12 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
}
public Map<UUID,JsonNode> addToContext(UUID contextUUID) throws NotFoundException, ContextException {
setAsEntryPoint(Operation.ADD_TO_CONTEXT);
logger.debug("Going to add {} with UUID {} to Context with UUID {}", accessType.getName(), uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
workingContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = workingContext.getDatabaseDocument(PermissionMode.WRITER);
setAsEntryPoint();
targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
@ -663,6 +666,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
public Map<UUID,JsonNode> internalRemoveFromContext()
throws ContextException, ResourceRegistryException {
try {
setOperation(Operation.REMOVE_FROM_CONTEXT);
Map<UUID,JsonNode> affectedInstances = reallyRemoveFromContext();
if(!dryRunContextSharing) {
HeaderUtility.updateModifiedByAndLastUpdate(element);
@ -680,13 +684,13 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
public Map<UUID,JsonNode> removeFromContext(UUID contextUUID)
throws NotFoundException, ContextException, ResourceRegistryException {
setAsEntryPoint(Operation.REMOVE_FROM_CONTEXT);
logger.debug("Going to remove {} with UUID {} from Context with UUID {}", typeName, uuid, contextUUID);
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
workingContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = workingContext.getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
setAsEntryPoint();
targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);

View File

@ -150,22 +150,9 @@ public class BasicTest extends MultiContextTest {
Assert.assertTrue(deleted);
}
@Test(expected = ResourceRegistryException.class)
public void testResourceWithOnlyOneFacet() throws Exception {
EService eService = new EServiceImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("InformationSystem");
softwareFacet.setName("resource-registry");
softwareFacet.setVersion("1.1.0");
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setAddConstraint(AddConstraint.unpropagate);
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
IsIdentifiedBy<EService, Facet> isIdentifiedBy = new IsIdentifiedByImpl<EService, Facet>(
eService, softwareFacet, propagationConstraint);
eService.addFacet(isIdentifiedBy);
@Test
public void testResource() throws Exception {
EService eService = ERManagementTest.instantiateValidEService();
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(EService.NAME);
@ -180,7 +167,9 @@ public class BasicTest extends MultiContextTest {
try {
addToContextThenTestIfBehaveProperly(eService, true, ALTERNATIVE_TEST_SCOPE);
}finally {
} catch (Exception e) {
throw e;
} finally {
UUID eServiceUUID = eService.getHeader().getUUID();
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);