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