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