View Javadoc

1   /**
2    * This file Copyright (c) 2003-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.templating;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.cms.util.ContentUtil;
40  import info.magnolia.rendering.template.TemplateAvailability;
41  import info.magnolia.rendering.template.TemplateDefinition;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import javax.jcr.Node;
47  
48  import org.apache.commons.lang.StringUtils;
49  
50  /**
51   * Represents a template definition.
52   * Following are some of the most common properties you can use to configure your templates. Of course, if you're using specific subclasses, other properties could be available.
53   * <br/>
54   * <br/>
55   * <table border="1">
56   * <tbody>
57   * <tr>
58   * <th>Name of Property</th>
59   * <th>Default Value</th>
60   * <th>Value Example or Range</th>
61   * <th>Description</th>
62   * </tr>
63   * <tr>
64   * <td>class</td>
65   * <td>{@link Template}</td>
66   * <td>&nbsp;</td>
67   * <td>&nbsp;</td>
68   * </tr>
69   * <tr>
70   * <td>type</td>
71   * <td> <code>jsp</code></td>
72   * <td> <code>jsp</code>, <code>freemarker</code>, �</td>
73   * <td>Determines which <code>TemplateRenderer</code> to use. Out of the box,
74   * Magnolia provides support for JSP and FreeMarker.</td>
75   * </tr>
76   * <tr>
77   * <td>templatePath</td>
78   * <td>&nbsp;</td>
79   * <td>Conventional path syntax is used for this property.</td>
80   * <td>This property defines the URI to the template which is normally a JSP.
81   * The path for the template is relative to the root and structure of the webapp
82   * folder.</td>
83   * </tr>
84   * <tr>
85   * <td>visible</td>
86   * <td><code>true</code></td>
87   * <td><code>true</code>, <code>false</code></td>
88   * <td>This property determines if the template is visible in the template
89   * drop-down list. If a template is to be restricted so that only certain users
90   * can see it in the drop-down list, a role needs to be defined in security
91   * which denies access to the particular template definition content node.</td>
92   * </tr>
93   * <tr>
94   * <td>modelClass</td>
95   * <td>&nbsp;</td>
96   * <td>The fully qualified name of a class implementing {@link RenderingModel}
97   * </td>
98   * <td>The bean created by the renderer based on the modelClass defined on the
99   * paragraph or template definition. The current content, definition and the
100  * parent model are passed to the constructor. This object is instantiated for
101  * each rendering of a template or a paragraph.</td>
102  * </tr>
103  * <tr>
104  * <td>subTemplates</td>
105  * <td>&nbsp;</td>
106  * <td>Any valid node identifier.</td>
107  * <td>This property designates a node containing any subtemplates to be used by
108  * the template.</td>
109  * </tr>
110  * <tr>
111  * <td>name</td>
112  * <td>nodeName</td>
113  * <td>Naming conventions should be followed using standard alphanumerical
114  * characters only.</td>
115  * <td>This property lists the name of the template.</td>
116  * </tr>
117  * <tr>
118  * <td>i18nBasename</td>
119  * <td>&nbsp;</td>
120  * <td>Naming conventions should be followed using standard alphanumerical
121  * characters only.</td>
122  * <td>This property defines the message bundle to use for this template.</td>
123  * </tr>
124  * <tr>
125  * <td>title</td>
126  * <td>&nbsp;</td>
127  * <td>The title or a message bundle key to be used with the bundle defined by
128  * <code>i18nBasename</code>.</td>
129  * <td>This property designates the title of the template. The i18nBasename
130  * (designated message bundle) renders the title.</td>
131  * </tr>
132  * <tr>
133  * <td>description</td>
134  * <td>&nbsp;</td>
135  * <td>The description or a message bundle key to be used with the bundle
136  * defined by <code>i18nBasename</code>.</td>
137  * <td>This property contains the description of the template. Descriptions
138  * should be intuitive and provide a context for users and/or developers to
139  * understand how the overall template functions.</td>
140  * </tr>
141  * </tbody>
142  * </table>
143  *
144  * @deprecated since 4.5, the differentiation of paragraphs and templates were removed. Use {@link RenderableDefinition} instead.
145  */
146 public class Template extends AbstractRenderable {
147     private Content content;
148 
149     private Map<String, Template> subTemplates = new HashMap<String, Template>();
150 
151     public Template() {
152         // bridge to legacy isAvailable() method
153         setTemplateAvailability(new TemplateAvailability() {
154             @Override
155             public boolean isAvailable(Node content, TemplateDefinition templateDefinition) {
156                 return Template.this.isAvailable(ContentUtil.asContent(content));
157             }
158         });
159     }
160 
161     public String getI18NTitle() {
162         Messages msgs = MessagesManager.getMessages(getI18nBasename());
163 
164         return msgs.getWithDefault(getTitle(), getTitle());
165     }
166 
167     public String getParameter(String key) {
168         return (String) getParameters().get(key);
169     }
170 
171     /**
172      * Getter for <code>visible</code>.
173      * @return Returns the visible.
174      */
175     public boolean isVisible() {
176         return this.getVisible() == null || this.getVisible();
177     }
178 
179     public Template getSubTemplate(String extension) {
180         return this.subTemplates.get(extension);
181     }
182 
183     public void addSubTemplate(String extension, Template subTemplate) {
184         this.subTemplates.put(extension, subTemplate);
185     }
186 
187     public Map<String, Template> getSubTemplates() {
188         return this.subTemplates;
189     }
190 
191     public void setSubTemplates(Map<String, Template> subTemplates) {
192         this.subTemplates = subTemplates;
193     }
194 
195     public boolean isAvailable(Content node) {
196         return node.getHierarchyManager().getName().equals("website") &&
197                 StringUtils.substringAfter(getId(), ":").startsWith("pages/");
198     }
199 
200     public Content getContent() {
201         return this.content;
202     }
203 
204     // this is set by content2bean
205     public void setContent(Content content) {
206         this.content = content;
207     }
208 
209 }