Search in sources :

Example 1 with SyndImage

use of com.rometools.rome.feed.synd.SyndImage in project nci-ctd2-dashboard by CBIIT.

the class RssController method generateFeed.

private String generateFeed(List<? extends DashboardEntity> dashboardEntities, String rssTitlePostfix, String rssDescription, String rssLink) {
    String dashboardUrl = context.getScheme() + "://" + context.getServerName() + context.getContextPath() + "/";
    // Set the stage and define metadata on the RSS feed
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("CTD^2 Dashboard - " + rssTitlePostfix);
    feed.setDescription(rssDescription);
    feed.setLink(rssLink);
    // We will have two major categories: Subject and Submission
    // and will have subcategories of Subject for each particular subject
    List<SyndCategory> categories = new ArrayList<SyndCategory>();
    SyndCategory subjectCategory = new SyndCategoryImpl();
    subjectCategory.setName("Subject");
    categories.add(subjectCategory);
    SyndCategory submissionCategory = new SyndCategoryImpl();
    submissionCategory.setName("Submission");
    categories.add(submissionCategory);
    feed.setCategories(categories);
    feed.setLanguage("en");
    feed.setManagingEditor("ocg@mail.nih.gov (CTD^2 Network)");
    SyndImage feedImg = new SyndImageImpl();
    feedImg.setTitle("CTD^2 Logo");
    feedImg.setUrl(dashboardUrl + "img/logos/ctd2_overall.png");
    feedImg.setLink(dashboardUrl);
    feed.setImage(feedImg);
    // And prepare the items to be put into the RSS
    List<SyndEntry> rssItems = new ArrayList<SyndEntry>();
    for (DashboardEntity entity : dashboardEntities) {
        if (entity instanceof Subject) {
            Subject subject = (Subject) entity;
            // Collect all role & submission pairs for this particular subject
            Map<Submission, Set<String>> roleMap = new HashMap<Submission, Set<String>>();
            Map<Submission, Set<ObservedSubject>> osMap = new HashMap<Submission, Set<ObservedSubject>>();
            List<ObservedSubject> sObservations = dashboardDao.findObservedSubjectBySubject((Subject) entity);
            for (ObservedSubject sObservation : sObservations) {
                String role = sObservation.getObservedSubjectRole().getSubjectRole().getDisplayName();
                Submission submission = sObservation.getObservation().getSubmission();
                Set<String> roles = roleMap.get(submission);
                if (roles == null) {
                    roles = new HashSet<String>();
                    roleMap.put(submission, roles);
                }
                roles.add(role);
                Set<ObservedSubject> oses = osMap.get(submission);
                if (oses == null) {
                    oses = new HashSet<ObservedSubject>();
                    osMap.put(submission, oses);
                }
                oses.add(sObservation);
            }
            // These will always belong to "Subject"
            List<SyndCategory> sCategories = new ArrayList<SyndCategory>();
            sCategories.add(subjectCategory);
            // and "Subject/<name>" categories
            SyndCategory sCategory = new SyndCategoryImpl();
            sCategory.setName(subjectCategory.getName() + "/" + entity.getDisplayName());
            categories.add(sCategory);
            // Also add this specific category to the global list
            sCategories.add(sCategory);
            // And now combine this information into individual items around submissions/subject
            for (Submission submission : roleMap.keySet()) {
                Set<String> roles = roleMap.get(submission);
                assert roles != null;
                Set<ObservedSubject> oses = osMap.get(submission);
                assert oses != null;
                SyndEntry item = new SyndEntryImpl();
                item.setCategories(sCategories);
                // Construct title
                ObservationTemplate observationTemplate = submission.getObservationTemplate();
                String description = observationTemplate.getDescription();
                Integer tier = observationTemplate.getTier();
                String combinedRoles = StringUtils.join(roles, ", ");
                String name = subject.getDisplayName();
                int noOfObservations = oses.size();
                StringBuilder title = new StringBuilder();
                title.append("Role '").append(combinedRoles).append("'").append(": ").append(description).append(" ").append("(").append("Tier ").append(tier).append(" - ").append(noOfObservations).append(" ").append(noOfObservations > 1 ? "observations" : "observation").append(" on ").append(name).append(")");
                item.setTitle(title.toString());
                String centerName = observationTemplate.getSubmissionCenter().getDisplayName();
                item.setAuthor(centerName);
                item.setPublishedDate(submission.getSubmissionDate());
                String submissionDescription = observationTemplate.getSubmissionDescription();
                StringBuilder itemDescStr = new StringBuilder();
                itemDescStr.append("<b>Project</b>: ").append(observationTemplate.getProject()).append("<br />");
                if (submissionDescription != null && !submissionDescription.isEmpty()) {
                    itemDescStr.append("<b>Summary</b>: ").append(submissionDescription);
                }
                SyndContent itemDesc = new SyndContentImpl();
                itemDesc.setType("text/html");
                itemDesc.setValue(itemDescStr.toString());
                item.setDescription(itemDesc);
                // Create a link to the subject page with filters enabled
                item.setLink(dashboardUrl + "#subject/" + subject.getId() + "/" + combinedRoles + "/" + tier);
                item.setUri(String.format("#ctd2#subject#%s#%d", name, title.hashCode() & Integer.MAX_VALUE));
                rssItems.add(item);
            }
        // End of subject-centered items
        } else if (entity instanceof Submission) {
            Submission submission = (Submission) entity;
            SyndEntry item = new SyndEntryImpl();
            ObservationTemplate observationTemplate = submission.getObservationTemplate();
            int noOfObservations = dashboardDao.findObservationsBySubmission(submission).size();
            StringBuilder title = new StringBuilder();
            Boolean isStory = observationTemplate.getIsSubmissionStory();
            title.append(isStory ? "Story" : "Submission").append(": ");
            title.append(observationTemplate.getDescription().replaceAll("\\.$", "")).append(" (").append("Tier ").append(observationTemplate.getTier()).append(" - ").append(noOfObservations).append(" ").append(noOfObservations > 1 ? "observations" : "observation").append(")");
            item.setTitle(title.toString());
            String submissionDescription = observationTemplate.getSubmissionDescription();
            StringBuilder itemDescStr = new StringBuilder();
            itemDescStr.append("<b>Project</b>: ").append(observationTemplate.getProject()).append("<br />");
            if (submissionDescription != null && !submissionDescription.isEmpty()) {
                itemDescStr.append("<b>Summary</b>: ").append(submissionDescription);
            }
            SyndContent itemDesc = new SyndContentImpl();
            itemDesc.setType("text/html");
            itemDesc.setValue(itemDescStr.toString());
            item.setDescription(itemDesc);
            item.setPublishedDate(submission.getSubmissionDate());
            String centerName = observationTemplate.getSubmissionCenter().getDisplayName();
            item.setAuthor(centerName);
            item.setLink(dashboardUrl + "#submission/" + submission.getId());
            item.setUri(String.format("ctd2#submission#%d", title.hashCode() & Integer.MAX_VALUE));
            rssItems.add(item);
        }
    // End of submission item(s)
    }
    // Add all items into the feed
    feed.setEntries(rssItems);
    // And print the XML/RSS version out
    SyndFeedOutput feedOutput = new SyndFeedOutput();
    String feedStr = null;
    try {
        feedStr = feedOutput.outputString(feed, true);
    } catch (FeedException e) {
        e.printStackTrace();
    }
    return feedStr;
}
Also used : SyndCategory(com.rometools.rome.feed.synd.SyndCategory) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) DashboardEntity(gov.nih.nci.ctd2.dashboard.model.DashboardEntity) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) SyndImage(com.rometools.rome.feed.synd.SyndImage) ObservationTemplate(gov.nih.nci.ctd2.dashboard.model.ObservationTemplate) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) Submission(gov.nih.nci.ctd2.dashboard.model.Submission) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) Subject(gov.nih.nci.ctd2.dashboard.model.Subject) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject) SyndCategoryImpl(com.rometools.rome.feed.synd.SyndCategoryImpl) SyndContent(com.rometools.rome.feed.synd.SyndContent) ObservedSubject(gov.nih.nci.ctd2.dashboard.model.ObservedSubject)

