public class GenericQuery extends FlexibleSearchTranslatable
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 )
)
);
| Constructor and Description |
|---|
GenericQuery(java.lang.String typeCode) |
GenericQuery(java.lang.String typeCode,
boolean typeExclusive) |
GenericQuery(java.lang.String typeCode,
GenericCondition condition) |
GenericQuery(java.lang.String typeCode,
GenericCondition condition,
boolean typeExclusive) |
| Modifier and Type | Method and Description |
|---|---|
GenericQuery |
addCondition(GenericCondition condition)
Adds a
GenericCondition |
GenericQuery |
addConditions(GenericCondition... conditions)
Adds one or more conditions to this query at once.
|
GenericTypeJoin |
addInnerJoin(java.lang.String typeCode,
GenericCondition joinCondition) |
GenericTypeJoin |
addInnerJoin(java.lang.String typeCode,
GenericCondition joinCondition,
boolean isTypeExclusive) |
GenericTypeJoin |
addInnerJoin(java.lang.String typeCode,
java.lang.String alias,
GenericCondition joinCondition)
Adds an InnerJoin to the query.
|
GenericTypeJoin |
addInnerJoin(java.lang.String typeCode,
java.lang.String alias,
GenericCondition joinCondition,
boolean isTypeExclusive)
Adds an InnerJoin to the query.
|
GenericQuery |
addOrderBy(GenericSearchOrderBy orderBy)
Adds an
GenericSearchOrderBy to the query. |
GenericTypeJoin |
addOuterJoin(java.lang.String typeCode,
GenericCondition joinCondition) |
GenericTypeJoin |
addOuterJoin(java.lang.String typeCode,
GenericCondition joinCondition,
boolean isTypeExclusive) |
GenericTypeJoin |
addOuterJoin(java.lang.String typeCode,
java.lang.String alias,
GenericCondition joinCondition)
Adds an OuterJoin to the query.
|
GenericTypeJoin |
addOuterJoin(java.lang.String typeCode,
java.lang.String alias,
GenericCondition joinCondition,
boolean isTypeExclusive)
Adds an OuterJoin to the query.
|
GenericQuery |
addSelectField(GenericSelectField field)
Adds a
GenericSelectField to the query. |
GenericQuery |
addSubQuery(GenericSearchField field,
Operator operator,
java.lang.String subqueryTypeCode)
Creates an adds a new
GenericSubQueryCondition to this query. |
GenericQuery |
addSubQuery(Operator operator,
java.lang.String subqueryTypeCode) |
GenericQuery |
addSubQuery(java.lang.String fieldQualifier,
Operator operator,
java.lang.String subqueryTypeCode) |
GenericCondition |
getCondition()
Returns the condition hold by this instance.
|
java.lang.String |
getInitialTypeCode()
Returns the initial type code.
|
java.util.Collection<GenericSearchOrderBy> |
getOrderByList()
Returns a copy of the orderBy list.
|
java.util.List<java.lang.Class> |
getResultClasses()
Returns a List containing all result
Classes which have been previously set. |
java.util.List<GenericSelectField> |
getSelectFields()
Returns a List containing
GenericSelectFields instances which have been previously set. |
java.util.Collection<GenericTypeJoin> |
getTypeJoinList()
Returns a copy of the join list, containing
GenericTypeJoin instances. |
boolean |
isTypeExclusive()
Indicates whether the initial type is marked exclusive, which means subtype are excluded from searching; default
is false
|
protected void |
setCondition(GenericCondition genericCondition) |
void |
setInitialTypeAlias(java.lang.String alias) |
protected void |
setInitialTypeCode(java.lang.String typeCode)
Sets the initial type code.
|
void |
setTypeExclusive(boolean exclusive)
Marks the initial type exclusive, which means subtype are excluded from searching; default is false.
|
java.lang.String |
toFlexibleSearch(java.util.Map valueMap)
Convenience method instead of using
toFlexibleSearch(StringBuilder, Map, Map). |
void |
toFlexibleSearch(java.lang.StringBuilder queryBuffer,
java.util.Map<java.lang.String,java.lang.String> _aliasTypeMap,
java.util.Map<java.lang.String,java.lang.Object> valueMap)
compiles this instance in order to append its query snippet and add its value(s), if any
|
getAliasFromTypeMappublic GenericQuery(java.lang.String typeCode,
GenericCondition condition,
boolean typeExclusive)
public GenericQuery(java.lang.String typeCode,
GenericCondition condition)
public GenericQuery(java.lang.String typeCode)
public GenericQuery(java.lang.String typeCode,
boolean typeExclusive)
public java.lang.String getInitialTypeCode()
protected void setInitialTypeCode(java.lang.String typeCode)
typeCode - the new initial type codepublic void setInitialTypeAlias(java.lang.String alias)
public void setTypeExclusive(boolean exclusive)
exclusive - true marks the initial type as exclusive typepublic boolean isTypeExclusive()
true if the initial type is marked exclusivepublic GenericQuery addCondition(GenericCondition condition)
GenericCondition to this query.
A GenericConditionList is created automatically if necessary.condition - the GenericCondition which will be addedpublic GenericQuery addConditions(GenericCondition... conditions)
GenericConditionList.conditions - the new conditionspublic GenericCondition getCondition()
protected void setCondition(GenericCondition genericCondition)
genericCondition - The condition to be set.public java.util.Collection<GenericSearchOrderBy> getOrderByList()
public GenericQuery addOrderBy(GenericSearchOrderBy orderBy)
GenericSearchOrderBy to the query.orderBy - the GenericSearchOrderBy instance to addpublic GenericTypeJoin addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
joinCondition - the GenericCondition instance, representing the joinpublic GenericTypeJoin addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition)
public GenericTypeJoin addOuterJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
joinCondition - the GenericCondition instance, representing the joinisTypeExclusive - whether the joined type is marked exclusivepublic GenericTypeJoin addOuterJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition)
joinCondition - the GenericCondition instance, representing the joinpublic GenericTypeJoin addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition)
public GenericTypeJoin addInnerJoin(java.lang.String typeCode, java.lang.String alias, GenericCondition joinCondition, boolean isTypeExclusive)
joinCondition - the GenericCondition instance, representing the joinisTypeExclusive - whether the joined type is marked exclusivepublic GenericTypeJoin addInnerJoin(java.lang.String typeCode, GenericCondition joinCondition, boolean isTypeExclusive)
public java.util.Collection<GenericTypeJoin> getTypeJoinList()
GenericTypeJoin instances.public GenericQuery addSubQuery(GenericSearchField field, Operator operator, java.lang.String subqueryTypeCode)
GenericSubQueryCondition to this query.field - the field to compare the subquery result tooperator - the comparison operatorsubqueryTypeCode - the inital type code of the subquerypublic GenericQuery addSubQuery(java.lang.String fieldQualifier, Operator operator, java.lang.String subqueryTypeCode)
public GenericQuery addSubQuery(Operator operator, java.lang.String subqueryTypeCode)
public GenericQuery addSelectField(GenericSelectField field)
GenericSelectField to the query.field - the GenericSearchField which will be addedpublic java.util.List<java.lang.Class> getResultClasses()
Classes which have been previously set.public java.util.List<GenericSelectField> getSelectFields()
GenericSelectFields instances which have been previously set.public java.lang.String toFlexibleSearch(java.util.Map valueMap)
toFlexibleSearch(StringBuilder, Map, Map).valueMap - the value map to be passed to flexiblesearch (which is filled automatically)public void toFlexibleSearch(java.lang.StringBuilder queryBuffer,
java.util.Map<java.lang.String,java.lang.String> _aliasTypeMap,
java.util.Map<java.lang.String,java.lang.Object> valueMap)
FlexibleSearchTranslatabletoFlexibleSearch in class FlexibleSearchTranslatablequeryBuffer - contains the query_aliasTypeMap - contains typeCode <> typeIndex mappingsvalueMap - contains valueQualifier <> value mappingsFlexibleSearchTranslatable.toFlexibleSearch(java.lang.StringBuilder, java.util.Map,
java.util.Map)Copyright © 2018 SAP SE. All Rights Reserved.