Search in sources :

Example 1 with Filter

use of org.hippoecm.hst.content.beans.query.filter.Filter 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 Filter

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

the class EventsComponent method addIntervalConstraint.

protected void addIntervalConstraint(final List filters, final HstQuery hstQuery, final String dateField, final HstRequest request) throws FilterException {
    Calendar calendar = Calendar.getInstance();
    int year = Integer.parseInt(DocumentUtils.findYearOrDefault(getSelectedYear(request), calendar.get(Calendar.YEAR)));
    final Filter filter = hstQuery.createFilter();
    calendar.set(Calendar.YEAR, year);
    filter.addBetween(dateField, calendar, calendar, DateTools.Resolution.YEAR);
    filters.add(filter);
}
Also used : BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) Filter(org.hippoecm.hst.content.beans.query.filter.Filter)

Example 3 with Filter

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

the class CommonFieldsBean method getRelatedEvents.

public List<Event> getRelatedEvents() throws HstComponentException, QueryException {
    List<String> linkPaths = new ArrayList<>();
    linkPaths.add("website:relatedDocuments/@hippo:docbase");
    linkPaths.add("website:peoplementioned/@hippo:docbase");
    Filter filter = getInitialQuery(linkPaths, Event.class).createFilter();
    Calendar today = Calendar.getInstance();
    filter.addGreaterOrEqualThan("website:events/website:enddatetime", today, DateTools.Resolution.DAY);
    List<BaseFilter> filters = new ArrayList<BaseFilter>();
    filters.add(filter);
    return getRelatedDocuments(linkPaths, NO_LIMIT, null, null, Event.class, filters);
}
Also used : Filter(org.hippoecm.hst.content.beans.query.filter.Filter) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter)

Example 4 with Filter

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

the class CommonFieldsBean method applyRestrictionsToQuery.

private void applyRestrictionsToQuery(HstQuery query, int limit, String orderBy, String orderDirection, List<BaseFilter> andFilters) {
    if (limit > NO_LIMIT) {
        query.setLimit(limit);
    }
    if (orderBy != null) {
        if (orderDirection == "ascending") {
            query.addOrderByAscending(orderBy);
        } else {
            query.addOrderByDescending(orderBy);
        }
    }
    // applying AND filters
    if (andFilters != null && andFilters.size() > 0) {
        Filter initFilter = (Filter) query.getFilter();
        Filter mainFilter = query.createFilter();
        mainFilter.addAndFilter(initFilter);
        for (BaseFilter filter : andFilters) {
            mainFilter.addAndFilter(filter);
        }
        query.setFilter(mainFilter);
    }
}
Also used : Filter(org.hippoecm.hst.content.beans.query.filter.Filter) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter)

Example 5 with Filter

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

the class ProjectUpdateFeedComponent method addOrganisationFilter.

private void addOrganisationFilter(List<BaseFilter> filters, HstRequest request, HstQuery query) {
    String organisationTitle = getSelectedOrganisationTitle(request);
    if (StringUtils.isNotBlank(organisationTitle)) {
        HippoBeanIterator organisationBeans;
        try {
            final HstRequestContext requestContext = request.getRequestContext();
            HstQuery orgQuery = requestContext.getQueryManager().createQuery(requestContext.getSiteContentBaseBean(), Organisation.class);
            Filter orgNameFilter = orgQuery.createFilter();
            orgNameFilter.addEqualToCaseInsensitive("website:title", organisationTitle);
            orgQuery.setFilter(orgNameFilter);
            organisationBeans = orgQuery.execute().getHippoBeans();
        } catch (QueryException e) {
            log.debug("Error finding organisation with title {}: {}", organisationTitle, e);
            return;
        }
        if (organisationBeans.getSize() == 0) {
            return;
        }
        String jcrQuery;
        if (organisationBeans.getSize() > 1) {
            StringBuilder jcrQueryBuilder = new StringBuilder(String.format("((website:organisation/@hippo:docbase = '%s')", ((HippoDocument) organisationBeans.nextHippoBean()).getCanonicalHandleUUID()));
            while (organisationBeans.hasNext()) {
                jcrQueryBuilder.append(String.format(" or (website:organisation/@hippo:docbase = '%s')", ((HippoDocument) organisationBeans.nextHippoBean()).getCanonicalHandleUUID()));
            }
            jcrQueryBuilder.append(")");
            jcrQuery = jcrQueryBuilder.toString();
        } else {
            jcrQuery = String.format("(website:organisation/@hippo:docbase = '%s')", ((HippoDocument) organisationBeans.nextHippoBean()).getCanonicalHandleUUID());
        }
        Filter filter = query.createFilter();
        filter.addJCRExpression(jcrQuery);
        filters.add(filter);
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HippoDocument(org.hippoecm.hst.content.beans.standard.HippoDocument) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) BaseFilter(org.hippoecm.hst.content.beans.query.filter.BaseFilter) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Aggregations

Filter (org.hippoecm.hst.content.beans.query.filter.Filter)17 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)15 FilterException (org.hippoecm.hst.content.beans.query.exceptions.FilterException)8 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)4 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)4 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)2 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)2 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)2 FeedListComponentInfo (uk.nhs.digital.common.components.info.FeedListComponentInfo)2 Month (java.time.Month)1 Node (javax.jcr.Node)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 HstQueryManager (org.hippoecm.hst.content.beans.query.HstQueryManager)1 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)1 HippoDocument (org.hippoecm.hst.content.beans.standard.HippoDocument)1 JSONObject (org.json.simple.JSONObject)1 EssentialsEventsComponentInfo (org.onehippo.cms7.essentials.components.info.EssentialsEventsComponentInfo)1