View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.module.delta;
35  
36  import info.magnolia.module.InstallContext;
37  
38  import javax.jcr.Node;
39  import javax.jcr.Property;
40  import javax.jcr.RepositoryException;
41  import javax.jcr.Session;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  /**
46   * A task to move and rename properties, taking default values into account. If the property
47   * existed with original default, it is replaced by the new default, otherwise its value is kept.
48   * If the new property exists, its value is replaced by either the new default of the original
49   * property's value. This permits bootstrapping complex nodes, and migrating a few simple properties
50   * when necessary.
51   * Override the modifyCurrentValue() method to provide special behaviour when replacing existing
52   * values.
53   */
54  public class MoveAndRenamePropertyTask extends AbstractRepositoryTask {
55      private final String originalParentNode;
56      private final String originalPropertyName;
57      private final String originalDefaultValue;
58  
59      private final String newParentNode;
60      private final String newPropertyName;
61      private final String newDefaultValue;
62  
63      public MoveAndRenamePropertyTask(String name, String originalParentNode, String originalPropertyName, String newParentNode, String newPropertyName) {
64          this(name, originalParentNode, originalPropertyName, null, newParentNode, newPropertyName, null);
65      }
66  
67      public MoveAndRenamePropertyTask(String name, String originalParentNode, String originalPropertyName, String originalDefaultValue, String newParentNode, String newPropertyName, String newDefaultValue) {
68          super(name, "The property at " + originalParentNode + "/" + originalPropertyName +
69                  " is now at " + newParentNode + "/" + newPropertyName +
70                  (originalDefaultValue != null ? (", and its default value is now \"" + newDefaultValue + "\" instead of \"" + originalDefaultValue + "\"") : "")
71                  + ".");
72          this.originalParentNode = originalParentNode;
73          this.originalPropertyName = originalPropertyName;
74          this.originalDefaultValue = originalDefaultValue;
75          this.newParentNode = newParentNode;
76          this.newPropertyName = newPropertyName;
77          this.newDefaultValue = newDefaultValue;
78      }
79  
80      @Override
81      protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
82          final Session jcrSession = ctx.getConfigJCRSession();
83          if (!jcrSession.nodeExists(originalParentNode)) {
84              ctx.warn("Can't find node at " + originalParentNode + ". Please create its replacement at " + newParentNode + " with a property named " + newPropertyName + " and a value of " + newDefaultValue);
85              return;
86          }
87          final Node originalNode = jcrSession.getNode(originalParentNode);
88          final Property originalProp = originalNode.getProperty(originalPropertyName);
89          final String currentValue = originalProp.getString();
90  
91          if (originalNode.hasProperty(originalPropertyName)) {
92              originalProp.remove();
93          }
94  
95          final String newValue;
96          if (StringUtils.isEmpty(currentValue) || currentValue.equals(originalDefaultValue)) {
97              newValue = newDefaultValue;
98          } else {
99              newValue = modifyCurrentValue(currentValue);
100         }
101 
102         if (!jcrSession.nodeExists(newParentNode)) {
103             ctx.warn("Can't find node at " + newParentNode + ". Please create it with a property named " + newPropertyName + " and a value of " + newDefaultValue);
104             return;
105         }
106         final Node newNode = jcrSession.getNode(newParentNode);
107         if (newNode.hasProperty(newPropertyName)) {
108             final Property newProp = newNode.getProperty(newPropertyName);
109             ctx.info("Replacing property " + newPropertyName + " at " + newParentNode + " with value " + newValue + ". Previous value was " + newProp.getString());
110             newProp.setValue(newValue);
111         } else {
112             newNode.setProperty(newPropertyName, newValue);
113         }
114     }
115 
116     protected String modifyCurrentValue(String currentValue) {
117         return currentValue;
118     }
119 }