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.templatingkit.templates.components;
35  
36  
37  import info.magnolia.cms.beans.config.ContentRepository;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.jcr.util.PropertyUtil;
41  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
42  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
43  import info.magnolia.module.templatingkit.templates.category.TemplateCategory;
44  import info.magnolia.module.templatingkit.util.STKPager;
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.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.lang.StringUtils;
57  
58  
59  
60  /**
61   * Abstract STK Rendable definition dedicated to Content list manipulation.
62   *
63   * @author cringele
64   * @version $Id$
65   * @param <RD>
66   */
67  
68  public abstract class AbstractItemListModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
69  
70      @Inject
71      public AbstractItemListModel(Node content, RD definition, RenderingModel< ? > parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
72          super(content, definition, parent, stkFunctions, templatingFunctions);
73      }
74  
75      /**
76       * FIXME usage of deprecated classes: {@link ContentRepository}.
77       */
78      protected String getRepository() {
79          return ContentRepository.WEBSITE;
80      }
81  
82      protected String getCategoryName() {
83          return StringUtils.defaultString((String) definition.getParameters().get("searchForCategory"), TemplateCategory.CONTENT);
84      }
85  
86      protected String getSubcategoryName() {
87          return (String) definition.getParameters().get("searchForSubcategory");
88      }
89  
90      abstract protected int getMaxResults();
91  
92  
93      protected Node getSearchRoot() throws RepositoryException {
94          // Default value as searchRoot is the site root
95          Node siteRoot = getSiteRoot();
96  
97          // "getConstrainedSearchRoot()" provides the possibility to define a manually chosen sub node
98          // as search root.
99          Node constrainedSearchRoot = getConstrainedSearchRoot();
100         if (constrainedSearchRoot != null) {
101             return constrainedSearchRoot;
102         }
103 
104         return siteRoot;
105     }
106 
107     private Node getConstrainedSearchRoot() {
108         // The "searchLink" parameter provides a search on a manually chosen sub node as search
109         // root.
110         // The "searchLink" parameter must be defined on the paragraph using this model class.
111         // This method can be overwritten for defining a different parameter/parameter name.
112         String id = PropertyUtil.getString(content, "searchLink");
113         Node subNodeSearchPath = null;
114         if (StringUtils.isNotEmpty(id)) {
115             try {
116                 subNodeSearchPath = NodeUtil.getNodeByIdentifier(getRepository(),id);
117             }
118             catch (RepositoryException e) {
119             }
120         }
121         return subNodeSearchPath;
122     }
123 
124     public Collection<Node> getItems() throws RepositoryException {
125         List<Node> itemsList = search();
126 
127         this.filter(itemsList);
128         this.sort(itemsList);
129         itemsList = this.shrink(itemsList);
130 
131         return itemsList;
132     }
133 
134     public STKPager getPager() throws RepositoryException {
135 
136         final String currentPageLink;
137         if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
138             currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
139         }
140         else {
141             currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContent().getJCRNode());
142         }
143         // FIXME : Review STKPAger?
144         return new STKPager(currentPageLink, getItems(), content);
145     }
146 
147     // FIXME: What to do?
148     protected List<Node> search() throws RepositoryException {
149         return stkFunctions.getContentListByTemplateCategorySubCategory(getSearchRoot(), getCategoryName(), getSubcategoryName());
150     }
151 
152     abstract protected void filter(List<Node> itemList);
153 
154     abstract protected void sort(List<Node> itemList);
155 
156     private List<Node> shrink(List<Node> itemsList) {
157         return stkFunctions.cutList(itemsList, getMaxResults());
158     }
159 
160 }