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 static java.lang.String.format;
37
38 import info.magnolia.cms.beans.config.ServerConfiguration;
39 import info.magnolia.jcr.util.PropertyUtil;
40 import info.magnolia.jcr.util.SessionUtil;
41 import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
42 import info.magnolia.module.data.DataConsts;
43 import info.magnolia.module.data.DataModule;
44 import info.magnolia.module.rssaggregator.util.ContentMapper;
45 import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
46 import info.magnolia.objectfactory.Components;
47
48 import java.util.ArrayList;
49 import java.util.List;
50
51 import javax.jcr.Node;
52 import javax.jcr.Property;
53 import javax.jcr.PropertyIterator;
54 import javax.jcr.RepositoryException;
55
56 import com.sun.syndication.feed.synd.SyndCategory;
57 import com.sun.syndication.feed.synd.SyndCategoryImpl;
58 import com.sun.syndication.feed.synd.SyndContent;
59 import com.sun.syndication.feed.synd.SyndContentImpl;
60 import com.sun.syndication.feed.synd.SyndEntry;
61 import com.sun.syndication.feed.synd.SyndEntryImpl;
62 import com.sun.syndication.feed.synd.SyndFeed;
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 @Override
93 public void setFeedInfo(SyndFeed feed) {
94
95
96 String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
97 Node feedDescr = SessionUtil.getNode(DataModule.getRepository(), feedPath);
98 if (feedDescr != null) {
99 feed.setTitle(PropertyUtil.getString(feedDescr, "title", ""));
100 feed.setDescription(PropertyUtil.getString(feedDescr, "description", ""));
101 } else {
102 feed.setTitle("");
103 feed.setDescription("");
104 }
105 }
106
107 @Override
108 public List<SyndEntry> loadFeedEntries() {
109 String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
110 return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, DataConsts.MODULE_DATA_CONTENT_NODE_TYPE, MAPPER);
111 }
112
113 @Override
114 public Object clone() throws CloneNotSupportedException {
115 return super.clone();
116 }
117
118 private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
119
120 @Override
121 public SyndEntry map(Node content) throws RepositoryException {
122 SyndEntry entry = new SyndEntryImpl();
123 entry.setAuthor(PropertyUtil.getString(content, "author"));
124 entry.setTitle(PropertyUtil.getString(content, "title"));
125 entry.setLink(PropertyUtil.getString(content, "link"));
126 if (content.hasProperty("pubDate")) {
127 entry.setPublishedDate(PropertyUtil.getProperty(content, "pubDate").getDate().getTime());
128 }
129 SyndContent description = new SyndContentImpl();
130 description.setType("text/html");
131 description.setValue(PropertyUtil.getString(content, "description"));
132 entry.setDescription(description);
133 List<SyndCategory> categories = new ArrayList<SyndCategory>();
134 if (content.hasNode("categories")) {
135 PropertyIterator categoriesNodeData = new JCRMgnlPropertiesFilteringNodeWrapper(content.getNode("categories")).getProperties();
136 while (categoriesNodeData.hasNext()) {
137 Property property = categoriesNodeData.nextProperty();
138 SyndCategory category = new SyndCategoryImpl();
139 category.setName(property.getString());
140 categories.add(category);
141 }
142 }
143 entry.setCategories(categories);
144 return entry;
145 }
146
147 }
148
149 }