use of org.hippoecm.hst.content.beans.query.exceptions.QueryException in project hippo by NHS-digital-website.
the class FeedHubComponent method getEventFilters.
private void getEventFilters(List<HippoBean> eventFeed) throws QueryException {
Map<String, Long> yearFilters = eventFeed.stream().map(e -> (Event) e).flatMap(e -> e.getEvents().parallelStream().map(Interval::getStartdatetime).map(date -> String.valueOf(date.get(Calendar.YEAR))).distinct()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
addFilter("Year", "year", yearFilters);
if (yearFilters.size() > 0 && filterValues.get("year").length > 0) {
Map<String, Long> monthFilters = eventFeed.stream().map(e -> (Event) e).flatMap(e -> e.getEvents().parallelStream().map(Interval::getStartdatetime).map(date -> new SimpleDateFormat("MMMMM").format(date.getTime())).distinct()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
addFilter("Month", "month", monthFilters);
}
Map<String, Long> typeFilters = eventFeed.stream().map(e -> (Event) e).flatMap(e -> Arrays.stream(e.getType())).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
addFilter("Type", "type[]", typeFilters);
}
use of org.hippoecm.hst.content.beans.query.exceptions.QueryException in project hippo by NHS-digital-website.
the class FeedListComponent method executeQuery.
/**
* Copied from uk.nhs.digital.common.components.EventsComponent
* Added a check for the "website:event" doctype.
* Runs super if not,
* Super from org.onehippo.cms7.essentials.components.EssentialsListComponent;
*/
@Override
protected <T extends EssentialsListComponentInfo> Pageable<HippoBean> executeQuery(HstRequest request, T paramInfo, HstQuery query) throws QueryException {
final FeedListComponentInfo info = getComponentParametersInfo(request);
final String documentTypes = info.getDocumentTypes();
if (documentTypes.equals("website:event")) {
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());
}
} else {
return super.executeQuery(request, paramInfo, query);
}
}
use of org.hippoecm.hst.content.beans.query.exceptions.QueryException 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.exceptions.QueryException 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.exceptions.QueryException 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 "";
}
Aggregations