use of org.hippoecm.hst.content.beans.query.HstQuery in project hippo by NHS-digital-website.
the class FeedListComponent method addIntervalFilter.
/**
* This method is creating an xpath query that will be appended to the query produced by the EssentialListComponent.
* The main goal of this component is to query the interval compound and NOT the event documents: for this reason
* we need to customize the query manually to have this behavior
* <p>
* Copied from uk.nhs.digital.common.components.EventsComponent
*
* @param request needed to build the new query
* @return String containing the query for the interval compound
*/
protected String addIntervalFilter(final HstRequest request) {
final FeedListComponentInfo paramInfo = getComponentParametersInfo(request);
final String dateField = paramInfo.getDocumentDateField();
if (!Strings.isNullOrEmpty(dateField)) {
// filter list containing dates contraints
try {
HstQueryBuilder hstQueryBuider = HstQueryBuilder.create(request.getRequestContext().getSiteContentBaseBean());
hstQueryBuider.ofTypes("website:interval");
HstQuery hstQuery = hstQueryBuider.build();
List<BaseFilter> filters = new ArrayList<>();
// adding the interval date range constraint
addIntervalConstraint(filters, hstQuery, dateField, request);
if (paramInfo.getHidePastEvents()) {
try {
final Filter filter = hstQuery.createFilter();
filter.addGreaterOrEqualThan(dateField, Calendar.getInstance(), DateTools.Resolution.DAY);
filters.add(filter);
} catch (FilterException e) {
log.error("Error while creating query filter to hide past events using date field {}", dateField, e);
}
}
final Filter queryFilter = createQueryFilter(request, hstQuery);
if (queryFilter != null) {
filters.add(queryFilter);
}
// appling the filters on the hstQuery object
applyAndFilters(hstQuery, filters);
// Apply sort
if (paramInfo.getHidePastEvents()) {
hstQuery.addOrderByAscending(dateField);
} else {
// past events sorted descending order by date, most recent first
hstQuery.addOrderByDescending(dateField);
}
// removing existing filters the query, since it shouldn't include path, availability and query constraint
String intervalQueryString = hstQuery.getQueryAsString(true).replaceAll("\\(@hippo:paths='[^']*'\\)( and)?", "");
intervalQueryString = intervalQueryString.replaceAll("\\(@hippo:availability='[^']*'\\)( and)?", "");
intervalQueryString = intervalQueryString.replaceAll("not\\(@jcr:primaryType='nt:frozenNode'\\)( and)?", "");
// removing the first slash, since the string must be appended to an existing xpath query
return intervalQueryString.substring(1, intervalQueryString.length());
} catch (FilterException filterException) {
log.warn("Exceptions while adding event date range filter {} ", filterException, filterException);
} catch (QueryException queryException) {
log.warn("Exceptions while getting the string representation of the query {} ", queryException, queryException);
}
}
return "";
}
use of org.hippoecm.hst.content.beans.query.HstQuery in project hippo by NHS-digital-website.
the class EventsComponent method executeQuery.
@Override
protected <T extends EssentialsListComponentInfo> Pageable<HippoBean> executeQuery(HstRequest request, T paramInfo, HstQuery query) throws QueryException {
int pageSize = this.getPageSize(request, paramInfo);
int page = this.getCurrentPage(request);
query.setLimit(pageSize);
query.setOffset((page - 1) * pageSize);
this.applyExcludeScopes(request, query, paramInfo);
this.buildAndApplyFilters(request, query);
try {
// the query coming from the component is manually extended since it needs to consider intervals
String eventQueryString = query.getQueryAsString(true);
// appending the query containing filters the on the interval compound
String queryString = eventQueryString + addIntervalFilter(request);
HstRequestContext requestContext = request.getRequestContext();
QueryManager jcrQueryManager = requestContext.getSession().getWorkspace().getQueryManager();
Query jcrQuery = jcrQueryManager.createQuery(queryString, "xpath");
QueryResult queryResult = jcrQuery.execute();
ObjectConverter objectConverter = requestContext.getContentBeansTool().getObjectConverter();
NodeIterator it = queryResult.getNodes();
List parentNodes = new ArrayList();
List<String> parentPath = new ArrayList();
// For this reason this component needs to fetch the parent node
while (it.hasNext() && parentPath.size() < pageSize) {
Node interval = it.nextNode();
Node eventNode = interval.getParent();
if (eventNode.getPrimaryNodeType().isNodeType("website:event") && !parentPath.contains(eventNode.getPath())) {
parentPath.add(eventNode.getPath());
parentNodes.add(objectConverter.getObject(eventNode));
}
}
return this.getPageableFactory().createPageable(parentNodes, page, pageSize);
} catch (RepositoryException repositoryEx) {
throw new QueryException(repositoryEx.getMessage());
} catch (ObjectBeanManagerException converterEx) {
throw new QueryException(converterEx.getMessage());
}
}
use of org.hippoecm.hst.content.beans.query.HstQuery in project hippo by NHS-digital-website.
the class EventsComponent method addIntervalFilter.
/**
* This method is creating an xpath query that will be appended to the query produced by the EssentialListComponent.
* The main goal of this component is to query the interval compound and NOT the event documents: for this reason
* we need to customize the query manually to have this behavior
*
* @param request neeeded to build the new query
* @return String containing the query for the interval compound
*/
protected String addIntervalFilter(final HstRequest request) {
final EssentialsEventsComponentInfo paramInfo = getComponentParametersInfo(request);
final String dateField = paramInfo.getDocumentDateField();
if (!Strings.isNullOrEmpty(dateField)) {
// filter list containing dates contraints
try {
HstQueryBuilder hstQueryBuider = HstQueryBuilder.create(request.getRequestContext().getSiteContentBaseBean());
hstQueryBuider.ofTypes("website:interval");
HstQuery hstQuery = hstQueryBuider.build();
List<BaseFilter> filters = new ArrayList<>();
// adding the interval date range constraint
addIntervalConstraint(filters, hstQuery, dateField, request);
final Filter queryFilter = createQueryFilter(request, hstQuery);
if (queryFilter != null) {
filters.add(queryFilter);
}
// appling the filters on the hstQuery object
applyAndFilters(hstQuery, filters);
// Apply sort
if (paramInfo.getHidePastEvents()) {
hstQuery.addOrderByAscending(dateField);
} else {
// past events sorted descending order by date, most recent first
hstQuery.addOrderByDescending(dateField);
}
// removing existing filters the query, since it shouldn't include path, availability and query constraint
String intervalQueryString = hstQuery.getQueryAsString(true).replaceAll("\\(@hippo:paths='[^']*'\\)( and)?", "");
intervalQueryString = intervalQueryString.replaceAll("\\(@hippo:availability='[^']*'\\)( and)?", "");
intervalQueryString = intervalQueryString.replaceAll("not\\(@jcr:primaryType='nt:frozenNode'\\)( and)?", "");
// removing the first slash, since the string must be appended to an existing xpath query
return intervalQueryString.substring(1, intervalQueryString.length());
} catch (FilterException filterException) {
log.warn("Exceptions while adding event date range filter {} ", filterException);
} catch (QueryException queryException) {
log.warn("Exceptions while getting the string representation of the query {} ", queryException);
}
}
return "";
}
use of org.hippoecm.hst.content.beans.query.HstQuery in project hippo by NHS-digital-website.
the class Series method getRelatedDocuments.
protected <T extends HippoBean> List<T> getRelatedDocuments(String property, Class<T> beanClass) throws HstComponentException, QueryException {
final HstRequestContext context = RequestContextProvider.get();
HstQuery query = ContentBeanUtils.createIncomingBeansQuery(this.getCanonicalBean(), context.getSiteContentBaseBean(), property, beanClass, false);
return toList(query.execute().getHippoBeans());
}
use of org.hippoecm.hst.content.beans.query.HstQuery in project hippo by NHS-digital-website.
the class BloomreachSearchProvider method getBloomreachResults.
public Pageable<HippoBean> getBloomreachResults(String queryString, int pageSize, int currentPage, SearchArea searchArea) {
HstQuery query = getQuery(queryString, searchArea);
query.setLimit(pageSize);
query.setOffset((currentPage - 1) * pageSize);
if (queryString == null) {
query.addOrderByDescending("hippostdpubwf:lastModificationDate");
}
try {
final HstQueryResult execute = query.execute();
return pageableFactory.createPageable(execute.getHippoBeans(), execute.getTotalSize(), pageSize, currentPage);
} catch (QueryException e) {
LOG.error("Error running query: {}", e.getMessage());
LOG.debug("Query exception: ", e);
return null;
}
}
Aggregations