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 java.util.HashMap;
37 import java.util.Map;
38 import java.util.Map.Entry;
39
40 import javax.jcr.Node;
41 import javax.jcr.RepositoryException;
42
43 import org.apache.commons.lang.StringUtils;
44
45 import info.magnolia.cms.beans.config.ServerConfiguration;
46 import info.magnolia.context.MgnlContext;
47 import info.magnolia.context.WebContext;
48 import info.magnolia.rendering.context.RenderingContext;
49 import info.magnolia.rendering.engine.RenderException;
50
51
52
53
54
55
56
57 public abstract class AbstractContentTemplatingElement extends AbstractTemplatingElement {
58
59
60 private Node content;
61 private String workspace;
62 private String nodeIdentifier;
63 private String path;
64 private Map<String, Object> savedCtxAttributes = new HashMap<String, Object>();
65
66 public AbstractContentTemplatingElement(ServerConfiguration server, RenderingContext renderingContext) {
67 super(server, renderingContext);
68 }
69
70 protected String getNodePath(Node node) throws RenderException {
71 try {
72 return node.getSession().getWorkspace().getName() + ":" + node.getPath();
73 } catch (RepositoryException e) {
74 throw new RenderException("Can't construct node path for node " + node);
75 }
76 }
77
78 protected Node getTargetContent() 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
107
108 return currentContent();
109 }
110
111 public Node getContent() {
112 return content;
113 }
114
115 public void setContent(Node node) {
116 this.content = node;
117 }
118
119 public String getWorkspace() {
120 return workspace;
121 }
122
123 public void setWorkspace(String workspace) {
124 this.workspace = workspace;
125 }
126
127 public String getNodeIdentifier() {
128 return nodeIdentifier;
129 }
130
131 public void setNodeIdentifier(String nodeIdentifier) {
132 this.nodeIdentifier = nodeIdentifier;
133 }
134
135 public String getPath() {
136 return path;
137 }
138
139 public void setPath(String path) {
140 this.path = path;
141 }
142
143
144
145
146
147
148
149
150 protected void setAttributesInWebContext(final Map<String, Object> attributes, int scope) {
151 if(attributes == null){
152 return;
153 }
154 switch(scope) {
155 case WebContext.APPLICATION_SCOPE:
156 case WebContext.SESSION_SCOPE:
157 case WebContext.LOCAL_SCOPE:
158 break;
159 default:
160 throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
161 }
162 final WebContext webContext = MgnlContext.getWebContext();
163 for(Entry<String, Object> entry : attributes.entrySet()) {
164 final String key = entry.getKey();
165 if(webContext.containsKey(key)) {
166 savedCtxAttributes.put(key, webContext.get(key));
167 }
168 webContext.setAttribute(key, entry.getValue(), scope);
169 }
170 }
171
172
173
174
175
176
177
178 protected void restoreAttributesInWebContext(final Map<String, Object> attributes, int scope) {
179 if(attributes == null) {
180 return;
181 }
182 switch(scope) {
183 case WebContext.APPLICATION_SCOPE:
184 case WebContext.SESSION_SCOPE:
185 case WebContext.LOCAL_SCOPE:
186 break;
187 default:
188 throw new IllegalArgumentException("Scope is not valid. Use one of the scopes defined in info.magnolia.context.WebContext");
189 }
190 final WebContext webContext = MgnlContext.getWebContext();
191
192 for(Entry<String, Object> entry : attributes.entrySet()) {
193 final String key = entry.getKey();
194 if(webContext.containsKey(key) && savedCtxAttributes.get(key) != null) {
195 webContext.setAttribute(key, savedCtxAttributes.get(key), scope);
196 continue;
197 }
198 webContext.removeAttribute(key, scope);
199 }
200 }
201 }