View Javadoc
1   /**
2    * This file Copyright (c) 2008-2017 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.context.MgnlContext;
37  import info.magnolia.jcr.util.ContentMap;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.rendering.model.RenderingModel;
40  import info.magnolia.rendering.template.TemplateDefinition;
41  import info.magnolia.templating.functions.TemplatingFunctions;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  import java.util.List;
46  
47  import javax.inject.Inject;
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.lang3.StringUtils;
53  import org.apache.jackrabbit.util.ISO9075;
54  
55  /**
56   * Model class for combinedFeedParagraph.
57   *
58   * @param <RD> {@link TemplateDefinition}
59   */
60  public class CombinedFeedModel<RD extends TemplateDefinition> extends AbstractFeedModel<TemplateDefinition> {
61  
62      protected String orderBy = ORDER_BY_PUBLICATION_DATE;
63      protected String sortDirection = SORT_DESCENDING;
64  
65      @Inject
66      public CombinedFeedModel(Node content, RD definition, RenderingModel<?> parent, TemplatingFunctions templatingFunctions) {
67          super(content, definition, parent, templatingFunctions);
68      }
69  
70      @Override
71      public String execute() {
72          log.debug("Executing " + this.getClass().getName());
73          return "success";
74      }
75  
76      @Override
77      public String getContextPath() {
78          return MgnlContext.getContextPath();
79      }
80  
81      public Collection<ContentMap> getEntries() {
82          List<ContentMap> results = new ArrayList<>();
83          try {
84              String orderByNodeValue = PropertyUtil.getString(content, "orderBy");
85              String sortDirectionNodeValue = PropertyUtil.getString(content, "direction");
86              String link = PropertyUtil.getString(content, "link");
87  
88              if (StringUtils.isEmpty(orderByNodeValue)) {
89                  orderByNodeValue = orderBy;
90              }
91  
92              if (StringUtils.isEmpty(sortDirectionNodeValue)) {
93                  sortDirectionNodeValue = sortDirection;
94              }
95  
96              String queryString = "/jcr:root" + ISO9075.encodePath(link) + "/data[1]/*/* order by " + orderByNodeValue + " " + sortDirectionNodeValue;
97              NodeIterator feeds = this.runQuery(queryString);
98  
99              while (feeds.hasNext()) {
100                 Node feed = feeds.nextNode();
101                 results.add(new ContentMap(feed));
102             }
103             log.debug("returning collection with {} items.", "" + results.size());
104             return results;
105         } catch (RepositoryException e) {
106             log.error(e.getLocalizedMessage(), e);
107         }
108         return null;
109     }
110 
111     public String getOrderBy() {
112         return this.orderBy;
113     }
114 
115     public void setOrderBy(String orderBy) {
116         if (orderBy.equals(ORDER_BY_PUBLICATION_DATE) || orderBy.equals(ORDER_BY_TITLE_NAME)) {
117             this.orderBy = orderBy;
118         }
119     }
120 
121     public String getSortDirection() {
122         return this.sortDirection;
123     }
124 
125     public void setSortDirection(String sortDirection) {
126         if (sortDirection.equals(AbstractFeedModel.SORT_ASCENDING) || sortDirection.equals(AbstractFeedModel.SORT_DESCENDING)) {
127             this.sortDirection = sortDirection;
128         }
129     }
130 }