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 [dataItemNode] 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 NodeIterator nit = QueryUtil.search("data", sql);
135
136 int resCount = 0;
137 while (nit.hasNext()) {
138 resCount++;
139 Node statNode = nit.nextNode();
140 if (limit) {
141 if (resCount <= maxResults) {
142 results.add(statNode);
143 } else {
144 break;
145 }
146
147 } else {
148 results.add(statNode);
149 }
150 }
151 return results;
152 } catch (RepositoryException e) {
153 log.error(e.getLocalizedMessage(), e);
154 }
155 return null;
156 }
157
158
159
160
161
162
163
164 @SuppressWarnings("unused")
165 public String getFeedTitle(String feedLink) {
166 try {
167 if (content.hasProperty("feed")) {
168 Node rssParent = SessionUtil.getNodeByIdentifier(DATA_WORKSPACE, content.getProperty("feed").getString());
169 return getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "title");
170 }
171 } catch (RepositoryException e) {
172 log.error("Problem while fetching feed title for statistics: " + e.getMessage());
173 }
174 return null;
175 }
176
177
178
179
180
181
182
183
184
185
186 public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
187 String propertyValue = null;
188 try {
189 if (parentNode != null) {
190 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
191 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
192 + "and t." + searchProperty + "='" + searchValue + "'";
193
194 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
195
196 if (nit != null && nit.hasNext()) {
197 Node fsn = nit.nextNode();
198
199 if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
200 if (fsn.hasProperty(resultProperty)) {
201 propertyValue = fsn.getProperty(resultProperty).getString();
202 }
203 }
204 }
205 }
206 } catch (RepositoryException e) {
207 log.error("Problem while getting node property: " + e.getMessage());
208 }
209 return propertyValue;
210 }
211
212 public String getContextPath() {
213 return MgnlContext.getContextPath();
214 }
215
216 }