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.templates.components;
35
36 import info.magnolia.cms.util.QueryUtil;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.jcr.util.PropertyUtil;
40 import info.magnolia.jcr.util.SessionUtil;
41 import info.magnolia.rendering.model.RenderingModel;
42 import info.magnolia.rendering.model.RenderingModelImpl;
43 import info.magnolia.rendering.template.RenderableDefinition;
44 import info.magnolia.templating.functions.TemplatingFunctions;
45
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.Date;
49 import java.util.List;
50
51 import javax.inject.Inject;
52 import javax.jcr.Node;
53 import javax.jcr.NodeIterator;
54 import javax.jcr.RepositoryException;
55
56 import org.apache.commons.lang.StringUtils;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60
61
62
63
64
65
66
67
68 public abstract class AbstractFeedModel<RD extends RenderableDefinition> extends RenderingModelImpl<RD> implements RenderingModel<RD> {
69 protected final static Logger log = LoggerFactory.getLogger(AbstractFeedModel.class);
70
71 public static final String ORDER_BY_PUBLICATION_DATE = "@pubDate";
72 public static final String ORDER_BY_TITLE_NAME = "@title";
73 public static final String SORT_ASCENDING = "ascending";
74 public static final String SORT_DESCENDING = "descending";
75
76 protected static final String AGGREGATOR_NODE = "/rssaggregator";
77 protected static final String DATA_WORKSPACE = "data";
78 protected static final String FEEDS_NODE = "feeds";
79
80 protected static final int MAX_RESULTS = 20;
81
82 protected final TemplatingFunctions templatingFunctions;
83
84 @Inject
85 public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
86 super(content, definition, parent);
87 this.templatingFunctions = templatingFunctions;
88 }
89
90
91
92
93
94 @SuppressWarnings("unused")
95 public Date convertPubDate(Object timestamp) {
96 if (timestamp == null) {
97 return null;
98 }
99 if (timestamp instanceof String) {
100 if (StringUtils.isEmpty((String) timestamp)) {
101 return null;
102 }
103 return new Date(Long.parseLong((String) timestamp));
104 }
105
106 return new Date((Long) timestamp);
107 }
108
109
110
111
112
113
114
115
116
117 public Collection<Node> getPlanetItems(String feedPath, String condition, String order, boolean limit) {
118 int maxResults = MAX_RESULTS;
119
120 List<Node> results = new ArrayList<Node>();
121 try {
122 if (limit && content.hasProperty("maxResults")) {
123 maxResults = (int) content.getProperty("maxResults").getLong();
124 }
125 String feedName = NodeUtil.getNodeByIdentifier(DATA_WORKSPACE, PropertyUtil.getString(content, "feed")).getName();
126
127 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + AGGREGATOR_NODE + "/" + feedName + "/" + feedPath + "])";
128 if (StringUtils.isNotBlank(condition)) {
129 sql += " " + condition;
130 }
131 if (StringUtils.isNotBlank(order)) {
132 sql += " " + order;
133 }
134 log.debug(sql);
135
136 NodeIterator nit = QueryUtil.search("data", sql);
137
138 int resCount = 0;
139 while (nit.hasNext()) {
140 resCount++;
141 Node statNode = nit.nextNode();
142 if (limit) {
143 if (resCount <= maxResults) {
144 results.add(statNode);
145 } else {
146 break;
147 }
148
149 } else {
150 results.add(statNode);
151 }
152 }
153 log.debug("returning node collection with {} items.", "" + results.size());
154 return results;
155 } catch (RepositoryException e) {
156 log.error(e.getLocalizedMessage(), e);
157 }
158 return null;
159 }
160
161
162
163
164
165
166
167 @SuppressWarnings("unused")
168 public String getFeedTitle(String feedLink) {
169 try {
170 if (content.hasProperty("feed")) {
171 Node rssParent = SessionUtil.getNodeByIdentifier(DATA_WORKSPACE, content.getProperty("feed").getString());
172 return getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "title");
173 }
174 } catch (RepositoryException e) {
175 log.error("Problem while fetching feed title for statistics: " + e.getMessage());
176 }
177 return null;
178 }
179
180
181
182
183
184
185
186
187
188
189 public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
190 String propertyValue = null;
191 try {
192 if (parentNode != null) {
193 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
194 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
195 + "and t." + searchProperty + "='" + searchValue + "'";
196
197
198 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
199
200 if (nit != null && nit.hasNext()) {
201 Node fsn = nit.nextNode();
202
203 if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
204 if (fsn.hasProperty(resultProperty)) {
205 propertyValue = fsn.getProperty(resultProperty).getString();
206 }
207 }
208 }
209 }
210 } catch (RepositoryException e) {
211 log.error("Problem while getting node property: " + e.getMessage());
212 }
213 return propertyValue;
214 }
215
216 public String getContextPath() {
217 return MgnlContext.getContextPath();
218 }
219
220 }