Class QueryTemplateDefinitionValidator

java.lang.Object
org.gcube.informationsystem.resourceregistry.queries.templates.QueryTemplateDefinitionValidator

public class QueryTemplateDefinitionValidator extends Object
Validates a Query Template definition against the closed definition and TemplateVariable contract, and applies its variables: the substitution of defaults at save time and of the supplied values at execution time. Works on the JSON representation only, so it is independent of the persistence layer.
Author:
Luca Frosini (ISTI - CNR)
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Values resolved for one execution of a Query Template.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The maximum length, in UTF-16 code units, of a String value without an explicit max.
    static final Pattern
    The grammar of a Query Template name: lowercase words separated by hyphens.
    static final Pattern
    The grammar of a template variable name: $ followed by a lowerCamelCase name.
    static final String
    The prefix of every template variable reference.
  • Constructor Summary

    Constructors
    Constructor
    Description
  • Method Summary

    Modifier and Type
    Method
    Description
    org.gcube.com.fasterxml.jackson.databind.node.ObjectNode
    comparable(org.gcube.com.fasterxml.jackson.databind.JsonNode definition)
    Returns the comparable content of a Query Template definition, submitted or persisted: the complete definition except id, metadata, type, and supertypes, with each variable complete (absent required is false, absent regexp is empty, absent bounds are omitted).
    Map<String,org.gcube.com.fasterxml.jackson.databind.JsonNode>
    defaultValues(org.gcube.com.fasterxml.jackson.databind.JsonNode definition)
    Returns the default value of every variable of a normalized definition.
    resolveExecutionValues(org.gcube.com.fasterxml.jackson.databind.JsonNode definition, org.gcube.com.fasterxml.jackson.databind.JsonNode params)
    Resolves the value of every variable for one execution.
    boolean
    sameValue(org.gcube.com.fasterxml.jackson.databind.JsonNode first, org.gcube.com.fasterxml.jackson.databind.JsonNode second)
    Tells whether two JSON values are equal, numbers being compared by numeric value.
    org.gcube.com.fasterxml.jackson.databind.JsonNode
    substitute(org.gcube.com.fasterxml.jackson.databind.JsonNode template, Map<String,org.gcube.com.fasterxml.jackson.databind.JsonNode> values)
    Returns a copy of the template in which every string node equal to a variable name is replaced by that variable's value.
    org.gcube.com.fasterxml.jackson.databind.node.ObjectNode
    validate(String pathName, org.gcube.com.fasterxml.jackson.databind.JsonNode definition)
    Validates a submitted Query Template definition (closed members, name grammar, description, version and changelog, variable declarations and reference coverage) and returns its normalized form: type set to QueryTemplate, supertypes removed, each variable complete and typed TemplateVariable.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NAME_PATTERN

      public static final Pattern NAME_PATTERN
      The grammar of a Query Template name: lowercase words separated by hyphens.
    • VARIABLE_NAME_PATTERN

      public static final Pattern VARIABLE_NAME_PATTERN
      The grammar of a template variable name: $ followed by a lowerCamelCase name.
    • VARIABLE_PREFIX

      public static final String VARIABLE_PREFIX
      The prefix of every template variable reference.
      See Also:
    • DEFAULT_STRING_MAX_LENGTH

      public static final int DEFAULT_STRING_MAX_LENGTH
      The maximum length, in UTF-16 code units, of a String value without an explicit max.
      See Also:
  • Constructor Details

  • Method Details

    • validate

      public org.gcube.com.fasterxml.jackson.databind.node.ObjectNode validate(String pathName, org.gcube.com.fasterxml.jackson.databind.JsonNode definition) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateException
      Validates a submitted Query Template definition (closed members, name grammar, description, version and changelog, variable declarations and reference coverage) and returns its normalized form: type set to QueryTemplate, supertypes removed, each variable complete and typed TemplateVariable. id and metadata are kept.
      Parameters:
      pathName - the name addressed by the request, or null when none is addressed
      definition - the submitted definition
      Returns:
      the normalized definition
      Throws:
      org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateException - if the definition violates the contract
    • comparable

      public org.gcube.com.fasterxml.jackson.databind.node.ObjectNode comparable(org.gcube.com.fasterxml.jackson.databind.JsonNode definition)
      Returns the comparable content of a Query Template definition, submitted or persisted: the complete definition except id, metadata, type, and supertypes, with each variable complete (absent required is false, absent regexp is empty, absent bounds are omitted).
      Parameters:
      definition - the definition
      Returns:
      its comparable content
    • sameValue

      public boolean sameValue(org.gcube.com.fasterxml.jackson.databind.JsonNode first, org.gcube.com.fasterxml.jackson.databind.JsonNode second)
      Tells whether two JSON values are equal, numbers being compared by numeric value.
      Parameters:
      first - the first value, possibly null
      second - the second value, possibly null
      Returns:
      true if they are equal
    • defaultValues

      public Map<String,org.gcube.com.fasterxml.jackson.databind.JsonNode> defaultValues(org.gcube.com.fasterxml.jackson.databind.JsonNode definition)
      Returns the default value of every variable of a normalized definition.
      Parameters:
      definition - the normalized definition
      Returns:
      the default value of each variable, keyed by variable name
    • resolveExecutionValues

      public QueryTemplateDefinitionValidator.ExecutionValues resolveExecutionValues(org.gcube.com.fasterxml.jackson.databind.JsonNode definition, org.gcube.com.fasterxml.jackson.databind.JsonNode params) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException
      Resolves the value of every variable for one execution. A supplied value must be valid for the variable; an omitted variable takes its default unless it is required.
      Parameters:
      definition - the persisted definition
      params - the supplied values, a JSON object, or null when no body was sent
      Returns:
      the resolved values and the supplied keys that declare no variable
      Throws:
      org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException - if a required variable is omitted or a supplied value is invalid
    • substitute

      public org.gcube.com.fasterxml.jackson.databind.JsonNode substitute(org.gcube.com.fasterxml.jackson.databind.JsonNode template, Map<String,org.gcube.com.fasterxml.jackson.databind.JsonNode> values)
      Returns a copy of the template in which every string node equal to a variable name is replaced by that variable's value. Object keys are not substitution targets, strings are never partially interpolated, and inserted values are not scanned again.
      Parameters:
      template - the template
      values - the value of each variable
      Returns:
      the substituted query