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.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   * Model class for combinedFeedParagraph.
57   *
58   * @author had
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              //String queryString = "SELECT * FROM mgnl:contentNode WHERE jcr:path LIKE \'/rssaggregator/news/data/%/%\' order by pubDate";
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