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> </td> 65 * <td> </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> </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> </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> </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> </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> </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> </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 boolean visible = true; 148 149 private Map<String, Template> subTemplates = new HashMap<String, Template>(); 150 151 /** 152 * Used internally for SubTemplates. 153 */ 154 public Template() { 155 156 } 157 158 public String getI18NTitle() { 159 Messages msgs = MessagesManager.getMessages(getI18nBasename()); 160 161 return msgs.getWithDefault(getTitle(), getTitle()); 162 } 163 164 public String getParameter(String key) { 165 return (String) getParameters().get(key); 166 } 167 168 /** 169 * Getter for <code>visible</code>. 170 * @return Returns the visible. 171 */ 172 public boolean isVisible() { 173 return this.visible; 174 } 175 176 public Template getSubTemplate(String extension) { 177 return this.subTemplates.get(extension); 178 } 179 180 public void addSubTemplate(String extension, Template subTemplate) { 181 this.subTemplates.put(extension, subTemplate); 182 } 183 184 public Map<String, Template> getSubTemplates() { 185 return this.subTemplates; 186 } 187 188 public void setSubTemplates(Map<String, Template> subTemplates) { 189 this.subTemplates = subTemplates; 190 } 191 192 public void setVisible(boolean visible) { 193 this.visible = visible; 194 } 195 196 public boolean isAvailable(Content node) { 197 // was: TODO is called quite often and should be faster 198 try { 199 return PermissionUtil.isGranted(MgnlContext.getJCRSession(getContent().getWorkspace().getName()), getContent().getHandle(), Session.ACTION_READ); 200 } catch (RepositoryException e) { 201 return false; 202 } 203 } 204 205 public Content getContent() { 206 return this.content; 207 } 208 209 // this is set by content2bean 210 public void setContent(Content content) { 211 this.content = content; 212 } 213 214 }