Search in sources :

Example 1 with FilterException

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

the class RssModifier method modifyHstQuery.

@Override
public void modifyHstQuery(final HstRequestContext context, final HstQuery query, final RSS20FeedDescriptor descriptor) {
    try {
        String strQuery = query.getQueryAsString(true);
        if (strQuery.contains("jcr:primaryType=\'website:news\'")) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -3);
            Filter filter = query.createFilter();
            try {
                filter.addGreaterOrEqualThan("website:publisheddatetime", calendar, DateTools.Resolution.DAY);
                query.setFilter(filter);
            } catch (final FilterException exception) {
                exception.printStackTrace();
            }
        } else if (strQuery.contains("jcr:primaryType=\'publicationsystem:publication\'")) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -1);
            Filter filter = query.createFilter();
            try {
                filter.addGreaterOrEqualThan("publicationsystem:NominalDate", calendar, DateTools.Resolution.DAY);
                filter.addEqualTo("publicationsystem:PubliclyAccessible", true);
                query.setFilter(filter);
            } catch (final FilterException exception) {
                exception.printStackTrace();
            }
        }
    } catch (QueryException e) {
        e.printStackTrace();
    }
}
Also used : QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) FilterException(org.hippoecm.hst.content.beans.query.exceptions.FilterException)

Example 2 with FilterException

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

the class ProjectUpdateFeedComponent method addUpdateTypeFilter.

private void addUpdateTypeFilter(List<BaseFilter> filters, String updateType, HstQuery query) {
    if (StringUtils.isNotBlank(updateType)) {
        Filter filter = query.createFilter();
        try {
            filter.addEqualTo("website:typeofupdate", updateType);
            filters.add(filter);
        } catch (final FilterException exception) {
            log.error("Error trying to add type of update filter", exception);
        }
    }
}
Also used : 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)

Example 3 with FilterException

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

the class SupplementaryInformationHubComponent method contributeAndFilters.

@Override
protected void contributeAndFilters(List<BaseFilter> filters, HstRequest request, HstQuery query) throws FilterException {
    super.contributeAndFilters(filters, request, query);
    // Add year filter
    final String selectedYear = DocumentUtils.findYearOrDefault(getSelectedYear(request), Calendar.getInstance().get(Calendar.YEAR));
    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, Integer.parseInt(selectedYear));
    Filter yearFilter = query.createFilter();
    try {
        yearFilter.addEqualTo("publicationsystem:NominalDate", calendar, DateTools.Resolution.YEAR);
        filters.add(yearFilter);
    } catch (final FilterException exception) {
        log.error("Error trying to add year filter", exception);
    }
    // Add month filter
    final Month selectedMonth = findMonthOrNull(getSelectedMonth(request));
    if (Objects.nonNull(selectedMonth)) {
        calendar.set(Calendar.MONTH, calenderMonthOrdinate(selectedMonth));
        Filter monthFilter = query.createFilter();
        try {
            monthFilter.addEqualTo("publicationsystem:NominalDate", calendar, DateTools.Resolution.MONTH);
            filters.add(monthFilter);
        } catch (final FilterException exception) {
            log.error("Error trying to add year filter", exception);
        }
    }
}
Also used : Month(java.time.Month) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) FilterException(org.hippoecm.hst.content.beans.query.exceptions.FilterException)

Example 4 with FilterException

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

the class FeedListComponent method contributeAndFilters.

@Override
protected void contributeAndFilters(final List<BaseFilter> filters, final HstRequest request, final HstQuery query) {
    FeedListComponentInfo info = getComponentParametersInfo(request);
    // Filter for News
    if (info.getDocumentTypes().equals("website:news")) {
        if (info.getHideFutureItems()) {
            final String documentDateField = info.getDocumentDateField();
            if (!Strings.isNullOrEmpty(documentDateField)) {
                try {
                    Filter filter = query.createFilter();
                    filter.addLessOrEqualThan(documentDateField, Calendar.getInstance(), DateTools.Resolution.DAY);
                    filters.add(filter);
                } catch (FilterException e) {
                    log.error("An exception occurred while trying to create a query filter for hiding future items: {}", e, e);
                }
            }
        }
        query.addOrderByDescending("website:publisheddatetime");
        // filtering on 'Display on news hub and homepage' property, if it has been selected
        try {
            Filter filter = query.createFilter();
            filter.addEqualTo("website:display", true);
            filters.add(filter);
        } catch (FilterException var7) {
            // Exception is entered twice - once as a parameter for the string, and once as the actual exception thrown
            // see: http://www.slf4j.org/faq.html#paramException
            log.error("An exception occurred while trying to create a query filter showing document with display field on : {}", var7, var7);
        }
    }
    // Filter for Blogs
    if (info.getDocumentTypes().equals("website:blog")) {
        query.addOrderByDescending("website:dateofpublication");
    }
    // Filter for Events
    if (info.getDocumentTypes().equals("website:event")) {
        // 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, 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, filterException);
                }
            }
            filters.add(filter);
        }
    }
}
Also used : 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)

Example 5 with FilterException

use of org.hippoecm.hst.content.beans.query.exceptions.FilterException 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)

Aggregations

FilterException (org.hippoecm.hst.content.beans.query.exceptions.FilterException)8 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)8 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)7 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)3 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)2 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)2 FeedListComponentInfo (uk.nhs.digital.common.components.info.FeedListComponentInfo)2 Month (java.time.Month)1 EssentialsEventsComponentInfo (org.onehippo.cms7.essentials.components.info.EssentialsEventsComponentInfo)1