View Javadoc

1   /**
2    * This file Copyright (c) 2011-2013 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.assignment;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.cms.security.PermissionUtil;
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.jcr.util.MetaDataUtil;
42  import info.magnolia.jcr.util.NodeUtil;
43  import info.magnolia.registry.RegistrationException;
44  import info.magnolia.rendering.template.TemplateAvailability;
45  import info.magnolia.rendering.template.TemplateDefinition;
46  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
47  
48  import java.util.Collection;
49  import java.util.Collections;
50  import java.util.Map;
51  import java.util.TreeMap;
52  
53  import javax.inject.Inject;
54  import javax.inject.Singleton;
55  import javax.jcr.Node;
56  import javax.jcr.RepositoryException;
57  import javax.jcr.Session;
58  
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  /**
63   * Uses the template id stored in the node's meta data.
64   * 
65   * @version $Id$
66   */
67  @Singleton
68  public class MetaDataBasedTemplateDefinitionAssignment implements TemplateDefinitionAssignment {
69  
70      private static final String DELETED_PAGE_TEMPLATE = "adminInterface:mgnlDeleted";
71  
72      private static final Logger log = LoggerFactory.getLogger(MetaDataBasedTemplateDefinitionAssignment.class);
73  
74      private TemplateDefinitionRegistry templateDefinitionRegistry;
75  
76      @Inject
77      public MetaDataBasedTemplateDefinitionAssignment(TemplateDefinitionRegistry templateDefinitionRegistry) {
78          this.templateDefinitionRegistry = templateDefinitionRegistry;
79      }
80  
81      @Override
82      public TemplateDefinition getAssignedTemplateDefinition(Node content) throws RegistrationException {
83          final String templateId = MetaDataUtil.getMetaData(content).getTemplate();
84          return templateDefinitionRegistry.getTemplateDefinition(templateId);
85      }
86  
87      /**
88       * Get the Template that could be used for the provided content as a default.
89       */
90      @Override
91      public TemplateDefinition getDefaultTemplate(Node content) {
92  
93          // try to use the same as the parent
94          TemplateDefinition parentTemplate = null;
95          try {
96              parentTemplate = getAssignedTemplateDefinition(content.getParent());
97          } catch (RepositoryException e) {
98              log.error("Failed to determine template assigned to parent of node: " + NodeUtil.getNodePathIfPossible(content), e);
99          } catch (RegistrationException e) {
100             // No template assigned or the assigned template is missing
101         }
102 
103         if (parentTemplate != null && isAvailable(content, parentTemplate)) {
104             return parentTemplate;
105         }
106 
107         // otherwise use the first available template
108         Collection<TemplateDefinition> templates = getAvailableTemplates(content);
109         if (templates.isEmpty()) {
110             return null;
111         }
112 
113         return templates.iterator().next();
114     }
115 
116     @Override
117     public Collection<TemplateDefinition> getAvailableTemplates(Node content) {
118 
119         try {
120             if (content != null && NodeUtil.hasMixin(content, MgnlNodeType.MIX_DELETED)) {
121                 return Collections.singleton(templateDefinitionRegistry.getTemplateDefinition(DELETED_PAGE_TEMPLATE));
122             }
123         } catch (RepositoryException e) {
124             log.error("Failed to check node for deletion status.", e);
125         } catch (RegistrationException e) {
126             log.error("Deleted content template is not correctly registered.", e);
127         }
128         final Map<String, TemplateDefinition> availableTemplateDefinitions = new TreeMap<String, TemplateDefinition>();
129         final Collection<TemplateDefinition> templateDefinitions = templateDefinitionRegistry.getTemplateDefinitions();
130         for (TemplateDefinition templateDefinition : templateDefinitions) {
131             if (isTemplateAvailable(content, templateDefinition)) {
132                 Messages messages = MessagesManager.getMessages(templateDefinition.getI18nBasename());
133                 String key = messages.getWithDefault(templateDefinition.getTitle(), templateDefinition.getTitle());
134                 availableTemplateDefinitions.put(key, templateDefinition);
135             }
136         }
137         return availableTemplateDefinitions.values();
138     }
139 
140     protected boolean isTemplateAvailable(Node content, TemplateDefinition templateDefinition) {
141         return hasReadAccess(content) &&
142                 isVisible(templateDefinition) &&
143                 isAvailable(content, templateDefinition);
144     }
145 
146     protected boolean isVisible(TemplateDefinition templateDefinition) {
147         return templateDefinition.getVisible() == null || templateDefinition.getVisible();
148     }
149 
150     protected boolean isAvailable(Node content, TemplateDefinition templateDefinition) {
151         TemplateAvailability templateAvailability = templateDefinition.getTemplateAvailability();
152         return templateAvailability == null || templateAvailability.isAvailable(content, templateDefinition);
153     }
154 
155     protected boolean hasReadAccess(Node content) {
156         try {
157             // should not fact that we are able to get path already show that we can read this node???
158             // ... unless of course this "content" was created with system session ... so make sure we check using user session and not the node session
159             return PermissionUtil.isGranted(
160                     MgnlContext.getJCRSession(content.getSession().getWorkspace().getName()),
161                     content.getPath(),
162                     Session.ACTION_READ);
163         } catch (RepositoryException e) {
164             return false;
165         }
166     }
167 }