Implementing QueryTemplate manager

This commit is contained in:
Luca Frosini 2021-10-25 18:12:24 +02:00
parent 7aff57a3d1
commit 2578a7cecb
3 changed files with 116 additions and 8 deletions

View File

@ -13,6 +13,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresen
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
@ -34,6 +35,7 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
private static Logger logger = LoggerFactory.getLogger(QueryTemplateManagement.class);
protected String name;
protected JsonNode params;
public QueryTemplateManagement() {
super(AccessType.QUERY_TEMPLATE);
@ -46,6 +48,10 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
getWorkingContext();
}
protected void checkERMatch() throws ResourceRegistryException {
getOClass();
}
public void setName(String name) {
this.name = name;
}
@ -177,5 +183,36 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
throw new ResourceRegistryException(e);
}
}
public void setParams(String params) throws ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(params);
setParams(jsonNode);
} catch (ResourceRegistryException e) {
throw e;
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public void setParams(JsonNode params) throws ResourceRegistryException {
// TODO checks here
this.params = params;
}
/**
* Run the query after having replaced query parameter with the provided values
* @return
*/
public String run() {
// TODO Auto-generated method stub
return null;
}
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// No sanity check required
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.informationsystem.resourceregistry.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -88,6 +89,21 @@ public class QueryTemplateManager {
return queryTemplateManagement.read();
}
@POST
@Path("{" + AccessPath.QUERY_TEMPLATE_NAME_PATH_PARAM + "}")
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
public String run(@PathParam(AccessPath.QUERY_TEMPLATE_NAME_PATH_PARAM) String queryTemplateName, String params)
throws NotFoundException, InvalidQueryException, ResourceRegistryException {
logger.info("Requested {} with name", QueryTemplate.NAME, queryTemplateName);
CalledMethodProvider.instance.set("readQueryTemplate");
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(queryTemplateName);
queryTemplateManagement.setParams(params);
return queryTemplateManagement.run();
}
/*
* DELETE /query-templates/{QUERY_TEMPLATE_NAME}
* e.g. DELETE /query-templates/GetAllRunningInstances

View File

@ -1,6 +1,16 @@
package org.gcube.informationsystem.resourceregistry.queries.templates;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import java.io.File;
import java.net.URL;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.informationsystem.queries.templates.impl.entities.QueryTemplateImpl;
import org.gcube.informationsystem.queries.templates.impl.properties.TemplateVariableImpl;
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.queries.templates.reference.properties.TemplateVariable;
import org.gcube.informationsystem.resourceregistry.queries.JsonQueryTest;
import org.gcube.informationsystem.utils.ElementMapper;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -9,13 +19,58 @@ public class QueryTemplateManagementTest {
private static Logger logger = LoggerFactory.getLogger(QueryTemplateManagementTest.class);
//@Test
public void testCreate() throws ResourceRegistryException {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName("Test");
queryTemplateManagement.setJson("");
String created = queryTemplateManagement.create();
logger.debug(created);
public File getQueryTemplatesDirectory() throws Exception {
URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "queryTemplates");
}
@Test
public void testCreate() throws Exception {
File queryTemplatesDirectory = getQueryTemplatesDirectory();
File jsonQueryTemaplateFile = new File(queryTemplatesDirectory, "queryTemplate" + 1 + ".json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryTemaplateFile);
String name = "Test";
QueryTemplate queryTemplate = new QueryTemplateImpl();
queryTemplate.setName(name);
queryTemplate.setDescription("A Test Query Template");
queryTemplate.setTemplate(jsonNode);
TemplateVariable stateTemplateVariable = new TemplateVariableImpl();
stateTemplateVariable.setName("$state");
stateTemplateVariable.setDescription("StateFacet value");
stateTemplateVariable.setDefaultValue("running");
queryTemplate.addTemplateVariable(stateTemplateVariable);
TemplateVariable nameTemplateVariable = new TemplateVariableImpl();
nameTemplateVariable.setName("$name");
nameTemplateVariable.setDescription("SoftwareFacet name");
nameTemplateVariable.setDefaultValue("resource-registry");
queryTemplate.addTemplateVariable(nameTemplateVariable);
TemplateVariable groupTemplateVariable = new TemplateVariableImpl();
groupTemplateVariable.setName("$group");
groupTemplateVariable.setDescription("SoftwareFacet group");
groupTemplateVariable.setDefaultValue("information-system");
queryTemplate.addTemplateVariable(nameTemplateVariable);
String json = ElementMapper.marshal(queryTemplate);
logger.info("Going to create {}: {}", QueryTemplate.NAME, json);
QueryTemplate gotQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, json);
JsonNode gotTemplate = gotQueryTemplate.getTemplate();
logger.info("{}", gotTemplate.toString());
// QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
// queryTemplateManagement.setName("Test");
// queryTemplateManagement.setJson(json);
// String created = queryTemplateManagement.create();
// logger.debug(created);
}
@Test