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.contentapp.setup.for5_3;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.delta.NodeVisitorTask;
41  import info.magnolia.repository.RepositoryConstants;
42  import info.magnolia.ui.api.action.ActionDefinition;
43  import info.magnolia.ui.contentapp.detail.action.EditItemActionDefinition;
44  import info.magnolia.ui.contentapp.detail.action.RestorePreviousVersionActionDefinition;
45  
46  import java.util.ArrayList;
47  import java.util.Arrays;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  import javax.inject.Inject;
52  import javax.jcr.Node;
53  import javax.jcr.Property;
54  import javax.jcr.RepositoryException;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  /**
60   * Moves <code>nodeType</code> property from action definition to its availability definition.
61   * This task normally is not meant to be used standalone.
62   *
63   * @see {@link info.magnolia.ui.contentapp.detail.action.AbstractItemActionDefinition AbstractItemActionDefinition} and its sub-classes
64   * @see {@link ContentAppMigrationTask}
65   */
66  public class MoveActionNodeTypeRestrictionToAvailabilityTask extends NodeVisitorTask {
67  
68      private static final Logger log = LoggerFactory.getLogger(MoveActionNodeTypeRestrictionToAvailabilityTask.class);
69  
70      public static final String NODE_TYPE = "nodeType";
71      public static final String AVAILABILITY_NODE = "availability";
72      public static final String NODE_TYPES = "nodeTypes";
73  
74      /**
75       * The list of the common classes could also contain {@link info.magnolia.ui.contentapp.detail.action.CreateItemActionDefinition}
76       * but its <code>nodeType</code> property could have been used for purposes different from availability checking. In reality that
77       * property was not used at all, so in order to avoid problems with it - we skip {@link info.magnolia.ui.contentapp.detail.action.CreateItemActionDefinition}.
78       */
79      private static Class<?>[] commonActionDefinitionClasses = new Class[] {
80              EditItemActionDefinition.class,
81              RestorePreviousVersionActionDefinition.class,
82      };
83  
84      private final List<Class<?>> matchingActionDefinitionClasses = new ArrayList<Class<?>>();
85  
86      @Inject
87      public MoveActionNodeTypeRestrictionToAvailabilityTask(String path, Class<? extends ActionDefinition>... additionalActionDefinitionClasses) {
88          super(
89                "Move nodeType property to availability definition",
90                "Fix availability checking of actions defined with AbstractItemActionDefinition sub-class by moving the nodeType property from action definition to availability.",
91                  RepositoryConstants.CONFIG, path);
92  
93          matchingActionDefinitionClasses.addAll(Arrays.asList(commonActionDefinitionClasses));
94          matchingActionDefinitionClasses.addAll(Arrays.asList(additionalActionDefinitionClasses));
95      }
96  
97      @Override
98      protected boolean nodeMatches(Node node) {
99          try {
100             return node.getPrimaryNodeType().getName().equals(NodeTypes.ContentNode.NAME) &&
101                     (node.hasProperty("class") && containsDefinitionClass(node.getProperty("class").getString()));
102         } catch (RepositoryException e) {
103             log.error("Couldn't evaluate visited node's type or class property", e);
104         }
105         return false;
106     }
107 
108     private boolean containsDefinitionClass(String className) {
109         Iterator<Class<?>> it = matchingActionDefinitionClasses.iterator();
110         while (it.hasNext()) {
111             if (className.equals(it.next().getName())) {
112                 return true;
113             }
114         }
115         return false;
116     }
117 
118     @Override
119     protected void operateOnNode(InstallContext installContext, Node node) {
120         try {
121             if (node.hasProperty(NODE_TYPE)) {
122                 final Property nodeTypeProperty = node.getProperty(NODE_TYPE);
123 
124                 final Node availability = NodeUtil.createPath(node, AVAILABILITY_NODE, NodeTypes.ContentNode.NAME, true);
125                 final Node nodeTypes = NodeUtil.createPath(availability, NODE_TYPES, NodeTypes.ContentNode.NAME, true);
126 
127                 final String nodeType = nodeTypeProperty.getString();
128                 nodeTypes.setProperty(Path.getValidatedLabel(nodeType), nodeType);
129 
130                 nodeTypeProperty.remove();
131             }
132         } catch (RepositoryException e) {
133             log.error("Failed to move nodeType property to action availability for node {0} due to: {1}", node, e.getMessage());
134         }
135     }
136 }