1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.generator;
35
36 import com.sun.syndication.feed.synd.SyndCategory;
37 import com.sun.syndication.feed.synd.SyndCategoryImpl;
38 import com.sun.syndication.feed.synd.SyndContent;
39 import com.sun.syndication.feed.synd.SyndContentImpl;
40 import com.sun.syndication.feed.synd.SyndEntry;
41 import com.sun.syndication.feed.synd.SyndEntryImpl;
42 import com.sun.syndication.feed.synd.SyndFeed;
43
44 import info.magnolia.cms.beans.config.ServerConfiguration;
45 import info.magnolia.cms.core.Content;
46 import info.magnolia.cms.core.HierarchyManager;
47 import info.magnolia.cms.core.ItemType;
48 import info.magnolia.cms.core.NodeData;
49 import info.magnolia.context.MgnlContext;
50 import info.magnolia.module.data.DataConsts;
51 import info.magnolia.module.data.DataModule;
52 import info.magnolia.module.rssaggregator.util.ContentMapper;
53 import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
54
55 import javax.jcr.RepositoryException;
56
57 import org.apache.commons.lang.StringUtils;
58
59 import static java.lang.String.*;
60 import java.util.ArrayList;
61 import java.util.Collection;
62 import java.util.List;
63
64
65
66
67
68
69 public class RSSModuleFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
70
71 private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
72
73 private MagnoliaTemplate magnoliaTemplate;
74
75 private String feedPath;
76
77
78
79
80
81
82
83
84 public RSSModuleFeedGenerator() {
85 this.magnoliaTemplate = new MagnoliaTemplate();
86 }
87
88 public void setFeedPath(String feedPath) {
89 this.feedPath = feedPath;
90 }
91
92 public void setFeedInfo(SyndFeed feed) {
93
94
95 HierarchyManager hm = MgnlContext.getHierarchyManager(DataModule.getRepository());
96 try {
97 Content feedDescr = hm.getContent(feedPath);
98 feed.setTitle(StringUtils.defaultIfEmpty(feedDescr.getNodeData("title").getString(), ""));
99 feed.setLink(ServerConfiguration.getInstance().getDefaultBaseUrl());
100 feed.setDescription(StringUtils.defaultIfEmpty(feedDescr.getNodeData("description").getString(), ""));
101 } catch (RepositoryException e) {
102 throw new FeedGenerationException("Failed to retrieve feed description from " + feedPath, e);
103 }
104 }
105
106 public List<SyndEntry> loadFeedEntries() {
107 String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
108 return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, new ItemType(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE), MAPPER);
109 }
110
111 @Override
112 public Object clone() throws CloneNotSupportedException {
113 return super.clone();
114 }
115
116 private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
117
118 public SyndEntry map(Content content) throws RepositoryException {
119 SyndEntry entry = new SyndEntryImpl();
120 entry.setAuthor(content.getNodeData("author").getString());
121 entry.setTitle(content.getNodeData("title").getString());
122 entry.setLink(content.getNodeData("link").getString());
123 if (content.getNodeData("pubDate").getDate() != null) {
124 entry.setPublishedDate(content.getNodeData("pubDate").getDate().getTime());
125 }
126 SyndContent description = new SyndContentImpl();
127 description.setType("text/html");
128 description.setValue(content.getNodeData("description").getString());
129 entry.setDescription(description);
130 List<SyndCategory> categories = new ArrayList<SyndCategory>();
131 Collection<NodeData> categoriesNodeData = content.getContent("categories").getNodeDataCollection();
132 for (NodeData nodeData : categoriesNodeData) {
133 SyndCategory category = new SyndCategoryImpl();
134 category.setName(nodeData.getString());
135 categories.add(category);
136 }
137 entry.setCategories(categories);
138 return entry;
139 }
140
141 }
142
143 }