View Javadoc

1   /**
2    * This file Copyright (c) 2012 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.templating.elements;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.WebContext;
41  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
42  import info.magnolia.registry.RegistrationException;
43  import info.magnolia.rendering.context.RenderingContext;
44  import info.magnolia.rendering.engine.AppendableOnlyOutputProvider;
45  import info.magnolia.rendering.engine.RenderException;
46  import info.magnolia.rendering.engine.RenderingEngine;
47  import info.magnolia.rendering.template.TemplateDefinition;
48  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
49  import info.magnolia.templating.freemarker.AreaDirective;
50  
51  import java.io.IOException;
52  import java.util.HashMap;
53  import java.util.Map;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  import javax.jcr.Session;
59  
60  import org.apache.commons.lang.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * Renders a piece of content.
66   *
67   * @version $Id$
68   */
69  public class ComponentElement extends AbstractContentTemplatingElement {
70  
71      private static final Logger log = LoggerFactory.getLogger(ComponentElement.class);
72  
73      private Map<String, Object> contextAttributes = new HashMap<String, Object>();
74      private final RenderingEngine renderingEngine;
75      private Node content;
76      private final TemplateDefinitionAssignment templateDefinitionAssignment;
77      private TemplateDefinition componentDefinition;
78  
79      private boolean renderEditbar = true;
80      private String dialog;
81      private Boolean editable;
82  
83      @Inject
84      public ComponentElement(ServerConfiguration server, RenderingContext renderingContext, RenderingEngine renderingEngine, TemplateDefinitionAssignment templateDefinitionAssignment ) {
85          super(server, renderingContext);
86          this.renderingEngine = renderingEngine;
87          this.templateDefinitionAssignment = templateDefinitionAssignment;
88      }
89  
90      @Override
91      public void begin(Appendable out) throws IOException, RenderException {
92  
93          content = getPassedContent();
94  
95          if(content == null) {
96              throw new RenderException("The 'content' or 'workspace' and 'path' attribute have to be set to render a component.");
97          }
98  
99          if(isAdmin() && hasPermission(content)){
100 
101             try {
102                 this.componentDefinition = templateDefinitionAssignment.getAssignedTemplateDefinition(content);
103             } catch (RegistrationException e) {
104                 throw new RenderException("No template definition found for the current content", e);
105             }
106 
107             final Messages messages = MessagesManager.getMessages(componentDefinition.getI18nBasename());
108 
109             if (isRenderEditbar()) {
110                 MarkupHelper helper = new MarkupHelper(out);
111 
112                 helper.openComment("cms:component");
113 
114                 helper.attribute(AreaDirective.CONTENT_ATTRIBUTE, getNodePath(content));
115 
116                 if(content instanceof InheritanceNodeWrapper) {
117                     if (((InheritanceNodeWrapper) content).isInherited()) {
118                         helper.attribute("inherited", "true");
119                     }
120                 }
121 
122                 this.editable = resolveEditable();
123                 if (this.editable != null) {
124                     helper.attribute("editable", String.valueOf(this.editable));
125                 }
126 
127                 if(StringUtils.isEmpty(dialog)) {
128                     dialog = resolveDialog();
129                 }
130                 helper.attribute("dialog", dialog);
131 
132                 String label = StringUtils.defaultIfEmpty(componentDefinition.getTitle(),componentDefinition.getName());
133                 helper.attribute("label", messages.getWithDefault(label, label));
134 
135                 if(StringUtils.isNotEmpty(componentDefinition.getDescription())){
136                     helper.attribute("description", componentDefinition.getDescription());
137                 }
138                 helper.append(" -->\n");
139             }
140         }
141 
142         // TODO not sure how to pass editable
143 
144         WebContext webContext = MgnlContext.getWebContext();
145         webContext.push(webContext.getRequest(), webContext.getResponse());
146         setAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
147 
148 
149         try {
150             if(componentDefinition != null) {
151                 renderingEngine.render(content, componentDefinition, new HashMap<String, Object>(), new AppendableOnlyOutputProvider(out));
152             } else {
153                 renderingEngine.render(content, new AppendableOnlyOutputProvider(out));
154             }
155         } finally {
156             webContext.pop();
157             webContext.setPageContext(null);
158             restoreAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
159         }
160     }
161 
162     private boolean hasPermission(Node node) {
163         try {
164             return node.getSession().hasPermission(node.getPath(), Session.ACTION_SET_PROPERTY);
165         } catch (RepositoryException e) {
166             log.error("Could not determine permission for node {}", node);
167         }
168         return false;
169     }
170 
171     private Boolean resolveEditable() {
172         return editable != null ? editable : componentDefinition != null && componentDefinition.getEditable() != null ? componentDefinition.getEditable() : null;
173     }
174 
175     @Override
176     public void end(Appendable out) throws IOException, RenderException {
177         if(isAdmin()){
178             if (renderEditbar) {
179                 new MarkupHelper(out).closeComment("cms:component");
180             }
181         }
182     }
183 
184     public Map<String, Object> getContextAttributes() {
185         return contextAttributes;
186     }
187 
188     public void setContextAttributes(Map<String, Object> contextAttributes) {
189         this.contextAttributes = contextAttributes;
190     }
191 
192     private String resolveDialog() {
193         if (StringUtils.isNotEmpty(this.dialog)) {
194             return this.dialog;
195         }
196         String dialog = componentDefinition.getDialog();
197         if (StringUtils.isNotEmpty(dialog)) {
198             return dialog;
199         }
200         return null;
201     }
202 
203     public void setDialog(String dialog) {
204         this.dialog = dialog;
205     }
206 
207     public void setEditable(Boolean editable) {
208         this.editable = editable;
209     }
210 
211     public Boolean getEditable() {
212         return editable;
213     }
214 
215     public boolean isRenderEditbar() {
216         return renderEditbar;
217     }
218 
219     public void setRenderEditbar(boolean renderEditbar) {
220         this.renderEditbar = renderEditbar;
221     }
222 }