View Javadoc
1   /**
2    * This file Copyright (c) 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.module.InstallContext;
37  import info.magnolia.module.delta.AbstractRepositoryTask;
38  import info.magnolia.module.delta.TaskExecutionException;
39  
40  import java.io.InputStream;
41  import java.util.Map;
42  
43  import javax.jcr.Node;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.Session;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.yaml.snakeyaml.Yaml;
49  
50  /**
51   * Task that removes specified JCR field type definitions from a module.
52   */
53  public class RemoveFieldTypeDefinitionsFromJcrTask extends AbstractRepositoryTask {
54  
55      private static final String FIELD_TYPES_NODE = "fieldTypes";
56      private static final String DEFINITION_CLASS_PROPERTY = "definitionClass";
57      private static final String FACTORY_CLASS_PROPERTY = "factoryClass";
58  
59      private static final Yaml YAML = new Yaml();
60  
61      private final String modulePath;
62  
63      private final Map<String, String> jcrFieldTypes;
64  
65      public RemoveFieldTypeDefinitionsFromJcrTask(String modulePath, Map<String, String> jcrFieldTypes) {
66          super("Remove field type definitions from JCR", String.format("Remove JCR field type definitions from %s module as they are now being moved to YAML", StringUtils.substringAfterLast(modulePath, "/")));
67          this.modulePath = modulePath;
68          this.jcrFieldTypes = jcrFieldTypes;
69      }
70  
71      @Override
72      @SuppressWarnings("unchecked")
73      public void execute(InstallContext ctx) throws TaskExecutionException {
74          super.execute(ctx);
75      }
76  
77      @Override
78      protected void doExecute(InstallContext ctx) throws RepositoryException {
79          Session session = ctx.getConfigJCRSession();
80          if (session.nodeExists(modulePath)) {
81              Node moduleNode = session.getNode(modulePath);
82              if (moduleNode.hasNode(FIELD_TYPES_NODE)) {
83                  Node fieldTypesNode = moduleNode.getNode(FIELD_TYPES_NODE);
84                  for (Map.Entry<String, String> entry : jcrFieldTypes.entrySet()) {
85                      if (fieldTypesNode.hasNode(entry.getKey())) {
86                          Node oldFieldType = fieldTypesNode.getNode(entry.getKey());
87                          InputStream stream = getClass().getResourceAsStream(String.format("/%s/fieldTypes/%s.yaml", StringUtils.substringAfterLast(modulePath, "/"), entry.getValue()));
88                          if (stream != null) {
89                              Map<String, String> newDefinition = YAML.load(stream);
90  
91                              if (oldFieldType.hasProperty(DEFINITION_CLASS_PROPERTY) && !oldFieldType.getProperty(DEFINITION_CLASS_PROPERTY).getString().equals(newDefinition.get(DEFINITION_CLASS_PROPERTY))) {
92                                  ctx.warn(String.format("Field type definition class is different from default one. Expected: [%s], got: [%s] in [%s].", newDefinition.get(DEFINITION_CLASS_PROPERTY), oldFieldType.getProperty(DEFINITION_CLASS_PROPERTY).getString(), entry.getKey()));
93                              }
94  
95                              if (oldFieldType.hasProperty(FACTORY_CLASS_PROPERTY) && !oldFieldType.getProperty(FACTORY_CLASS_PROPERTY).getString().equals(newDefinition.get(FACTORY_CLASS_PROPERTY))) {
96                                  ctx.warn(String.format("Field type factory class is different from default one. Expected: [%s], got: [%s] in [%s].", newDefinition.get(FACTORY_CLASS_PROPERTY), oldFieldType.getProperty(FACTORY_CLASS_PROPERTY).getString(), entry.getKey()));
97                              }
98                          }
99                          oldFieldType.remove();
100                     }
101                 }
102 
103                 if (!fieldTypesNode.hasNodes()) {
104                     fieldTypesNode.remove();
105                 }
106             }
107         }
108     }
109 }