Revisited serilization

This commit is contained in:
Luca Frosini 2021-02-17 11:29:43 +01:00
parent 845ddde2c8
commit 74be68d2b6
12 changed files with 61 additions and 59 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.serializeSelf().toString());
message.append(parentContext.getElement().toString());
logger.trace("Checking if {} -> {}", message, select);
@ -161,9 +161,9 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
JsonNode context = serializeSelf().deepCopy();
JsonNode context = serializeSelfAsJsonNode();
int count = 0;
Iterable<OEdge> parents = getElement().getEdges(ODirection.IN);
@ -174,7 +174,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
try {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
isParentOfManagement.setElement(edge);
JsonNode isParentOf = isParentOfManagement.serializeAsJson(true, false);
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
if(isParentOf!=null) {
((ObjectNode) context).replace(Context.PARENT_PROPERTY, isParentOf);
}
@ -190,7 +190,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
isParentOfManagement.setElement(edge);
try {
JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode();
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY);
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);

View File

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

View File

@ -262,7 +262,8 @@ public abstract class ElementManagement<El extends OElement> {
}
}
protected JsonNode createSelfJsonNode() throws ResourceRegistryException {
private JsonNode createSelfJsonNode() throws ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
@ -294,20 +295,20 @@ public abstract class ElementManagement<El extends OElement> {
}
}
public JsonNode serializeSelf() throws ResourceRegistryException {
public JsonNode serializeSelfAsJsonNode() throws ResourceRegistryException {
try {
if(self==null) {
self = createSelfJsonNode();
}
return self;
return self.deepCopy();
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
public abstract JsonNode createCompleteJsonNode() throws ResourceRegistryException;
protected abstract JsonNode createCompleteJsonNode() throws ResourceRegistryException;
public JsonNode serializeComplete() throws ResourceRegistryException {
public JsonNode serializeAsJsonNode() throws ResourceRegistryException {
try {
if(complete==null) {
complete = createCompleteJsonNode();
@ -516,7 +517,7 @@ public abstract class ElementManagement<El extends OElement> {
// TODO Notify to subscriptionNotification
return serializeComplete().toString();
return serializeAsJsonNode().toString();
} catch(ResourceRegistryException e) {
logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid);
@ -553,7 +554,7 @@ public abstract class ElementManagement<El extends OElement> {
// TODO Notify to subscriptionNotification
return serializeComplete().toString();
return serializeAsJsonNode().toString();
} catch(ResourceRegistryException e) {
logger.error("Unable to create {}", accessType.getName());
@ -586,7 +587,7 @@ public abstract class ElementManagement<El extends OElement> {
getElement();
return serializeComplete().toString();
return serializeAsJsonNode().toString();
} catch(ResourceRegistryException e) {
logger.error("Unable to read {} with UUID {}", accessType.getName(), uuid);
throw e;
@ -618,7 +619,7 @@ public abstract class ElementManagement<El extends OElement> {
// TODO Notify to subscriptionNotification
return serializeComplete().toString();
return serializeAsJsonNode().toString();
} catch(ResourceRegistryException e) {
logger.error("Unable to update {} with UUID {}", accessType.getName(), uuid);

View File

@ -86,22 +86,22 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
}
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeAsJson(true, true);
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return createCompleteJsonNode(true, true);
}
public JsonNode serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelf().deepCopy();
protected JsonNode createCompleteJsonNode(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JsonNode relation = serializeSelfAsJsonNode();
try {
if(includeSource) {
EntityElementManagement<? extends EntityElement> sourceEntityManagement = getSourceEntityManagement();
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelf());
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfAsJsonNode());
}
if(includeTarget) {
EntityElementManagement<? extends EntityElement> targetEntityManagement = getTargetEntityManagement();
((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeComplete());
((ObjectNode) relation).replace(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJsonNode());
}
} catch(ResourceRegistryException e) {
@ -159,12 +159,11 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
}
}
logger.trace("Creating {} beetween {} -> {}", elementType, getSourceEntityManagement().serializeSelf().toString(),
getTargetEntityManagement().serializeSelf().toString());
OVertex source = (OVertex) getSourceEntityManagement().getElement();
OVertex target = (OVertex) getTargetEntityManagement().getElement();
logger.trace("Going to create {} beetween {} -> {}", elementType, source.toString(), target.toString());
element = oDatabaseDocument.newEdge(source, target, elementType);
updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);

View File

@ -327,7 +327,7 @@ public abstract class EntityManagement<E extends Entity> extends EntityElementMa
HeaderUtility.updateModifiedByAndLastUpdate(element);
element.save();
}
affectedInstances.put(uuid, serializeSelf());
affectedInstances.put(uuid, serializeSelfAsJsonNode());
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, serializeSelf());
affectedInstances.put(uuid, serializeSelfAsJsonNode());
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.serializeComplete();
JsonNode jsonNode = entityManagement.serializeAsJsonNode();
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.createCompleteJsonNode();
jsonNode = relationManagement.serializeAsJsonNode();
}else {
jsonNode = entityManagement.serializeComplete();
jsonNode = entityManagement.serializeAsJsonNode();
}
arrayNode.add(jsonNode);
@ -760,7 +760,7 @@ public abstract class EntityManagement<E extends Entity> extends EntityElementMa
continue;
}
}
JsonNode jsonNode = entityManagement.serializeComplete();
JsonNode jsonNode = entityManagement.serializeAsJsonNode();
arrayNode.add(jsonNode);
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",

View File

@ -41,7 +41,7 @@ public class FacetManagement extends EntityManagement<Facet> {
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelf().deepCopy();
return serializeSelfAsJsonNode();
}
@Override

View File

@ -71,7 +71,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
JsonNode sourceResource = serializeSelf().deepCopy();
JsonNode sourceResource = serializeSelfAsJsonNode();
/*
* Cannot get ConsistsOf edge only because is not polymorphic for a
@ -101,7 +101,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
if(relationManagement instanceof ConsistsOfManagement) {
try {
JsonNode consistsOf = relationManagement.serializeAsJson(true, true);
JsonNode consistsOf = relationManagement.serializeAsJsonNode();
sourceResource = addConsistsOf(sourceResource, consistsOf);
} catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);

View File

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

View File

@ -66,8 +66,8 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
}
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelf().deepCopy();
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelfAsJsonNode();
}
@Override

View File

@ -73,8 +73,8 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
}
@Override
public JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelf().deepCopy();
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelfAsJsonNode();
}
@Override

View File

@ -97,12 +97,12 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
targetEntityManagement.setJsonNode(jsonNode.get(Relation.TARGET_PROPERTY));
}
logger.trace("Creating {} beetween {} -> {}", elementType, getSourceEntityManagement().serializeSelf().toString(),
getTargetEntityManagement().serializeSelf().toString());
OVertex source = (OVertex) getSourceEntityManagement().getElement();
OVertex target = (OVertex) getTargetEntityManagement().getElement();
logger.trace("Creating {} beetween {} -> {}", elementType, source.toString(),
target.toString());
element = oDatabaseDocument.newEdge(source, target, elementType);
updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);