1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.rendering.context.RenderingContext;
40 import info.magnolia.rendering.engine.RenderException;
41
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.Map.Entry;
45
46 import javax.jcr.Node;
47 import javax.jcr.RepositoryException;
48
49 import org.apache.commons.lang.StringUtils;
50
51
52
53
54 public abstract class AbstractContentTemplatingElement extends AbstractTemplatingElement {
55
56
57 private Node content;
58 private String workspace;
59 private String nodeIdentifier;
60 private String path;
61 private final Map<String, Object> savedCtxAttributes = new HashMap<String, Object>();
62
63 public AbstractContentTemplatingElement(ServerConfiguration server, RenderingContext renderingContext) {
64 super(server, renderingContext);
65 }
66
67 protected String getNodePath(Node node) throws RenderException {
68 try {
69 return node.getSession().getWorkspace().getName() + ":" + node.getPath();
70 } catch (RepositoryException e) {
71 throw new RenderException("Can't construct node path for node " + node);
72 }
73 }
74
75
76
77
78 protected Node getPassedContent() throws RenderException {
79
80
81
82
83
84
85 if (content != null) {
86 return content;
87 }
88 if (StringUtils.isNotEmpty(workspace)) {
89 if (StringUtils.isNotEmpty(nodeIdentifier)) {
90 try {
91 return MgnlContext.getJCRSession(workspace).getNodeByIdentifier(nodeIdentifier);
92 } catch (RepositoryException e) {
93 throw new RenderException("Can't read content from workspace [" + workspace + "] with identifier [" + nodeIdentifier + "].");
94 }
95 }
96 if (StringUtils.isNotEmpty(path)) {
97 try {
98 return MgnlContext.getJCRSession(workspace).getNode(path);
99 } catch (RepositoryException e) {
100 throw new RenderException("Can't read content from workspace [" + workspace + "] with path [" + path + "].");
101 }
102 }
103 throw new IllegalArgumentException("Need to specify either uuid or path in combination with workspace");
104 }
105
106 return null;
107 }
108
109 public Node getContent() {
110 return content;
111 }
112
113 public void setContent(Node node) {
114 this.content = node;
115 }
116
117 public String getWorkspace() {
118 return workspace;
119 }
120
121 public void setWorkspace(String workspace) {
122 this.workspace = workspace;
123 }
124
125 public String getNodeIdentifier() {
126 return nodeIdentifier;
127 }
128
129 public void setNodeIdentifier(String nodeIdentifier) {
130 this.nodeIdentifier = nodeIdentifier;
131 }
132
133 public String getPath() {
134 return path;
135 }
136
137 public void setPath(String path) {
138 this.path = path;
139 }
140
141
142
143
144
145
146
147 protected void setAttributesInWebContext(final Map<String, Object> attributes, int scope) {
148 if (attributes == null) {
149 return;
150 }
151 switch (scope) {
152 case WebContext.APPLICATION_SCOPE:
153 case WebContext.SESSION_SCOPE:
154 case WebContext.LOCAL_SCOPE:
155 break;
156 default:
157 throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
158 }
159 final WebContext webContext = MgnlContext.getWebContext();
160 for (Entry<String, Object> entry : attributes.entrySet()) {
161 final String key = entry.getKey();
162 if (webContext.containsKey(key)) {
163 savedCtxAttributes.put(key, webContext.get(key));
164 }
165 webContext.setAttribute(key, entry.getValue(), scope);
166 }
167 }
168
169
170
171
172
173
174 protected void restoreAttributesInWebContext(final Map<String, Object> attributes, int scope) {
175 if (attributes == null) {
176 return;
177 }
178 switch (scope) {
179 case WebContext.APPLICATION_SCOPE:
180 case WebContext.SESSION_SCOPE:
181 case WebContext.LOCAL_SCOPE:
182 break;
183 default:
184 throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
185 }
186 final WebContext webContext = MgnlContext.getWebContext();
187
188 for (Entry<String, Object> entry : attributes.entrySet()) {
189 final String key = entry.getKey();
190 if (webContext.containsKey(key) && savedCtxAttributes.get(key) != null) {
191 webContext.setAttribute(key, savedCtxAttributes.get(key), scope);
192 continue;
193 }
194 webContext.removeAttribute(key, scope);
195 }
196 }
197
198
199
200
201 protected boolean renderComments() {
202 return isAdmin();
203 }
204 }