Rivisiting element serilization

This commit is contained in:
Luca Frosini 2021-02-16 18:43:47 +01:00
parent fe37358ea7
commit 845ddde2c8
12 changed files with 114 additions and 107 deletions

View File

@ -134,7 +134,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
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<Context> {
}
@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<OEdge> parents = getElement().getEdges(ODirection.IN);
@ -195,7 +190,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
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<Context> {
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. {}",

View File

@ -55,26 +55,26 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeAsJson(false, true);
}
public JsonNode serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelfOnly();
JsonNode relation = serializeSelf();
try {
OVertex source = element.getVertex(ODirection.OUT);
ContextManagement sourceContextManagement = new ContextManagement(oDatabaseDocument);
sourceContextManagement.setElement(source);
if(includeSource) {
((ObjectNode)relation).replace(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly());
((ObjectNode)relation).replace(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelf());
}
OVertex target = element.getVertex(ODirection.IN);
ContextManagement targetContextManagement = new ContextManagement(oDatabaseDocument);
targetContextManagement.setElement(target);
if(includeTarget) {
((ObjectNode)relation).replace(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly());
((ObjectNode)relation).replace(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelf());
}
} catch(ResourceRegistryException e) {

View File

@ -90,6 +90,10 @@ public abstract class ElementManagement<El extends OElement> {
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<El extends OElement> {
}
}
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<String> 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<String> 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<El extends OElement> {
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<El extends OElement> {
}
}
}
public boolean exists() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
@ -466,7 +516,7 @@ public abstract class ElementManagement<El extends OElement> {
// 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<El extends OElement> {
// 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<El extends OElement> {
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<El extends OElement> {
// 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<El extends OElement> {
if(current!=null) {
current.activateOnCurrentThread();
}
}
}
@ -678,6 +729,8 @@ public abstract class ElementManagement<El extends OElement> {
public ArrayNode getContextsAsArrayNode(ObjectMapper objectMapper) throws NotFoundException, ContextException, ResourceRegistryException {
try {
Set<String> contexts = getContextsSet();
ArrayNode arrayNode = objectMapper.createArrayNode();
for(String contextUUID : contexts) {
arrayNode.add(contextUUID);
@ -730,6 +783,8 @@ public abstract class ElementManagement<El extends OElement> {
return value.asBoolean();
case NULL:
break;
case NUMBER:
@ -775,7 +830,9 @@ public abstract class ElementManagement<El extends OElement> {
}
Iterator<Entry<String,JsonNode>> fields = jsonNode.fields();
OUTER_WHILE: while(fields.hasNext()) {
Entry<String,JsonNode> entry = fields.next();
@ -933,7 +990,9 @@ public abstract class ElementManagement<El extends OElement> {
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<El extends OElement> {
return superClasses;
}
public JsonNode toJsonNode() throws ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
OElement element = getElement();
Set<String> 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<String> 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);

View File

@ -86,22 +86,22 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeAsJson(true, true);
}
public JsonNode serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelfOnly();
JsonNode relation = serializeSelf().deepCopy();
try {
if(includeSource) {
EntityElementManagement<? extends EntityElement> sourceEntityManagement = getSourceEntityManagement();
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelf());
}
if(includeTarget) {
EntityElementManagement<? extends EntityElement> 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<SEM extends EntityElementManagem
}
}
logger.trace("Creating {} beetween {} -> {}", 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();

View File

@ -327,7 +327,7 @@ public abstract class EntityManagement<E extends Entity> 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<E extends Entity> 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<E extends Entity> 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<E extends Entity> 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<E extends Entity> 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. {}",

View File

@ -38,14 +38,10 @@ public class FacetManagement extends EntityManagement<Facet> {
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

View File

@ -69,9 +69,9 @@ public class ResourceManagement extends EntityManagement<Resource> {
}
@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

View File

@ -201,22 +201,22 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeAsJson(true, true);
}
public JsonNode serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelfOnly();
JsonNode relation = serializeSelf();
try {
if(includeSource) {
EntityManagement<? extends Resource> sourceEntityManagement = getSourceEntityManagement();
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelf());
}
if(includeTarget) {
EntityManagement<? extends Entity> 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<T extends EntityManagement<? extends En
resourceManagement = (ResourceManagement) ElementManagementUtility.getEntityManagement(getWorkingContext(),
oDatabaseDocument, source);
if(this instanceof IsRelatedToManagement) {
sourceResource = resourceManagement.serializeAsJson();
sourceResource = resourceManagement.createCompleteJsonNode();
} else if(this instanceof ConsistsOfManagement) {
sourceResource = resourceManagement.serializeSelfOnly();
sourceResource = resourceManagement.serializeSelf();
} else {
String error = String.format("{%s is not a %s nor a %s. %s", this,
IsRelatedToManagement.class.getSimpleName(), ConsistsOfManagement.class.getSimpleName(),
@ -256,9 +256,9 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
}
if(this instanceof IsRelatedToManagement) {
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource, serializeAsJson());
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource, createCompleteJsonNode());
} else if(this instanceof ConsistsOfManagement) {
sourceResource = ResourceManagement.addConsistsOf(sourceResource, serializeAsJson());
sourceResource = ResourceManagement.addConsistsOf(sourceResource, createCompleteJsonNode());
} else {
String error = String.format("{%s is not a %s nor a %s. %s", this,
IsRelatedToManagement.class.getSimpleName(), ConsistsOfManagement.class.getSimpleName(),
@ -443,7 +443,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
affectedInstances.put(uuid, serializeSelfOnly());
affectedInstances.put(uuid, serializeSelf());
}
return affectedInstances;
} catch(ResourceRegistryException e) {
@ -475,7 +475,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
if(!dryRunContextSharing) {
targetSecurityContext.addElement(getElement(), oDatabaseDocument);
}
affectedInstances.put(uuid, serializeSelfOnly());
affectedInstances.put(uuid, serializeSelf());
return affectedInstances;
}
@ -545,7 +545,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
if(!dryRunContextSharing) {
targetSecurityContext.removeElement(getElement(), oDatabaseDocument);
}
affectedInstances.put(uuid, serializeSelfOnly());
affectedInstances.put(uuid, serializeSelf());
T targetEntityManagement = getTargetEntityManagement();
targetEntityManagement.setDryRunContextSharing(dryRunContextSharing);
@ -604,7 +604,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
affectedInstances.put(uuid, serializeSelfOnly());
affectedInstances.put(uuid, serializeSelf());
return affectedInstances;
} catch(ResourceRegistryException e) {
throw e;

View File

@ -78,7 +78,7 @@ public class QueryImpl implements Query {
@SuppressWarnings("rawtypes")
ElementManagement erManagement = ElementManagementUtility.getERManagement(securityContext, oDatabaseDocument,
element);
jsonNode = erManagement.serializeAsJson();
jsonNode = erManagement.createCompleteJsonNode();
}
arrayNode.add(jsonNode);

View File

@ -66,14 +66,8 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> 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

View File

@ -73,13 +73,8 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
}
@Override
public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
return serializeSelfOnly();
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelf().deepCopy();
}
@Override

View File

@ -97,8 +97,8 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
targetEntityManagement.setJsonNode(jsonNode.get(Relation.TARGET_PROPERTY));
}
logger.trace("Creating {} beetween {} -> {}", 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();