Search in sources :

Example 1 with QueryException

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

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

the class CyberAlertComponent method doBeforeRender.

@Override
public void doBeforeRender(HstRequest request, HstResponse response) {
    super.doBeforeRender(request, response);
    final CyberAlertComponentInfo componentParametersInfo = getComponentParametersInfo(request);
    final int configuredAlertSize = componentParametersInfo.getNumberOfAlertsToDisplay();
    request.setAttribute("title", componentParametersInfo.getTitle());
    try {
        final HippoBean baseContentBean = request.getRequestContext().getSiteContentBaseBean();
        final HippoBean cyberAlertScope = (HippoBean) request.getRequestContext().getObjectBeanManager().getObject(baseContentBean.getPath() + "/cyber-alerts");
        HstQueryBuilder builder = HstQueryBuilder.create(cyberAlertScope);
        HstQueryResult alertsQueryResult = builder.ofTypes(CyberAlert.class).orderByDescending("publicationsystem:NominalDate").build().execute();
        List<CyberAlert> alertsListToDisplay;
        if (alertsQueryResult != null && configuredAlertSize > 0) {
            alertsListToDisplay = createCyberAlertsList(configuredAlertSize, alertsQueryResult);
            request.setAttribute("cyberAlertList", alertsListToDisplay);
        }
    } catch (QueryException | ObjectBeanManagerException e) {
        LOGGER.error("Failed to execute Cyber Alerts Query ", e);
    }
}
Also used : HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) ObjectBeanManagerException(org.hippoecm.hst.content.beans.ObjectBeanManagerException) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) CyberAlertComponentInfo(uk.nhs.digital.common.components.info.CyberAlertComponentInfo) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 3 with QueryException

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

the class FeedHubComponent method getSupInfoFilters.

private void getSupInfoFilters(List<HippoBean> supInfoFeed) throws QueryException {
    Map<String, Long> yearFilters = supInfoFeed.stream().map(e -> (SupplementaryInformation) e).map(SupplementaryInformation::getPublishedDate).map(e -> e != null ? String.valueOf(e.get(Calendar.YEAR)) : "Unknown").collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    addFilter("Year", "year", yearFilters);
    if (yearFilters.size() > 0 && filterValues.get("year").length > 0) {
        Map<String, Long> monthFilters = supInfoFeed.stream().map(e -> (SupplementaryInformation) e).map(SupplementaryInformation::getPublishedDate).filter(Objects::nonNull).map(e -> new SimpleDateFormat("MMMMM").format(e.getTime())).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        addFilter("Month", "month", monthFilters);
    }
}
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 4 with QueryException

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

the class Publishedworkchapter method getPublishedWork.

public Publishedwork getPublishedWork() {
    final HstRequestContext context = RequestContextProvider.get();
    // Publishedworkchapter publishedWorkChapter = context.getContentBean(Publishedworkchapter.class);
    try {
        HstQuery linkedBeanQuery = ContentBeanUtils.createIncomingBeansQuery(this.getCanonicalBean(), context.getSiteContentBaseBean(), "website:links/@hippo:docbase", Publishedwork.class, false);
        linkedBeanQuery.setLimit(1);
        return (Publishedwork) linkedBeanQuery.execute().getHippoBeans().nextHippoBean();
    } catch (QueryException queryException) {
        log.warn("QueryException ", queryException);
    }
    return null;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Example 5 with QueryException

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

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