Search in sources :

Example 1 with HstQueryBuilder

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

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

the class CiBreadcrumbProvider method loadCiLandingBean.

/**
 * <p>
 * Query the repository to see if this document sits under one of the Clinical Indicator
 * folders, and if so load in the CiLanding bean (which is used to create the breadcrumb).
 * The CiLanding document has a property called urlNameOfContentFolder which links
 * the CiLanding page to the appropriate content folder
 * </p>
 */
private void loadCiLandingBean() {
    HstQueryBuilder queryBuilder = HstQueryBuilder.create(RequestContextProvider.get().getSiteContentBaseBean());
    final HstQueryResult hstQueryResult;
    Constraint[] constraints = Arrays.stream(currentDocumentBean.getPath().split("/")).map((pathSegment) -> constraint("publicationsystem:urlNameOfContentFolder").equalTo(pathSegment)).toArray(Constraint[]::new);
    final HstQuery query = queryBuilder.ofTypes(CiLanding.class).where(or(constraints)).build();
    try {
        hstQueryResult = query.execute();
    } catch (QueryException queryException) {
        throw new HstComponentException("Exception occurred during ci folder search.", queryException);
    }
    if (hstQueryResult.getHippoBeans().hasNext()) {
        isClinicalIndicator = true;
        ciLandingBean = (CiLanding) hstQueryResult.getHippoBeans().nextHippoBean();
    }
}
Also used : BreadcrumbItem(org.onehippo.forge.breadcrumb.om.BreadcrumbItem) java.util(java.util) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstComponentException(org.hippoecm.hst.core.component.HstComponentException) HstRequest(org.hippoecm.hst.core.component.HstRequest) ConstraintBuilder.or(org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.or) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HippoFolder(org.hippoecm.hst.content.beans.standard.HippoFolder) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) uk.nhs.digital.ps.beans(uk.nhs.digital.ps.beans) RequestContextProvider(org.hippoecm.hst.container.RequestContextProvider) ConstraintBuilder.constraint(org.hippoecm.hst.content.beans.query.builder.ConstraintBuilder.constraint) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) 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) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint) HstComponentException(org.hippoecm.hst.core.component.HstComponentException) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 3 with HstQueryBuilder

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

the class BloomreachSearchProvider method getQuery.

private HstQuery getQuery(String queryString, SearchArea searchArea) {
    HstQueryBuilder queryBuilder = HstQueryBuilder.create(RequestContextProvider.get().getSiteContentBaseBean());
    queryBuilder.ofTypes(DOCUMENT_TYPES_BY_SEARCH_AREA.getOrDefault(searchArea, DOCUMENT_TYPES_BY_SEARCH_AREA.get(SearchArea.ALL)));
    List<Constraint> constraintList = new ArrayList<>();
    constraintList.add(or(constraint("common:searchable").equalTo(true), constraint("common:searchable").notExists()));
    if (SearchArea.ALL.equals(searchArea) || SearchArea.NEWS.equals(searchArea)) {
        constraintList.add(or(constraint("intranet:typeofnews").notExists(), constraint("intranet:typeofnews").equalTo("permanent"), constraint("intranet:expirydate").greaterOrEqualThan(Calendar.getInstance(), Resolution.HOUR)));
    }
    final Optional<String> optionalSearchQuery = Optional.ofNullable(queryString);
    optionalSearchQuery.ifPresent(searchQuery -> {
        String queryIncWildcards = ComponentUtils.parseAndApplyWildcards(searchQuery);
        Constraint searchStringConstraint = or(constraint(".").contains(searchQuery), constraint(".").contains(queryIncWildcards));
        constraintList.add(searchStringConstraint);
    });
    return queryBuilder.where(and(constraintList.toArray(new Constraint[0]))).build();
}
Also used : HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint) ArrayList(java.util.ArrayList)

Example 4 with HstQueryBuilder

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

the class SearchComponent method buildQuery.

private HstQuery buildQuery(HstRequest request) {
    String queryParameter = getQueryParameter(request);
    Constraint searchStringConstraint = null;
    HstQueryBuilder queryBuilder = HstQueryBuilder.create(deriveScope(request));
    if (queryParameter != null) {
        String query = SearchInputParsingUtils.parse(queryParameter, true);
        String queryIncWildcards = ComponentUtils.parseAndApplyWildcards(queryParameter);
        searchStringConstraint = or(// forcing specific fields first: this will boost the weight of a hit fot those specific property
        constraint(".").contains(query), constraint(".").contains(queryIncWildcards), constraint("website:title").contains(query), constraint("website:summary").contains(query), constraint("publicationsystem:title").contains(query), constraint("publicationsystem:summary").contains(query), constraint("nationalindicatorlibrary:title").contains(query));
    }
    // register content classes
    deriveTypes(queryBuilder, request);
    String sortParam = getSortOption(request);
    switch(sortParam) {
        case SORT_DATE_KEY:
            queryBuilder.orderByDescending(PROPERTY_ORDERED_SEARCH_DATE, "nationalindicatorlibrary:assuranceDate", PROPERTY_SEARCH_RANK, HippoStdPubWfNodeType.HIPPOSTDPUBWF_LAST_MODIFIED_DATE);
            break;
        case SORT_RELEVANCE:
            // This is what we want for data and info - when we have tabs this will need to be made specific
            queryBuilder.orderByDescending(PROPERTY_SEARCH_RANK, "jcr:score", PROPERTY_ORDERED_SEARCH_DATE, "nationalindicatorlibrary:assuranceDate", HippoStdPubWfNodeType.HIPPOSTDPUBWF_LAST_MODIFIED_DATE);
            break;
        default:
            LOGGER.error("Unknown sort mode: " + sortParam);
            break;
    }
    return constructQuery(queryBuilder, searchStringConstraint);
}
Also used : HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint)

