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.rendering.template.configured;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.RuntimeRepositoryException;
38  import info.magnolia.jcr.predicate.AbstractPredicate;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.rendering.template.InheritanceConfiguration;
42  
43  import java.util.Comparator;
44  
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  
48  import org.apache.commons.lang.StringUtils;
49  
50  /**
51   * Defines behavior for inheritance. Allows for enabling
52   *
53   * @version $Id$
54   */
55  public class ConfiguredInheritance implements InheritanceConfiguration {
56  
57      public static final String COMPONENTS_ALL = "all";
58      public static final String COMPONENTS_NONE = "none";
59      public static final String COMPONENTS_FILTERED = "filtered";
60  
61      public static final String PROPERTIES_ALL = "all";
62      public static final String PROPERTIES_NONE = "none";
63  
64      private Boolean enabled = false;
65      private String components = COMPONENTS_FILTERED;
66      private String properties = PROPERTIES_ALL;
67      private Class<? extends AbstractPredicate<Node>> predicateClass;
68      private Class<? extends Comparator<Node>> nodeComparatorClass;
69  
70      @Override
71      public Boolean isEnabled() {
72          return enabled;
73      }
74  
75      public void setEnabled(Boolean enabled) {
76          this.enabled = enabled;
77      }
78  
79      public void setComponents(String components) {
80          this.components = components;
81      }
82  
83      public void setProperties(String properties) {
84          this.properties = properties;
85      }
86  
87      @Override
88      public Boolean isInheritsProperties() {
89          return isEnabled() != null && isEnabled() && StringUtils.equalsIgnoreCase(StringUtils.trim(properties), PROPERTIES_ALL);
90      }
91  
92      @Override
93      public Boolean isInheritsComponents() {
94          return isEnabled() != null && isEnabled() && (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL) || StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED));
95      }
96  
97      @Override
98      public AbstractPredicate<Node> getComponentPredicate() {
99          if (isEnabled() == null || !isEnabled()) {
100             return new InheritNothingInheritancePredicate();
101         }
102         if (predicateClass != null) {
103             return Components.newInstance(predicateClass);
104         }
105         if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_ALL)) {
106             return new AllComponentsInheritancePredicate();
107         }
108         if (StringUtils.equalsIgnoreCase(StringUtils.trim(components), COMPONENTS_FILTERED)) {
109             return new FilteredComponentInheritancePredicate();
110         }
111         return new InheritNothingInheritancePredicate();
112     }
113 
114     public void setPredicateClass(Class<? extends AbstractPredicate<Node>> predicateClass) {
115         this.predicateClass = predicateClass;
116     }
117 
118     @Override
119     public Comparator<Node> getComponentComparator() {
120         if (nodeComparatorClass != null) {
121             return Components.newInstance(nodeComparatorClass);
122         }
123         return new NodeDepthComparator();
124     }
125 
126     public void setNodeComparatorClass(Class<? extends Comparator<Node>> nodeComparatorClass) {
127         this.nodeComparatorClass = nodeComparatorClass;
128     }
129 
130     /**
131      * Predicate for component inheritance that includes only nodes with a a property named 'inheritable' that needs to
132      * be present and set to 'true'.
133      */
134     public static class FilteredComponentInheritancePredicate extends AbstractPredicate<Node> {
135 
136         public static final String INHERITED_PROPERTY_NAME = "inheritable";
137 
138         @Override
139         public boolean evaluateTyped(Node node) {
140             try {
141                 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT) && (node.hasProperty(INHERITED_PROPERTY_NAME) && Boolean.parseBoolean(node.getProperty(INHERITED_PROPERTY_NAME).getString()));
142             } catch (RepositoryException e) {
143                 throw new RuntimeRepositoryException(e);
144             }
145         }
146     }
147 
148     /**
149      * Predicate for component inheritance that includes all components.
150      */
151     public static class AllComponentsInheritancePredicate extends AbstractPredicate<Node> {
152 
153         @Override
154         public boolean evaluateTyped(Node node) {
155             try {
156                 return NodeUtil.isNodeType(node, MgnlNodeType.NT_COMPONENT);
157             } catch (RepositoryException e) {
158                 throw new RuntimeRepositoryException(e);
159             }
160         }
161     }
162 
163     /**
164      * Predicate for component inheritance that includes no components.
165      */
166     public static class InheritNothingInheritancePredicate extends AbstractPredicate<Node> {
167 
168         @Override
169         public boolean evaluateTyped(Node node) {
170             return false;
171         }
172     }
173 
174     /**
175      * Comparator for ordering nodes by depth placing inherited nodes first.
176      */
177     public static class NodeDepthComparator implements Comparator<Node> {
178         @Override
179         public int compare(Node lhs, Node rhs) {
180             try {
181                 return rhs.getDepth() - lhs.getDepth();
182             } catch (RepositoryException e) {
183                 throw new RuntimeRepositoryException(e);
184             }
185         }
186     }
187 }