Fixed issue on template property of QueryTemplate serialization

This commit is contained in:
Luca Frosini 2022-02-07 22:20:56 +01:00
parent 6bd1cc11d0
commit c6305c9570
2 changed files with 91 additions and 65 deletions

View File

@ -971,6 +971,82 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
return map;
}
public void setProperty(OProperty oProperty, String key, JsonNode value) throws Exception {
switch (oProperty.getType()) {
case EMBEDDED:
ODocument oDocument = PropertyElementManagement.getPropertyDocument(value);
element.setProperty(key, oDocument, OType.EMBEDDED);
break;
case EMBEDDEDLIST:
List<Object> list = new ArrayList<Object>();
Iterator<JsonNode> arrayElement = value.elements();
while(arrayElement.hasNext()) {
JsonNode elementOfArray = arrayElement.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(elementOfArray);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfArray);
}
list.add(object);
}
element.setProperty(key, list, OType.EMBEDDEDLIST);
break;
case EMBEDDEDSET:
Set<Object> set = new HashSet<Object>();
Iterator<JsonNode> setElement = value.elements();
while(setElement.hasNext()) {
JsonNode elementOfSet = setElement.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(elementOfSet);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfSet);
}
set.add(object);
}
element.setProperty(key, set, OType.EMBEDDEDSET);
break;
case EMBEDDEDMAP:
Map<String, Object> map = new HashMap<>();
Iterator<String> fieldNames = value.fieldNames();
while(fieldNames.hasNext()) {
String fieldKey = fieldNames.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(value.get(fieldKey));
}else {
object = PropertyElementManagement.getPropertyDocument(value.get(fieldKey));
}
map.put(fieldKey, object);
}
element.setProperty(key, map, OType.EMBEDDEDMAP);
break;
case STRING:
if(value.getNodeType() == JsonNodeType.OBJECT) {
element.setProperty(key, value.toString());
}else {
element.setProperty(key, getObjectFromJsonNode(value));
}
break;
default:
Object obj = getObjectFromJsonNode(value);
if(obj != null) {
element.setProperty(key, obj);
}
break;
}
}
public OElement updateProperties(OClass oClass, OElement element, JsonNode jsonNode, Set<String> ignoreKeys,
Set<String> ignoreStartWithKeys) throws ResourceRegistryException {
@ -1004,69 +1080,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
}
}
}else {
switch (oProperty.getType()) {
case EMBEDDED:
ODocument oDocument = PropertyElementManagement.getPropertyDocument(value);
element.setProperty(key, oDocument, OType.EMBEDDED);
break;
case EMBEDDEDLIST:
List<Object> list = new ArrayList<Object>();
Iterator<JsonNode> arrayElement = value.elements();
while(arrayElement.hasNext()) {
JsonNode elementOfArray = arrayElement.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(elementOfArray);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfArray);
}
list.add(object);
}
element.setProperty(key, list, OType.EMBEDDEDLIST);
break;
case EMBEDDEDSET:
Set<Object> set = new HashSet<Object>();
Iterator<JsonNode> setElement = value.elements();
while(setElement.hasNext()) {
JsonNode elementOfSet = setElement.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(elementOfSet);
}else {
object = PropertyElementManagement.getPropertyDocument(elementOfSet);
}
set.add(object);
}
element.setProperty(key, set, OType.EMBEDDEDSET);
break;
case EMBEDDEDMAP:
Map<String, Object> map = new HashMap<>();
Iterator<String> fieldNames = value.fieldNames();
while(fieldNames.hasNext()) {
String fieldKey = fieldNames.next();
Object object = null;
if(oProperty.getLinkedType()!=null) {
object = getObjectFromJsonNode(value.get(fieldKey));
}else {
object = PropertyElementManagement.getPropertyDocument(value.get(fieldKey));
}
map.put(fieldKey, object);
}
element.setProperty(key, map, OType.EMBEDDEDMAP);
break;
default:
Object object = getObjectFromJsonNode(value);
if(object != null) {
element.setProperty(key, object);
}
break;
}
setProperty(oProperty, key, value);
}
} catch(Exception e) {

View File

@ -6,6 +6,7 @@ import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
@ -100,8 +101,19 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
@Override
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
JsonNode queryTemplate = serializeSelfAsJsonNode();
return queryTemplate;
try {
JsonNode queryTemplate = serializeSelfAsJsonNode();
ObjectMapper objectMapper = new ObjectMapper();
String templateString = element.getProperty(QueryTemplate.TEMPLATE_PROPERTY);
JsonNode templateJsonNode = objectMapper.readTree(templateString);
((ObjectNode) queryTemplate).replace(QueryTemplate.TEMPLATE_PROPERTY, templateJsonNode);
return queryTemplate;
}catch (ResourceRegistryException e) {
throw e;
}catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
protected StringBuffer getSelectQuery() {