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