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.cms.util.ContentWrapper;
41 import info.magnolia.cms.util.NodeMapWrapper;
42 import info.magnolia.context.MgnlContext;
43 import info.magnolia.module.data.DataConsts;
44 import info.magnolia.module.templating.RenderableDefinition;
45 import info.magnolia.module.templating.RenderingModel;
46
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.List;
50
51 import javax.jcr.RepositoryException;
52
53 import org.apache.commons.lang.StringUtils;
54
55
56
57
58
59
60 public class CombinedFeedModel extends AbstractFeedModel {
61 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CombinedFeedModel.class);
62
63 protected String orderBy = ORDER_BY_PUBLICATION_DATE;
64 protected String sortDirection = SORT_DESCENDING;
65
66 public CombinedFeedModel(Content content, RenderableDefinition renderable, RenderingModel parent) {
67 super(content, renderable, parent);
68 }
69
70 public String execute() {
71 log.debug("Executing " + this.getClass().getName());
72 return "success";
73 }
74
75 public Collection<ContentWrapper> getItems() {
76 List<ContentWrapper> results = new ArrayList<ContentWrapper>();
77 try {
78 QueryManager qm = MgnlContext.getQueryManager("data");
79
80 String orderByNodeValue = this.getContent().getNodeData("orderBy").getString();
81 String sortDirectionNodeValue = this.getContent().getNodeData("direction").getString();
82
83 if(StringUtils.isEmpty(orderByNodeValue)){
84 orderByNodeValue = orderBy;
85 }
86
87 if(StringUtils.isEmpty(sortDirectionNodeValue)){
88 sortDirectionNodeValue = sortDirection;
89 }
90
91 String queryString = "/jcr:root"+this.getContent().getNodeData("link").getString()+"/data[1]/*/* order by " + orderByNodeValue + " " + sortDirectionNodeValue;
92
93 log.debug(queryString);
94 Query q = qm.createQuery(queryString, "xpath");
95 QueryResult res = q.execute();
96 Collection<Content> col = res.getContent(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE);
97 for (Content c : col) {
98 results.add(new NodeMapWrapper(c, c.getHandle()));
99 }
100 log.debug("returning collection with {} items.", "" + results.size());
101 return results;
102 } catch (RepositoryException e) {
103 log.error(e.getLocalizedMessage(),e);
104 }
105 return null;
106 }
107
108 public String getContextPath() {
109 return MgnlContext.getContextPath();
110 }
111
112 public String getOrderBy() {
113 return this.orderBy;
114 }
115
116 public void setOrderBy(String orderBy) {
117 if(orderBy.equals(ORDER_BY_PUBLICATION_DATE) || orderBy.equals(ORDER_BY_TITLE_NAME)){
118 this.orderBy = orderBy;
119 }
120 }
121
122 public String getSortDirection() {
123 return this.sortDirection;
124 }
125
126 public void setSortDirection(String sortDirection) {
127 if(sortDirection.equals(AbstractFeedModel.SORT_ASCENDING) || sortDirection.equals(AbstractFeedModel.SORT_DESCENDING)){
128 this.sortDirection = sortDirection;
129 }
130 }
131 }
132