Example 2 with SyndImage

use of com.rometools.rome.feed.synd.SyndImage in project coffeenet-frontpage-plugin-rss by coffeenet.

the class FeedParserTest method parseBlogRss.

@Test
public void parseBlogRss() throws FeedException, IOException {
    SyndContentImpl description = new SyndContentImpl();
    description.setValue("description");
    SyndEntryImpl syndEntry = new SyndEntryImpl();
    syndEntry.setDescription(description);
    syndEntry.setLink("link");
    syndEntry.setAuthor("author");
    syndEntry.setTitle("title");
    syndEntry.setPublishedDate(getDate());
    SyndImage syndImage = new SyndImageImpl();
    syndImage.setUrl("some-url");
    syndImage.setLink("some-link");
    SyndFeed result = new SyndFeedImpl();
    result.setEntries(singletonList(syndEntry));
    result.setImage(syndImage);
    when(feedFactory.build(any(URL.class))).thenReturn(result);
    FeedDto feedDto = sut.parse("http://blog/feed/", 10, 150);
    final List<FeedEntryDto> blogEntries = feedDto.getEntries();
    assertThat(blogEntries, hasSize(1));
    assertThat(blogEntries.get(0).getDescription(), is("description"));
    assertThat(blogEntries.get(0).getLink(), is("link"));
    assertThat(blogEntries.get(0).getUserSeenPublishedDate(), is("3. December 2014"));
    assertThat(blogEntries.get(0).getGregorianPublishedDate(), is("2014-12-03 10:15"));
    assertThat(blogEntries.get(0).getAuthor(), is("author"));
    assertThat(blogEntries.get(0).getTitle(), is("title"));
    final FeedImageDto image = feedDto.getImage();
    assertThat(image.getUrl(), is("some-url"));
    assertThat(image.getLink(), is("some-link"));
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) SyndImage(com.rometools.rome.feed.synd.SyndImage) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) URL(java.net.URL) Test(org.junit.Test)

