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.cms.taglibs;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.NodeData;
38 import info.magnolia.cms.i18n.I18nContentSupport;
39 import info.magnolia.cms.i18n.I18nContentSupportFactory;
40 import info.magnolia.cms.util.ContentUtil;
41 import info.magnolia.context.MgnlContext;
42 import info.magnolia.repository.RepositoryConstants;
43
44 import javax.jcr.RepositoryException;
45 import javax.servlet.jsp.tagext.TagSupport;
46
47 import org.apache.commons.lang.StringUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57 public abstract class BaseContentTag extends TagSupport {
58 private final static Logger log = LoggerFactory.getLogger(BaseContentTag.class);
59
60 private String nodeDataName;
61
62 private Content contentNode;
63
64 private String contentNodeName;
65
66 private String contentNodeCollectionName;
67
68 private String uuid;
69
70 private String path;
71
72 private String repository = RepositoryConstants.WEBSITE;
73
74 private boolean inherit;
75
76
77
78
79 private boolean actpage;
80
81
82
83
84
85 public void setNodeDataName(String name) {
86 this.nodeDataName = name;
87 }
88
89
90
91
92
93
94
95
96
97 public void setContentNodeName(String name) {
98 this.contentNodeName = name;
99 }
100
101
102
103
104
105 public void setContentNodeCollectionName(String name) {
106 this.contentNodeCollectionName = name;
107 }
108
109
110
111
112
113 public void setInherit(boolean inherit) {
114 this.inherit = inherit;
115 }
116
117
118
119
120 @Deprecated
121 protected Content getFirtMatchingNode() {
122 return getFirstMatchingNode();
123 }
124
125
126
127
128
129 protected Content getFirstMatchingNode() {
130 if (actpage) {
131 return getCurrentPage();
132 }
133
134 Content contentNode = null;
135 if (this.getContentNode() != null) {
136 contentNode = this.getContentNode();
137 }
138 if (StringUtils.isNotEmpty(this.getUuid())) {
139 contentNode = ContentUtil.getContentByUUID(this.getRepository(), this.getUuid());
140 }
141
142 if (StringUtils.isNotEmpty(this.getPath())) {
143 contentNode = ContentUtil.getContent(this.getRepository(), this.getPath());
144 }
145
146 if (contentNode == null) {
147 Content currentPage = getCurrentPage();
148 contentNode = resolveNode(currentPage);
149
150 try {
151 while (inherit && currentPage.getLevel() > 0 && contentNode == null) {
152 currentPage = currentPage.getParent();
153 contentNode = resolveNode(currentPage);
154 }
155 }
156 catch (RepositoryException e) {
157 log.error(e.getMessage(), e);
158 }
159 }
160
161 if (contentNode == null) {
162 return null;
163 }
164
165 final I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
166 if (StringUtils.isNotEmpty(this.nodeDataName)) {
167 Content currentPage = getCurrentPage();
168 NodeData nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
169 try {
170 while (inherit && currentPage.getLevel() > 0 && !nodeData.isExist()) {
171 currentPage = currentPage.getParent();
172 contentNode = resolveNode(currentPage);
173 if (contentNode != null) {
174 nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
175 }
176 }
177 }
178 catch (RepositoryException e) {
179 log.error(e.getMessage(), e);
180 }
181 }
182
183 return contentNode;
184 }
185
186 protected Content getCurrentPage() {
187 return Resource.getCurrentActivePage();
188 }
189
190 protected Content resolveNode(Content currentPage) {
191 Content currentParagraph = MgnlContext.getAggregationState().getCurrentContent();
192
193 try {
194 if (StringUtils.isNotEmpty(contentNodeName)) {
195
196 if (StringUtils.isEmpty(contentNodeCollectionName)) {
197
198 return currentPage.getContent(contentNodeName);
199 }
200
201
202
203 return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName);
204 } else if (currentParagraph == null || currentParagraph.getHandle().equals(MgnlContext.getAggregationState().getMainContent().getHandle())) {
205
206 if (StringUtils.isEmpty(contentNodeCollectionName)) {
207
208
209
210 return currentPage;
211 }
212
213
214
215
216 if (currentPage.hasContent(contentNodeCollectionName)) {
217 return currentPage.getContent(contentNodeCollectionName);
218 }
219
220 } else {
221
222 if (contentNodeName == null && contentNodeCollectionName == null) {
223
224 return currentParagraph;
225 } else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
226 || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
227
228
229 return currentPage;
230 } else if (contentNodeCollectionName != null && !StringUtils.isEmpty(contentNodeCollectionName)) {
231 return currentParagraph.getContent(contentNodeCollectionName);
232 }
233 }
234 }
235 catch (RepositoryException re) {
236 log.debug(re.getMessage(), re);
237 }
238 return null;
239 }
240
241 @Override
242 public void release() {
243 super.release();
244
245 this.nodeDataName = null;
246 this.contentNodeName = null;
247 this.contentNodeCollectionName = null;
248 this.contentNode = null;
249 this.inherit = false;
250 this.actpage = false;
251 this.uuid = null;
252 this.path = null;
253 this.repository = RepositoryConstants.WEBSITE;
254 }
255
256 public boolean isActpage() {
257 return this.actpage;
258 }
259
260
261
262
263
264
265 @Deprecated
266 public void setActpage(boolean actpage) {
267 this.actpage = actpage;
268 }
269
270 public String getRepository() {
271 return this.repository;
272 }
273
274
275
276
277
278 public void setRepository(String repository) {
279 this.repository = repository;
280 }
281
282 public String getUuid() {
283 return this.uuid;
284 }
285
286
287
288
289
290
291 public void setUuid(String uuid) {
292 this.uuid = uuid;
293 }
294
295 public String getPath() {
296 return this.path;
297 }
298
299
300
301
302
303
304 public void setPath(String path) {
305 this.path = path;
306 }
307
308 public Content getContentNode() {
309 return this.contentNode;
310 }
311
312
313
314
315
316 public void setContentNode(Content content) {
317 this.contentNode = content;
318 }
319
320 protected String getNodeDataName() {
321 return nodeDataName;
322 }
323
324 protected String getContentNodeName() {
325 return contentNodeName;
326 }
327
328 protected String getContentNodeCollectionName() {
329 return contentNodeCollectionName;
330 }
331 }