public class QueryBuilder extends Object
Used to build a query for Experience Optimization Promotions and items.
Example getting Promotions from Publication 'tcm:0-3-1' in the Header or Footer region:
TcmUri publicationId = new TcmUri("tcm:0-3-1");
String regionHeader = "Header";
String regionFooter = "Footer";
QueryBuilder builder = new QueryBuilder(publicationId, Arrays.asList(regionHeader, regionFooter));
ResultSet resultSet = builder.execute();
Example getting Experiments adding criteria for a specific Page and an Audience Management based trigger value:
TcmUri publicationId = new TcmUri("tcm:0-3-1");
TcmUri pageId = new TcmUri("tcm:3-6409-64");
String regionHeader = "Header";
String triggerTypeAgeUrlParam = "am_ex_age";
QueryBuilder builder = new QueryBuilder(publicationId, Arrays.asList(regionHeader));
builder.addCriteria(new PageCriteria(pageId));
builder.addCriteria(new CustomParameterCriteria(triggerTypeAgeUrlParam, Integer.toString(30)));
ResultSet resultSet = builder.execute();
List<Experiment> experiments = new ArrayList<Experiment>();
for(Promotion promotion : resultSet.getPromotions()) {
if(promotion instanceof Experiment) experiments.add((Experiment)promotion);
}
log.info(String.format("Found %s experiments.", experiments.size()));
Constructor and Description |
---|
QueryBuilder()
Initializes a QueryBuilder.
|
QueryBuilder(String universe,
String locale)
Initializes a QueryBuilder for the specified universe and locale.
|
QueryBuilder(String universe,
String locale,
com.tridion.smarttarget.utils.TcmUri publication,
List<String> regions)
Initializes a QueryBuilder for the given universe, locale, Publication and regions.
|
QueryBuilder(com.tridion.smarttarget.utils.TcmUri publication,
List<String> regions)
Initializes a QueryBuilder for the given Publication and regions.
|
Modifier and Type | Method and Description |
---|---|
QueryBuilder |
addCriteria(com.tridion.smarttarget.query.builder.Criteria criteria)
Adds a Criteria object to the QueryBuilder.
|
void |
addListener(QueryListener listener)
Adds a listener.
|
void |
clearAllCriteria()
Clears All criteria.
|
com.tridion.smarttarget.query.ResultSet |
execute()
Executes the query and returns a
ResultSet exposing the Promotions, search results and navigation links. |
String |
getContextPublicationUri()
Gets the URI of the context Publication, where the resulting items will be rendered.
|
Date |
getCurrentDate()
Gets the 'currentDate' that will be used to trigger on.
|
String |
getLocale()
Gets the locale.
|
List<com.tridion.smarttarget.query.builder.Criteria> |
getLocationCriteria()
Gets all location based criteria.
|
int |
getMaxItems()
Gets the number of Items that will be returned from a SmartTarget query.
|
List<com.tridion.smarttarget.query.builder.Criteria> |
getParameterCriteria()
Gets all parameter based criteria.
|
ParameterCriteria |
getParameterCriteriaByName(String parameterName)
Gets the ParameterCriteria from the list for the specified name.
|
List<com.tridion.smarttarget.query.builder.Criteria> |
getSearchCriteria()
Gets all search based criteria.
|
int |
getStartIndex()
Gets the start index of Items that will be returned from a SmartTarget query.
|
int |
getTimeout()
Gets the timeout in milliseconds for the SmartTarget query.
|
String |
getUniverse()
Gets the Universe.
|
boolean |
isParsedFromNavigationLink()
Indicates if this QueryBuilder object has been constructed from a Fredhopper Link.
|
void |
parseQueryString(String query)
Parses a query.
|
void |
parseQueryString(String queryString,
boolean clearCriteria)
Parses a query and adds
Criteria objects to the QueryBuilder. |
QueryBuilder |
removeCriteria(com.tridion.smarttarget.query.builder.Criteria criteria)
Removes a Criteria object.
|
void |
removeListener(QueryListener listener)
Removes a listener.
|
void |
setCurrentDate(Date value)
Overrides the 'currentDate' that will be used to trigger on.
|
void |
setMaxItems(int value)
Sets the number of Items that will be returned from a SmartTarget query.
|
void |
setStartIndex(int value)
Sets the start index of Items that will be returned from a SmartTarget query.
|
void |
setTimeout(int timeoutInMilliseconds)
Sets the timeout in milliseconds for the SmartTarget query.
|
String |
toString()
Returns the current query, represented as a URL query string.
|
public QueryBuilder()
Initializes a QueryBuilder.
Gets the universe and locale from the configuration.
public QueryBuilder(String universe, String locale)
Initializes a QueryBuilder for the specified universe and locale.
universe
- the universe where this query executes.locale
- the locale for which this query executes.public QueryBuilder(com.tridion.smarttarget.utils.TcmUri publication, List<String> regions) throws com.tridion.smarttarget.SmartTargetException
Initializes a QueryBuilder for the given Publication and regions.
Gets the universe and locale from the configuration.publication
- The TcmUri of the Publication.regions
- a list of regions.com.tridion.smarttarget.SmartTargetException
public QueryBuilder(String universe, String locale, com.tridion.smarttarget.utils.TcmUri publication, List<String> regions) throws com.tridion.smarttarget.SmartTargetException
Initializes a QueryBuilder for the given universe, locale, Publication and regions.
The Publication and regions are added as PublicationCriteria and RegionCriteria.
Results will be 'scoped' for this publication.
Example:
String universe = "catalog01";
String locale = "en_US";
TcmUri publicationId = new TcmUri("tcm:0-3-1");
TcmUri pageId = new TcmUri("tcm:3-6409-64");
String regionHeader = "Header";
String regionFooter = "Footer";
QueryBuilder builder = new QueryBuilder(universe, locale, publicationId, Arrays.asList(regionHeader,
regionFooter));
ResultSet resultSet = builder.execute();
universe
- the universe where this query executes.locale
- the locale for which this query executes.publication
- The TcmUri of the Publication.regions
- a list of regions.com.tridion.smarttarget.SmartTargetException
public void clearAllCriteria()
Clears All criteria.
public QueryBuilder addCriteria(com.tridion.smarttarget.query.builder.Criteria criteria) throws com.tridion.smarttarget.SmartTargetException
Adds a Criteria object to the QueryBuilder.
criteria
- A concrete subclass of Criteria to add to the QueryBuilder.com.tridion.smarttarget.SmartTargetException
public QueryBuilder removeCriteria(com.tridion.smarttarget.query.builder.Criteria criteria)
Removes a Criteria object.
criteria
- The Criteria to remove.public List<com.tridion.smarttarget.query.builder.Criteria> getLocationCriteria()
Gets all location based criteria.
Criteria
objects.public List<com.tridion.smarttarget.query.builder.Criteria> getParameterCriteria()
Gets all parameter based criteria.
Criteria
objects.public List<com.tridion.smarttarget.query.builder.Criteria> getSearchCriteria()
Gets all search based criteria.
Criteria
objects.public Date getCurrentDate()
Gets the 'currentDate' that will be used to trigger on.
public void setCurrentDate(Date value)
Overrides the 'currentDate' that will be used to trigger on.
value
- The Date to override the default 'currentDate' with.public ParameterCriteria getParameterCriteriaByName(String parameterName)
Gets the ParameterCriteria from the list for the specified name. Null if no ParameterCriteria with that name was found.
parameterName
- The name of the ParameterCriteria to get.public void parseQueryString(String query) throws com.tridion.smarttarget.SmartTargetException
Parses a query.
query
- The query string to parse.com.tridion.smarttarget.SmartTargetException
- when query string is not parsable.public void parseQueryString(String queryString, boolean clearCriteria) throws com.tridion.smarttarget.SmartTargetException
Parses a query and adds Criteria
objects to the QueryBuilder.
This method can be used to add triggers found on the Ambient Data Framework to the query as
CustomParameterCriteria
objects.
Example:
TcmUri publicationId = new TcmUri("tcm:0-3-1");
TcmUri pageId = new TcmUri("tcm:3-6409-64");
String regionHeader = "Header";
QueryBuilder builder = new QueryBuilder(publicationId, Arrays.asList(regionHeader));
builder.addCriteria(new PageCriteria(pageId));
ClaimStore claimStore = AmbientDataContext.getCurrentClaimStore();
if(claimStore != null) {
String triggers = AmbientDataHelper.getTriggers(claimStore);
builder.parseQueryString(triggers);
}
ResultSet resultSet = builder.execute();
queryString
- The query string to parse.clearCriteria
- When set to true clears any Criteria before parsing.com.tridion.smarttarget.SmartTargetException
- when string is not parsable.public com.tridion.smarttarget.query.ResultSet execute() throws com.tridion.smarttarget.SmartTargetException
Executes the query and returns a ResultSet
exposing the Promotions, search results and navigation links.
ResultSet
.com.tridion.smarttarget.SmartTargetException
public String toString()
Returns the current query, represented as a URL query string.
public String getUniverse()
Gets the Universe.
public String getLocale()
Gets the locale.
public String getContextPublicationUri()
Gets the URI of the context Publication, where the resulting items will be rendered.
This is used to ensure that the correct content is displayed for Promotions that return content from a parent Publication. Setting the context Publication is done by adding PublicationCriteria to the QueryBuilder.public int getStartIndex()
Gets the start index of Items that will be returned from a SmartTarget query.
public void setStartIndex(int value)
Sets the start index of Items that will be returned from a SmartTarget query.
Use this method in combination with setMaxItems before executing a query to implement paging through SmartTarget items. Example: A SmartTarget query requests data based on a Keyword. There are 100 Component Presentations related to that keyword. Calling setStartIndex(0) and setMaxItems(5) will result in the first up to fifth item to be returned. Calling setStartIndex(5) and setMaxItems(5) will result in the next five items to be returned. etc.
value
- The start index to use when retrieving items through a SmartTarget query.public int getMaxItems()
Gets the number of Items that will be returned from a SmartTarget query.
public void setMaxItems(int value)
Sets the number of Items that will be returned from a SmartTarget query.
Use this method in combination with setStartIndex before executing a query to implement paging through SmartTarget items. Example: A SmartTarget query requests data based on a Keyword. There are 100 Component Presentations related to that keyword. Calling setStartIndex(0) and setMaxItems(5) will result in the first up to fifth item to be returned. Calling setStartIndex(5) and setMaxItems(5) will result in the next five items to be returned. etc.
value
- The number of Items returned from a SmartTarget query.public int getTimeout()
Gets the timeout in milliseconds for the SmartTarget query.
public void setTimeout(int timeoutInMilliseconds)
Sets the timeout in milliseconds for the SmartTarget query.
timeoutInMilliseconds
- The timeout in milliseconds.public boolean isParsedFromNavigationLink()
Indicates if this QueryBuilder object has been constructed from a Fredhopper Link.
public void addListener(QueryListener listener)
Adds a listener.
listener
- The QueryListener
to add.public void removeListener(QueryListener listener)
Removes a listener.
listener
- The QueryListener
to remove.Copyright © 2021 RWS Group for and on behalf of its affiliates and subsidiaries. All rights reserved.