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      /**
95       * @return all TemplateDefinitions - in case of errors it'll just deliver the ones that are properly registerd and logs error's for the others.
96       */
97      public Collection<TemplateDefinition> getTemplateDefinitions() {
98          final Collection<TemplateDefinition> templateDefinitions = new ArrayList<TemplateDefinition>();
99          final Map<String, TemplateDefinitionProvider> providers = getProviders();
100         synchronized (providers) {
101             for (Map.Entry<String, TemplateDefinitionProvider> entry : providers.entrySet()) {
102                 final String id = entry.getKey();
103                 final TemplateDefinitionProvider provider = entry.getValue();
104                 try {
105                     final TemplateDefinition templateDefinition = provider.getDefinition();
106                     if (templateDefinition == null) {
107                         log.error("Provider's TemplateDefinition is null: " + provider);
108                     } else {
109                         templateDefinition.setId(id);
110                         templateDefinitions.add(templateDefinition);
111                     }
112                 } catch (RegistrationException e) {
113                     log.error("Failed to read template definition from " + provider + ".", e);
114                 }
115             }
116         }
117         return templateDefinitions;
118     }
119 
120     public Collection<TemplateDefinition> getAvailableTemplates(Node content) {
121 
122         try {
123             if (content != null && NodeUtil.hasMixin(content, MgnlNodeType.MIX_DELETED)) {
124                 return Collections.singleton(get(DELETED_PAGE_TEMPLATE));
125             }
126         } catch (RepositoryException e) {
127             log.error("Failed to check node for deletion status.", e);
128         } catch (RegistrationException e) {
129             log.error("Deleted content template is not correctly registered.", e);
130         }
131 
132         final ArrayList<TemplateDefinition> availableTemplateDefinitions = new ArrayList<TemplateDefinition>();
133         final Collection<TemplateDefinition> templateDefinitions = getTemplateDefinitions();
134         for (TemplateDefinition templateDefinition : templateDefinitions) {
135             if (isAvailable(templateDefinition, content)) {
136                 availableTemplateDefinitions.add(templateDefinition);
137             }
138         }
139         return availableTemplateDefinitions;
140     }
141 
142     protected boolean isAvailable(TemplateDefinition templateDefinition, Node content) {
143         if (templateDefinition.getId().equals(DELETED_PAGE_TEMPLATE)) {
144             return false;
145         }
146         // TODO temporary fix for limiting only website to <moduleName>:pages/*
147         try {
148             if (content.getSession().getWorkspace().getName().equals("website") && !StringUtils.substringAfter(templateDefinition.getId(), ":").startsWith("pages/")) {
149                 return false;
150             }
151         } catch (RepositoryException e) {
152         }
153         return templateAvailability.isAvailable(content, templateDefinition);
154     }
155 
156     /**
157      * Get the Template that could be used for the provided content as a default.
158      */
159     public TemplateDefinition getDefaultTemplate(Node content) {
160 
161         // try to use the same as the parent
162         TemplateDefinition parentTemplate = null;
163         try {
164             parentTemplate = getTemplateDefinition(content.getParent());
165         } catch (RepositoryException e) {
166             log.error("Failed to determine template assigned to parent of node: " + NodeUtil.getNodePathIfPossible(content), e);
167         }
168 
169         if (parentTemplate != null && templateAvailability.isAvailable(content, parentTemplate)) {
170             return parentTemplate;
171         }
172 
173         // otherwise use the first available template
174         Collection<TemplateDefinition> templates = getAvailableTemplates(content);
175         if (templates.isEmpty()) {
176             return null;
177         }
178 
179         return templates.iterator().next();
180     }
181 
182     private TemplateDefinition getTemplateDefinition(Node node) throws RepositoryException {
183         String templateId = MetaDataUtil.getTemplate(node);
184         if (StringUtils.isEmpty(templateId)) {
185             return null;
186         }
187         try {
188             // TODO Ioc
189             TemplateDefinitionAssignment templateDefinitionAssignment = Components.getComponent(TemplateDefinitionAssignment.class);
190             return templateDefinitionAssignment.getAssignedTemplateDefinition(node);
191         } catch (RegistrationException e) {
192             return null;
193         }
194     }
195 }