Clover icon

magnolia-module-groovy 2.4.2

  1. Project Clover database Thu Jan 14 2016 16:38:54 CET
  2. Package info.magnolia.module.groovy.app.action

File SaveScriptAction.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
51% of files have more coverage

Code metrics

14
29
5
1
120
65
18
0.62
5.8
5
3.6

Classes

Class Line # Actions
SaveScriptAction 57 29 0% 18 48
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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  0 toggle public SaveScriptAction(SaveFormActionDefinition definition, Item item, EditorCallback callback, EditorValidator validator) {
60  0 super(definition, (JcrNodeAdapter) item, callback, validator);
61    }
62   
 
63  0 toggle @Override
64    public void execute() throws ActionExecutionException {
65  0 validator.showValidation(true);
66  0 if (validator.isValid()) {
67  0 try {
68  0 final Node node = item.applyChanges();
69  0 setNodeName(node, item);
70  0 node.getSession().save();
71    } catch (RepositoryException e) {
72  0 throw new ActionExecutionException(e);
73    }
74  0 callback.onSuccess(getDefinition().getSuccessMessage());
75    }
76    }
77   
 
78  0 toggle @Override
79    protected void setNodeName(Node node, JcrNodeAdapter item) throws RepositoryException {
80  0 final Property<Boolean> scriptProperty = item.getItemProperty("script");
81  0 final boolean isScript = scriptProperty != null && scriptProperty.getValue() != null ? scriptProperty.getValue() : false;
82   
83  0 final String source = (String) item.getItemProperty("text").getValue();
84   
85  0 String newNodeName = MgnlGroovyClassLoader.resolveScriptNameFromSource(source);
86  0 if ((!isScript && !node.getName().equals(newNodeName)) || isScript && item.isNew()) {
87  0 newNodeName = Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), Path.getValidatedLabel(newNodeName));
88  0 item.setNodeName(newNodeName);
89  0 NodeUtil.renameNode(node, newNodeName);
90    }
91    }
92   
93    /**
94    * @deprecated since 2.2.1, please use {@link #verifySource(JcrNodeAdapter, MgnlGroovyClassLoader)}.
95    */
 
96  0 toggle protected void verifySource(Node node, JcrNodeAdapter item, MgnlGroovyClassLoader groovyClassLoader) throws RepositoryException, CompilationFailedException {
97  0 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  0 toggle protected void verifySource(JcrNodeAdapter item, MgnlGroovyClassLoader groovyClassLoader) throws RepositoryException, CompilationFailedException {
104  0 final com.vaadin.data.Property<Boolean> scriptProperty = item.getItemProperty("script");
105  0 final boolean isScript = scriptProperty != null && scriptProperty.getValue() != null ? scriptProperty.getValue() : false;
106  0 final String source = (String) item.getItemProperty("text").getValue();
107   
108  0 if (isScript) {
109  0 groovyClassLoader.verify(source, false, null);
110    } else {
111  0 String path = item.getJcrItem().getPath();
112  0 if (item.isNew()) {
113  0 String nodeName = MgnlGroovyClassLoader.resolveScriptNameFromSource(source);
114  0 path = path.endsWith("/") ? path + nodeName : path + "/" + nodeName;
115  0 item.setNodeName(nodeName);
116    }
117  0 groovyClassLoader.verify(source, true, path);
118    }
119    }
120    }