View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 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.templatingkit.templates.components;
35  
36  
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.PropertyUtil;
40  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
41  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
42  import info.magnolia.module.templatingkit.templates.category.TemplateCategory;
43  import info.magnolia.module.templatingkit.util.STKPager;
44  import info.magnolia.rendering.model.RenderingModel;
45  import info.magnolia.rendering.template.TemplateDefinition;
46  import info.magnolia.repository.RepositoryConstants;
47  import info.magnolia.templating.functions.TemplatingFunctions;
48  
49  import java.util.Collection;
50  import java.util.List;
51  
52  import javax.inject.Inject;
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  
56  import org.apache.commons.lang3.StringUtils;
57  
58  
59  /**
60   * Abstract STK renderable definition dedicated to Content list manipulation.
61   *
62   * @param <RD> Renderable definition.
63   */
64  public abstract class AbstractItemListModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
65  
66      @Inject
67      public AbstractItemListModel(Node content, RD definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
68          super(content, definition, parent, stkFunctions, templatingFunctions);
69      }
70  
71  
72      protected String getRepository() {
73          return RepositoryConstants.WEBSITE;
74      }
75  
76      protected String getCategoryName() {
77          return StringUtils.defaultString((String) definition.getParameters().get("searchForCategory"), TemplateCategory.CONTENT);
78      }
79  
80      protected String getSubcategoryName() {
81          return (String) definition.getParameters().get("searchForSubcategory");
82      }
83  
84      abstract protected int getMaxResults();
85  
86  
87      protected Node getSearchRoot() throws RepositoryException {
88          // Default value as searchRoot is the site root
89          Node siteRoot = getSiteRoot();
90  
91          // "getConstrainedSearchRoot()" provides the possibility to define a manually chosen sub node
92          // as search root.
93          Node constrainedSearchRoot = getConstrainedSearchRoot();
94          if (constrainedSearchRoot != null) {
95              return constrainedSearchRoot;
96          }
97  
98          return siteRoot;
99      }
100 
101     private Node getConstrainedSearchRoot() {
102         // The "searchLink" parameter provides a search on a manually chosen sub node as search
103         // root.
104         // The "searchLink" parameter must be defined on the paragraph using this model class.
105         // This method can be overwritten for defining a different parameter/parameter name.
106         String id = PropertyUtil.getString(content, "searchLink");
107         Node subNodeSearchPath = null;
108         if (StringUtils.isNotEmpty(id)) {
109             try {
110                 subNodeSearchPath = NodeUtil.getNodeByIdentifier(getRepository(), id);
111             } catch (RepositoryException e) {
112             }
113         }
114         return subNodeSearchPath;
115     }
116 
117     public Collection<Node> getItems() throws RepositoryException {
118         List<Node> itemsList = search();
119 
120         this.filter(itemsList);
121         this.sort(itemsList);
122         itemsList = this.shrink(itemsList);
123 
124         return itemsList;
125     }
126 
127     public STKPager getPager() throws RepositoryException {
128 
129         final String currentPageLink;
130         if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
131             currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
132         } else {
133             currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContentNode());
134         }
135         return new STKPager(currentPageLink, getItems(), content);
136     }
137 
138     protected List<Node> search() throws RepositoryException {
139         return stkFunctions.getContentListByTemplateCategorySubCategory(getSearchRoot(), getCategoryName(), getSubcategoryName());
140     }
141 
142     abstract protected void filter(List<Node> itemList);
143 
144     abstract protected void sort(List<Node> itemList);
145 
146     private List<Node> shrink(List<Node> itemsList) {
147         return stkFunctions.cutList(itemsList, getMaxResults());
148     }
149 
150 }