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.dam.templating.functions.DamTemplatingFunctions;
39 import info.magnolia.jcr.util.SessionUtil;
40 import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
41 import info.magnolia.module.rssaggregator.util.PagerUtil;
42 import info.magnolia.module.rssaggregator.util.PlanetUtil;
43 import info.magnolia.rendering.model.RenderingModel;
44 import info.magnolia.rendering.template.TemplateDefinition;
45 import info.magnolia.templating.functions.TemplatingFunctions;
46
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.Comparator;
51 import java.util.Date;
52 import java.util.List;
53
54 import javax.inject.Inject;
55 import javax.jcr.Node;
56 import javax.jcr.NodeIterator;
57 import javax.jcr.RepositoryException;
58
59 import org.apache.commons.lang.StringUtils;
60
61
62
63
64
65
66
67
68 public class PlanetFeedModel<RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
69
70 private static final String ARCHIVE_NODE = "planetData";
71 private static final String DATE_PROPERTY = "pubDate";
72 private final DamTemplatingFunctions damTemplatingFunction;
73
74 @Inject
75 public PlanetFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions, DamTemplatingFunctions damTemplatingFunction) {
76 super(content, definition, parent, templatingFunctions);
77 this.damTemplatingFunction = damTemplatingFunction;
78 }
79
80 @Override
81 public String execute() {
82 return "success";
83 }
84
85 public PagerUtil getPager() throws RepositoryException {
86 PagerUtil pager = null;
87 Collection<Node> listItems = getItems();
88 if (!listItems.isEmpty()) {
89
90 final String currentPageLink;
91 if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
92 currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
93 } else {
94 currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContent().getJCRNode());
95 }
96
97 pager = new PagerUtil(currentPageLink, listItems, content);
98 }
99 return pager;
100 }
101
102 public Collection<Node> getItems() {
103 List<Node> listItems = new ArrayList<Node>();
104
105 try {
106 if (content.hasProperty("link")) {
107 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
108
109 String sql2 = "select * from [mgnl:content] as t where ISDESCENDANTNODE([" + rssParent.getPath() + "/" + ARCHIVE_NODE
110 + "]) and t.hidden = 'false'";
111
112 NodeIterator nit = QueryUtil.search(RSSAggregatorConstants.WORKSPACE, sql2);
113 while (nit.hasNext()) {
114 Node post = nit.nextNode();
115 listItems.add(post);
116 }
117 sort(listItems);
118 } else {
119 log.info("No feed specified for planet feeds.");
120 }
121 } catch (Exception e) {
122 log.error("Problem while retrieving post items: " + e.getMessage());
123 }
124 return listItems;
125 }
126
127 protected void sort(Collection<Node> listItems) {
128 Collections.sort((List<Node>) listItems, new Comparator<Node>() {
129
130 @Override
131 public int compare(Node n1, Node n2) {
132 try {
133 Date d1 = n1.getProperty(DATE_PROPERTY).getDate().getTime();
134 Date d2 = n2.getProperty(DATE_PROPERTY).getDate().getTime();
135
136 return d2.compareTo(d1);
137 } catch (RepositoryException e) {
138 log.error("Problem while sorting posts: " + e.getMessage());
139 }
140 return 0;
141 }
142 });
143 }
144
145 public String getDescription(String entryDescription, Integer abbreviation) {
146 return PlanetUtil.formatDescription(entryDescription, abbreviation);
147 }
148
149
150
151
152
153
154
155 public String getHackergotchi(String feedLink) {
156 try {
157 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
158 String assetId = getFeedProperty(rssParent, FEEDS_NODE, "link", feedLink, "img");
159 if (!StringUtils.isBlank(assetId)) {
160 return damTemplatingFunction.getAssetLink(assetId);
161 }
162 } catch (RepositoryException e) {
163 log.error("Problem while fetching Hackergotchi: " + e.getMessage());
164 }
165 return null;
166 }
167
168
169
170
171
172
173
174 @Override
175 public String getFeedTitle(String feedLink) {
176 try {
177 Node rssParent = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, content.getProperty("link").getString());
178 return getFeedProperty(rssParent, "feeds", "link", feedLink, "title");
179 } catch (RepositoryException e) {
180 log.error("Problem while fetching feed title for the main planet feed: " + e.getMessage());
181 }
182 return null;
183 }
184
185 }