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