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.paragraphs;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.search.Query;
38 import info.magnolia.cms.core.search.QueryManager;
39 import info.magnolia.cms.core.search.QueryResult;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.module.data.DataConsts;
42 import info.magnolia.module.templating.RenderableDefinition;
43 import info.magnolia.module.templating.RenderingModel;
44
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.Comparator;
48 import java.util.Set;
49 import java.util.TreeSet;
50
51 import javax.jcr.RepositoryException;
52
53 import org.apache.commons.lang.StringUtils;
54
55
56
57
58
59
60 public class FeedListModel extends AbstractFeedModel {
61 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FeedListModel.class);
62
63 private String orderBy = ORDER_BY_PUBLICATION_DATE;
64 private String sortDirection = SORT_DESCENDING;
65
66 public FeedListModel(Content content, RenderableDefinition renderable, RenderingModel parent) {
67 super(content, renderable, parent);
68 }
69
70 public Collection<Content> getFeeds() {
71 QueryManager qm = MgnlContext.getQueryManager("data");
72 try {
73 String queryString = "/jcr:root"+this.getContent().getNodeData("link").getString() + "/data" + "/element(*, dataItemNode) order by @jcr:name ";
74 log.debug(queryString);
75 Query q = qm.createQuery(queryString, "xpath");
76 QueryResult res = q.execute();
77
78 Set<Content> results = new TreeSet<Content>(new Comparator<Content>() {
79
80 public int compare(Content o1, Content o2) {
81 return o1.getName().compareTo(o2.getName());
82 }});
83 results.addAll(res.getContent("dataItemNode"));
84 return results;
85 } catch (RepositoryException e) {
86 log.error(e.getLocalizedMessage(), e);
87 }
88 return null;
89 }
90
91 public Collection<Content> getItems(String singleFeedHandle) {
92 QueryManager qm = MgnlContext.getQueryManager("data");
93 try {
94 String orderByNodeValue = this.getContent().getNodeData("orderBy").getString();
95 String sortDirectionNodeValue = this.getContent().getNodeData("direction").getString();
96
97 if(StringUtils.isEmpty(orderByNodeValue)){
98 orderByNodeValue = orderBy;
99 }
100
101 if(StringUtils.isEmpty(sortDirectionNodeValue)){
102 sortDirectionNodeValue = sortDirection;
103 }
104
105
106 String queryString = "/jcr:root"+singleFeedHandle + "/* order by " + orderByNodeValue + " " + sortDirectionNodeValue;
107 log.debug(queryString);
108 Query q = qm.createQuery(queryString, "xpath");
109 QueryResult res = q.execute();
110 return res.getContent(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE);
111 } catch (RepositoryException e) {
112 log.error(e.getLocalizedMessage(),e);
113 }
114 return Collections.EMPTY_LIST;
115 }
116
117 public String getOrderBy() {
118 return this.orderBy;
119 }
120
121 public void setOrderBy(String orderBy) {
122 if(orderBy.equals(ORDER_BY_PUBLICATION_DATE) || orderBy.equals(ORDER_BY_TITLE_NAME)){
123 this.orderBy = orderBy;
124 }
125 }
126
127 public String getSortDirection() {
128 return this.sortDirection;
129 }
130
131 public void setSortDirection(String sortDirection) {
132 if(sortDirection.equals(AbstractFeedModel.SORT_ASCENDING) || sortDirection.equals(AbstractFeedModel.SORT_DESCENDING)){
133 this.sortDirection = sortDirection;
134 }
135 }
136 }