Search in sources :

Example 6 with HippoBeanIterator

use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator 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 7 with HippoBeanIterator

use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator 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 8 with HippoBeanIterator

use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator 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)

Example 9 with HippoBeanIterator

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

the class Series method getLatestPublication.

public Publication getLatestPublication() throws HstComponentException, QueryException {
    if (!getShowLatest()) {
        return null;
    }
    HippoBean folder = getCanonicalBean().getParentBean();
    HippoBeanIterator hippoBeans = HstQueryBuilder.create(folder).ofTypes(Publication.class).orderByDescending("publicationsystem:NominalDate").build().execute().getHippoBeans();
    boolean found = false;
    Publication publication = null;
    while (!found && hippoBeans.hasNext()) {
        HippoBean hippoBean = hippoBeans.nextHippoBean();
        publication = (Publication) hippoBean;
        found = publication.isPubliclyAccessible();
    }
    if (found) {
        return publication;
    }
    return null;
}
Also used : HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator)

Aggregations

HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)9 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)4 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)4 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)4 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)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 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)2 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HstQueryManager (org.hippoecm.hst.content.beans.query.HstQueryManager)1 Constraint (org.hippoecm.hst.content.beans.query.builder.Constraint)1 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)1 BaseFilter (org.hippoecm.hst.content.beans.query.filter.BaseFilter)1 HippoDocument (org.hippoecm.hst.content.beans.standard.HippoDocument)1 DateTools (org.hippoecm.repository.util.DateTools)1 JSONObject (org.json.simple.JSONObject)1