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