Search in sources :

Example 16 with HstRequestContext

use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.

the class ArchiveComponent 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 Archive archiveIndexDocument;
    if (contentBean.isHippoFolderBean()) {
        final List<Archive> archiveIndexDocuments = contentBean.getChildBeans(Archive.class);
        if (archiveIndexDocuments.size() != 1) {
            reportInvalidTarget(request, contentBean, archiveIndexDocuments.size());
            return;
        }
        archiveIndexDocument = archiveIndexDocuments.get(0);
    } else if (contentBean instanceof Archive) {
        archiveIndexDocument = (Archive) contentBean;
    } else {
        reportInvalidInvocation(request, contentBean);
        return;
    }
    request.setAttribute("archive", archiveIndexDocument);
    try {
        final HstQuery query = requestContext.getQueryManager().createQuery(archiveIndexDocument.getParentBean(), Publication.class, LegacyPublication.class);
        query.addOrderByDescending("publicationsystem:NominalDate");
        final HstQueryResult hstQueryResult = query.execute();
        request.setAttribute("publications", hstQueryResult.getHippoBeans());
    } catch (QueryException queryException) {
        log.error("Failed to find publications for archive " + archiveIndexDocument.getTitle(), queryException);
        reportDisplayError(request, archiveIndexDocument.getTitle());
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) Archive(uk.nhs.digital.ps.beans.Archive) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 17 with HstRequestContext

use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.

the class ApiEndpoint method getApiMasterParent.

public ApiMaster getApiMasterParent() {
    final HstRequestContext context = RequestContextProvider.get();
    try {
        HstQuery linkedBeanQuery = ContentBeanUtils.createIncomingBeansQuery(this.getCanonicalBean(), context.getSiteContentBaseBean(), "website:apiendpointgroups/website:apirequest/@hippo:docbase", ApiMaster.class, false);
        linkedBeanQuery.setLimit(1);
        HstQueryResult hstQueryResult = linkedBeanQuery.execute();
        HippoBeanIterator hippoBeanIterator = hstQueryResult.getHippoBeans();
        if (hippoBeanIterator.getSize() > 0) {
            return (ApiMaster) hippoBeanIterator.nextHippoBean();
        } else {
            return null;
        }
    } 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) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 18 with HstRequestContext

use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.

the class OrganisationDataResource method getOrg.

@GET
@Path("/")
public List<OdsOrganisation> getOrg(@Context HttpServletRequest request, @Context HttpServletResponse servletResponse, @QueryParam("orgName") String orgName) {
    if (odsResults == null) {
        try {
            log.debug("Loading ODS Data");
            HstRequestContext context = new DefaultRestContext(this, request).getRequestContext();
            QueryManager manager = context.getSession().getWorkspace().getQueryManager();
            Query jcrQuery = manager.createQuery("/jcr:root/content/assets//*[@jcr:primaryType='externalstorage:resource']", "xpath");
            QueryResult execute = jcrQuery.execute();
            NodeIterator iterator = execute.getNodes();
            while (iterator.hasNext()) {
                Node node = iterator.nextNode();
                if (node.getPath().contains("/content/assets/ODS_Data")) {
                    Value val = node.getProperty("jcr:data").getValue();
                    // convert JSON array to Java List
                    odsResults = new ObjectMapper().readValue(val.getString().replace("\n", ""), new TypeReference<List<OdsOrganisation>>() {
                    });
                    break;
                }
            }
        } catch (RepositoryException | JsonProcessingException e) {
            log.debug("Failed to load ODS Data ", e);
        }
    }
    List<OdsOrganisation> filterOrg = odsResults.stream().filter(b -> (b.getOrgName() + " " + b.getCode()).toUpperCase().contains(orgName.toUpperCase())).collect(Collectors.toList());
    return filterOrg;
}
Also used : DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) Logger(org.slf4j.Logger) OdsOrganisation(uk.nhs.digital.model.OdsOrganisation) Context(javax.ws.rs.core.Context) QueryManager(javax.jcr.query.QueryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) QueryResult(javax.jcr.query.QueryResult) Collectors(java.util.stream.Collectors) BaseRestResource(org.onehippo.cms7.essentials.components.rest.BaseRestResource) DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException) javax.ws.rs(javax.ws.rs) Node(javax.jcr.Node) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) OdsOrganisation(uk.nhs.digital.model.OdsOrganisation) RepositoryException(javax.jcr.RepositoryException) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 19 with HstRequestContext

use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.

the class Series method getRelatedDocuments.

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

Example 20 with HstRequestContext

use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.

the class CiLandingComponent method doBeforeRender.

@Override
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
    super.doBeforeRender(request, response);
    final HstRequestContext ctx = request.getRequestContext();
    request.setAttribute("document", ctx.getContentBean());
}
Also used : HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext)

Aggregations

HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)42 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)17 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)11 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)8 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)8 RepositoryException (javax.jcr.RepositoryException)6 Query (javax.jcr.query.Query)5 QueryManager (javax.jcr.query.QueryManager)5 QueryResult (javax.jcr.query.QueryResult)5 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)5 Node (javax.jcr.Node)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 NodeIterator (javax.jcr.NodeIterator)3 HstComponentException (org.hippoecm.hst.core.component.HstComponentException)3 ValueList (org.onehippo.forge.selection.hst.contentbean.ValueList)3 Collectors (java.util.stream.Collectors)2 IteratorUtils.toList (org.apache.commons.collections.IteratorUtils.toList)2 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)2 ObjectConverter (org.hippoecm.hst.content.beans.manager.ObjectConverter)2 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)2