Added functionality to instantiate query from template using default

values or provided values
This commit is contained in:
Luca Frosini 2021-10-26 20:49:03 +02:00
parent 05b06ce6fa
commit 1d93d60bdf
2 changed files with 75 additions and 2 deletions

View File

@ -2,12 +2,15 @@ package org.gcube.informationsystem.queries.templates.impl.entities;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.annotation.JsonTypeName;
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.impl.entities.EntityElementImpl;
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.queries.templates.reference.properties.TemplateVariable;
@ -29,8 +32,10 @@ public class QueryTemplateImpl extends EntityElementImpl implements QueryTemplat
protected ObjectMapper objectMapper;
protected JsonNode template;
protected Map<String, TemplateVariable> templateVariables;
protected JsonNode params;
public QueryTemplateImpl() {
this.templateVariables = new HashMap<>();
this.objectMapper = new ObjectMapper();
@ -87,4 +92,59 @@ public class QueryTemplateImpl extends EntityElementImpl implements QueryTemplat
this.templateVariables.put(name, templateVariable);
}
@Override
public JsonNode getJsonQuery() throws Exception {
ObjectNode objectNode = objectMapper.createObjectNode();
for(TemplateVariable tv : templateVariables.values()) {
objectNode.put(tv.getName(), tv.getDefaultValue());
}
return getJsonQuery(objectNode);
}
protected JsonNode replaceVariables(JsonNode jsonNode) throws Exception {
Iterator<String> fieldNames = jsonNode.fieldNames();
while(fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode node = jsonNode.get(fieldName);
switch (node.getNodeType()) {
case OBJECT:
node = replaceVariables(node);
((ObjectNode) jsonNode).set(fieldName, node);
break;
case ARRAY:
ArrayNode arrayNode = (ArrayNode) node;
for(int i = 0; i < arrayNode.size(); i++) {
JsonNode arrayElement = arrayNode.get(i);
arrayElement = replaceVariables(arrayElement);
arrayNode.set(i, arrayElement);
}
break;
case STRING:
String value = node.asText();
if(templateVariables.containsKey(value)) {
JsonNode jn = params.get(value);
if(jn == null) {
throw new Exception("No value provided for " + value + " variables");
}
((ObjectNode) jsonNode).set(fieldName, jn);
}
break;
default:
break;
}
}
return jsonNode;
}
@Override
public JsonNode getJsonQuery(JsonNode values) throws Exception {
this.params = values;
JsonNode query = template.deepCopy();
query = replaceVariables(query);
return query;
}
}

View File

@ -69,4 +69,17 @@ public interface QueryTemplate extends EntityElement {
*/
public void addTemplateVariable(TemplateVariable templateVariable);
}
/**
* @return the JsonQuery replacing the variables using the default values contained in TemplateVariables
* @throws Exception
*/
public JsonNode getJsonQuery() throws Exception;
/**
* @param values
* @return the JsonQuery replacing the variables using the provided values
* @throws Exception
*/
public JsonNode getJsonQuery(JsonNode values) throws Exception;
}