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.registry;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.jcr.util.MetaDataUtil;
38  import info.magnolia.jcr.util.NodeUtil;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.registry.AbstractRegistry;
41  import info.magnolia.registry.RegistrationException;
42  import info.magnolia.rendering.template.TemplateDefinition;
43  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.Collections;
48  import java.util.Map;
49  
50  import javax.inject.Inject;
51  import javax.inject.Singleton;
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  
55  import org.apache.commons.lang.StringUtils;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  
60  /**
61   * The central registry of all {@link TemplateDefinition}s.
62   *
63   * @version $Id$
64   */
65  @Singleton
66  public class TemplateDefinitionRegistry extends AbstractRegistry<TemplateDefinition, TemplateDefinitionProvider>{
67  
68      private static final Logger log = LoggerFactory.getLogger(TemplateDefinitionRegistry.class);
69      //FIXME this probably should not be hardcoded.
70      private static final String DELETED_PAGE_TEMPLATE = "adminInterface:mgnlDeleted";
71  
72      private TemplateAvailability templateAvailability;
73  
74      @Inject
75      public TemplateDefinitionRegistry(TemplateAvailability templateAvailability) {
76          this.templateAvailability = templateAvailability;
77      }
78  
79      public TemplateDefinition getTemplateDefinition(String id) throws RegistrationException {
80  
81          TemplateDefinitionProvider templateDefinitionProvider;
82          Map<String, TemplateDefinitionProvider> providers = getProviders();
83          synchronized (providers) {
84              templateDefinitionProvider = providers.get(id);
85          }
86          if (templateDefinitionProvider == null) {
87              throw new RegistrationException("No TemplateDefinition registered for id: " + id + ", available ids are " + providers.keySet());
88          }
89          TemplateDefinition templateDefinition = templateDefinitionProvider.getDefinition();
90          templateDefinition.setId(id);
91          return templateDefinition;
92      }
93  
94      public Collection<TemplateDefinition> getTemplateDefinitions() {
95          Collection<TemplateDefinition> templateDefinitions = new ArrayList<TemplateDefinition>();
96          Map<String, TemplateDefinitionProvider> providers = getProviders();
97          synchronized (providers) {
98              for (Map.Entry<String, TemplateDefinitionProvider> entry : providers.entrySet()) {
99                  String id = entry.getKey();
100                 TemplateDefinitionProvider provider = entry.getValue();
101                 try {
102                     TemplateDefinition templateDefinition = provider.getDefinition();
103                     templateDefinition.setId(id);
104                     templateDefinitions.add(templateDefinition);
105                 } catch (RegistrationException e) {
106                     // one failing provider is no reason to not show any templates
107                     log.error("Failed to read template definition from " + provider + ".", e);
108                 }
109             }
110         }
111         return templateDefinitions;
112     }
113 
114     public Collection<TemplateDefinition> getAvailableTemplates(Node content) {
115 
116         try {
117             if (content != null && NodeUtil.hasMixin(content, MgnlNodeType.MIX_DELETED)) {
118                 return Collections.singleton(get(DELETED_PAGE_TEMPLATE));
119             }
120         } catch (RepositoryException e) {
121             log.error("Failed to check node for deletion status.", e);
122         } catch (RegistrationException e) {
123             log.error("Deleted content template is not correctly registered.", e);
124         }
125 
126         final ArrayList<TemplateDefinition> availableTemplateDefinitions = new ArrayList<TemplateDefinition>();
127         final Collection<TemplateDefinition> templateDefinitions = getTemplateDefinitions();
128         for (TemplateDefinition templateDefinition : templateDefinitions) {
129             if (isAvailable(templateDefinition, content)) {
130                 availableTemplateDefinitions.add(templateDefinition);
131             }
132         }
133         return availableTemplateDefinitions;
134     }
135 
136     protected boolean isAvailable(TemplateDefinition templateDefinition, Node content) {
137         if (templateDefinition.getId().equals(DELETED_PAGE_TEMPLATE)) {
138             return false;
139         }
140         // TODO temporary fix for limiting only website to <moduleName>:pages/*
141         try {
142             if (content.getSession().getWorkspace().getName().equals("website") && !StringUtils.substringAfter(templateDefinition.getId(), ":").startsWith("pages/")) {
143                 return false;
144             }
145         } catch (RepositoryException e) {
146         }
147         return templateAvailability.isAvailable(content, templateDefinition);
148     }
149 
150     /**
151      * Get the Template that could be used for the provided content as a default.
152      */
153     public TemplateDefinition getDefaultTemplate(Node content) {
154 
155         // try to use the same as the parent
156         TemplateDefinition parentTemplate = null;
157         try {
158             parentTemplate = getTemplateDefinition(content.getParent());
159         } catch (RepositoryException e) {
160             log.error("Failed to determine template assigned to parent of node: " + NodeUtil.getNodePathIfPossible(content), e);
161         }
162 
163         if (parentTemplate != null && templateAvailability.isAvailable(content, parentTemplate)) {
164             return parentTemplate;
165         }
166 
167         // otherwise use the first available template
168         Collection<TemplateDefinition> templates = getAvailableTemplates(content);
169         if (templates.isEmpty()) {
170             return null;
171         }
172 
173         return templates.iterator().next();
174     }
175 
176     private TemplateDefinition getTemplateDefinition(Node node) throws RepositoryException {
177         String templateId = MetaDataUtil.getTemplate(node);
178         if (StringUtils.isEmpty(templateId)) {
179             return null;
180         }
181         try {
182             // TODO Ioc
183             TemplateDefinitionAssignment templateDefinitionAssignment = Components.getComponent(TemplateDefinitionAssignment.class);
184             return templateDefinitionAssignment.getAssignedTemplateDefinition(node);
185         } catch (RegistrationException e) {
186             return null;
187         }
188     }
189 }