View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.cms.util.ContentWrapper;
41  import info.magnolia.cms.util.NodeMapWrapper;
42  import info.magnolia.context.MgnlContext;
43  import info.magnolia.jcr.util.PropertyUtil;
44  import info.magnolia.module.data.DataConsts;
45  import info.magnolia.rendering.model.RenderingModel;
46  import info.magnolia.rendering.template.TemplateDefinition;
47  import info.magnolia.templating.functions.TemplatingFunctions;
48  
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.List;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Node;
55  import javax.jcr.RepositoryException;
56  
57  import org.apache.commons.lang.StringUtils;
58  
59  /**
60   * Model class for combinedFeedParagraph.
61   *
62   * @author had
63   * @param <RD>
64   */
65  public class CombinedFeedModel <RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
66  
67      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CombinedFeedModel.class);
68  
69      protected String orderBy = ORDER_BY_PUBLICATION_DATE;
70      protected String sortDirection = SORT_DESCENDING;
71  
72      @Inject
73      public CombinedFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
74          super(content, definition, parent,templatingFunctions);
75      }
76  
77      @Override
78      public String execute() {
79          log.debug("Executing " + this.getClass().getName());
80          return "success";
81      }
82  
83      /**
84       * .
85       * FIXME: Remove the old QueryManager and use the JCR API.
86       * @return
87       */
88      public Collection<ContentWrapper> getItems() {
89          List<ContentWrapper> results = new ArrayList<ContentWrapper>();
90          try {
91              QueryManager qm = MgnlContext.getQueryManager("data");
92  
93              String orderByNodeValue = PropertyUtil.getString(content, "orderBy");
94              String sortDirectionNodeValue = PropertyUtil.getString(content, "direction");
95              String link = PropertyUtil.getString(content, "link");
96  
97  
98              if(StringUtils.isEmpty(orderByNodeValue)){
99                  orderByNodeValue = orderBy;
100             }
101 
102             if(StringUtils.isEmpty(sortDirectionNodeValue)){
103                 sortDirectionNodeValue = sortDirection;
104             }
105 
106             String queryString = "/jcr:root"+link+"/data[1]/*/* order by " + orderByNodeValue + " " + sortDirectionNodeValue;
107             //String queryString = "SELECT * FROM mgnl:contentNode WHERE jcr:path LIKE \'/rssaggregator/news/data/%/%\' order by pubDate";
108             log.debug(queryString);
109             Query q = qm.createQuery(queryString, "xpath");
110             QueryResult res = q.execute();
111             Collection<Content> col = res.getContent(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE);
112             for (Content c : col) {
113                 results.add(new NodeMapWrapper(c, c.getHandle()));
114             }
115             log.debug("returning collection with {} items.", "" + results.size());
116             return results;
117         } catch (RepositoryException e) {
118             log.error(e.getLocalizedMessage(),e);
119         }
120         return null;
121     }
122 
123     @Override
124     public String getContextPath() {
125         return MgnlContext.getContextPath();
126     }
127 
128     public String getOrderBy() {
129         return this.orderBy;
130     }
131 
132     public void setOrderBy(String orderBy) {
133         if(orderBy.equals(ORDER_BY_PUBLICATION_DATE) || orderBy.equals(ORDER_BY_TITLE_NAME)){
134             this.orderBy = orderBy;
135         }
136     }
137 
138     public String getSortDirection() {
139         return this.sortDirection;
140     }
141 
142     public void setSortDirection(String sortDirection) {
143         if(sortDirection.equals(AbstractFeedModel.SORT_ASCENDING) || sortDirection.equals(AbstractFeedModel.SORT_DESCENDING)){
144             this.sortDirection = sortDirection;
145         }
146     }
147 }
148