View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.templating.jsp.taglib;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.gui.inline.ButtonEdit;
39  import info.magnolia.cms.security.Permission;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.jcr.util.NodeUtil;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.registry.RegistrationException;
44  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
45  
46  import java.io.Writer;
47  
48  import javax.servlet.jsp.JspWriter;
49  import javax.servlet.jsp.tagext.TagSupport;
50  
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  import org.tldgen.annotations.BodyContent;
56  import org.tldgen.annotations.Tag;
57  
58  
59  /**
60   * Displays an edit button.
61   *
62   * @jsp.tag name="editButton" body-content="empty"
63   *
64   * @author Marcel Salathe
65   * @author Fabrizio Giustina
66   * @version $Revision $ ($Author $)
67   */
68  @Tag(name="editButton", bodyContent=BodyContent.EMPTY)
69  
70  public class EditButton extends TagSupport {
71      private static final Logger log = LoggerFactory.getLogger(EditButton.class);
72  
73      private String nodeName;
74  
75      private String nodeCollectionName;
76  
77      private String dialog;
78  
79      private String label;
80  
81      private String displayHandler;
82  
83      private boolean small = true;
84  
85      private boolean adminOnly = true;
86  
87      @Override
88      public int doStartTag() {
89          this.displayHandler = StringUtils.EMPTY;
90          return EVAL_BODY_INCLUDE;
91      }
92  
93      @Override
94      public int doEndTag() {
95          if ((!adminOnly || ServerConfiguration.getInstance().isAdmin()) && NodeUtil.isGranted(MgnlContext.getAggregationState().getMainContent().getJCRNode(), Permission.SET)) {
96  
97              try {
98                  if (this.getNodeCollectionName() != null && this.getNodeName() == null) {
99                      // cannot draw edit button with nodeCllection and without node
100                     return EVAL_PAGE;
101                 }
102                 JspWriter out = pageContext.getOut();
103                 ButtonEdit button = new ButtonEdit();
104                 button.setPath(this.getPath());
105                 button.setDialog(this.getDialog());
106                 button.setNodeCollectionName(this.getNodeCollectionName());
107                 button.setNodeName(this.getNodeName());
108                 button.setDefaultOnclick();
109                 if (this.getLabel() != null) {
110                     button.setLabel(this.getLabel());
111                 }
112                 if (this.small) {
113                     button.setSmall(true);
114                 }
115                 button.drawHtml((Writer) out);
116             }
117             catch (Exception e) {
118                 log.error(e.getMessage(), e);
119             }
120         }
121         return EVAL_PAGE;
122     }
123 
124     /**
125      * If attribute is not used, a content node with the name of the page will be added. If contentNodeName attribute is set but with no value
126      * between the quotes, the properties are stored directly under the Web page. If a contentNodeName is set then a content node with the
127      * according name will be added.
128      *
129      * @param name Container name which will be used to access/write content.
130      * @jsp.attribute required="false" rtexprvalue="true"
131      */
132     public void setContentNodeName(String name) {
133         this.nodeName = name;
134     }
135 
136     private String getNodeName() {
137         if (this.nodeName == null) {
138             Content current = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
139             if (current == null) {
140                 return null;
141             }
142             return current.getName();
143         }
144         return this.nodeName;
145     }
146 
147     /**
148      * @param name , container name which will be used to access/write content.
149      * @jsp.attribute required="false" rtexprvalue="true"
150      */
151     public void setContentNodeCollectionName(String name) {
152         this.nodeCollectionName = name;
153     }
154 
155     /**
156      * @return content node collection name
157      */
158     private String getNodeCollectionName() {
159         if (this.nodeCollectionName == null) {
160             try{
161             return (String) MgnlContext.getAttribute("localContentNodeCollectionName");
162             }catch (Exception e){
163                 return StringUtils.EMPTY;
164             }
165         }
166         return this.nodeCollectionName;
167     }
168 
169     /**
170      *  The paragraph type.
171      * @deprecated since 4.1, use the setDialog() instead.
172      * @param type , content type
173      * @jsp.attribute required="false" rtexprvalue="true"
174      */
175     @Deprecated
176     public void setParagraph(String type) {
177         setDialog(type);
178     }
179 
180     /**
181      * @return String paragraph (type of par)
182      * @deprecated since 4.1, use the getDialog() instead.
183      */
184     @Deprecated
185     private String getParagraph() {
186         return getDialog();
187     }
188 
189     /**
190      * The dialog type.
191      * @param type , content type
192      * @jsp.attribute required="false" rtexprvalue="true"
193      */
194     public void setDialog (String dialog) {
195         this.dialog = dialog;
196     }
197 
198     public String getDialog () {
199         return this.dialog;
200     }
201 
202     /**
203      * Set display handler (JSP / Servlet), needs to know the relative path from WEB-INF.
204      * TODO: deprecate this ???
205      * @param path relative to WEB-INF.
206      * @jsp.attribute required="false" rtexprvalue="true"
207      */
208     public void setTemplate(String path) {
209         this.displayHandler = path;
210     }
211 
212     /**
213      * Show only in admin instance, defaults to true.
214      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
215      */
216     public void setAdminOnly(boolean adminOnly) {
217         this.adminOnly = adminOnly;
218     }
219 
220     /**
221      * @return template path
222      */
223     public String getTemplate() {
224         if (this.displayHandler == null) {
225             Content localContainer = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
226             String templateName = localContainer.getNodeData("dialog").getString();
227             // TODO - use IoC for TemplateDefinitionRegistry?
228             try {
229                 return Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateName).getTemplateScript();
230             } catch (RegistrationException e) {
231                 // TODO dlipp: implement proper, consisitent ExceptionHandling!
232                 throw new RuntimeException(e);
233             }
234         }
235         return this.displayHandler;
236     }
237 
238     /**
239      * get the content path (Page or Node).
240      * @return String path
241      */
242     private String getPath() {
243         try {
244             return MgnlContext.getWebContext().getAggregationState().getCurrentContent().getHandle();
245         }
246         catch (Exception re) {
247             return StringUtils.EMPTY;
248         }
249     }
250 
251     /**
252      * @deprecated use the label attribute instead.
253      * @jsp.attribute required="false" rtexprvalue="true"
254      */
255     @Deprecated
256     public void setEditLabel(String label) {
257         this.setLabel(label);
258     }
259 
260     /**
261      * The button's label. (defaults to "Edit")
262      * @jsp.attribute required="false" rtexprvalue="true"
263      */
264     public void setLabel(String label) {
265         this.label = label;
266     }
267 
268     /**
269      * @return String , label for the edit bar
270      */
271     private String getLabel() {
272         return this.label;
273     }
274 
275     /**
276      * Sets the size of the button, true for small button, false for a large one (defaults to true).
277      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
278      */
279     public void setSmall(boolean small) {
280         this.small = small;
281     }
282 
283     @Override
284     public void release() {
285         this.nodeName = null;
286         this.nodeCollectionName = null;
287         this.dialog = null;
288         this.label = null;
289         this.displayHandler = null;
290         this.small = true;
291         this.adminOnly=true;
292         super.release();
293     }
294 }