View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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.context.MgnlContext;
38  import info.magnolia.context.WebContext;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.rendering.context.RenderingContext;
41  import info.magnolia.rendering.engine.RenderException;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Map.Entry;
46  
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  /**
53   * Abstract base class for elements that operate on a specified piece of content.
54   */
55  public abstract class AbstractContentTemplatingElement extends AbstractTemplatingElement {
56  
57      // TODO should also support a JSP ContentMap
58      private Node content;
59      private String workspace;
60      private String nodeIdentifier;
61      private String path;
62      private final Map<String, Object> savedCtxAttributes = new HashMap<String, Object>();
63  
64      public AbstractContentTemplatingElement(ServerConfiguration server, RenderingContext renderingContext) {
65          super(server, renderingContext);
66      }
67  
68      protected String getNodePath(Node node) throws RenderException {
69          try {
70              return node.getSession().getWorkspace().getName() + ":" + node.getPath();
71          } catch (RepositoryException e) {
72              throw new RenderException("Can't construct node path for node " + node);
73          }
74      }
75  
76      /**
77       * Returns the content passed to the element (content or workspace/path attribute) or null if nothing was passed.
78       */
79      protected Node getPassedContent() throws RenderException {
80  
81          // TODO should we default workspace to 'website' ?
82          // TODO should we be strict and fail on invalid combinations ?
83  
84          // TODO we can safely keep the node around after we've resolved it
85  
86          if (content != null) {
87              return content;
88          }
89          if (StringUtils.isNotEmpty(workspace)) {
90              if (StringUtils.isNotEmpty(nodeIdentifier)) {
91                  try {
92                      return MgnlContext.getJCRSession(workspace).getNodeByIdentifier(nodeIdentifier);
93                  } catch (RepositoryException e) {
94                      throw new RenderException("Can't read content from workspace [" + workspace + "] with identifier [" + nodeIdentifier + "].");
95                  }
96              }
97              if (StringUtils.isNotEmpty(path)) {
98                  try {
99                      return MgnlContext.getJCRSession(workspace).getNode(path);
100                 } catch (RepositoryException e) {
101                     throw new RenderException("Can't read content from workspace [" + workspace + "] with path [" + path + "].");
102                 }
103             }
104             throw new IllegalArgumentException("Need to specify either uuid or path in combination with workspace");
105         }
106 
107         return null;
108     }
109 
110     public Node getContent() {
111         return content;
112     }
113 
114     public void setContent(Node node) {
115         this.content = node;
116     }
117 
118     public String getWorkspace() {
119         return workspace;
120     }
121 
122     public void setWorkspace(String workspace) {
123         this.workspace = workspace;
124     }
125 
126     public String getNodeIdentifier() {
127         return nodeIdentifier;
128     }
129 
130     public void setNodeIdentifier(String nodeIdentifier) {
131         this.nodeIdentifier = nodeIdentifier;
132     }
133 
134     public String getPath() {
135         return path;
136     }
137 
138     public void setPath(String path) {
139         this.path = path;
140     }
141 
142     /**
143      * Sets attributes in web context under the specified scope. If an attribute already exists its value will be overwritten
144      * with the new one and the old value saved for subsequent restore.
145      *
146      * @param scope one of {@link info.magnolia.context.Context#APPLICATION_SCOPE} {@link info.magnolia.context.Context#SESSION_SCOPE} {@link info.magnolia.context.Context#LOCAL_SCOPE}.
147      */
148     protected void setAttributesInWebContext(final Map<String, Object> attributes, int scope) {
149         if (attributes == null) {
150             return;
151         }
152         switch (scope) {
153         case WebContext.APPLICATION_SCOPE:
154         case WebContext.SESSION_SCOPE:
155         case WebContext.LOCAL_SCOPE:
156             break;
157         default:
158             throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
159         }
160         final WebContext webContext = MgnlContext.getWebContext();
161         for (Entry<String, Object> entry : attributes.entrySet()) {
162             final String key = entry.getKey();
163             if (webContext.containsKey(key)) {
164                 savedCtxAttributes.put(key, webContext.get(key));
165             }
166             webContext.setAttribute(key, entry.getValue(), scope);
167         }
168     }
169 
170     /**
171      * Restores the original values of attributes in web context under the specified scope.
172      *
173      * @param scope one of {@link info.magnolia.context.Context#APPLICATION_SCOPE} {@link info.magnolia.context.Context#SESSION_SCOPE} {@link info.magnolia.context.Context#LOCAL_SCOPE}.
174      */
175     protected void restoreAttributesInWebContext(final Map<String, Object> attributes, int scope) {
176         if (attributes == null) {
177             return;
178         }
179         switch (scope) {
180         case WebContext.APPLICATION_SCOPE:
181         case WebContext.SESSION_SCOPE:
182         case WebContext.LOCAL_SCOPE:
183             break;
184         default:
185             throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
186         }
187         final WebContext webContext = MgnlContext.getWebContext();
188 
189         for (Entry<String, Object> entry : attributes.entrySet()) {
190             final String key = entry.getKey();
191             if (webContext.containsKey(key) && savedCtxAttributes.get(key) != null) {
192                 webContext.setAttribute(key, savedCtxAttributes.get(key), scope);
193                 continue;
194             }
195             webContext.removeAttribute(key, scope);
196         }
197     }
198 
199     /**
200      * Override to set conditions for rendering of cms:comments.
201      */
202     protected boolean renderComments() {
203         return isAdmin();
204     }
205 
206     protected String getActivationStatus(Node node) {
207         int status = NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
208 
209         if (node != null) {
210             try {
211                 status = NodeTypes.Activatable.getActivationStatus(node);
212             } catch (RepositoryException e) {
213                 // page has no activation status
214             }
215         }
216         return String.valueOf(status);
217     }
218 }