Search in sources :

Example 21 with HstQuery

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 "";
}
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 22 with HstQuery

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());
    }
}
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 23 with HstQuery

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 "";
}
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)

Example 24 with HstQuery

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());
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Example 25 with HstQuery

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;
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Aggregations

HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)26 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)18 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)15 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)12 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)7 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)4 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)4 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)4 Node (javax.jcr.Node)3 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)3 ValueList (org.onehippo.forge.selection.hst.contentbean.ValueList)3 NodeIterator (javax.jcr.NodeIterator)2 RepositoryException (javax.jcr.RepositoryException)2 Query (javax.jcr.query.Query)2 QueryManager (javax.jcr.query.QueryManager)2 QueryResult (javax.jcr.query.QueryResult)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)2 ObjectConverter (org.hippoecm.hst.content.beans.manager.ObjectConverter)2