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