Search in sources :

Example 11 with HstQueryResult

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

the class SeriesComponent method doBeforeRender.

@Override
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
    super.doBeforeRender(request, response);
    final HstRequestContext requestContext = request.getRequestContext();
    final HippoBean contentBean = requestContext.getContentBean();
    final Series seriesIndexDocument;
    if (contentBean.isHippoFolderBean()) {
        final List<Series> seriesIndexDocuments = contentBean.getChildBeans(Series.class);
        if (seriesIndexDocuments.size() != 1) {
            reportInvalidTarget(request, contentBean, seriesIndexDocuments.size());
            return;
        }
        seriesIndexDocument = seriesIndexDocuments.get(0);
    } else if (contentBean instanceof Series) {
        seriesIndexDocument = (Series) contentBean;
    } else {
        reportInvalidInvocation(request, contentBean);
        return;
    }
    /* Setting frequency & date naming map on request  */
    final ValueList frequencyValueList = SelectionUtil.getValueListByIdentifier("frequency", RequestContextProvider.get());
    if (frequencyValueList != null) {
        request.setAttribute("frequencyMap", SelectionUtil.valueListAsMap(frequencyValueList));
    }
    final ValueList dateNamingConvention = SelectionUtil.getValueListByIdentifier("datenamingconvention", RequestContextProvider.get());
    if (dateNamingConvention != null) {
        request.setAttribute("dateNamingMap", SelectionUtil.valueListAsMap(dateNamingConvention));
    }
    request.setAttribute("series", seriesIndexDocument);
    try {
        final HstQuery query = requestContext.getQueryManager().createQuery(seriesIndexDocument.getParentBean(), Publication.class, LegacyPublication.class);
        query.addOrderByDescending("publicationsystem:NominalDate");
        final HstQueryResult hstQueryResult = query.execute();
        List<PublicationBase> allPublications = toList(hstQueryResult.getHippoBeans());
        Map<Boolean, List<PublicationBase>> publicationByStatus = allPublications.stream().collect(Collectors.groupingBy(PublicationBase::isPubliclyAccessible));
        List<PublicationBase> livePublications = publicationByStatus.get(true);
        List<PublicationBase> upcomingPublications = publicationByStatus.get(false);
        // Want upcoming in reverse date order to the closest to now is first
        if (!isEmpty(upcomingPublications)) {
            Collections.reverse(upcomingPublications);
        }
        if (!seriesIndexDocument.getShowLatest() && !isEmpty(livePublications)) {
            livePublications.sort(DateComparator.COMPARATOR);
        }
        if (!isEmpty(livePublications) && seriesIndexDocument.getShowLatest()) {
            // removes first publication as the publication available from Series.latestPublication
            livePublications.remove(0);
        }
        request.setAttribute("upcomingPublications", upcomingPublications);
        List<Pair> pastPublicationsAndSeriesChanges = new ArrayList<>();
        for (PublicationBase publicationBase : livePublications) {
            Pair<String, PublicationBase> pair = new Pair("publication", publicationBase, publicationBase.getNominalPublicationDateCalendar());
            pastPublicationsAndSeriesChanges.add(pair);
        }
        if (seriesIndexDocument.getSeriesReplaces() != null) {
            SeriesReplaces seriesReplaces = seriesIndexDocument.getSeriesReplaces();
            if (seriesReplaces.getChangeDate() != null) {
                Pair<String, Series> pair = new Pair("replacedSeries", seriesReplaces, seriesReplaces.getChangeDate().getTime());
                pastPublicationsAndSeriesChanges.add(pair);
            }
        }
        pastPublicationsAndSeriesChanges.sort(Comparator.comparing(Pair::getDate, Comparator.reverseOrder()));
        request.setAttribute("pastPublicationsAndSeriesChanges", pastPublicationsAndSeriesChanges);
    } catch (QueryException queryException) {
        log.error("Failed to find publications for series " + seriesIndexDocument.getTitle(), queryException);
        reportDisplayError(request, seriesIndexDocument.getTitle());
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) ValueList(org.onehippo.forge.selection.hst.contentbean.ValueList) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) IteratorUtils.toList(org.apache.commons.collections.IteratorUtils.toList) ValueList(org.onehippo.forge.selection.hst.contentbean.ValueList) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Example 12 with HstQueryResult

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

