View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.setup.for5_2;
35  
36  import info.magnolia.jcr.predicate.NodeTypePredicate;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.jcr.util.NodeVisitor;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.delta.AbstractRepositoryTask;
41  import info.magnolia.module.delta.TaskExecutionException;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Session;
49  
50  import org.apache.commons.lang.StringUtils;
51  import org.apache.jackrabbit.JcrConstants;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Identify all sibling nodes for a defined workspace, sub path and nodeTypes.<br>
57   * If the nodesTypes is empty or null, search all 'nt:base' nodes.
58   */
59  public class IdentifySameNameSiblingNodesTask extends AbstractRepositoryTask {
60      private static final Logger log = LoggerFactory.getLogger(IdentifySameNameSiblingNodesTask.class);
61  
62      protected final String workspace;
63      protected Session session;
64      private final String subPath;
65      private final List<String> nodeTypes;
66      private final boolean evaluateSupertypes;
67      private final String siblingMatcherPattern = ".*\\[\\d+\\]";
68  
69      /**
70       * @param name task name
71       * @param description task description
72       * @param workspace task is running against this workspace. TaskExecutionException is throws if the workspace is invalid or null.
73       * @param subPath used to limit the operation to a specified subPath '/demo-project/about'
74       * @param nodeTypes List of nodeTypes that has to search. If null or empty, the search of siblings is performed against 'nt:base'
75       * @param evaluateSupertypes if set to true, the child nodeTypes of a defined 'nodeType' are also part of the evaluation.
76       */
77      public IdentifySameNameSiblingNodesTask(String name, String description, String workspace, String subPath, List<String> nodeTypes, boolean evaluateSupertypes) {
78          super(name, description);
79          this.workspace = workspace;
80          this.subPath = StringUtils.isBlank(subPath) ? "/" : subPath;
81          this.nodeTypes = nodeTypes != null ? nodeTypes : new ArrayList<String>();
82          this.evaluateSupertypes = evaluateSupertypes;
83      }
84  
85      @Override
86      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
87          try{
88              session = installContext.getJCRSession(workspace);
89              // Check
90              if (nodeTypes.isEmpty()) {
91                  nodeTypes.add(JcrConstants.NT_BASE);
92              }
93              for (String nodeType : nodeTypes) {
94                  Node rootNode = session.getNode(subPath);
95                  NodeTypePredicate predicate = new NodeTypePredicate(nodeType, evaluateSupertypes);
96                  NodeUtil.visit(rootNode, createSiblingsVisitor(installContext), predicate);
97  
98              }
99          }catch(RepositoryException re) {
100             installContext.error("Unable to perform Migration task " + getName(), re);
101             throw new TaskExecutionException(re.getMessage());
102         }
103 
104     }
105 
106     /**
107      * Create a Siblings visitor.
108      */
109     private NodeVisitor createSiblingsVisitor(final InstallContext installContext) {
110         NodeVisitor visitor = new NodeVisitor() {
111 
112             @Override
113             public void visit(Node node) throws RepositoryException {
114                 if (isSameNameSiblingNode(node)) {
115                     handleNode(node, installContext);
116                 }
117             }
118         };
119         return visitor;
120     }
121 
122     /**
123      * Return true if the node match the siblingMatcherPattern.
124      */
125     protected boolean isSameNameSiblingNode(Node node) {
126         try {
127             String path = node.getPath();
128             return path.matches(siblingMatcherPattern);
129         } catch (RepositoryException re) {
130             log.warn("Got exception by checking if '{}' is a sibling node", NodeUtil.getName(node), re);
131             return false;
132         }
133     }
134 
135     /**
136      * Take action on a sibling node.<br>
137      * In this case, simply display in the install screen the sibling node name and path.
138      */
139     protected void handleNode(Node node, InstallContext installContext) throws RepositoryException {
140         installContext.warn("Found same name sibling of '" + node.getName() + "' at '" + node.getPath() + "'");
141     }
142 
143 }