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