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.NodeTypes;
40 import info.magnolia.jcr.util.PropertyUtil;
41 import info.magnolia.jcr.util.SessionUtil;
42 import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
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 public RSSModuleFeedGenerator() {
83 this.magnoliaTemplate = new MagnoliaTemplate();
84 }
85
86
87
88
89 public void setFeedPath(String feedPath) {
90 this.feedPath = feedPath;
91 }
92
93 @Override
94 public void setFeedInfo(SyndFeed feed) {
95
96
97 String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
98 Node feedDescr = SessionUtil.getNode(NodeTypes.Content.NAME, feedPath);
99 if (feedDescr != null) {
100 feed.setTitle(PropertyUtil.getString(feedDescr, "title", ""));
101 feed.setDescription(PropertyUtil.getString(feedDescr, "description", ""));
102 } else {
103 feed.setTitle("");
104 feed.setDescription("");
105 }
106 }
107
108 @Override
109 public List<SyndEntry> loadFeedEntries() {
110 String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
111 return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, NodeTypes.Content.NAME, MAPPER);
112 }
113
114 @Override
115 public Object clone() throws CloneNotSupportedException {
116 return super.clone();
117 }
118
119 private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
120
121 @Override
122 public SyndEntry map(Node content) throws RepositoryException {
123 SyndEntry entry = new SyndEntryImpl();
124 entry.setAuthor(PropertyUtil.getString(content, "author"));
125 entry.setTitle(PropertyUtil.getString(content, "title"));
126 entry.setLink(PropertyUtil.getString(content, "link"));
127 if (content.hasProperty("pubDate")) {
128 entry.setPublishedDate(PropertyUtil.getProperty(content, "pubDate").getDate().getTime());
129 }
130 SyndContent description = new SyndContentImpl();
131 description.setType("text/html");
132 description.setValue(PropertyUtil.getString(content, "description"));
133 entry.setDescription(description);
134 List<SyndCategory> categories = new ArrayList<SyndCategory>();
135 if (content.hasNode("categories")) {
136 PropertyIterator categoriesNodeData = new JCRMgnlPropertiesFilteringNodeWrapper(content.getNode("categories")).getProperties();
137 while (categoriesNodeData.hasNext()) {
138 Property property = categoriesNodeData.nextProperty();
139 SyndCategory category = new SyndCategoryImpl();
140 category.setName(property.getString());
141 categories.add(category);
142 }
143 }
144 entry.setCategories(categories);
145 return entry;
146 }
147
148 }
149
150 }