View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.templating.inheritance;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.inheritance.InheritanceContentDecorator;
38  import info.magnolia.jcr.iterator.RangeIteratorImpl;
39  import info.magnolia.jcr.predicate.AbstractPredicate;
40  import info.magnolia.rendering.template.InheritanceConfiguration;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.Collections;
45  import java.util.Comparator;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.RepositoryException;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  /**
55   * Provides an inheritance model that can be customized with configuration on the nodes. Inheritance can be completely
56   * turned off or inheritance of nodes or properties can be turned off separately.
57   * <p/>
58   * The inheritance sources are found by looking at the node hierarchy, each node that qualifies as an anchor (node type
59   * is mgnl:content) and has a node that with the same sub-path as the destination node has to its nearest parent is used.
60   * <p/>
61   * That is, for a destination node /page1/page2/main, the nearest anchor node is /page1/page2, therefor if there is a
62   * node /page1/main then that is used as a source.
63   *
64   * @version $Id$
65   */
66  public class DefaultInheritanceContentDecorator extends InheritanceContentDecorator {
67  
68      private final InheritanceConfiguration configuration;
69      private final AbstractPredicate<Node> componentPredicate;
70      private final Comparator<Node> componentComparator;
71  
72      public DefaultInheritanceContentDecorator(Node destination, InheritanceConfiguration configuration) throws RepositoryException {
73          super(destination);
74          this.configuration = configuration;
75  
76          componentPredicate = configuration.getComponentPredicate();
77  
78          componentComparator = configuration.getComponentComparator();
79  
80          if (configuration.isEnabled() != null && configuration.isEnabled()) {
81  
82              Node firstAnchor = findFirstAnchor();
83  
84              if (firstAnchor != null && firstAnchor.getDepth() != 0) {
85  
86                  // relativePath is null if the destination and the first anchor is the same node
87                  String relativePathToAnchor = getPathRelativeToParent(firstAnchor, getDestination());
88  
89                  Node node = firstAnchor.getParent();
90                  while (node.getDepth() != 0) {
91  
92                      if (isAnchor(node)) {
93  
94                          Node source = null;
95                          if (relativePathToAnchor == null) {
96                              source = node;
97                          } else {
98                              if (node.hasNode(relativePathToAnchor)) {
99                                  source = node.getNode(relativePathToAnchor);
100                             }
101                         }
102 
103                         if (source != null) {
104                             addSource(source);
105                         }
106                     }
107 
108                     node = node.getParent();
109                 }
110             }
111         }
112     }
113 
114     protected Node findFirstAnchor() throws RepositoryException {
115         Node node = getDestination();
116         while (node.getDepth() != 0) {
117             if (isAnchor(node)) {
118                 return node;
119             }
120             node = node.getParent();
121         }
122         return null;
123     }
124 
125     private String getPathRelativeToParent(Node parent, Node child) throws RepositoryException {
126         String childPath = child.getPath();
127         if (parent.getDepth() == 0) {
128             return childPath;
129         }
130         String parentPathWithTrailingSlash = parent.getPath() + "/";
131         if (!childPath.startsWith(parentPathWithTrailingSlash)) {
132             return null;
133         }
134         return StringUtils.removeStart(childPath, parentPathWithTrailingSlash);
135     }
136 
137     /**
138      * True if this node is an anchor. By default true if this node is of type {@link info.magnolia.cms.core.MgnlNodeType#NT_CONTENT}.
139      *
140      * @param node the node to evaluate
141      * @return true if the node is an anchor
142      * @throws javax.jcr.RepositoryException if a problem occurs accessing the node
143      */
144     protected boolean isAnchor(Node node) throws RepositoryException {
145         return node.isNodeType(MgnlNodeType.NT_CONTENT);
146     }
147 
148     @Override
149     protected boolean inheritsNodes(Node node) throws RepositoryException {
150         return configuration.isInheritsComponents();
151     }
152 
153     @Override
154     protected boolean inheritsProperties(Node node) throws RepositoryException {
155         return configuration.isInheritsProperties();
156     }
157 
158     @Override
159     protected boolean isSourceChildInherited(Node node) throws RepositoryException {
160         return componentPredicate.evaluateTyped(node);
161     }
162 
163     @Override
164     protected NodeIterator sortInheritedNodes(NodeIterator destinationChildren, List<NodeIterator> sourceChildren) throws RepositoryException {
165         ArrayList<Node> nodes = new ArrayList<Node>();
166         while (destinationChildren.hasNext()) {
167             Node node = destinationChildren.nextNode();
168             nodes.add(node);
169         }
170         for (NodeIterator nodeIterator : sourceChildren) {
171             while (nodeIterator.hasNext()) {
172                 Node node = nodeIterator.nextNode();
173                 if (isSourceChildInherited(node)) {
174                     nodes.add(node);
175                 }
176             }
177         }
178         Collections.sort(nodes, componentComparator);
179         return new NodeIteratorImpl(nodes);
180     }
181 
182     private static class NodeIteratorImpl extends RangeIteratorImpl<Node> implements NodeIterator {
183 
184         private NodeIteratorImpl(Collection<Node> nodes) {
185             super(nodes);
186         }
187 
188         @Override
189         public Node nextNode() {
190             return super.next();
191         }
192     }
193 }