Example 5 with HstQueryBuilder

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

the class FeedHubComponent method getFeed.

private <T extends HippoBean> List<T> getFeed(HstRequest request) throws QueryException {
    final HstRequestContext context = request.getRequestContext();
    FeedHub feedHub = (FeedHub) context.getContentBean();
    HippoBean folder = feedHub.getParentBean();
    ArrayList<Constraint> constraints = new ArrayList<>();
    if ("Site-wide documents".equalsIgnoreCase(feedHub.getHubType())) {
        folder = RequestContextProvider.get().getSiteContentBaseBean();
        if (feedHub.getFeedType().equals("Cyber Alerts")) {
            constraints.add(constraint("website:publicallyaccessible").equalTo(true));
        } else if (!feedHub.getFeedType().equals("Supplementary information")) {
            constraints.add(constraint("website:display").equalTo(true));
        }
    }
    String dateField = "website:publisheddatetime";
    Class feedClass = null;
    switch(feedHub.getFeedType()) {
        case "News":
            feedClass = News.class;
            if (filterValues.get("year").length > 0) {
                Calendar newsDateFilter = Calendar.getInstance();
                newsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
                newsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
                constraints.add(constraint(dateField).equalTo(newsDateFilter, DateTools.Resolution.YEAR));
            }
            break;
        case "Events":
            feedClass = Event.class;
            dateField = "website:events/@website:startdatetime";
            if (filterValues.get("year").length > 0) {
                Calendar eventsDateFilter = Calendar.getInstance();
                eventsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
                eventsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
                constraints.add(constraint("website:events/website:startdatetime").equalTo(eventsDateFilter, DateTools.Resolution.YEAR));
                if (filterValues.get("month").length > 0) {
                    Integer month = getMonth(filterValues.get("month")[0]);
                    if (month != null) {
                        eventsDateFilter.set(Calendar.MONTH, month);
                        eventsDateFilter.set(Calendar.DAY_OF_MONTH, 1);
                        constraints.add(constraint("website:events/website:startdatetime").equalTo(eventsDateFilter, DateTools.Resolution.MONTH));
                    }
                }
            }
            if (filterValues.get("type[]").length > 0) {
                String[] types = filterValues.get("type[]");
                for (String type : types) {
                    constraints.add(constraint("website:type").equalTo(type));
                }
            }
            break;
        case "Cyber Alerts":
            feedClass = CyberAlert.class;
            dateField = "publicationsystem:NominalDate";
            if (filterValues.get("year").length > 0) {
                Calendar cyberAlertsDateFilter = Calendar.getInstance();
                cyberAlertsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
                cyberAlertsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
                constraints.add(constraint(dateField).equalTo(cyberAlertsDateFilter, DateTools.Resolution.YEAR));
            }
            if (filterValues.get("type[]").length > 0) {
                String[] types = filterValues.get("type[]");
                for (String type : types) {
                    constraints.add(constraint("website:threattype").equalTo(type));
                }
            }
            if (filterValues.get("severity").length > 0) {
                constraints.add(constraint("website:severity").equalTo(filterValues.get("severity")[0]));
            }
            break;
        case "Supplementary information":
            feedClass = SupplementaryInformation.class;
            dateField = "publicationsystem:NominalDate";
            if (filterValues.get("year").length > 0) {
                Calendar supplimentaryInfoDateFilter = Calendar.getInstance();
                String year = filterValues.get("year")[0];
                if (year.equals("Unknown")) {
                    constraints.add(constraint(dateField).notExists());
                } else {
                    supplimentaryInfoDateFilter.set(Calendar.YEAR, Integer.parseInt(year));
                    supplimentaryInfoDateFilter.set(Calendar.DAY_OF_YEAR, 1);
                    DateTools.Resolution dateResolution = DateTools.Resolution.YEAR;
                    if (filterValues.get("month").length > 0) {
                        Integer month = getMonth(filterValues.get("month")[0]);
                        if (month != null) {
                            supplimentaryInfoDateFilter.set(Calendar.MONTH, month);
                            supplimentaryInfoDateFilter.set(Calendar.DAY_OF_MONTH, 1);
                            dateResolution = DateTools.Resolution.MONTH;
                        }
                    }
                    constraints.add(constraint(dateField).equalTo(supplimentaryInfoDateFilter, dateResolution));
                }
            }
            break;
        default:
    }
    if (queryText != null && !queryText.isEmpty()) {
        constraints.add(or(constraint("website:title").contains(queryText), constraint("website:shortsummary").contains(queryText)));
    }
    HstQueryBuilder query = HstQueryBuilder.create(folder);
    query.where(and(constraints.toArray(new Constraint[0]))).ofTypes(feedClass);
    if (sort.equals("date-asc")) {
        query.orderByAscending(dateField);
    } else {
        query.orderByDescending(dateField);
    }
    HippoBeanIterator beanIterator = query.build().execute().getHippoBeans();
    return toList(beanIterator);
}
Also used : Constraint(org.hippoecm.hst.content.beans.query.builder.Constraint) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) DateTools(org.hippoecm.repository.util.DateTools) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

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