All Implemented Interfaces:
Serializable, Element, EntityElement, IdentifiableElement, QueryTemplate

public class QueryTemplateImpl extends EntityElementImpl implements QueryTemplate
The default implementation of the QueryTemplate interface.
Author:
Luca Frosini (ISTI - CNR)
See Also:
  • Field Details

    • name

      protected String name
      The name of the query template.
    • description

      protected String description
      The description of the query template.
    • version

      protected Version version
      The version of the query template.
    • changelog

      protected Map<Version,String> changelog
      The changelog of the query template.
    • objectMapper

      protected org.gcube.com.fasterxml.jackson.databind.ObjectMapper objectMapper
      The object mapper.
    • template

      protected org.gcube.com.fasterxml.jackson.databind.JsonNode template
      The template.
    • templateVariables

      protected Map<String,TemplateVariable> templateVariables
      The template variables.
    • params

      protected org.gcube.com.fasterxml.jackson.databind.JsonNode params
      The parameters.
  • Constructor Details

    • QueryTemplateImpl

      public QueryTemplateImpl()
      Default constructor.
  • Method Details

    • getName

      public String getName()
      Returns the name of the query template.
      Specified by:
      getName in interface QueryTemplate
      Returns:
      The template name.
    • setName

      public void setName(String name)
      Sets the name of the query template.
      Specified by:
      setName in interface QueryTemplate
      Parameters:
      name - The name to set.
    • getDescription

      public String getDescription()
      Returns the description of the query template.
      Specified by:
      getDescription in interface QueryTemplate
      Returns:
      The template description.
    • setDescription

      public void setDescription(String description)
      Sets the description of the query template.
      Specified by:
      setDescription in interface QueryTemplate
      Parameters:
      description - The description to set.
    • getVersion

      public Version getVersion()
      Returns the version of the query template.
      Specified by:
      getVersion in interface QueryTemplate
      Returns:
      The template version, or null when not set.
    • getVersionAsString

      public String getVersionAsString()
      Returns the version of the query template as a string.
      Specified by:
      getVersionAsString in interface QueryTemplate
      Returns:
      The version string, or null when not set.
    • setVersion

      public void setVersion(String version)
      Sets the version of the query template.
      Specified by:
      setVersion in interface QueryTemplate
      Parameters:
      version - The version string, in the form major.minor.patch.
    • getChangelog

      public Map<Version,String> getChangelog()
      Returns the changelog of the query template.
      Specified by:
      getChangelog in interface QueryTemplate
      Returns:
      A map of versions to change descriptions, or null when not set.
    • getChangelogWithVersionAsString

      public Map<String,String> getChangelogWithVersionAsString()
      Returns the changelog with versions represented as strings.
      Specified by:
      getChangelogWithVersionAsString in interface QueryTemplate
      Returns:
      A map of version strings to change descriptions, or null when not set.
    • setChangelog

      public void setChangelog(Map<String,String> changelog)
      Sets the changelog of the query template.
      Specified by:
      setChangelog in interface QueryTemplate
      Parameters:
      changelog - A map of version strings to change descriptions.
    • getTemplateAsString

      public String getTemplateAsString() throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException
      Returns the template body as a string.
      Specified by:
      getTemplateAsString in interface QueryTemplate
      Returns:
      The template as a string.
      Throws:
      org.gcube.com.fasterxml.jackson.core.JsonProcessingException - if an error occurs during serialization.
    • setTemplate

      public void setTemplate(String template) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException, IOException
      Sets the template body from a string.
      Specified by:
      setTemplate in interface QueryTemplate
      Parameters:
      template - The template string.
      Throws:
      org.gcube.com.fasterxml.jackson.core.JsonProcessingException - if an error occurs during JSON processing.
      IOException - if an error occurs during parsing.
    • getTemplate

      public org.gcube.com.fasterxml.jackson.databind.JsonNode getTemplate()
      Returns the template body as a JsonNode.
      Specified by:
      getTemplate in interface QueryTemplate
      Returns:
      The template as a JsonNode.
    • setTemplate

      public void setTemplate(org.gcube.com.fasterxml.jackson.databind.JsonNode template)
      Sets the template body from a JsonNode.
      Specified by:
      setTemplate in interface QueryTemplate
      Parameters:
      template - The template as a JsonNode.
    • getTemplateVariables

      public Map<String,TemplateVariable> getTemplateVariables()
      Returns the variables defined in this template.
      Specified by:
      getTemplateVariables in interface QueryTemplate
      Returns:
      A map of template variables, with variable names as keys.
    • addTemplateVariable

      public void addTemplateVariable(TemplateVariable templateVariable)
      Adds a variable to the template. If a variable with the same name already exists, it will be overridden.
      Specified by:
      addTemplateVariable in interface QueryTemplate
      Parameters:
      templateVariable - The variable to add.
    • getParamsFromDefaultValues

      public org.gcube.com.fasterxml.jackson.databind.node.ObjectNode getParamsFromDefaultValues()
      Creates an ObjectNode containing the default values of all template variables.
      Specified by:
      getParamsFromDefaultValues in interface QueryTemplate
      Returns:
      An ObjectNode with default parameter values.
    • getJsonQuery

      public org.gcube.com.fasterxml.jackson.databind.JsonNode getJsonQuery() throws Exception
      Generates a runnable query by substituting variables with their default values.
      Specified by:
      getJsonQuery in interface QueryTemplate
      Returns:
      The generated query as a JsonNode.
      Throws:
      Exception - if an error occurs during query generation.
    • replaceVariables

      protected org.gcube.com.fasterxml.jackson.databind.JsonNode replaceVariables(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode) throws Exception
      Replaces the variables in the given JSON node. Every string node whose entire value equals a declared variable name, as an object value or as an array element at any depth, is replaced by the variable's value node. Object keys are not substitution targets, strings are never partially interpolated, and inserted values are not scanned again.
      Parameters:
      jsonNode - the JSON node
      Returns:
      the JSON node with the variables replaced
      Throws:
      Exception - if a variable has neither a provided value nor a default value
    • getJsonQuery

      public org.gcube.com.fasterxml.jackson.databind.JsonNode getJsonQuery(org.gcube.com.fasterxml.jackson.databind.JsonNode values) throws Exception
      Generates a runnable query by substituting variables with the provided values.
      Specified by:
      getJsonQuery in interface QueryTemplate
      Parameters:
      values - A JsonNode containing the values to substitute.
      Returns:
      The generated query as a JsonNode.
      Throws:
      Exception - if an error occurs during query generation.