Class QueryCriteriaUtil


  • public abstract class QueryCriteriaUtil
    extends Object
    • Constructor Detail

      • QueryCriteriaUtil

        public QueryCriteriaUtil​(Map<Class,​Map<String,​javax.persistence.metamodel.Attribute>> criteriaAttributes)
      • QueryCriteriaUtil

        protected QueryCriteriaUtil()
    • Method Detail

      • initialize

        protected void initialize​(Map<Class,​Map<String,​javax.persistence.metamodel.Attribute>> criteriaAttributes)
      • getCriteriaAttributes

        protected Map<Class,​Map<String,​javax.persistence.metamodel.Attribute>> getCriteriaAttributes()
      • convertListToInterfaceList

        public static <C,​I> List<I> convertListToInterfaceList​(List<C> internalResult,
                                                                     Class<I> interfaceType)
      • addCriteria

        public static void addCriteria​(Map<Class,​Map<String,​javax.persistence.metamodel.Attribute>> criteriaAttributes,
                                       String listId,
                                       javax.persistence.metamodel.Attribute attr)
      • addCriteria

        public static void addCriteria​(Map<Class,​Map<String,​javax.persistence.metamodel.Attribute>> criteriaAttributes,
                                       String listId,
                                       Class table,
                                       javax.persistence.metamodel.Attribute attr)
      • initializeCriteriaAttributes

        protected abstract boolean initializeCriteriaAttributes()
        The implementation of this method should be synchronized!
      • getCriteriaBuilder

        protected abstract javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()
      • doCriteriaQuery

        public <T> List<T> doCriteriaQuery​(QueryWhere queryWhere,
                                           Class<T> queryType)
        This method takes the high-level steps needed in order to create a JPA CriteriaQuery.
        1. A CriteriaBuilder and CriteriaQuery instance are created.
        2. The tables being selected from are defined in the query.
        3. The CriteriaQuery instance is filled using the criteria in the QueryWhere instance
        4. A JPA Query instance is created
        5. The meta criteria (max results, offset) are applied to the query
        6. The results are retrieved and returned
        Parameters:
        queryWhere - a QueryWhere instance containing the query criteria
        queryType - The type (Class) of the result
        Returns:
        The result of the query, a List.
      • fillCriteriaQuery

        protected <R,​T> void fillCriteriaQuery​(javax.persistence.criteria.CriteriaQuery<R> query,
                                                     QueryWhere queryWhere,
                                                     javax.persistence.criteria.CriteriaBuilder builder,
                                                     Class<T> queryType)
        This is the main ("highest"? "most abstract"?) method that is used to create a CriteriaQuery from a QueryWhere instance.
        Parameters:
        query - The (empty) CriteriaQuery that will be filled using the QueryCriteria and other information in the QueryWhere instance
        queryWhere - The QueryWhere instance, with abstract information that should be added to the CriteriaQuery
        builder - The CriteriaBuilder, helpful when creating Predicates to add to the CriteriaQuery
        queryType - The Class indicating the main Root of the CriteriaQuery
      • getRoot

        public static <T> javax.persistence.criteria.Root getRoot​(javax.persistence.criteria.AbstractQuery<T> query,
                                                                  Class queryType)
        This is a helper method to retrieve a particular Root from a CriteriaQuery instance
        Parameters:
        query - The CriteriaQuery instance that we're building
        queryType - The Class matching the Root we want
        Returns:
        The Root matching the given Class or null if it's not in the query
      • getEntityField

        protected <T> javax.persistence.criteria.Expression getEntityField​(javax.persistence.criteria.CriteriaQuery<T> query,
                                                                           String listId,
                                                                           javax.persistence.metamodel.Attribute attr)
        This method retrieves the entity "field" that can be used as the LHS of a Predicate

        This method is overridden in some extended QueryCriteriaUtil implementations
        Parameters:
        query - The CriteriaQuery that we're building
        listId - The list id of the given QueryCriteria
        Returns:
        An Expression with the Path to the field represented by the QueryCriteria.getListId() value
      • defaultGetEntityField

        public static <T> javax.persistence.criteria.Expression defaultGetEntityField​(javax.persistence.criteria.CriteriaQuery<T> query,
                                                                                      String listId,
                                                                                      javax.persistence.metamodel.Attribute attr)
      • basicCreatePredicateFromSingleCriteria

        public static javax.persistence.criteria.Predicate basicCreatePredicateFromSingleCriteria​(javax.persistence.criteria.CriteriaBuilder builder,
                                                                                                  javax.persistence.criteria.Expression entityField,
                                                                                                  QueryCriteria criteria)
        This method creates the basic types of Predicate from trivial QueryCriteria (NORMAL/REGEXP/RANGE).
        Parameters:
        builder - The CriteriaBuilder, helpful when creating Predicates to add to the CriteriaQuery
        entityField - The Expression representing a field/column in an entity/table.
        criteria - The QueryCriteria with the values to use as the RHS of a Predicate
        Returns:
        The created Predicate
      • convertRegexToJPALikeExpression

        protected static String convertRegexToJPALikeExpression​(String regexInput)
        Conver the regex (parameter) string to the JPA like syntax
        Parameters:
        regexInput - The parameter string
        Returns:
        The String in JPA syntax for a regular expressions
      • implSpecificCreatePredicateFromSingleCriteria

        protected abstract <R,​T> javax.persistence.criteria.Predicate implSpecificCreatePredicateFromSingleCriteria​(javax.persistence.criteria.CriteriaQuery<R> query,
                                                                                                                          javax.persistence.criteria.CriteriaBuilder builder,
                                                                                                                          Class queryType,
                                                                                                                          QueryCriteria criteria,
                                                                                                                          QueryWhere queryWhere)
        Some criteria do not directly refer to a field, such as those stored in the criteria attributes Map passed as an argument to the constructor.

        For example, the QueryParameterIdentifiers.LAST_VARIABLE_LIST criteria specifies that only the most recent VariableInstanceLog should be retrieved.

        This method is called from the #createPredicateFromSingleCriteria(CriteriaQuery, QueryCriteria, CriteriaBuilder, Class) method when no Attribute instance can be found in the instances criteria attributes Map.

        Parameters:
        criteriaQuery - The CriteriaQuery instance.
        criteria - The QueryCriteria instance with the criteria information.
        criteriaBuilder - The CriteriaBuilder instance used to help create the query predicate.
        queryType - The Class of the query, used to identify the type of query
        resultType - The Class of the result being request.
        Returns:
        A Predicate representin the information in the QueryCriteria instance.
      • createQueryAndCallApplyMetaCriteriaAndGetResult

        protected abstract <T> List<T> createQueryAndCallApplyMetaCriteriaAndGetResult​(QueryWhere queryWhere,
                                                                                       javax.persistence.criteria.CriteriaQuery<T> criteriaQuery,
                                                                                       javax.persistence.criteria.CriteriaBuilder builder)
        This method does the persistence-related logic related to executing a query.

        All implementations of this method should do the following, in approximately the following order:
        1. Get an EntityManager instance
        2. Join a transaction using the entity manager
        3. Create a Query from the given CriteriaQuery instance.
        4. Call the applyMetaCriteriaToQuery(Query, QueryWhere) method
        5. Retrieve the result from the Query instance.
        6. Close the transaction created, and the created EntityManager instance.
        7. Return the query result
        Parameters:
        criteriaQuery - The created and filled CriteriaQuery instance
        builder - The CriteriaBuilder, helpful when creating Predicates to add to the CriteriaQuery
        queryWhere - The QueryWhere instance containing the meta criteria information.
        Returns:
        A List of instances, representing the query result.
      • applyMetaCriteriaToQuery

        public static void applyMetaCriteriaToQuery​(javax.persistence.Query query,
                                                    QueryWhere queryWhere)
        Small method to apply the meta criteria from the QueryWhere instance to the Query instance
        Parameters:
        query - The Query instance
        queryWhere - The QueryWhere instance, with the abstract information about the query
      • getOrderByExpression

        protected <T,​R> javax.persistence.criteria.Expression getOrderByExpression​(javax.persistence.criteria.CriteriaQuery<R> query,
                                                                                         Class<T> queryType,
                                                                                         String orderByListId)
        Parameters:
        orderByListId -
        queryType -
        query -
        Returns: