View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.dam.app.assets.form.action;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.dam.jcr.AssetNodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.ui.form.EditorCallback;
40  import info.magnolia.ui.form.EditorValidator;
41  import info.magnolia.ui.form.action.SaveFormAction;
42  import info.magnolia.ui.form.action.SaveFormActionDefinition;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.io.FilenameUtils;
50  import org.apache.commons.lang3.StringUtils;
51  
52  import com.vaadin.v7.data.Item;
53  
54  /**
55   * Action for saving asset.<br>
56   * In case of new Asset, set :
57   * <ul>
58   * <li>Asset node name = file name, including extension, without JCR invalid chars
59   * <li>Asset name if empty = file name excluding extension.
60   * </ul>
61   * We currently can't rename the node on change.
62   * This must be properly solved by passing the node identifier to {@link info.magnolia.ui.api.event.ContentChangedEvent}.
63   * See MGNLUI-226.
64   */
65  public class SaveAssetFormAction extends SaveFormAction {
66  
67      public SaveAssetFormAction(SaveFormActionDefinition definition, Item item, EditorCallback callback, EditorValidator validator) {
68          super(definition, (JcrNodeAdapter) item, callback, validator);
69      }
70  
71      @Override
72      protected void setNodeName(Node assetNode, JcrNodeAdapter item) throws RepositoryException {
73  
74          if (item instanceof JcrNewNodeAdapter) {
75              // Get File Name
76              Node resourceNode = AssetNodeTypes.AssetResource.getResourceNodeFromAsset(assetNode);
77              String fileName = extractFileName(resourceNode);
78  
79              if (StringUtils.isNotBlank(fileName)) {
80                  setAssetPropertyName(item, assetNode, fileName);
81                  // set the node name
82                  String newNodeName = generateUniqueNodeNameForAsset(assetNode, fileName);
83  
84                  item.setNodeName(newNodeName);
85                  NodeUtil.renameNode(assetNode, newNodeName);
86              }
87          }
88      }
89  
90      public static String extractFileName(Node resourceNode) throws RepositoryException {
91          String fileName = StringUtils.EMPTY;
92          if (resourceNode != null) {
93              if (resourceNode.hasProperty(AssetNodeTypes.AssetResource.FILENAME) && StringUtils.isNotBlank(resourceNode.getProperty(AssetNodeTypes.AssetResource.FILENAME).getString())) {
94                  fileName = resourceNode.getProperty(AssetNodeTypes.AssetResource.FILENAME).getString();
95              }
96          }
97          return fileName;
98      }
99  
100     public static void setAssetPropertyName(Item item, Node assetNode, String fileName) throws RepositoryException {
101         // Set the Asset name property if empty
102         if (item.getItemProperty(AssetNodeTypes.Asset.ASSET_NAME) == null ||
103                 item.getItemProperty(AssetNodeTypes.Asset.ASSET_NAME).getValue() == null ||
104                 StringUtils.isBlank(item.getItemProperty(AssetNodeTypes.Asset.ASSET_NAME).getValue().toString())) {
105             assetNode.setProperty(AssetNodeTypes.Asset.ASSET_NAME, FilenameUtils.removeExtension(fileName));
106         }
107     }
108 
109     /**
110      * Create a new Node Unique NodeName.
111      */
112     public static String generateUniqueNodeNameForAsset(final Node node, String newNodeName) throws RepositoryException {
113         return Path.getUniqueLabel(node.getSession(), node.getParent().getPath(), Path.getValidatedLabel(newNodeName));
114     }
115 
116 
117 }