View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.gui.inline;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.cms.gui.control.Button;
38  import info.magnolia.cms.i18n.I18nContentSupport;
39  import info.magnolia.cms.i18n.I18nContentSupportFactory;
40  import info.magnolia.cms.i18n.MessagesManager;
41  import info.magnolia.cms.security.Permission;
42  import info.magnolia.cms.security.PermissionUtil;
43  import info.magnolia.context.MgnlContext;
44  
45  import java.io.IOException;
46  import java.io.Writer;
47  
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.jsp.JspWriter;
52  
53  import org.apache.commons.lang.StringUtils;
54  
55  
56  /**
57   * @author Vinzenz Wyser
58   * @version 2.0
59   */
60  public class ButtonEdit extends Button {
61      private String label = "buttons.edit"; //$NON-NLS-1$
62      private String dialogPath;
63      private String dialog;
64  
65      private final I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
66  
67      public ButtonEdit() {
68      }
69  
70      /**
71       * @deprecated since 4.0 - use the empty constructor.
72       */
73      @Deprecated
74      public ButtonEdit(HttpServletRequest request) {
75      }
76  
77      /**
78       * @deprecated since 4.0 - do not pass an HttpServletRequest
79       */
80      @Deprecated
81      public ButtonEdit(HttpServletRequest request, String path, String nodeCollectionName, String nodeName, String dialog) {
82          this(path, nodeCollectionName, nodeName, dialog);
83      }
84  
85      public ButtonEdit(String path, String nodeCollectionName, String nodeName, String dialog) {
86          this.setPath(path);
87          this.setNodeCollectionName(nodeCollectionName);
88          this.setNodeName(nodeName);
89          this.setDialog(dialog);
90      }
91  
92      /**
93       * @deprecated since 4.0 - do not pass an HttpServletRequest
94       */
95      @Deprecated
96      public void setDefaultOnclick(HttpServletRequest request) {
97          setDefaultOnclick();
98      }
99  
100     public void setDefaultOnclick() {
101         String nodeCollectionName = this.getNodeCollectionName();
102         if (nodeCollectionName == null) {
103             nodeCollectionName = StringUtils.EMPTY;
104         }
105         String nodeName = this.getNodeName();
106         if (nodeName == null) {
107             nodeName = StringUtils.EMPTY;
108         }
109 
110         String repository = MgnlContext.getAggregationState().getRepository();
111         this.setOnclick("mgnlOpenDialog('" //$NON-NLS-1$
112                 + this.getPath()
113                 + "','" //$NON-NLS-1$
114                 + nodeCollectionName
115                 + "','" //$NON-NLS-1$
116                 + nodeName
117                 + "','" //$NON-NLS-1$
118                 + this.getDialog()
119                 + "','" //$NON-NLS-1$
120                 + repository
121                 + "',"
122                 + (dialogPath != null ? "'" + dialogPath + "'" : "null")
123                 + ", null" //width
124                 + ", null" //height
125                 + (i18nSupport.isEnabled()? ", '" + i18nSupport.getLocale().toString() + "'":"")
126                 +");");
127     }
128 
129     @Override
130     public String getLabel() {
131         return MessagesManager.getWithDefault(label, label);
132     }
133 
134     @Override
135     public void setLabel(String s) {
136         this.label = s;
137     }
138 
139     public void setDialogPath(String dialogPath) {
140         this.dialogPath = dialogPath;
141     }
142 
143     public void setDialog (String dialog) {
144         this.dialog = dialog;
145     }
146 
147     public String getDialog () {
148         return this.dialog;
149     }
150 
151     /**
152      * @deprecated use drawHtml(Writer out) instead.
153      */
154     @Deprecated
155     public void drawHtml(JspWriter out) throws IOException {
156         drawHtml((Writer)out);
157     }
158 
159     /**
160      * Draws the edit button. The request has to be set!
161      */
162     public void drawHtml(Writer out) throws IOException {
163         if (this.getRequest() != null) {
164             final AggregationState aggregationState = MgnlContext.getAggregationState();
165             final Node mainContent = aggregationState.getMainContent().getJCRNode();
166             boolean isGranted;
167             try {
168                 isGranted = PermissionUtil.isGranted(mainContent.getSession(), mainContent.getPath(), Permission.SET);
169             } catch (RepositoryException e) {
170                 // TODO dlipp - apply consistent ExceptionHandling
171                 throw new RuntimeException(e);
172             }
173             if (!aggregationState.isPreviewMode() && isGranted) {
174                 println(out, this.getHtml());
175             }
176         }
177     }
178 }