View Javadoc
1   /**
2    * This file Copyright (c) 2013-2017 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.groovy.app.action;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.module.groovy.support.classes.MgnlGroovyClassLoader;
39  import info.magnolia.ui.api.action.ActionExecutionException;
40  import info.magnolia.ui.form.EditorCallback;
41  import info.magnolia.ui.form.EditorValidator;
42  import info.magnolia.ui.form.action.SaveFormAction;
43  import info.magnolia.ui.form.action.SaveFormActionDefinition;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.codehaus.groovy.control.CompilationFailedException;
50  
51  import com.vaadin.data.Item;
52  import com.vaadin.data.Property;
53  
54  /**
55   * SaveScriptAction.
56   */
57  public class SaveScriptAction extends SaveFormAction {
58  
59      public SaveScriptAction(SaveFormActionDefinition definition, Item item, EditorCallback callback, EditorValidator validator) {
60          super(definition, (JcrNodeAdapter) item, callback, validator);
61      }
62  
63      @Override
64      public void execute() throws ActionExecutionException {
65          validator.showValidation(true);
66          if (validator.isValid()) {
67              try {
68                  final Node node = item.applyChanges();
69                  setNodeName(node, item);
70                  node.getSession().save();
71              } catch (RepositoryException e) {
72                  throw new ActionExecutionException(e);
73              }
74              callback.onSuccess(getDefinition().getSuccessMessage());
75          }
76      }
77  
78      @Override
79      protected void setNodeName(Node node, JcrNodeAdapter item) throws RepositoryException {
80          final Property<Boolean> scriptProperty = item.getItemProperty("script");
81          final boolean isScript = scriptProperty != null && scriptProperty.getValue() != null ? scriptProperty.getValue() : false;
82  
83          final String source = (String) item.getItemProperty("text").getValue();
84  
85          String newNodeName = MgnlGroovyClassLoader.resolveScriptNameFromSource(source);
86          if ((!isScript && !node.getName().equals(newNodeName)) || isScript && item.isNew()) {
87              newNodeName = Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), Path.getValidatedLabel(newNodeName));
88              item.setNodeName(newNodeName);
89              NodeUtil.renameNode(node, newNodeName);
90          }
91      }
92  
93      /**
94       * @deprecated since 2.2.1, please use {@link #verifySource(JcrNodeAdapter, MgnlGroovyClassLoader)}.
95       */
96      protected void verifySource(Node node, JcrNodeAdapter item, MgnlGroovyClassLoader groovyClassLoader) throws RepositoryException, CompilationFailedException {
97          verifySource(item, groovyClassLoader);
98      }
99  
100     /**
101      * @deprecated since 2.3.1. This method is no longer used and the action delegates to {@link info.magnolia.module.groovy.validator.GroovyValidator} to check for compilation errors.
102      */
103     protected void verifySource(JcrNodeAdapter item, MgnlGroovyClassLoader groovyClassLoader) throws RepositoryException, CompilationFailedException {
104         final com.vaadin.data.Property<Boolean> scriptProperty = item.getItemProperty("script");
105         final boolean isScript = scriptProperty != null && scriptProperty.getValue() != null ? scriptProperty.getValue() : false;
106         final String source = (String) item.getItemProperty("text").getValue();
107 
108         if (isScript) {
109             groovyClassLoader.verify(source, false, null);
110         } else {
111             String path = item.getJcrItem().getPath();
112             if (item.isNew()) {
113                 String nodeName = MgnlGroovyClassLoader.resolveScriptNameFromSource(source);
114                 path = path.endsWith("/") ? path + nodeName : path + "/" + nodeName;
115                 item.setNodeName(nodeName);
116             }
117             groovyClassLoader.verify(source, true, path);
118         }
119     }
120 }