View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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.ui.form.action;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.ui.api.action.AbstractAction;
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.vaadin.integration.jcr.JcrNodeAdapter;
43  import info.magnolia.ui.vaadin.integration.jcr.ModelConstants;
44  
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.RepositoryException;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * Action for saving Items in Forms.<br>
54   * The name of the created node will be set to:<br>
55   * <b>the value of the property called 'jcrName' if</b>
56   * <ul>
57   * <li>the node has no property called 'jcrName'</li>
58   * </ul>
59   * <b>the value of the property called 'name' if</b>
60   * <ul>
61   * <li>the node has a property called 'name' AND</li>
62   * <li>the node has no property called 'jcrName' AND</li>
63   * </ul>
64   * <b>a default name if non of these conditions are valid</b> <br>
65   * To change this behavior extend this Action and override {@link SaveFormAction#setNodeName(Node, JcrNodeAdapter)}
66   *
67   * @see SaveFormActionDefinition
68   * @deprecated since 6.0. Use info.magnolia.ui.databinding.SaveFormAction instead.
69   */
70  @Deprecated
71  public class SaveFormAction extends AbstractAction<SaveFormActionDefinition> {
72  
73      private static final Logger log = LoggerFactory.getLogger(SaveFormAction.class);
74  
75      protected final JcrNodeAdapter item;
76      protected final EditorCallback callback;
77      protected final EditorValidator validator;
78  
79      public SaveFormAction(SaveFormActionDefinition definition, JcrNodeAdapter item, EditorCallback callback, EditorValidator validator) {
80          super(definition);
81          this.item = item;
82          this.callback = callback;
83          this.validator = validator;
84      }
85  
86      @Override
87      public void execute() throws ActionExecutionException {
88          if (validateForm()) {
89              try {
90                  final Node node = item.applyChanges();
91                  setNodeName(node, item);
92                  node.getSession().save();
93              } catch (final RepositoryException e) {
94                  throw new ActionExecutionException(e);
95              }
96              callback.onSuccess(getDefinition().getName());
97          }
98      }
99  
100     protected boolean validateForm() {
101         boolean isValid = validator.isValid();
102         validator.showValidation(!isValid);
103         if (!isValid) {
104             log.info("Validation error(s) occurred. No save performed.");
105         }
106         return isValid;
107     }
108 
109     /**
110      * Set the node Name.
111      * Node name is set to: <br>
112      * the value of the property 'name' if it is present.
113      */
114     protected void setNodeName(Node node, JcrNodeAdapter item) throws RepositoryException {
115         String propertyName = "name";
116         if (node.hasProperty(propertyName) && !node.hasProperty(ModelConstants.JCR_NAME)) {
117             Property property = node.getProperty(propertyName);
118             String newNodeName = property.getString();
119             if (!node.getName().equals(Path.getValidatedLabel(newNodeName))) {
120                 newNodeName = Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), Path.getValidatedLabel(newNodeName));
121                 item.setNodeName(newNodeName);
122                 NodeUtil.renameNode(node, newNodeName);
123             }
124         }
125     }
126 }