From 845ddde2c87f6210e391732140a88b174ddbd996 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 16 Feb 2021 18:43:47 +0100 Subject: [PATCH] Rivisiting element serilization --- .../contexts/entities/ContextManagement.java | 15 +-- .../relations/IsParentOfManagement.java | 8 +- .../instances/base/ElementManagement.java | 113 +++++++++++------- .../relations/RelationElementManagement.java | 12 +- .../model/entities/EntityManagement.java | 12 +- .../model/entities/FacetManagement.java | 8 +- .../model/entities/ResourceManagement.java | 4 +- .../model/relations/RelationManagement.java | 24 ++-- .../resourceregistry/query/QueryImpl.java | 2 +- .../EntityTypeDefinitionManagement.java | 10 +- .../PropertyTypeDefinitionManagement.java | 9 +- .../RelationTypeDefinitionManagement.java | 4 +- 12 files changed, 114 insertions(+), 107 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java index a5056fa..616c86d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java @@ -134,7 +134,7 @@ public class ContextManagement extends EntityElementManagement { message.append("A context with name ("); message.append(getName()); message.append(") has been already created as child of "); - message.append(parentContext.serializeSelfOnly().toString()); + message.append(parentContext.serializeSelf().toString()); logger.trace("Checking if {} -> {}", message, select); @@ -161,14 +161,9 @@ public class ContextManagement extends EntityElementManagement { } @Override - public String serialize() throws ResourceRegistryException { - return serializeAsJson().toString(); - } - - @Override - public JsonNode serializeAsJson() throws ResourceRegistryException { + public JsonNode createCompleteJsonNode() throws ResourceRegistryException { - JsonNode context = serializeSelfOnly(); + JsonNode context = serializeSelf().deepCopy(); int count = 0; Iterable parents = getElement().getEdges(ODirection.IN); @@ -195,7 +190,7 @@ public class ContextManagement extends EntityElementManagement { IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument); isParentOfManagement.setElement(edge); try { - JsonNode isParentOf = isParentOfManagement.serializeAsJson(); + JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode(); context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY); } catch(ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); @@ -410,7 +405,7 @@ public class ContextManagement extends EntityElementManagement { ContextManagement contextManagement = new ContextManagement(); contextManagement.setElement((OVertex) vertex); try { - JsonNode jsonObject = contextManagement.serializeAsJson(); + JsonNode jsonObject = contextManagement.createCompleteJsonNode(); arrayNode.add(jsonObject); } catch(ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java index c211485..c84ef87 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/relations/IsParentOfManagement.java @@ -55,26 +55,26 @@ public class IsParentOfManagement extends RelationElementManagement { protected OClass oClass; protected String elementType; + protected JsonNode self; + protected JsonNode complete; + + protected El element; protected boolean reload; @@ -258,19 +262,61 @@ public abstract class ElementManagement { } } - public JsonNode serializeSelfOnly() throws ResourceRegistryException { + protected JsonNode createSelfJsonNode() throws ResourceRegistryException { try { - return toJsonNode(); + + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode objectNode = objectMapper.createObjectNode(); + OElement element = getElement(); + Set keys = element.getPropertyNames(); + for(String key : keys) { + Object object = element.getProperty(key); + JsonNode jsonNode = getPropertyForJson(key, object); + if(jsonNode != null) { + objectNode.replace(key, jsonNode); + } + } + + OClass oClass = getOClass(); + String type = oClass.getName(); + objectNode.put(Element.CLASS_PROPERTY, type); + + Collection superClasses = getSuperclasses(); + ArrayNode arrayNode = objectMapper.valueToTree(superClasses); + + objectNode.replace(Element.SUPERCLASSES_PROPERTY, arrayNode); + + return objectNode; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException("Error while serializing " + getElement().toString(), e); + } + } + + public JsonNode serializeSelf() throws ResourceRegistryException { + try { + if(self==null) { + self = createSelfJsonNode(); + } + return self; } catch(Exception e) { throw new ResourceRegistryException(e); } } - public String serialize() throws ResourceRegistryException { - return serializeAsJson().toString(); - } + public abstract JsonNode createCompleteJsonNode() throws ResourceRegistryException; - public abstract JsonNode serializeAsJson() throws ResourceRegistryException; + public JsonNode serializeComplete() throws ResourceRegistryException { + try { + if(complete==null) { + complete = createCompleteJsonNode(); + } + return complete; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } protected abstract El reallyCreate() throws AlreadyPresentException, ResourceRegistryException; @@ -369,6 +415,8 @@ public abstract class ElementManagement { if(uuid == null) { throw new NotFoundException("null UUID does not allow to retrieve the Element"); } + + return Utility.getElementByUUID(oDatabaseDocument, elementType == null ? accessType.getName() : elementType, uuid, elementClass); } catch(NotFoundException e) { @@ -414,7 +462,9 @@ public abstract class ElementManagement { } } } + + public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); try { @@ -466,7 +516,7 @@ public abstract class ElementManagement { // TODO Notify to subscriptionNotification - return serialize(); + return serializeComplete().toString(); } catch(ResourceRegistryException e) { logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid); @@ -503,7 +553,7 @@ public abstract class ElementManagement { // TODO Notify to subscriptionNotification - return serialize(); + return serializeComplete().toString(); } catch(ResourceRegistryException e) { logger.error("Unable to create {}", accessType.getName()); @@ -536,8 +586,7 @@ public abstract class ElementManagement { getElement(); - jsonNode = serializeAsJson(); - return jsonNode.toString(); + return serializeComplete().toString(); } catch(ResourceRegistryException e) { logger.error("Unable to read {} with UUID {}", accessType.getName(), uuid); throw e; @@ -569,7 +618,7 @@ public abstract class ElementManagement { // TODO Notify to subscriptionNotification - return serialize(); + return serializeComplete().toString(); } catch(ResourceRegistryException e) { logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid); @@ -632,6 +681,8 @@ public abstract class ElementManagement { if(current!=null) { current.activateOnCurrentThread(); + + } } } @@ -678,6 +729,8 @@ public abstract class ElementManagement { public ArrayNode getContextsAsArrayNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException { try { Set contexts = getContextsSet(); + + ArrayNode arrayNode = objectMapper.createArrayNode(); for(String contextUUID : contexts) { arrayNode.add(contextUUID); @@ -730,6 +783,8 @@ public abstract class ElementManagement { return value.asBoolean(); case NULL: + + break; case NUMBER: @@ -775,7 +830,9 @@ public abstract class ElementManagement { } Iterator> fields = jsonNode.fields(); + + OUTER_WHILE: while(fields.hasNext()) { Entry entry = fields.next(); @@ -933,7 +990,9 @@ public abstract class ElementManagement { if(object instanceof Collection) { Collection collection = (Collection) object; ArrayNode arrayNode = objectMapper.createArrayNode(); + + for(Object o : collection) { Object obj = getPropertyForJson("PLACEHOLDER", o); @@ -975,38 +1034,6 @@ public abstract class ElementManagement { return superClasses; } - public JsonNode toJsonNode() throws ResourceRegistryException { - try { - - ObjectMapper objectMapper = new ObjectMapper(); - ObjectNode objectNode = objectMapper.createObjectNode(); - OElement element = getElement(); - Set keys = element.getPropertyNames(); - for(String key : keys) { - Object object = element.getProperty(key); - JsonNode jsonNode = getPropertyForJson(key, object); - if(jsonNode != null) { - objectNode.replace(key, jsonNode); - } - } - - OClass oClass = getOClass(); - String type = oClass.getName(); - objectNode.put(Element.CLASS_PROPERTY, type); - - Collection superClasses = getSuperclasses(); - ArrayNode arrayNode = objectMapper.valueToTree(superClasses); - - objectNode.replace(Element.SUPERCLASSES_PROPERTY, arrayNode); - - return objectNode; - } catch(ResourceRegistryException e) { - throw e; - } catch(Exception e) { - throw new ResourceRegistryException("Error while serializing " + getElement().toString(), e); - } - } - public boolean isAvailableOnContext(OElement element, SecurityContext securityContext) { try { return securityContext.isElementInContext(element); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java index 7ed6cd2..6fbc4cb 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/base/relations/RelationElementManagement.java @@ -86,22 +86,22 @@ public abstract class RelationElementManagement sourceEntityManagement = getSourceEntityManagement(); - ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly()); + ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelf()); } if(includeTarget) { EntityElementManagement targetEntityManagement = getTargetEntityManagement(); - ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson()); + ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeComplete()); } } catch(ResourceRegistryException e) { @@ -159,8 +159,8 @@ public abstract class RelationElementManagement {}", elementType, getSourceEntityManagement().serialize(), - getTargetEntityManagement().serialize()); + logger.trace("Creating {} beetween {} -> {}", elementType, getSourceEntityManagement().serializeSelf().toString(), + getTargetEntityManagement().serializeSelf().toString()); OVertex source = (OVertex) getSourceEntityManagement().getElement(); OVertex target = (OVertex) getTargetEntityManagement().getElement(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java index 194253d..f27cd5e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/EntityManagement.java @@ -327,7 +327,7 @@ public abstract class EntityManagement extends EntityElementMa HeaderUtility.updateModifiedByAndLastUpdate(element); element.save(); } - affectedInstances.put(uuid, serializeSelfOnly()); + affectedInstances.put(uuid, serializeSelf()); return affectedInstances; } catch(ResourceRegistryException e) { throw e; @@ -384,7 +384,7 @@ public abstract class EntityManagement extends EntityElementMa HeaderUtility.updateModifiedByAndLastUpdate(element); element.save(); } - affectedInstances.put(uuid, serializeSelfOnly()); + affectedInstances.put(uuid, serializeSelf()); return affectedInstances; } catch(ResourceRegistryException e) { throw e; @@ -473,7 +473,7 @@ public abstract class EntityManagement extends EntityElementMa EntityManagement entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), oDatabaseDocument, (OVertex) vertex); try { - JsonNode jsonNode = entityManagement.serializeAsJson(); + JsonNode jsonNode = entityManagement.serializeComplete(); arrayNode.add(jsonNode); } catch(ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", @@ -647,9 +647,9 @@ public abstract class EntityManagement extends EntityElementMa @SuppressWarnings("rawtypes") RelationManagement relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(), oDatabaseDocument, edge); - jsonNode = relationManagement.serializeAsJson(); + jsonNode = relationManagement.createCompleteJsonNode(); }else { - jsonNode = entityManagement.serializeAsJson(); + jsonNode = entityManagement.serializeComplete(); } arrayNode.add(jsonNode); @@ -760,7 +760,7 @@ public abstract class EntityManagement extends EntityElementMa continue; } } - JsonNode jsonNode = entityManagement.serializeAsJson(); + JsonNode jsonNode = entityManagement.serializeComplete(); arrayNode.add(jsonNode); } catch(ResourceRegistryException e) { logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java index e3799e7..4d9c05c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/FacetManagement.java @@ -38,14 +38,10 @@ public class FacetManagement extends EntityManagement { return new FacetAlreadyPresentException(message); } - @Override - public String serialize() throws ResourceRegistryException { - return serializeSelfOnly().toString(); - } @Override - public JsonNode serializeAsJson() throws ResourceRegistryException { - return serializeSelfOnly(); + public JsonNode createCompleteJsonNode() throws ResourceRegistryException { + return serializeSelf().deepCopy(); } @Override diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java index e42d06b..543bce4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/entities/ResourceManagement.java @@ -69,9 +69,9 @@ public class ResourceManagement extends EntityManagement { } @Override - public JsonNode serializeAsJson() throws ResourceRegistryException { + public JsonNode createCompleteJsonNode() throws ResourceRegistryException { - JsonNode sourceResource = serializeSelfOnly(); + JsonNode sourceResource = serializeSelf().deepCopy(); /* * Cannot get ConsistsOf edge only because is not polymorphic for a diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java index e6e97b2..fa26a7f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/instances/model/relations/RelationManagement.java @@ -201,22 +201,22 @@ public abstract class RelationManagement sourceEntityManagement = getSourceEntityManagement(); - ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly()); + ((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelf()); } if(includeTarget) { EntityManagement targetEntityManagement = getTargetEntityManagement(); - ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson()); + ((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeComplete()); } } catch(ResourceRegistryException e) { @@ -244,9 +244,9 @@ public abstract class RelationManagement exten } @Override - public String serialize() throws ResourceRegistryException { - return serializeAsJson().toString(); - } - - @Override - public JsonNode serializeAsJson() throws ResourceRegistryException { - return serializeSelfOnly(); - // For Resources Definition include mandatory and suggested IsRelatedTo and ConsistsOf + public JsonNode createCompleteJsonNode() throws ResourceRegistryException { + return serializeSelf().deepCopy(); } @Override diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java index 1b42f45..fac1947 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManagement.java @@ -73,13 +73,8 @@ public class PropertyTypeDefinitionManagement extends ElementManagement {}", elementType, getSourceEntityManagement().serialize(), - getTargetEntityManagement().serialize()); + logger.trace("Creating {} beetween {} -> {}", elementType, getSourceEntityManagement().serializeSelf().toString(), + getTargetEntityManagement().serializeSelf().toString()); OVertex source = (OVertex) getSourceEntityManagement().getElement(); OVertex target = (OVertex) getTargetEntityManagement().getElement();