View Javadoc

1   /**
2    * This file Copyright (c) 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.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.HierarchyManagerProvider.UserContextHierarchyManagerProvider;
39  import info.magnolia.module.groovy.support.classes.MgnlGroovyClassLoader;
40  import info.magnolia.ui.api.action.ActionExecutionException;
41  import info.magnolia.ui.form.EditorCallback;
42  import info.magnolia.ui.form.EditorValidator;
43  import info.magnolia.ui.form.action.SaveFormAction;
44  import info.magnolia.ui.form.action.SaveFormActionDefinition;
45  import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
47  
48  import java.util.List;
49  
50  import javax.jcr.Node;
51  import javax.jcr.RepositoryException;
52  
53  import org.codehaus.groovy.ast.ASTNode;
54  import org.codehaus.groovy.ast.ClassNode;
55  import org.codehaus.groovy.ast.builder.AstBuilder;
56  import org.codehaus.groovy.control.CompilationFailedException;
57  import org.codehaus.groovy.control.CompilePhase;
58  
59  import com.vaadin.data.Item;
60  
61  /**
62   * SaveScriptAction.
63   */
64  public class SaveScriptAction extends SaveFormAction {
65      private final static MgnlGroovyClassLoader CL_TO_VERIFY_SOURCE = new MgnlGroovyClassLoader(new UserContextHierarchyManagerProvider());
66  
67      public SaveScriptAction(SaveFormActionDefinition definition, Item item, EditorCallback callback, EditorValidator validator) {
68          super(definition, (JcrNodeAdapter) item, callback, validator);
69      }
70  
71      @Override
72      public void execute() throws ActionExecutionException {
73          validator.showValidation(true);
74          if (validator.isValid()) {
75              try {
76                  final Node node = item.applyChanges();
77                  if (node.isNew()) {
78                      setNodeName(node, item);
79                  }
80                  verifySource(node, item, CL_TO_VERIFY_SOURCE);
81                  node.getSession().save();
82              } catch (RepositoryException e) {
83                  throw new ActionExecutionException(e);
84              } catch (CompilationFailedException e) {
85                  throw new ActionExecutionException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
86              }
87          }
88          callback.onSuccess(getDefinition().getName());
89  
90      }
91  
92      @Override
93      protected void setNodeName(Node node, JcrNodeAdapter item) throws RepositoryException {
94          final String source = (String) item.getItemProperty("text").getValue();
95          String newNodeName = resolveNewGroovyFullyQualifiedClassName(source, item);
96          if (!node.getName().equals(newNodeName)) {
97              newNodeName = Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), Path.getValidatedLabel(newNodeName));
98              item.setNodeName(newNodeName);
99              NodeUtil.renameNode(node, newNodeName);
100         }
101     }
102 
103     protected void verifySource(Node node, 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 = node.getPath();
112             if (node.isNew()) {
113                 AbstractJcrNodeAdapter parent = item.getParent();
114                 String parentPath = parent != null ? parent.getJcrItem().getPath() + "/" : "/";
115                 path = parentPath + resolveNewGroovyFullyQualifiedClassName(source, item);
116                 item.setNodeName(path);
117                 setNodeName(node, item);
118             }
119             groovyClassLoader.verify(source, true, path);
120         }
121     }
122 
123     private String resolveNewGroovyFullyQualifiedClassName(String source, JcrNodeAdapter item) throws RepositoryException {
124         final List<ASTNode> nodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, source);
125         for (ASTNode astNode : nodes) {
126             if (astNode instanceof ClassNode) {
127                 ClassNode cn = (ClassNode) astNode;
128                 String name = cn.getNameWithoutPackage();
129                 if (!name.startsWith("script")) {
130                     return name;
131                 }
132             }
133         }
134         return "untitledGroovyScript";
135     }
136 
137 }