Class GenericQuery

  • All Implemented Interfaces:
    java.io.Serializable

    public class GenericQuery
    extends FlexibleSearchTranslatable
    The GenericQuery class holds and manages all objects necessary for searching on a specific Type. That include Conditions, SelectFields and OrderBy as well as Joins and SubQueries.

    To setup a particular query, you have to do the following:

     // assuming we've got a type to search upon
     GenericQuery query = new GenericQuery(MyModel._TYPECODE);
     query.addCondition(
                    GenericCondition.createConditionForValueComparison(new GenericSearchField(MyModel.ATTRIBUTE), Operator.EQUALS, "test"));
     query.addOrderBy(new GenericSearchOrderBy(new GenericSearchField(MyModel.ATTRIBUTE), true));
    
     final SearchResult<MyModel> searchResult = genericSearchService.search(gsquery);
     final List<MyModel> result = searchResult.getResult();
     

    Also quite common is joining types:

     // create query as usual
     GenericQuery query = new GenericQuery( MyModel._TYPECODE );
     // now add joined type and specify join condition
     query.addInnerJoin(
            MyOtherTypeModel._TYPECODE ,
            GenericCondition.createConditionForFieldComparison(
                    new GenericSearchField(MyModel._TYPECODE, MyModel.ATTRIBUTE),
                    Operator.EQUALS,
                    new GenericSearchField(MyOtherTypeModel._TYPECODE, MyOtherTypeModel.ATTRIBUTE),
            )
     );
     // now we may add query conditions for both types
     query.addCondition(
            GenericCondition.createIsNotNullCondition(
                    new GenericSearchField( MyModel._TYPECODE, MyModel.ATTRIBUTE )
            )
     );
     query.addCondition(
            GenericCondition.createIsNullCondition(
                    new GenericSearchField(MyOtherType._TYPECODE, MyOtherTypeModel.ATTRIBUTE )
            )
     );
     // ... execute search as usual using GenericSearchService
     

    And instead of searching for Items, you're able to define your own select fields. Please note that the correct return class for each select field must be provided.

     ...
     query.addSelectField(
            new GenericSelectField( MyModel._TYPECODE, MyModel.ATTRIBUTE, String.class )
     );
     ...
     

    If the same type occurs several time within one query it is necessary to use aliases for each type.

     // the initial type does not need to have a alias
     GenericQuery query = new GenericQuery( MyModel._TYPECODE );
     // now join the same type again
     query.addInnerJoin(
            MyModel._TYPECODE ,
            "alias1",
            GenericCondition.getComparison(
                    new GenericSearchField( T.ATTR1 ), // fields of the initial type dont need aliases either
                    Operator.EQUALS,
                    new GenericSearchField( "alias1", T.ATTR2 ) // this field refers to the joined type!
        )
     );
     // and again ...
     query.addInnerJoin(
            MyModel._TYPECODE,
            "alias2",
            GenericCondition.getComparison(
                    new GenericSearchField( "alias1" T.ATTR3 ),
                    Operator.EQUALS,
                    new GenericSearchField( "alias2", T.ATTR4 )
        )
     );
     
    See Also:
    Serialized Form
    • Constructor Detail

      • GenericQuery

        public GenericQuery​(java.lang.String typeCode,
                            GenericCondition condition,
                            boolean typeExclusive)
      • GenericQuery

        public GenericQuery​(java.lang.String typeCode,
                            GenericCondition condition)
      • GenericQuery

        public GenericQuery​(java.lang.String typeCode)
      • GenericQuery

        public GenericQuery​(java.lang.String typeCode,
                            boolean typeExclusive)
    • Method Detail

      • getInitialTypeCode

        public java.lang.String getInitialTypeCode()
        Returns the initial type code.
        Returns:
        the initial type code
      • setInitialTypeCode

        protected void setInitialTypeCode​(java.lang.String typeCode)
        Sets the initial type code.
        Parameters:
        typeCode - the new initial type code
      • setInitialTypeAlias

        public void setInitialTypeAlias​(java.lang.String alias)
      • isTypeExclusive

        public boolean isTypeExclusive()
        Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default is false
        Returns:
        true if the initial type is marked exclusive
      • setTypeExclusive

        public void setTypeExclusive​(boolean exclusive)
        Marks the initial type exclusive, which means subtype are excluded from searching; default is false.
        Parameters:
        exclusive - true marks the initial type as exclusive type
      • addCondition

        public GenericQuery addCondition​(GenericCondition condition)
        Adds a GenericCondition to this query. A GenericConditionList is created automatically if necessary.
        Parameters:
        condition - the GenericCondition which will be added
      • addConditions

        public GenericQuery addConditions​(GenericCondition... conditions)
        Adds one or more conditions to this query at once. If the query will contain more than one condition afterwards it automatically wraps them into a GenericConditionList.
        Parameters:
        conditions - the new conditions
      • getCondition

        public GenericCondition getCondition()
        Returns the condition hold by this instance.
        Returns:
        the condition
      • setCondition

        protected void setCondition​(GenericCondition genericCondition)
        Parameters:
        genericCondition - The condition to be set.
      • getOrderByList

        public java.util.Collection<GenericSearchOrderBy> getOrderByList()
        Returns a copy of the orderBy list.
        Returns:
        orderBy list
      • addOrderBy

        public GenericQuery addOrderBy​(GenericSearchOrderBy orderBy)
        Adds an GenericSearchOrderBy to the query.
        Parameters:
        orderBy - the GenericSearchOrderBy instance to add
      • addOuterJoin

        public GenericTypeJoin addOuterJoin​(java.lang.String typeCode,
                                            java.lang.String alias,
                                            GenericCondition joinCondition)
        Adds an OuterJoin to the query.
        Parameters:
        joinCondition - the GenericCondition instance, representing the join
      • addOuterJoin

        public GenericTypeJoin addOuterJoin​(java.lang.String typeCode,
                                            java.lang.String alias,
                                            GenericCondition joinCondition,
                                            boolean isTypeExclusive)
        Adds an OuterJoin to the query.
        Parameters:
        joinCondition - the GenericCondition instance, representing the join
        isTypeExclusive - whether the joined type is marked exclusive
      • addInnerJoin

        public GenericTypeJoin addInnerJoin​(java.lang.String typeCode,
                                            java.lang.String alias,
                                            GenericCondition joinCondition)
        Adds an InnerJoin to the query.
        Parameters:
        joinCondition - the GenericCondition instance, representing the join
      • addInnerJoin

        public GenericTypeJoin addInnerJoin​(java.lang.String typeCode,
                                            java.lang.String alias,
                                            GenericCondition joinCondition,
                                            boolean isTypeExclusive)
        Adds an InnerJoin to the query.
        Parameters:
        joinCondition - the GenericCondition instance, representing the join
        isTypeExclusive - whether the joined type is marked exclusive
      • getTypeJoinList

        public java.util.Collection<GenericTypeJoin> getTypeJoinList()
        Returns a copy of the join list, containing GenericTypeJoin instances.
        Returns:
        join list
      • addSubQuery

        public GenericQuery addSubQuery​(GenericSearchField field,
                                        Operator operator,
                                        java.lang.String subqueryTypeCode)
        Creates an adds a new GenericSubQueryCondition to this query.
        Parameters:
        field - the field to compare the subquery result to
        operator - the comparison operator
        subqueryTypeCode - the inital type code of the subquery
        Returns:
        the subquery object
      • addSubQuery

        public GenericQuery addSubQuery​(java.lang.String fieldQualifier,
                                        Operator operator,
                                        java.lang.String subqueryTypeCode)
      • addSubQuery

        public GenericQuery addSubQuery​(Operator operator,
                                        java.lang.String subqueryTypeCode)
      • addSelectField

        public GenericQuery addSelectField​(GenericSelectField field)
        Adds a GenericSelectField to the query.
        Parameters:
        field - the GenericSearchField which will be added
      • getResultClasses

        public java.util.List<java.lang.Class> getResultClasses()
        Returns a List containing all result Classes which have been previously set.
        Returns:
        list of classes
      • getSelectFields

        public java.util.List<GenericSelectField> getSelectFields()
        Returns a List containing GenericSelectFields instances which have been previously set.
        Returns:
        list of SelectFields
      • toFlexibleSearch

        public java.lang.String toFlexibleSearch​(java.util.Map valueMap)
        Convenience method instead of using toFlexibleSearch(StringBuilder, Map, Map).
        Parameters:
        valueMap - the value map to be passed to flexiblesearch (which is filled automatically)
        Returns:
        the generated flexiblesearch query
      • toPolyglotSearch

        public java.lang.String toPolyglotSearch​(java.util.Map valueMap)
      • toPolyglotSearch

        public void toPolyglotSearch​(java.lang.StringBuilder queryBuffer,
                                     java.util.Map<java.lang.String,​java.lang.String> aliasTypeMap,
                                     java.util.Map<java.lang.String,​java.lang.Object> valueMap)
        Description copied from class: FlexibleSearchTranslatable
        compiles this instance in order to append its query snippet and add its value(s), if any
        Overrides:
        toPolyglotSearch in class FlexibleSearchTranslatable
        Parameters:
        queryBuffer - contains the query
        aliasTypeMap - contains typeCode <> typeIndex mappings
        valueMap - contains valueQualifier <> value mappings
      • isTranslatableToPolyglotDialect

        public boolean isTranslatableToPolyglotDialect()