View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.ui.framework.setup;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  import info.magnolia.module.InstallContext;
38  import info.magnolia.module.delta.QueryTask;
39  import info.magnolia.ui.form.field.transformer.multi.MultiValueJSONTransformer;
40  import info.magnolia.ui.form.field.transformer.multi.MultiValueSubChildrenNodeTransformer;
41  
42  import javax.jcr.Node;
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Rename or remove the 5.0 node field definition 'saveModeType'.<br>
51   * In 5.1, this definition do not exist anymore and is replaced by 'transformerClass' definition.
52   */
53  public class ReplaceSaveModeTypeFieldDefinitionTask extends QueryTask {
54  
55      private static final Logger log = LoggerFactory.getLogger(ReplaceSaveModeTypeFieldDefinitionTask.class);
56  
57      public ReplaceSaveModeTypeFieldDefinitionTask(String name, String description, String repositoryName, String query) {
58          super(name, description, repositoryName, query);
59      }
60  
61      @Override
62      protected void operateOnNode(InstallContext installContext, Node node) {
63          String nodePath = NodeUtil.getPathIfPossible(node);
64          try {
65              if (StringUtils.contains(nodePath, "fields")) {
66                  if (node.hasProperty("multiValueHandlerClass")) {
67                      String multiValueHandlerClass = node.getProperty("multiValueHandlerClass").getString();
68                      if (StringUtils.equals("info.magnolia.ui.form.field.property.MultiValuesHandler", multiValueHandlerClass)) {
69                          // Simply remove the node. The field definition already contains the default transformerClass.
70                          node.remove();
71                          log.debug("The following node will be removed {}. The field definition already contain the definition of the default transformerClass", nodePath);
72                      } else if (StringUtils.equals("info.magnolia.ui.form.field.property.SubNodesValueHandler", multiValueHandlerClass)) {
73                          Node parent = node.getParent();
74                          parent.setProperty("transformerClass", MultiValueSubChildrenNodeTransformer.class.getName());
75                          node.remove();
76  
77                      } else if (StringUtils.equals("info.magnolia.ui.form.field.property.CommaSeparatedValueHandler", multiValueHandlerClass)) {
78                          Node parent = node.getParent();
79                          parent.setProperty("transformerClass", MultiValueJSONTransformer.class.getName());
80                          node.remove();
81                      } else {
82                          log.warn("Unknown value for property 'multiValueHandlerClass' : {}. This node {} will not be handled", multiValueHandlerClass, nodePath);
83                      }
84                  }
85              } else {
86                  log.debug("The following node {} is not a field configuration. The child 'saveModeType' will not be handled. ", nodePath);
87              }
88          } catch (RepositoryException re) {
89              log.warn("Could not Migrate 'saveModeType' child node of the following node {}.", nodePath);
90          }
91      }
92  
93  }