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