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.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.module.InstallContext;
40  
41  import javax.jcr.RepositoryException;
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 HierarchyManager hm = ctx.getConfigHierarchyManager();
83          if (!hm.isExist(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 Content originalNode = hm.getContent(originalParentNode);
88          final NodeData originalProp = originalNode.getNodeData(originalPropertyName);
89          final String currentValue = originalProp.getString();
90          if (originalProp.isExist()) {
91              originalProp.delete();
92          }
93  
94          final String newValue;
95          if (StringUtils.isEmpty(currentValue) || currentValue.equals(originalDefaultValue)) {
96              newValue = newDefaultValue;
97          } else {
98              newValue = modifyCurrentValue(currentValue);
99          }
100 
101         if (!hm.isExist(newParentNode)) {
102             ctx.warn("Can't find node at " + newParentNode + ". Please create it with a property named " + newPropertyName + " and a value of " + newDefaultValue);
103             return;
104         }
105         final Content newNode = hm.getContent(newParentNode);
106         if (newNode.hasNodeData(newPropertyName)) {
107             final NodeData newProp = newNode.getNodeData(newPropertyName);
108             ctx.info("Replacing property " + newPropertyName + " at " + newParentNode + " with value " + newValue + ". Previous value was " + newProp.getString());
109             newProp.setValue(newValue);
110         } else {
111             newNode.createNodeData(newPropertyName, newValue);
112         }
113     }
114 
115     protected String modifyCurrentValue(String currentValue) {
116         return currentValue;
117     }
118 }