Search in sources :

Example 11 with HstQuery

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

the class FinancialLinkedBeansComponent method doBeforeRender.

@Override
public void doBeforeRender(final HstRequest request, final HstResponse response) {
    super.doBeforeRender(request, response);
    // linkPath property contains the relative path of the links property in the published workflow document
    String linkPath = getComponentParameter("linkPath");
    final HstRequestContext context = request.getRequestContext();
    // we assume a Financial as content bean, thus expect a PublishedWork as "parent" document
    Financial financial = context.getContentBean(Financial.class);
    try {
        HstQuery linkedBeanQuery = ContentBeanUtils.createIncomingBeansQuery(financial, context.getSiteContentBaseBean(), linkPath, Publishedwork.class, false);
        // chapter cannot be re-used across different publishedworkflow
        linkedBeanQuery.setLimit(1);
        // linked documents will contain the publishedworkflow document containing all the chapters
        HstQueryResult linkedDocuments = linkedBeanQuery.execute();
        request.setAttribute("linkeddocuments", linkedDocuments);
    } catch (QueryException queryException) {
        log.warn("QueryException ", queryException);
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) Financial(uk.nhs.digital.website.beans.Financial) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 12 with HstQuery

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

the class CyberAlertResource method fetchCyberAlert.

@GET
@Path("/single/")
public CyberAlert fetchCyberAlert(@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse, @Context UriInfo uriInfo, @PathParam("threatid") String threatid) {
    CyberAlert cyberAlert = null;
    try {
        threatid = servletRequest.getParameter("threatid");
        if (threatid != null) {
            HstRequestContext requestContext = RequestContextProvider.get();
            HstQueryManager hstQueryManager = getHstQueryManager(requestContext.getSession(), requestContext);
            String mountContentPath = requestContext.getResolvedMount().getMount().getContentPath();
            Node mountContentNode = requestContext.getSession().getRootNode().getNode(PathUtils.normalizePath(mountContentPath));
            HstQuery hstQuery = hstQueryManager.createQuery(mountContentNode, CyberAlert.class);
            Filter filter = hstQuery.createFilter();
            filter.addEqualTo("website:threatid", threatid);
            hstQuery.setFilter(filter);
            hstQuery.setLimit(1);
            HstQueryResult result = hstQuery.execute();
            HippoBeanIterator iterator = result.getHippoBeans();
            if (iterator.hasNext()) {
                cyberAlert = (CyberAlert) iterator.nextHippoBean();
            } else {
                JSONObject json = new JSONObject();
                json.put("error", "The threatid=" + threatid + " is not found");
                servletResponse.resetBuffer();
                servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
                servletResponse.setHeader("Content-Type", "application/json");
                servletResponse.setCharacterEncoding("UTF-8");
                servletResponse.getWriter().write(json.toString());
                servletResponse.flushBuffer();
            }
        } else {
            JSONObject json = new JSONObject();
            json.put("error", "The URL is not correct. Use /single?threatid=<threatid>");
            servletResponse.resetBuffer();
            servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
            servletResponse.setHeader("Content-Type", "application/json");
            servletResponse.setCharacterEncoding("UTF-8");
            servletResponse.getWriter().write(json.toString());
            servletResponse.flushBuffer();
        }
    } catch (Exception queryException) {
        log.warn("QueryException ", queryException);
    }
    return cyberAlert;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) JSONObject(org.json.simple.JSONObject) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) Node(javax.jcr.Node) HstQueryManager(org.hippoecm.hst.content.beans.query.HstQueryManager) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 13 with HstQuery

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

the class CyberAlertResource method fetchAllThreatIds.

@GET
@Path("/getAllThreatIds/")
public ThreatIds fetchAllThreatIds(@Context HttpServletRequest request, @Context HttpServletResponse servletResponse) {
    ThreatIds threatId = new ThreatIds();
    List<ThreatIdDate> threatIdDateList = new ArrayList<ThreatIdDate>();
    try {
        final HstQuery query = createQuery(new DefaultRestContext(this, request), CyberAlert.class, Subtypes.INCLUDE);
        query.setLimit(100);
        final HstQueryResult result = query.execute();
        HippoBeanIterator iterator = result.getHippoBeans();
        while (iterator.hasNext()) {
            CyberAlert cyberAlert = (CyberAlert) iterator.nextHippoBean();
            List<Calendar> calList = new ArrayList<Calendar>();
            ThreatIdDate threDate = new ThreatIdDate();
            if (cyberAlert != null) {
                List<HippoBean> cyberAcknowledgementList = (List<HippoBean>) cyberAlert.getCyberAcknowledgements();
                for (HippoBean cyberAckn : cyberAcknowledgementList) {
                    if (cyberAckn instanceof CyberAcknowledgement) {
                        CyberAcknowledgement cybAck = (CyberAcknowledgement) cyberAckn;
                        calList.add(cybAck.getResponseDatetime());
                    }
                }
                threDate.setResponsedates(calList);
                threDate.setThreatid(cyberAlert.getThreatId());
            }
            threatIdDateList.add(threDate);
        }
    } catch (Exception e) {
        log.error("Error finding beans", e);
    }
    threatId.setThreatids(threatIdDateList);
    return threatId;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) CyberAcknowledgement(uk.nhs.digital.website.beans.CyberAcknowledgement) ThreatIds(uk.nhs.digital.website.beans.ThreatIds) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) ThreatIdDate(uk.nhs.digital.website.beans.ThreatIdDate) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 14 with HstQuery

use of org.hippoecm.hst.content.beans.query.HstQuery 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 15 with HstQuery

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

the class CommonFieldsBean method getInitialQuery.

protected <T extends HippoBean> HstQuery getInitialQuery(List<String> properties, Class<T> beanClass) throws HstComponentException, QueryException {
    final HstRequestContext context = RequestContextProvider.get();
    HstQuery query = ContentBeanUtils.createIncomingBeansQuery(this.getCanonicalBean(), context.getSiteContentBaseBean(), properties, beanClass, false);
    return query;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Aggregations

HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)26 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)18 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)15 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)12 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)7 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)4 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)4 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)4 Node (javax.jcr.Node)3 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)3 ValueList (org.onehippo.forge.selection.hst.contentbean.ValueList)3 NodeIterator (javax.jcr.NodeIterator)2 RepositoryException (javax.jcr.RepositoryException)2 Query (javax.jcr.query.Query)2 QueryManager (javax.jcr.query.QueryManager)2 QueryResult (javax.jcr.query.QueryResult)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)2 ObjectConverter (org.hippoecm.hst.content.beans.manager.ObjectConverter)2