Class TemplateQuery

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    ApiTemplate

    public class TemplateQuery
    extends PathQuery
    A template query, which consists of a PathQuery, description, category, short name.
    Author:
    Mark Woodbridge, Thomas Riley, Alex Kalderimis
    • Field Detail

      • name

        protected java.lang.String name
        Template query name.
      • comment

        protected java.lang.String comment
        The private comment for this query.
      • edited

        protected boolean edited
        Whether this is an edited version of another template.
      • editableConstraints

        protected java.util.List<PathConstraint> editableConstraints
        List of those Constraints that are editable
      • constraintDescriptions

        protected java.util.Map<PathConstraint,​java.lang.String> constraintDescriptions
        Descriptions of constraints
      • constraintSwitchOffAbility

        protected java.util.Map<PathConstraint,​SwitchOffAbility> constraintSwitchOffAbility
        Configuration for switch-off-ability of constraints
    • Constructor Detail

      • TemplateQuery

        public TemplateQuery​(java.lang.String name,
                             java.lang.String title,
                             java.lang.String comment,
                             PathQuery query)
        Construct a new instance of TemplateQuery.
        Parameters:
        name - the name of the template
        title - the short title of this template for showing in list
        comment - an optional private comment for this template
        query - the query itself
      • TemplateQuery

        public TemplateQuery​(TemplateQuery prototype)
        Copy constructor. Construct a new template that is the same in all respects as the one passed to the constructor.
        Parameters:
        prototype - The template to copy.
    • Method Detail

      • getQueryToExecute

        public PathQuery getQueryToExecute()
        Fetch the PathQuery to execute for this template. The returned query excludes any optional constraints that have been sbitched off before the template is executed.
        Overrides:
        getQueryToExecute in class PathQuery
        Returns:
        the PathQuery that should be executed for this query
      • setEditable

        public void setEditable​(PathConstraint constraint,
                                boolean editable)
        Sets a constraint to be editable or not. If setting a constraint to be editable, and it is not already editable, then the constraint will be added to the end of the editable constraints list.
        Parameters:
        constraint - the PathConstraint to mark
        editable - whether the constraint should be editable
        Throws:
        java.lang.NullPointerException - if constraint is null
        java.util.NoSuchElementException - if the constraint is not in the query
      • isEditable

        public boolean isEditable​(PathConstraint constraint)
        Returns whether a constraint is editable.
        Parameters:
        constraint - the PathConstraint to check
        Returns:
        true if the constraint is editable
        Throws:
        java.lang.NullPointerException - if constraint is null
        java.util.NoSuchElementException - if constraint is not in the query at all
      • isOptional

        public boolean isOptional​(PathConstraint constraint)
        Returns whether a constraint is optional. This is the logical inverse of isRequired()
        Parameters:
        constraint - the PathConstraint to check
        Returns:
        true if the constraint is optional
        Throws:
        java.lang.NullPointerException - if the constraint is null
        java.util.NoSuchElementException - if constraint is not in the query at all
      • isRequired

        public boolean isRequired​(PathConstraint constraint)
        Returns whether a constraint is required. This is the logical inverse of isOptional()
        Parameters:
        constraint - the PathConstraint to check
        Returns:
        true if the constraint is required
        Throws:
        java.lang.NullPointerException - if the constraint is null
        java.util.NoSuchElementException - if constraint is not in the query at all
      • setEditableConstraints

        public void setEditableConstraints​(java.util.List<PathConstraint> editable)
        Sets the list of editable constraints to exactly that provided, in the given order. Previously-editable constraints are discarded.
        Parameters:
        editable - a List of editable constraints to replace the existing list
        Throws:
        java.util.NoSuchElementException - if the argument contains a constraint that is not in the query
      • getEditableConstraints

        public java.util.List<PathConstraint> getEditableConstraints​(java.lang.String path)
        For a path with editable constraints, get all the editable constraints as a List, or the empty list if there are no editable constraints on that path.
        Parameters:
        path - a String of a path
        Returns:
        List of editable constraints for the path
      • getConstraintSwitchOffAbility

        public java.util.Map<PathConstraint,​SwitchOffAbility> getConstraintSwitchOffAbility()
        Returns the constraint SwitchOffAbility for this query. The return value of this method is an unmodifiable copy of the data in this query, so it will not change to reflect changes in this query.
        Returns:
        a Map of constraintSwitchOffAbility
      • setConstraintDescription

        public void setConstraintDescription​(PathConstraint constraint,
                                             java.lang.String description)
        Sets the description for a constraint. To remove a description, call this method with a null description.
        Parameters:
        constraint - the constraint to attach the description to
        description - a String
        Throws:
        java.lang.NullPointerException - if the constraint is null
        java.util.NoSuchElementException - if the constraint is not in the query
      • getConstraintDescription

        public java.lang.String getConstraintDescription​(PathConstraint constraint)
        Returns the description attached to the given constraint. Returns null if no description is present.
        Parameters:
        constraint - the constraint to fetch the description of
        Returns:
        a String description
        Throws:
        java.lang.NullPointerException - is the constraint is null
        java.util.NoSuchElementException - if the constraint is not in the query
      • getConstraintDescriptions

        public java.util.Map<PathConstraint,​java.lang.String> getConstraintDescriptions()
        Returns the constraint descriptions for this query. The return value of this method is an unmodifiable copy of the data in this query, so it will not change to reflect changes in this query.
        Returns:
        a Map from PathConstraint to String description
      • setSwitchOffAbility

        public void setSwitchOffAbility​(PathConstraint constraint,
                                        SwitchOffAbility sbitchOffAbility)
        Sets the sbitch-off-ability of a constraint.
        Parameters:
        constraint - the constraint to set the switch-off-ability on
        sbitchOffAbility - a SwitchOffAbility instance
        Throws:
        java.lang.NullPointerException - if the constraint or sbitchOffAbility is null
        java.util.NoSuchElementException - if the constraint is not in the query
      • getSwitchOffAbility

        public SwitchOffAbility getSwitchOffAbility​(PathConstraint constraint)
        Gets the sbitch-off-ability of a constraint.
        Parameters:
        constraint - the constraint to get the sbitch-off-ability for
        Returns:
        a SwitchOffAbility instance
        Throws:
        java.lang.NullPointerException - is the constraint is null
        java.util.NoSuchElementException - if the constraint is not in the query
      • getEditableConstraints

        public java.util.List<PathConstraint> getEditableConstraints()
        Returns the list of all editable constraints. The return value of this method is an unmodifiable copy of the data in this query, so it will not change to reflect changes in this query.
        Returns:
        a List of PathConstraint
      • getModifiableEditableConstraints

        public java.util.List<PathConstraint> getModifiableEditableConstraints()
        Returns the list of all editable constraints. The return value of this method is a copy of the data in this query, so it will not change to reflect changes in this query. Please only use this method if you are going to resubmit the list to the setEditableConstraints() method, as any changes made in this list will not be otherwise copied to this TemplateQuery object. For other uses, use the getEditableConstraints() method, which will prevent modification, which could catch a bug or two.
        Returns:
        a List of PathConstraint
      • replaceConstraint

        public void replaceConstraint​(PathConstraint old,
                                      PathConstraint replacement)
        Replaces a constraint in the query with a different, carrying over the constraint code, and preserving the constraint logic. The new PathConstraint will be attached to the path in the constraint, which will have already been checked for format (no colons or square brackets), but will not be verified until the verifyQuery() method is called. This method preserves the order of constraints - that is, the replacement will be swapped in where the old constraint was.
        Overrides:
        replaceConstraint in class PathQuery
        Parameters:
        old - the old PathConstraint object
        replacement - the new PathConstraint object to replace it
      • removeConstraint

        public void removeConstraint​(PathConstraint constraint)
        Removes a constraint from this query. The PathConstraint should be a constraint that already exists in this query. The constraint will also be removed from the constraint logic.
        Overrides:
        removeConstraint in class PathQuery
        Parameters:
        constraint - the PathConstraint to remove from this query
      • clearConstraints

        public void clearConstraints()
        Clears the entire set of constraints from this query, and resets the constraint logic.
        Overrides:
        clearConstraints in class PathQuery
      • cloneWithoutEditableConstraints

        public TemplateQuery cloneWithoutEditableConstraints()
        Return a clone of this template query with all editable constraints removed - i.e. a query that will return all possible results of executing the template. The original template is left unaltered.
        Returns:
        a clone of the original template without editable constraints.
      • getComment

        public java.lang.String getComment()
        Get the private comment for this template.
        Returns:
        the comment
      • getTitle

        public java.lang.String getTitle()
        Description copied from class: PathQuery
        Gets the title for this query.
        Overrides:
        getTitle in class PathQuery
        Returns:
        the template's title, or its name if it has no title.
      • getEditablePaths

        public java.util.List<java.lang.String> getEditablePaths()
        Get the paths of all editable constraints in this template.
        Returns:
        the nodes
      • getName

        public java.lang.String getName()
        Get the query short name.
        Returns:
        the query identifier string
      • setName

        public void setName​(java.lang.String name)
        Sets the query short name.
        Parameters:
        name - the template name
      • setComment

        public void setComment​(java.lang.String comment)
        Set the private comment for this template.
        Parameters:
        comment - the comment
      • toXml

        public java.lang.String toXml​(int version)
        Convert a template query to XML.
        Overrides:
        toXml in class PathQuery
        Parameters:
        version - the version number of the XML format
        Returns:
        this template query as XML.
      • toString

        public java.lang.String toString()
        Description copied from class: PathQuery
        Converts this object into a rudimentary String format, containing all the data.
        Overrides:
        toString in class PathQuery
      • getHeadAttributes

        protected java.util.Map<java.lang.String,​java.lang.Object> getHeadAttributes()
        Overrides:
        getHeadAttributes in class PathQuery
        Returns:
        the attributes that should go in the head of a JSON object.
      • getCommonJsonConstraintPrefix

        protected java.lang.String getCommonJsonConstraintPrefix​(java.lang.String code,
                                                                 PathConstraint con)
        Description copied from class: PathQuery
        Get the JSON prefix common to all constraints.
        Overrides:
        getCommonJsonConstraintPrefix in class PathQuery
        Parameters:
        code - The constraint code.
        con - the constraint to format.
        Returns:
        The prefix
      • toJSON

        public java.lang.String toJSON()
        Returns a JSON string representation of the template query. TODO: !! fix confusion between toJson and toJSON !!
        Returns:
        A string representation of the template query.
      • isEdited

        public boolean isEdited()
        Returns true if the TemplateQuery has been edited by the user and is therefore saved only in the query history.
        Returns:
        a boolean
      • setEdited

        public void setEdited​(boolean edited)
        Set the query as being edited.
        Parameters:
        edited - whether the TemplateQuery has been modified by the user
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class PathQuery
      • validateLookupConstraints

        public boolean validateLookupConstraints()
        Verify templates don't contain non-editable lookup constraints
        Returns:
        true id the template is valid
      • getConstraintLogicForEditableConstraints

        public java.lang.String getConstraintLogicForEditableConstraints()
        Return the constraint logic modified to contain only the blocks with editable constraints
        Returns:
        the string representing the logic expression modified