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.RegistryMap;
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.Set;
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 {
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 final RegistryMap<String, TemplateDefinitionProvider> registry = new RegistryMap<String, TemplateDefinitionProvider>() {
73  
74          @Override
75          protected String keyFromValue(TemplateDefinitionProvider provider) {
76              return provider.getId();
77          }
78      };
79      private TemplateAvailability templateAvailability;
80  
81      @Inject
82      public TemplateDefinitionRegistry(TemplateAvailability templateAvailability) {
83          this.templateAvailability = templateAvailability;
84      }
85  
86      public TemplateDefinition getTemplateDefinition(String id) throws RegistrationException {
87  
88          TemplateDefinitionProvider templateDefinitionProvider;
89          try {
90              templateDefinitionProvider = registry.getRequired(id);
91          } catch (RegistrationException e) {
92              throw new RegistrationException("No template definition registered for id: " + id, e);
93          }
94  
95          return templateDefinitionProvider.getTemplateDefinition();
96      }
97  
98      /**
99       * @return all TemplateDefinitions - in case of errors it'll just deliver the ones that are properly registered and logs error's for the others.
100      */
101     public Collection<TemplateDefinition> getTemplateDefinitions() {
102         final Collection<TemplateDefinition> templateDefinitions = new ArrayList<TemplateDefinition>();
103         for (TemplateDefinitionProvider provider : registry.values()) {
104             try {
105                 final TemplateDefinition templateDefinition = provider.getTemplateDefinition();
106                 if (templateDefinition == null) {
107                     log.error("Provider's TemplateDefinition is null: " + provider);
108                 } else {
109                     templateDefinitions.add(templateDefinition);
110                 }
111             } catch (RegistrationException e) {
112                 log.error("Failed to read template definition from " + provider + ".", e);
113             }
114         }
115         return templateDefinitions;
116     }
117 
118     public void register(TemplateDefinitionProvider provider) {
119         registry.put(provider);
120     }
121 
122     public void unregister(String id) {
123         registry.remove(id);
124     }
125 
126     public Set<String> unregisterAndRegister(Collection<String> registeredIds, Collection<TemplateDefinitionProvider> providers) {
127         return registry.removeAndPutAll(registeredIds, providers);
128     }
129 
130     public Collection<TemplateDefinition> getAvailableTemplates(Node content) {
131 
132         try {
133             if (content != null && NodeUtil.hasMixin(content, MgnlNodeType.MIX_DELETED)) {
134                 return Collections.singleton(getTemplateDefinition(DELETED_PAGE_TEMPLATE));
135             }
136         } catch (RepositoryException e) {
137             log.error("Failed to check node for deletion status.", e);
138         } catch (RegistrationException e) {
139             log.error("Deleted content template is not correctly registered.", e);
140         }
141 
142         final ArrayList<TemplateDefinition> availableTemplateDefinitions = new ArrayList<TemplateDefinition>();
143         final Collection<TemplateDefinition> templateDefinitions = getTemplateDefinitions();
144         for (TemplateDefinition templateDefinition : templateDefinitions) {
145             if (isAvailable(templateDefinition, content)) {
146                 availableTemplateDefinitions.add(templateDefinition);
147             }
148         }
149         return availableTemplateDefinitions;
150     }
151 
152     protected boolean isAvailable(TemplateDefinition templateDefinition, Node content) {
153         return isVisible(templateDefinition) &&
154                 isPageTemplate(templateDefinition, content) &&
155                 templateAvailability.isAvailable(content, templateDefinition);
156 
157     }
158 
159     protected boolean isPageTemplate(TemplateDefinition templateDefinition, Node content) {
160         // TODO temporary fix for limiting only website to <moduleName>:pages/*
161         try {
162             return content.getSession().getWorkspace().getName().equals("website") &&
163                     StringUtils.substringAfter(templateDefinition.getId(), ":").startsWith("pages/");
164         } catch (RepositoryException e) {
165             return false;
166         }
167     }
168 
169     protected boolean isVisible(TemplateDefinition templateDefinition) {
170         return templateDefinition.getVisible() == null || templateDefinition.getVisible();
171     }
172 
173     /**
174      * Get the Template that could be used for the provided content as a default.
175      */
176     public TemplateDefinition getDefaultTemplate(Node content) {
177 
178         // try to use the same as the parent
179         TemplateDefinition parentTemplate = null;
180         try {
181             parentTemplate = getTemplateDefinition(content.getParent());
182         } catch (RepositoryException e) {
183             log.error("Failed to determine template assigned to parent of node: " + NodeUtil.getNodePathIfPossible(content), e);
184         }
185 
186         if (parentTemplate != null && templateAvailability.isAvailable(content, parentTemplate)) {
187             return parentTemplate;
188         }
189 
190         // otherwise use the first available template
191         Collection<TemplateDefinition> templates = getAvailableTemplates(content);
192         if (templates.isEmpty()) {
193             return null;
194         }
195 
196         return templates.iterator().next();
197     }
198 
199     private TemplateDefinition getTemplateDefinition(Node node) throws RepositoryException {
200         String templateId = MetaDataUtil.getTemplate(node);
201         if (StringUtils.isEmpty(templateId)) {
202             return null;
203         }
204         try {
205             // TODO Ioc
206             TemplateDefinitionAssignment templateDefinitionAssignment = Components.getComponent(TemplateDefinitionAssignment.class);
207             return templateDefinitionAssignment.getAssignedTemplateDefinition(node);
208         } catch (RegistrationException e) {
209             return null;
210         }
211     }
212 }