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.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   * Model class for FeedList paragraph.
57   * 
58   * @author had
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              // not sorting by jcr:name in the query until jsr-283 is implemented so do this manually
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           //String queryString = "SELECT * FROM nt:base WHERE jcr:path LIKE '"+singleFeedHandle+"/%' ORDER BY pubDate asc";
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 }