View Javadoc

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