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.rendering.model.RenderingModel;
40 import info.magnolia.rendering.model.RenderingModelImpl;
41 import info.magnolia.rendering.template.RenderableDefinition;
42 import info.magnolia.templating.functions.TemplatingFunctions;
43
44 import java.util.Date;
45
46 import javax.inject.Inject;
47 import javax.jcr.Node;
48 import javax.jcr.NodeIterator;
49 import javax.jcr.RepositoryException;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60
61
62
63 public abstract class AbstractFeedModel<RD extends RenderableDefinition> extends RenderingModelImpl<RD> implements RenderingModel<RD> {
64 protected final static Logger log = LoggerFactory.getLogger(AbstractFeedModel.class);
65
66 public static final String ORDER_BY_PUBLICATION_DATE = "@pubDate";
67 public static final String ORDER_BY_TITLE_NAME = "@title";
68 public static final String SORT_ASCENDING = "ascending";
69 public static final String SORT_DESCENDING = "descending";
70
71 protected static final String AGGREGATOR_NODE = "/rssaggregator";
72 protected static final String DATA_WORKSPACE = "data";
73 protected static final String FEEDS_NODE = "feeds";
74
75 protected static final int MAX_RESULTS = 20;
76
77 protected final TemplatingFunctions templatingFunctions;
78
79 @Inject
80 public AbstractFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
81 super(content, definition, parent);
82 this.templatingFunctions = templatingFunctions;
83 }
84
85
86
87
88
89 public Date convertPubDate(Object timestamp) {
90 if (timestamp == null) {
91 return null;
92 }
93 if (timestamp instanceof String) {
94 if (StringUtils.isEmpty((String) timestamp)) {
95 return null;
96 }
97 return new Date(Long.parseLong((String) timestamp));
98 }
99
100 return new Date((Long) timestamp);
101 }
102
103
104
105
106
107
108
109
110
111
112 public String getFeedProperty(Node parentNode, String subNode, String searchProperty, String searchValue, String resultProperty) {
113 String propertyValue = null;
114 try {
115 if (parentNode != null) {
116 String searchNode = AGGREGATOR_NODE + "/" + parentNode.getName() + "/" + subNode;
117 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([" + searchNode + "]) "
118 + "and t." + searchProperty + "='" + searchValue + "'";
119
120
121 NodeIterator nit = QueryUtil.search(DATA_WORKSPACE, sql);
122
123 if (nit != null && nit.hasNext()) {
124 Node fsn = nit.nextNode();
125
126 if (NodeUtil.getCollectionFromNodeIterator(nit).size() == 0) {
127 if (fsn.hasProperty(resultProperty)) {
128 propertyValue = fsn.getProperty(resultProperty).getString();
129 }
130 }
131 }
132 }
133 } catch (RepositoryException e) {
134 log.error("Problem while getting node property: " + e.getMessage());
135 }
136 return propertyValue;
137 }
138
139 public String getContextPath() {
140 return MgnlContext.getContextPath();
141 }
142
143 }