the class HubNewsAndEvents method getLatestBlog.

public List<HippoBean> getLatestBlog() throws QueryException {
    HstRequestContext requestContext = RequestContextProvider.get();
    HippoBean scope = requestContext.getSiteContentBaseBean();
    HstQuery hstBlogQuery = HstQueryBuilder.create(scope).ofTypes(Blog.class).orderByDescending("website:dateofpublication").build();
    HstQueryResult blogResult = hstBlogQuery.execute();
    return toList(blogResult.getHippoBeans());
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 13 with HstQueryResult

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

the class HubNewsAndEvents method getLatestNewsArticles.

public List<HippoBean> getLatestNewsArticles() throws QueryException {
    HstRequestContext requestContext = RequestContextProvider.get();
    HippoBean scope = requestContext.getSiteContentBaseBean().getBean("news/latest-news");
    HstQuery hstNewsQuery = HstQueryBuilder.create(scope).ofTypes(News.class).orderByDescending("website:publisheddatetime").build();
    HstQueryResult newsResult = hstNewsQuery.execute();
    return toList(newsResult.getHippoBeans());
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 14 with HstQueryResult

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

the class BloomreachSearchProvider method getBloomreachResults.

public Pageable<HippoBean> getBloomreachResults(String queryString, int pageSize, int currentPage, SearchArea searchArea) {
    HstQuery query = getQuery(queryString, searchArea);
    query.setLimit(pageSize);
    query.setOffset((currentPage - 1) * pageSize);
    if (queryString == null) {
        query.addOrderByDescending("hippostdpubwf:lastModificationDate");
    }
    try {
        final HstQueryResult execute = query.execute();
        return pageableFactory.createPageable(execute.getHippoBeans(), execute.getTotalSize(), pageSize, currentPage);
    } catch (QueryException e) {
        LOG.error("Error running query: {}", e.getMessage());
        LOG.debug("Query exception: ", e);
        return null;
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 15 with HstQueryResult

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

the class PublicationBase method getDatasets.

public List<Dataset> getDatasets() throws HstComponentException {
    assertPropertyPermitted(PropertyKeys.DATASETS);
    HstQueryResult hstQueryResult;
    try {
        hstQueryResult = HstQueryBuilder.create(getParentBean()).ofTypes(Dataset.class).build().execute();
    } catch (QueryException queryException) {
        log.error("Failed to find datasets for publication " + getCanonicalPath(), queryException);
        throw new HstComponentException("Failed to find datasets for publication " + getCanonicalPath(), queryException);
    }
    ArrayList<Dataset> hippoBeans = Lists.newArrayList((Iterator) hstQueryResult.getHippoBeans());
    hippoBeans.sort(DocumentTitleComparator.COMPARATOR);
    return hippoBeans;
}
Also used : QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstComponentException(org.hippoecm.hst.core.component.HstComponentException) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Aggregations

HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)15 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)12 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)10 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)9 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)7 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)4 CyberAlert (uk.nhs.digital.website.beans.CyberAlert)3 Node (javax.jcr.Node)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)2 HstComponentException (org.hippoecm.hst.core.component.HstComponentException)2 java.util (java.util)1 IteratorUtils.toList (org.apache.commons.collections.IteratorUtils.toList)1 RequestContextProvider (org.hippoecm.hst.container.RequestContextProvider)1 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)1 HstQueryManager (org.hippoecm.hst.content.beans.query.HstQueryManager)1 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)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