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