Search in sources :

Example 16 with QueryException

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);
}
Also used : Pageable(org.onehippo.cms7.essentials.components.paging.Pageable) java.util(java.util) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) HstComponentException(org.hippoecm.hst.core.component.HstComponentException) HstRequest(org.hippoecm.hst.core.component.HstRequest) IteratorUtils.toList(org.apache.commons.collections.IteratorUtils.toList) ConstraintBuilder.or(org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.or) HstResponse(org.hippoecm.hst.core.component.HstResponse) SimpleDateFormat(java.text.SimpleDateFormat) ConstraintBuilder.and(org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.and) uk.nhs.digital.website.beans(uk.nhs.digital.website.beans) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) DateTools(org.hippoecm.repository.util.DateTools) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) RequestContextProvider(org.hippoecm.hst.container.RequestContextProvider) ParseException(java.text.ParseException) ConstraintBuilder.constraint(org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.constraint) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) SimpleDateFormat(java.text.SimpleDateFormat)

Example 17 with QueryException

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);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) Query(javax.jcr.query.Query) ObjectConverter(org.hippoecm.hst.content.beans.manager.ObjectConverter) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) FeedListComponentInfo(uk.nhs.digital.common.components.info.FeedListComponentInfo) ObjectBeanManagerException(org.hippoecm.hst.content.beans.ObjectBeanManagerException) QueryResult(javax.jcr.query.QueryResult) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) QueryManager(javax.jcr.query.QueryManager) ValueList(org.onehippo.forge.selection.hst.contentbean.ValueList) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Example 18 with QueryException

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 "";
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) FilterException(org.hippoecm.hst.content.beans.query.exceptions.FilterException) FeedListComponentInfo(uk.nhs.digital.common.components.info.FeedListComponentInfo) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter)

Example 19 with QueryException

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());
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) Query(javax.jcr.query.Query) ObjectConverter(org.hippoecm.hst.content.beans.manager.ObjectConverter) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ObjectBeanManagerException(org.hippoecm.hst.content.beans.ObjectBeanManagerException) QueryResult(javax.jcr.query.QueryResult) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) QueryManager(javax.jcr.query.QueryManager) ValueList(org.onehippo.forge.selection.hst.contentbean.ValueList) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Example 20 with QueryException

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 "";
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) FilterException(org.hippoecm.hst.content.beans.query.exceptions.FilterException) EssentialsEventsComponentInfo(org.onehippo.cms7.essentials.components.info.EssentialsEventsComponentInfo) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter)

Aggregations

QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)23 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)15 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)14 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)10 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)8 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)7 HstComponentException (org.hippoecm.hst.core.component.HstComponentException)6 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)5 java.util (java.util)4 IteratorUtils.toList (org.apache.commons.collections.IteratorUtils.toList)4 RequestContextProvider (org.hippoecm.hst.container.RequestContextProvider)4 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)4 ConstraintBuilder.constraint (org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.constraint)4 ConstraintBuilder.or (org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.or)4 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)4 HstRequest (org.hippoecm.hst.core.component.HstRequest)4 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3