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.pages;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
39  import info.magnolia.module.templatingkit.navigation.Link;
40  import info.magnolia.module.templatingkit.navigation.SiteNavigationModel;
41  import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
42  import info.magnolia.module.templatingkit.templates.category.TemplateCategory;
43  import info.magnolia.module.templatingkit.templates.category.TemplateCategoryUtil;
44  import info.magnolia.module.templatingkit.templates.components.ImageModel;
45  import info.magnolia.module.templatingkit.templates.components.TextImageModel;
46  import info.magnolia.objectfactory.Components;
47  import info.magnolia.rendering.model.RenderingModel;
48  import info.magnolia.templating.functions.TemplatingFunctions;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Base page model class of all pages providing page level functionality.
64   *
65   * @param <RD>
66   *            - an instance of {@link STKPage}
67   *
68   * @author cringele
69   * @version $Id$
70   */
71  public class STKPageModel<RD extends STKPage> extends AbstractSTKTemplateModel<RD> {
72  
73      private static Logger log = LoggerFactory.getLogger(STKPageModel.class);
74  
75      protected static final String LINK_PROP_NAME = "link";
76      protected static final String LINK_TITLE_PROP_NAME = "linkTitle";
77  
78      @Inject
79      public STKPageModel(Node content, RD definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
80          super(content, definition, parent, stkFunctions, templatingFunctions);
81      }
82  
83      public String getBodyClass() {
84          return stkFunctions.theme(getSite()).getBodyClassResolver().resolveBodyClass(this);
85      }
86  
87      public SiteNavigationModel getNavigation() {
88          return new SiteNavigationModel(getDefinition().getNavigation(), getSiteRoot(), content);
89      }
90  
91      public String getLogoImageLink() {
92          final Node root = getSiteRoot();
93          return stkFunctions.getAssetLink(root, "logoImg");
94      }
95  
96      public String getPrintLogoImageLink() {
97          final Node root = getSiteRoot();
98          return stkFunctions.getAssetLink(root, "printLogoImg");
99      }
100 
101     protected Node resolveSectionPage() throws RepositoryException {
102         return stkFunctions
103                 .wrap(TemplateCategoryUtil.findParentWithTemplateCategory(content, TemplateCategory.SECTION));
104     }
105 
106 
107 
108     public Collection<Link> getContentNavigation() throws RepositoryException {
109         int maxNavLevel =
110                 this.getNavigation().getHorizontalLevel() + this.getNavigation().getVerticalLevel()
111                         + getSiteRoot().getDepth();
112 
113         Iterable<Node> children = NodeUtil.getNodes(content, MgnlNodeType.NT_PAGE);
114         Iterator<Node> childrenIterator = children.iterator();
115 
116         if (content.getDepth() >= maxNavLevel && childrenIterator.hasNext()) {
117             List<Link> items = new ArrayList<Link>();
118 
119             while (childrenIterator.hasNext()) {
120                 Node current = childrenIterator.next();
121                 items.add(Components.newInstance(Link.class, current));
122             }
123             return items;
124         }
125 
126         return null;
127     }
128 
129     /**
130      * just for ee users. FIXME: will it always be Nodes, so List<Node> would be returnable?
131      */
132     public List<?> getCategories() {
133         return stkFunctions.getCategories(this.content);
134     }
135 
136     /**
137      * just for ee users.
138      */
139     public String getCategoryLink(String categoryName) {
140         return stkFunctions.getCategoryLink(this.content, categoryName);
141     }
142 
143     /**
144      * FIXME: Should be moved to into-area model.
145      */
146     public ImageModel<?> getImageModel() {
147         // we reuse the model of the text image paragraph as the same classes and names are used
148         return Components.newInstance(TextImageModel.class, content,  definition, this);
149     }
150 
151 }