Search in sources :

Example 6 with HstQueryBuilder

use of org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder 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 7 with HstQueryBuilder

use of org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder 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 8 with HstQueryBuilder

use of org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder in project hippo by NHS-digital-website.

the class FolderComponent method findAllDocuments.

private HippoBeanIterator findAllDocuments(HippoBean scope) throws QueryException {
    HstQueryBuilder queryBuilder = HstQueryBuilder.create(scope);
    HstQuery hstQuery = queryBuilder.ofTypes(Publication.class, Series.class, Dataset.class, LegacyPublication.class).limit(20).offset(0).build();
    return hstQuery.execute().getHippoBeans();
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) LegacyPublication(uk.nhs.digital.ps.beans.LegacyPublication) Series(uk.nhs.digital.ps.beans.Series) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) Dataset(uk.nhs.digital.ps.beans.Dataset) LegacyPublication(uk.nhs.digital.ps.beans.LegacyPublication) Publication(uk.nhs.digital.ps.beans.Publication)

Aggregations

HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)8 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)4 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)4 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)4 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)3 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)2 FilterException (org.hippoecm.hst.content.beans.query.exceptions.FilterException)2 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)2 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)2 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)2 java.util (java.util)1 ArrayList (java.util.ArrayList)1 RequestContextProvider (org.hippoecm.hst.container.RequestContextProvider)1 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)1 ConstraintBuilder.constraint (org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.constraint)1 ConstraintBuilder.or (org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.or)1 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)1 HippoFolder (org.hippoecm.hst.content.beans.standard.HippoFolder)1 HstComponentException (org.hippoecm.hst.core.component.HstComponentException)1 HstRequest (org.hippoecm.hst.core.component.HstRequest)1