use of org.hippoecm.hst.content.beans.query.filter.Filter 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.filter.Filter in project hippo by NHS-digital-website.
the class EventsComponent method contributeAndFilters.
@Override
protected void contributeAndFilters(final List<BaseFilter> filters, final HstRequest request, final HstQuery query) {
// filter's documents such that the website:display is set to true
try {
Filter filter = query.createFilter();
filter.addEqualTo("website:display", true);
filters.add(filter);
} catch (FilterException var7) {
log.error("An exception occurred while trying to create a query filter showing document with display field on : {}", var7);
}
// fetching the selected types from the request
String[] selectedTypes = getSelectedTypes(request);
if (selectedTypes.length > 0) {
final Filter filter = query.createFilter();
for (String type : selectedTypes) {
try {
filter.addEqualTo("@website:type", type);
} catch (FilterException filterException) {
log.warn("Errors while adding event type filter {}", filterException);
}
}
filters.add(filter);
}
}
Aggregations