Example 3 with SyndImage

use of com.rometools.rome.feed.synd.SyndImage in project moera-node by MoeraOrg.

the class RssController method rss.

@GetMapping("/rss")
@Transactional
@ResponseBody
public SyndFeed rss() {
    RequestContext rcp = requestContext.getPublic();
    PublicPage publicPage = publicPageRepository.findContaining(rcp.nodeId(), Long.MAX_VALUE);
    List<Story> stories = Collections.emptyList();
    if (publicPage != null) {
        stories = storyRepository.findInRange(rcp.nodeId(), Feed.TIMELINE, publicPage.getAfterMoment(), publicPage.getBeforeMoment()).stream().filter(t -> t.getEntry().isMessage()).sorted(Collections.reverseOrder(Comparator.comparingLong(Story::getMoment))).collect(Collectors.toList());
    }
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    String name = rcp.fullName() != null ? rcp.fullName() : rcp.nodeName();
    String title = !ObjectUtils.isEmpty(name) ? name + " - Moera" : "Moera";
    feed.setTitle(title);
    feed.setLink(rcp.getSiteUrl() + "/");
    if (rcp.avatarId() != null) {
        SyndImage image = new SyndImageImpl();
        image.setTitle(title);
        image.setUrl(rcp.getSiteUrl() + "/moera/media/" + new AvatarImage(rcp.getAvatar()).getPath());
        image.setLink(rcp.getSiteUrl() + "/");
        feed.setImage(image);
    }
    feed.setDescription(title);
    feed.setLanguage("en-us");
    feed.setPublishedDate(!stories.isEmpty() ? stories.get(0).getCreatedAt() : Util.now());
    feed.setGenerator("moera-node");
    feed.setWebMaster(buildWebmaster());
    feed.setEntries(stories.stream().map(this::buildEntry).collect(Collectors.toList()));
    return feed;
}
Also used : SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) Util(org.moera.node.util.Util) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) Inject(javax.inject.Inject) Story(org.moera.node.data.Story) StoryRepository(org.moera.node.data.StoryRepository) SyndImage(com.rometools.rome.feed.synd.SyndImage) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) GetMapping(org.springframework.web.bind.annotation.GetMapping) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) Transactional(javax.transaction.Transactional) Feed(org.moera.node.data.Feed) EntryRevision(org.moera.node.data.EntryRevision) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) ObjectUtils(org.springframework.util.ObjectUtils) PublicPageRepository(org.moera.node.data.PublicPageRepository) SyndContent(com.rometools.rome.feed.synd.SyndContent) Entry(org.moera.node.data.Entry) RequestContext(org.moera.node.global.RequestContext) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) AvatarImage(org.moera.node.model.AvatarImage) List(java.util.List) PublicPage(org.moera.node.data.PublicPage) Comparator(java.util.Comparator) Collections(java.util.Collections) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndImageImpl(com.rometools.rome.feed.synd.SyndImageImpl) SyndImage(com.rometools.rome.feed.synd.SyndImage) SyndFeedImpl(com.rometools.rome.feed.synd.SyndFeedImpl) RequestContext(org.moera.node.global.RequestContext) PublicPage(org.moera.node.data.PublicPage) AvatarImage(org.moera.node.model.AvatarImage) Story(org.moera.node.data.Story) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(javax.transaction.Transactional) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)3 SyndEntryImpl (com.rometools.rome.feed.synd.SyndEntryImpl)3 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)3 SyndFeedImpl (com.rometools.rome.feed.synd.SyndFeedImpl)3 SyndImage (com.rometools.rome.feed.synd.SyndImage)3 SyndImageImpl (com.rometools.rome.feed.synd.SyndImageImpl)3 SyndContent (com.rometools.rome.feed.synd.SyndContent)2 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndCategory (com.rometools.rome.feed.synd.SyndCategory)1 SyndCategoryImpl (com.rometools.rome.feed.synd.SyndCategoryImpl)1 FeedException (com.rometools.rome.io.FeedException)1 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)1 DashboardEntity (gov.nih.nci.ctd2.dashboard.model.DashboardEntity)1 ObservationTemplate (gov.nih.nci.ctd2.dashboard.model.ObservationTemplate)1 ObservedSubject (gov.nih.nci.ctd2.dashboard.model.ObservedSubject)1 Subject (gov.nih.nci.ctd2.dashboard.model.Subject)1 Submission (gov.nih.nci.ctd2.dashboard.model.Submission)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1