View Javadoc
1   /**
2    * This file Copyright (c) 2014-2016 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.setup.for2_0;
35  
36  import info.magnolia.dam.jcr.AssetNodeTypes;
37  import info.magnolia.dam.jcr.DamConstants;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.jcr.util.NodeVisitor;
40  import info.magnolia.jcr.util.PropertyUtil;
41  import info.magnolia.module.InstallContext;
42  import info.magnolia.module.delta.AbstractTask;
43  import info.magnolia.module.delta.TaskExecutionException;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  import javax.jcr.Session;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * Dam 1.x had a property for filename (myFileName) and extension (pdf).<br>
55   * Dam 2.0 use only one property containing both values (myFileName.pdf). <br>
56   * This task take care migrating (aggregating) those property for the complete dam workspace.
57   */
58  public class UpdateDamAssetFileNamePropertiesTask extends AbstractTask {
59      private static final Logger log = LoggerFactory.getLogger(UpdateDamAssetFileNamePropertiesTask.class);
60      private Session damSession;
61  
62      public UpdateDamAssetFileNamePropertiesTask(String taskName, String taskDescription) {
63          super(taskName, taskDescription);
64      }
65  
66      @Override
67      public void execute(InstallContext ctx) throws TaskExecutionException {
68          try {
69              damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
70              NodeUtil.visit(damSession.getRootNode(), new DamFileNamePropertyVisitor());
71          } catch (RepositoryException re) {
72              ctx.error("Could not update the dam filename properties ", re);
73          }
74      }
75  
76      /**
77       * Aggregate filename and extension.
78       */
79      private class DamFileNamePropertyVisitor implements NodeVisitor {
80          @Override
81          public void visit(Node asset) throws RepositoryException {
82              if (!StringUtils.equals(asset.getPrimaryNodeType().getName(), AssetNodeTypes.Asset.NAME)) {
83                  return;
84              }
85              if(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset) == null) {
86                  log.info("Skipping asset {} without a resource node", asset.getPath());
87                  return;
88              }
89              String extension = StringUtils.removeStart(getExtensionProperty(asset), ".");
90              String fileName = PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset), AssetNodeTypes.AssetResource.FILENAME, "");
91              if(StringUtils.endsWith(fileName, "." + extension)) {
92                  log.debug("Asset '{}' has already a well formed fileName property '{}'. Nothing to do.", asset.getPath(), fileName);
93                  return;
94              }
95              fileName = fileName + "." + extension;
96              log.debug("Set asset fileName property to '{}'", fileName);
97              AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset).getProperty(AssetNodeTypes.AssetResource.FILENAME).setValue(fileName);
98          }
99      }
100 
101     /**
102      * @return {@link Asset#TYPE} property of asset {@link Node}. If this property is not define return {@link AssetResource#EXTENSION} or null if this property is not defined.
103      */
104     private String getExtensionProperty(Node asset) throws RepositoryException {
105         String extension = PropertyUtil.getString(asset, AssetNodeTypes.Asset.TYPE);
106         if(StringUtils.isBlank(extension)) {
107             log.debug("Asset '{}' has an empty property '{}'. Asset.resource node property '{}' will be used", asset.getPath(), AssetNodeTypes.Asset.TYPE, AssetNodeTypes.AssetResource.EXTENSION);
108             extension = PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(asset), AssetNodeTypes.AssetResource.EXTENSION);
109             if(StringUtils.isBlank(extension)){
110                 log.warn("Asset '{}' has no extension defined.", asset.getPath());
111             }
112         }
113         return extension;
114     }
115 
116 }