Search in sources :

Example 1 with HstLink

use of org.hippoecm.hst.core.linking.HstLink in project hippo by NHS-digital-website.

the class RssModifier method modifyEntry.

@Override
public void modifyEntry(final HstRequestContext context, final Item entry, final HippoBean bean) {
    String scope = "";
    String requestPath = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    SimpleDateFormat lastUpdatedDateFormat = new SimpleDateFormat("dd MMM yyyy");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    if (bean instanceof Blog) {
        try {
            requestPath = context.getBaseURL().getRequestPath();
            final String statement = "/jcr:root/content/documents/corporate-website/feeds//*[@jcr:primaryType='feed:rss20descriptor']";
            Session session = context.getSession();
            final Query q = session.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
            final NodeIterator nodes = q.execute().getNodes();
            while (nodes.hasNext()) {
                Node node = nodes.nextNode();
                scope = node.getProperty("feed:scope").getString();
                if (requestPath.contains("/" + scope)) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        final Blog blogBean = (Blog) bean;
        final Date lastUpdated = blogBean.getLastModified();
        if (lastUpdated != null) {
            HstLink hstLink = context.getHstLinkCreator().create(bean, context);
            String blogPath = bean.getPath();
            if (hstLink != null && (StringUtils.isNotBlank(scope) && blogPath.contains("/" + scope)) || requestPath.contains("all-blog")) {
                List<Element> foreignMarkup = entry.getForeignMarkup();
                foreignMarkup.add(getElement("title", blogBean.getTitle()));
                Element lmElement = new Element("author");
                String emailAddress = "enquiries@nhsdigital.nhs.uk";
                StringBuilder strPerson = new StringBuilder();
                if (blogBean.getAuthors() != null && blogBean.getAuthors().size() > 0) {
                    Person person = (Person) blogBean.getAuthors().get(0);
                    if (person.getRoles() != null && person.getRoles().getContactdetails() != null) {
                        final ContactDetail contactdetails = person.getRoles().getContactdetails();
                        if (StringUtils.isBlank(contactdetails.getEmailaddress())) {
                            strPerson.append(emailAddress);
                        } else {
                            strPerson.append(contactdetails.getEmailaddress());
                        }
                        strPerson.append(" (" + contactdetails.getName() + ")");
                    }
                } else {
                    strPerson.append(emailAddress);
                    if (StringUtils.isNotBlank(blogBean.getAuthorName())) {
                        strPerson.append(" (" + blogBean.getAuthorName() + ")");
                    } else {
                        strPerson.append("(NHS Digital)");
                    }
                }
                lmElement.addContent(StringUtils.isNotBlank(strPerson.toString()) ? strPerson.toString() : "enquiries@nhsdigital.nhs.uk (NHS Digital)");
                foreignMarkup.add(lmElement);
                String category = blogBean.getCategories();
                if (StringUtils.isNotBlank(category)) {
                    foreignMarkup.add(getElement("category", category));
                }
                foreignMarkup.add(getElement("description", blogBean.getAllDescription()));
                Element guid = getElement("guid", blogBean.getGuid());
                guid.setAttribute("isPermaLink", "false");
                foreignMarkup.add(guid);
                foreignMarkup.add(getElement("pubDate", dateFormat.format(lastUpdated)));
                foreignMarkup.add(getElement("link", hstLink.toUrlForm(context, true)));
                Element source = getElement("source", "NHS Digital");
                source.setAttribute("url", "https://digital.nhs.uk");
                foreignMarkup.add(source);
                final HippoGalleryImageSetBean image = blogBean.getLeadImage();
                if (image != null) {
                    hstLink = context.getHstLinkCreator().create(image, context);
                    if (hstLink != null) {
                        foreignMarkup = entry.getForeignMarkup();
                        String imageUrl = hstLink.toUrlForm(context, true);
                        final Element element = getImageElement(imageUrl);
                        foreignMarkup.add(element);
                    }
                }
            }
        }
    } else if (bean instanceof News) {
        final News newsBean = (News) bean;
        List<Element> foreignMarkup = entry.getForeignMarkup();
        foreignMarkup.add(getElement("title", newsBean.getTitle()));
        ContactDetail contactDetail = newsBean.getMediacontact();
        String author = "NHS Digital";
        String emailAddress = "enquiries@nhsdigital.nhs.uk";
        if (contactDetail != null && StringUtils.isNotBlank(contactDetail.getName())) {
            author = contactDetail.getName();
            emailAddress = contactDetail.getEmailaddress();
        }
        foreignMarkup.add(getElement("author", (emailAddress + " (" + author + ")")));
        StringBuilder content = new StringBuilder();
        for (HippoBean sre : newsBean.getSections()) {
            String tempContent = new String();
            if (sre instanceof Section) {
                tempContent = ((Section) sre).getHtmlJson();
            } else if (sre instanceof EmphasisBox) {
                tempContent = ((EmphasisBox) sre).getBodyJson();
            } else if (sre instanceof Expander) {
                tempContent = ((Expander) sre).getContent().getContent();
            } else if (sre instanceof Quote) {
                tempContent = ((Quote) sre).getQuote().getContent();
            }
            content.append(tempContent);
        }
        foreignMarkup.add(getElement("description", content.toString()));
        String category = getNewsCategory(newsBean);
        if (StringUtils.isNotBlank(category)) {
            foreignMarkup.add(getElement("category", category));
        }
        Element guid = getElement("guid", newsBean.getCanonicalUUID());
        guid.setAttribute("isPermaLink", "false");
        foreignMarkup.add(guid);
        String formatted = dateFormat.format(newsBean.getLastModified());
        foreignMarkup.add(getElement("pubDate", formatted));
        HstLink hstLink = context.getHstLinkCreator().create(bean, context);
        String urlNews = hstLink.toUrlForm(context, true);
        foreignMarkup.add(getElement("link", urlNews));
        String tempUrl = urlNews.substring(0, urlNews.indexOf(context.getBaseURL().getHostName()));
        String finalUrl = tempUrl + context.getBaseURL().getHostName() + context.getBaseURL().getContextPath() + context.getHstLinkCreator().getBinariesPrefix();
        final LeadImageSection leadimagesection = newsBean.getLeadimagesection();
        if (leadimagesection != null && leadimagesection.getLeadImage() != null) {
            String imageUrl = finalUrl + leadimagesection.getLeadImage().getCanonicalHandlePath();
            Element imageElement = getImageElement(imageUrl);
            foreignMarkup.add(imageElement);
        }
        Element source = getElement("source", "NHS Digital");
        source.setAttribute("url", "https://digital.nhs.uk");
        foreignMarkup.add(source);
    } else if (bean instanceof Publication) {
        List<Element> foreignMarkup = entry.getForeignMarkup();
        final Publication publicationBean = (Publication) bean;
        String docPath = bean.getPath();
        if (docPath.contains("/publication-system/")) {
            foreignMarkup.add(getElement("title", publicationBean.getTitle()));
            foreignMarkup.add(getElement("author", "enquiries@nhsdigital.nhs.uk (NHS Digital)"));
            StringBuilder description = new StringBuilder();
            for (uk.nhs.digital.ps.beans.structuredText.Element tempDesc : publicationBean.getSummary().getElements()) {
                description.append(tempDesc);
            }
            foreignMarkup.add(getElement("description", description.toString()));
            String category = getCategory(publicationBean);
            if (StringUtils.isNotBlank(category)) {
                foreignMarkup.add(getElement("category", category));
            }
            Element guid = getElement("guid", publicationBean.getCanonicalUUID());
            guid.setAttribute("isPermaLink", "false");
            foreignMarkup.add(guid);
            String formatted = dateFormat.format(publicationBean.getLastModified());
            foreignMarkup.add(getElement("pubDate", formatted));
            HstLink hstLink = context.getHstLinkCreator().create(bean, context);
            String url = hstLink.toUrlForm(context, true);
            foreignMarkup.add(getElement("link", url));
            String tempUrl1 = url.substring(0, url.indexOf(context.getBaseURL().getHostName()));
            String finalUrl = tempUrl1 + context.getBaseURL().getHostName() + context.getBaseURL().getContextPath() + context.getHstLinkCreator().getBinariesPrefix();
            for (Infographic test : publicationBean.getKeyFactInfographics()) {
                if (test.getIcon() != null) {
                    foreignMarkup.add(getImageElement(finalUrl + test.getIcon().getCanonicalHandlePath()));
                }
            }
            Element source = getElement("source", "NHS Digital");
            source.setAttribute("url", "https://digital.nhs.uk");
            foreignMarkup.add(source);
        }
    } else if (bean instanceof CyberAlert) {
        List<Element> foreignMarkup = entry.getForeignMarkup();
        final CyberAlert cyberAlertBean = (CyberAlert) bean;
        foreignMarkup.add(getElement("title", cyberAlertBean.getThreatId() + " - " + cyberAlertBean.getTitle()));
        foreignMarkup.add(getElement("category", cyberAlertBean.getSeverity()));
        foreignMarkup.add(getElement("author", "enquiries@nhsdigital.nhs.uk (NHS Digital)"));
        foreignMarkup.add(getElement("description", "<p>Severity: " + cyberAlertBean.getSeverity() + "</p>" + " " + cyberAlertBean.getSummary().getContent() + "<p> " + cyberAlertBean.getShortsummary() + "</p><p> Updated: " + lastUpdatedDateFormat.format(cyberAlertBean.getLastModified()) + "</p>"));
        Element guid = getElement("guid", cyberAlertBean.getCanonicalUUID());
        guid.setAttribute("isPermaLink", "false");
        foreignMarkup.add(guid);
        String pubDate = dateFormat.format(cyberAlertBean.getPublishedDate().getTime());
        foreignMarkup.add(getElement("pubDate", pubDate));
        HstLink hstLink = context.getHstLinkCreator().create(bean, context);
        String url = hstLink.toUrlForm(context, true);
        foreignMarkup.add(getElement("link", url));
    }
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) Query(javax.jcr.query.Query) Node(javax.jcr.Node) HstLink(org.hippoecm.hst.core.linking.HstLink) Element(org.jdom2.Element) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) NodeIterator(javax.jcr.NodeIterator) HippoGalleryImageSetBean(org.hippoecm.hst.content.beans.standard.HippoGalleryImageSetBean) Publication(uk.nhs.digital.ps.beans.Publication) FilterException(org.hippoecm.hst.content.beans.query.exceptions.FilterException) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) SimpleDateFormat(java.text.SimpleDateFormat) Session(javax.jcr.Session)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)1 Node (javax.jcr.Node)1 NodeIterator (javax.jcr.NodeIterator)1 Session (javax.jcr.Session)1 Query (javax.jcr.query.Query)1 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)1 FilterException (org.hippoecm.hst.content.beans.query.exceptions.FilterException)1 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)1 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)1 HippoGalleryImageSetBean (org.hippoecm.hst.content.beans.standard.HippoGalleryImageSetBean)1 HstLink (org.hippoecm.hst.core.linking.HstLink)1 Element (org.jdom2.Element)1 Publication (uk.nhs.digital.ps.beans.Publication)1