View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.templates.components;
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.jcr.util.ContentMap;
42  import info.magnolia.jcr.util.NodeUtil;
43  import info.magnolia.jcr.util.PropertyUtil;
44  import info.magnolia.module.data.DataConsts;
45  import info.magnolia.module.rssaggregator.functions.RssAgregatorTemplatingFunctions;
46  import info.magnolia.rendering.model.RenderingModel;
47  import info.magnolia.rendering.template.TemplateDefinition;
48  
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.Collections;
52  import java.util.Comparator;
53  import java.util.Set;
54  import java.util.TreeSet;
55  
56  import javax.inject.Inject;
57  import javax.jcr.Node;
58  import javax.jcr.RepositoryException;
59  
60  import org.apache.commons.lang.StringUtils;
61  
62  /**
63   * Model class for FeedList paragraph.
64   *
65   * @author had
66   * @param <RD>
67   */
68  public class FeedListModel<RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
69  
70      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FeedListModel.class);
71  
72      private String orderBy = ORDER_BY_PUBLICATION_DATE;
73      private String sortDirection = SORT_DESCENDING;
74  
75      @Inject
76      public FeedListModel(Node content, RD definition, RenderingModel<?> parent,RssAgregatorTemplatingFunctions rssAgregatorTemplatingFunctions) {
77          super(content, definition, parent,rssAgregatorTemplatingFunctions);
78      }
79  
80      public Collection<ContentMap> getFeeds() {
81          QueryManager qm = MgnlContext.getQueryManager("data");
82          String link = PropertyUtil.getString(content, "link");
83          try {
84              String queryString = "/jcr:root"+link+ "/data" + "/element(*, dataItemNode) order by @jcr:name ";
85              log.debug(queryString);
86              Query q = qm.createQuery(queryString, "xpath");
87              QueryResult res = q.execute();
88              //FIXME.
89              Collection<Content> queryResContent = res.getContent("dataItemNode");
90              Collection<Node> queryResultNode = new ArrayList<Node>();
91              for(Content c:queryResContent){
92                  queryResultNode.add(c.getJCRNode());
93              }
94              // not sorting by jcr:name in the query until jsr-283 is implemented so do this manually
95              Set<Node> results = new TreeSet<Node>(new Comparator<Node>() {
96  
97                  public int compare(Node o1, Node o2) {
98                      return NodeUtil.getName(o1).compareTo(NodeUtil.getName(o2));
99                  }});
100             results.addAll(queryResultNode);
101 
102             return rssAgregatorTemplatingFunctions.asContentMapList(results);
103         } catch (RepositoryException e) {
104             log.error(e.getLocalizedMessage(), e);
105         }
106         return null;
107     }
108 
109     public Collection<ContentMap> getItems(String singleFeedHandle) {
110         QueryManager qm = MgnlContext.getQueryManager("data");
111         try {
112             String orderByNodeValue = PropertyUtil.getString(content, "orderBy");
113             String sortDirectionNodeValue = PropertyUtil.getString(content, "direction");
114 
115             if(StringUtils.isEmpty(orderByNodeValue)){
116                 orderByNodeValue = orderBy;
117             }
118 
119             if(StringUtils.isEmpty(sortDirectionNodeValue)){
120                 sortDirectionNodeValue = sortDirection;
121             }
122 
123           //String queryString = "SELECT * FROM nt:base WHERE jcr:path LIKE '"+singleFeedHandle+"/%' ORDER BY pubDate asc";
124           String queryString = "/jcr:root"+singleFeedHandle + "/* order by " + orderByNodeValue + " " + sortDirectionNodeValue;
125           log.debug(queryString);
126           Query q = qm.createQuery(queryString, "xpath");
127           QueryResult res = q.execute();
128           //FIXME: Temp Hack
129           Collection<Node> nodeCollection = new ArrayList<Node>();
130           for(Content c: res.getContent(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE)){
131               nodeCollection.add(c.getJCRNode());
132           }
133 
134           return rssAgregatorTemplatingFunctions.asContentMapList(nodeCollection);
135         } catch (RepositoryException e) {
136             log.error(e.getLocalizedMessage(),e);
137         }
138         return Collections.EMPTY_LIST;
139     }
140 
141     public String getOrderBy() {
142         return this.orderBy;
143     }
144 
145     public void setOrderBy(String orderBy) {
146         if(orderBy.equals(ORDER_BY_PUBLICATION_DATE) || orderBy.equals(ORDER_BY_TITLE_NAME)){
147             this.orderBy = orderBy;
148         }
149     }
150 
151     public String getSortDirection() {
152         return this.sortDirection;
153     }
154 
155     public void setSortDirection(String sortDirection) {
156         if(sortDirection.equals(AbstractFeedModel.SORT_ASCENDING) || sortDirection.equals(AbstractFeedModel.SORT_DESCENDING)){
157             this.sortDirection = sortDirection;
158         }
159     }
160 }