Class InstancesJsonQueryExecutor

java.lang.Object
org.gcube.informationsystem.resourceregistry.queries.json.InstancesJsonQueryExecutor

public class InstancesJsonQueryExecutor extends Object
Executes a JSON Query-By-Example against the Instances collection, enforcing role-based and context-scoped security rules.

Wraps a JsonQuery builder to obtain the SQL query text (query-text generation is generic across collections; execution is not), then opens the current request's InstanceEnvironment, runs the query, and serializes each matched element via ERManagementUtility/ERManagement, honouring polymorphic, hierarchical, includeContexts, metadataPolicy, and ContextVisibilityPolicy.

Author:
Luca Frosini (ISTI - CNR)
  • Field Details

    • jsonQueryBuilder

      protected final JsonQuery jsonQueryBuilder
      The underlying query-text builder.
    • oDatabaseDocument

      protected com.orientechnologies.orient.core.db.document.ODatabaseDocument oDatabaseDocument
      The active OrientDB database session.
    • limit

      protected Integer limit
      The pagination limit for the query.
    • offset

      protected Integer offset
      The pagination offset for the query.
    • polymorphic

      protected boolean polymorphic
      Whether the query results should include subtypes (polymorphic).
    • hierarchical

      protected boolean hierarchical
      Whether hierarchical lookup is enabled.
    • activeOnlyHierarchical

      protected boolean activeOnlyHierarchical
      Whether the active-only hierarchical reader channel must be used.
    • includeContexts

      protected boolean includeContexts
      Whether to include context availability information in results.
    • metadataPolicy

      protected org.gcube.informationsystem.resourceregistry.api.rest.MetadataPolicy metadataPolicy
      The metadata inclusion policy.
    • maskMetadata

      protected boolean maskMetadata
      Whether privacy-sensitive metadata identity fields must be masked.
    • contextVisibilityPolicy

      protected ContextVisibilityPolicy contextVisibilityPolicy
      The Context visibility policy applied to the contexts map of each result, when includeContexts is set.
    • currentContextPath

      protected String currentContextPath
      The caller's own Context path, required by ContextVisibilityPolicy.DESCENDANTS filtering.
    • filterNonActiveDescendants

      protected boolean filterNonActiveDescendants
      Whether descendant Contexts with a non-ACTIVE effectiveState are additionally filtered out of the contexts map.
    • projection

      protected boolean projection
      Whether the last query() was projection-based (an _emit block was present), set only after query() runs.
  • Constructor Details

    • InstancesJsonQueryExecutor

      public InstancesJsonQueryExecutor()
      Creates a new InstancesJsonQueryExecutor instance with default configuration.
  • Method Details

    • setJsonQuery

      public void setJsonQuery(String jsonQuery) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException
      Parses and sets the input JSON query string.
      Parameters:
      jsonQuery - the JSON query string
      Throws:
      org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException - if the JSON string is invalid
    • setJsonQuery

      public void setJsonQuery(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonQuery)
      Sets the input JSON query tree node.
      Parameters:
      jsonQuery - the JSON query node
    • setLimit

      public void setLimit(Integer limit)
      Sets the pagination limit.
      Parameters:
      limit - the limit to set
    • setOffset

      public void setOffset(Integer offset)
      Sets the pagination offset.
      Parameters:
      offset - the offset to set
    • setPolymorphic

      public void setPolymorphic(boolean polymorphic)
      Sets whether polymorphic query is enabled.
      Parameters:
      polymorphic - true to enable polymorphic results, false otherwise
    • setHierarchical

      public void setHierarchical(boolean hierarchical)
      Sets whether hierarchical lookup is enabled.
      Parameters:
      hierarchical - true to enable hierarchical, false otherwise
    • setActiveOnlyHierarchical

      public void setActiveOnlyHierarchical(boolean activeOnlyHierarchical)
      Sets whether the active-only hierarchical reader channel must be used instead of the unrestricted one. Meaningless unless setHierarchical(boolean) is also true.
      Parameters:
      activeOnlyHierarchical - true to use the active-only channel
    • setIncludeContexts

      public void setIncludeContexts(boolean includeContexts)
      Sets whether to include contexts in results.
      Parameters:
      includeContexts - true to enable includeContexts, false otherwise
    • setMetadataPolicy

      public void setMetadataPolicy(org.gcube.informationsystem.resourceregistry.api.rest.MetadataPolicy metadataPolicy)
      Sets the metadata policy.
      Parameters:
      metadataPolicy - the metadata policy to set
    • setMaskMetadata

      public void setMaskMetadata(boolean maskMetadata)
      Configures privacy masking for full representations and _emit aliases.
      Parameters:
      maskMetadata - true to redact sensitive metadata identity fields
    • setContextVisibilityPolicy

      public void setContextVisibilityPolicy(ContextVisibilityPolicy contextVisibilityPolicy)
      Sets the Context visibility policy applied to the contexts map of each result, when includeContexts is set. Must be resolved and set by the caller (PEP or Management class) based on the current caller's role; defaults to unfiltered ContextVisibilityPolicy.ALL if never called.
      Parameters:
      contextVisibilityPolicy - the ContextVisibilityPolicy to apply
    • setCurrentContextPath

      public void setCurrentContextPath(String currentContextPath)
      Sets the caller's own Context path, required by ContextVisibilityPolicy.DESCENDANTS filtering.
      Parameters:
      currentContextPath - the current context path to apply
    • setFilterNonActiveDescendants

      public void setFilterNonActiveDescendants(boolean filterNonActiveDescendants)
      Sets whether descendant Contexts with a non-ACTIVE effectiveState must additionally be filtered out of the contexts map, on top of whatever setContextVisibilityPolicy(ContextVisibilityPolicy) already applies.
      Parameters:
      filterNonActiveDescendants - true to filter non-ACTIVE descendants
    • isProjection

      public boolean isProjection()
      Reports whether the query executed by the last query() call was projection-based (i.e. contained an _emit block anywhere in its tree). Per the Canonical _emit Precedence Rules, a projecting query ignores metadataPolicy/includeContexts entirely - callers use this to report that fact back to the client via a QueryParameterWarning.
      Returns:
      true if the last executed query was projection-based, false otherwise (including before the first query() call)
    • collectSensitiveEmitAliases

      protected void collectSensitiveEmitAliases(org.gcube.com.fasterxml.jackson.databind.JsonNode node, Set<String> sensitiveAliases)
      Recursively scans the query JSON for all _emit blocks and collects the result-column aliases that correspond to privacy-sensitive metadata fields (metadata.createdBy and metadata.lastUpdateBy).

      In an _emit block the JSON structure is "<fieldPath>": "<alias>", so the alias (value) is what OrientDB returns as the column name in the result set.

      Parameters:
      node - the JSON node to scan (call with the root query node)
      sensitiveAliases - the set to populate
    • query

      public String query() throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException
      Executes the JSON query, enforcing role-based and context-scoped security rules.
      Returns:
      JSON array string of query results
      Throws:
      org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException - if the query is invalid or syntactically incorrect
      org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException - on database error