View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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 java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.Collection;
39  import java.util.HashSet;
40  import java.util.List;
41  import java.util.Set;
42  import javax.jcr.Node;
43  import javax.jcr.NodeIterator;
44  import javax.jcr.RepositoryException;
45  
46  import info.magnolia.cms.filters.FilterManager;
47  import info.magnolia.jcr.util.NodeUtil;
48  import info.magnolia.module.InstallContext;
49  
50  /**
51   * Orders a filter <strong>before</strong> a given set of other filters. The filter is placed directly before the first
52   * of the other filters. The other filters can be required or optional, if a required filter isn't present this task
53   * will do nothing and report a warning. If all other filters are optional and none are present this task does nothing.
54   * Does not take nested filter into account.
55   *
56   * @see FilterOrderingTask
57   */
58  public class OrderFilterBeforeTask extends AbstractRepositoryTask {
59  
60      private final String filterToBeOrderedName;
61      private final List<String> requiredFilters;
62      private final List<String> optionalFilters;
63  
64      public OrderFilterBeforeTask(String filterName, String[] filtersAfter) {
65          this(filterName, filtersAfter, new String[] {});
66      }
67  
68      public OrderFilterBeforeTask(String filterName, String[] filtersAfter, String[] optionalFiltersAfter) {
69          this(filterName, "Orders the " + filterName + " filter in the filter chain.", filtersAfter, optionalFiltersAfter);
70      }
71  
72      public OrderFilterBeforeTask(String filterName, String description, String[] requiredFiltersAfter) {
73          this(filterName, description, requiredFiltersAfter, new String[] {});
74      }
75  
76      public OrderFilterBeforeTask(String filterName, String description, String[] requiredFiltersAfter, String[] optionalFiltersAfter) {
77          super("Setup " + filterName + " filter", description);
78          this.filterToBeOrderedName = filterName;
79          this.requiredFilters = new ArrayList<String>(Arrays.asList(requiredFiltersAfter));
80          this.optionalFilters = new ArrayList<String>(Arrays.asList(optionalFiltersAfter));
81      }
82  
83      @Override
84      protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
85          final Node filtersParent = ctx.getConfigJCRSession().getNode(FilterManager.SERVER_FILTERS);
86  
87          if (!filtersParent.hasNode(filterToBeOrderedName)) {
88              throw new TaskExecutionException("Filter with name " + filterToBeOrderedName + " can't be found.");
89          }
90  
91          if (!requiredFilters.isEmpty() && !hasNodes(filtersParent, requiredFilters)) {
92              ctx.warn("Could not sort filter " + filterToBeOrderedName + ". It should be positioned before " + requiredFilters);
93              return;
94          }
95  
96          Set<String> combinedFilterNames = new HashSet<String>();
97          combinedFilterNames.addAll(requiredFilters);
98          combinedFilterNames.addAll(optionalFilters);
99  
100         orderBeforeSiblings(filtersParent.getNode(filterToBeOrderedName), combinedFilterNames);
101     }
102 
103     private void orderBeforeSiblings(Node node, Set<String> siblingNames) throws RepositoryException {
104         Node firstMatch = getFirstChild(node.getParent(), siblingNames);
105         if (firstMatch != null) {
106             NodeUtil.orderBefore(node, firstMatch.getName());
107         }
108     }
109 
110     private Node getFirstChild(Node parentNode, Collection<String> childNames) throws RepositoryException {
111         NodeIterator nodes = parentNode.getNodes();
112         while (nodes.hasNext()) {
113             Node child = nodes.nextNode();
114             if (childNames.contains(child.getName())) {
115                 return child;
116             }
117         }
118         return null;
119     }
120 
121     private boolean hasNodes(Node node, Collection<String> childNames) throws RepositoryException {
122         for (String childName : childNames) {
123             if (!node.hasNode(childName)) {
124                 return false;
125             }
126         }
127         return true;
128     }
129 }