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.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.i18n.I18nContentSupportFactory;
40 import info.magnolia.cms.i18n.I18nContentSupport;
41 import info.magnolia.cms.util.ContentUtil;
42
43 import info.magnolia.context.MgnlContext;
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 = ContentRepository.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 protected Content getFirtMatchingNode() {
121 return getFirstMatchingNode();
122 }
123
124
125
126
127
128 protected Content getFirstMatchingNode() {
129 if (actpage) {
130 return getCurrentPage();
131 }
132
133 Content contentNode = null;
134 if (this.getContentNode() != null) {
135 contentNode = this.getContentNode();
136 }
137 if (StringUtils.isNotEmpty(this.getUuid())) {
138 contentNode = ContentUtil.getContentByUUID(this.getRepository(), this.getUuid());
139 }
140
141 if (StringUtils.isNotEmpty(this.getPath())) {
142 contentNode = ContentUtil.getContent(this.getRepository(), this.getPath());
143 }
144
145 if (contentNode == null) {
146 Content currentPage = getCurrentPage();
147 contentNode = resolveNode(currentPage);
148
149 try {
150 while (inherit && currentPage.getLevel() > 0 && contentNode == null) {
151 currentPage = currentPage.getParent();
152 contentNode = resolveNode(currentPage);
153 }
154 }
155 catch (RepositoryException e) {
156 log.error(e.getMessage(), e);
157 }
158 }
159
160 if (contentNode == null) {
161 return null;
162 }
163
164 final I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
165 if (StringUtils.isNotEmpty(this.nodeDataName)) {
166 Content currentPage = getCurrentPage();
167 NodeData nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
168 try {
169 while (inherit && currentPage.getLevel() > 0 && !nodeData.isExist()) {
170 currentPage = currentPage.getParent();
171 contentNode = resolveNode(currentPage);
172 if (contentNode != null) {
173 nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
174 }
175 }
176 }
177 catch (RepositoryException e) {
178 log.error(e.getMessage(), e);
179 }
180 }
181
182 return contentNode;
183 }
184
185 protected Content getCurrentPage() {
186 return Resource.getCurrentActivePage();
187 }
188
189 protected Content resolveNode(Content currentPage) {
190 Content currentParagraph = MgnlContext.getAggregationState().getCurrentContent();
191
192 try {
193 if (StringUtils.isNotEmpty(contentNodeName)) {
194
195 if (StringUtils.isEmpty(contentNodeCollectionName)) {
196
197 return currentPage.getContent(contentNodeName);
198 }
199
200
201
202 return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName);
203 } else if (currentParagraph == null || currentParagraph.getHandle().equals(MgnlContext.getAggregationState().getMainContent().getHandle())) {
204
205 if (StringUtils.isEmpty(contentNodeCollectionName)) {
206
207
208
209 return currentPage;
210 }
211
212
213
214
215 if (currentPage.hasContent(contentNodeCollectionName)) {
216 return currentPage.getContent(contentNodeCollectionName);
217 }
218
219 } else {
220
221 if (contentNodeName == null && contentNodeCollectionName == null) {
222
223 return currentParagraph;
224 } else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
225 || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
226
227
228 return currentPage;
229 } else if (contentNodeCollectionName != null && !StringUtils.isEmpty(contentNodeCollectionName)) {
230 return currentParagraph.getContent(contentNodeCollectionName);
231 }
232 }
233 }
234 catch (RepositoryException re) {
235 log.debug(re.getMessage(), re);
236 }
237 return null;
238 }
239
240 public void release() {
241 super.release();
242
243 this.nodeDataName = null;
244 this.contentNodeName = null;
245 this.contentNodeCollectionName = null;
246 this.contentNode = null;
247 this.inherit = false;
248 this.actpage = false;
249 this.uuid = null;
250 this.path = null;
251 this.repository = ContentRepository.WEBSITE;
252 }
253
254 public boolean isActpage() {
255 return this.actpage;
256 }
257
258
259
260
261
262
263 public void setActpage(boolean actpage) {
264 this.actpage = actpage;
265 }
266
267 public String getRepository() {
268 return this.repository;
269 }
270
271
272
273
274
275 public void setRepository(String repository) {
276 this.repository = repository;
277 }
278
279 public String getUuid() {
280 return this.uuid;
281 }
282
283
284
285
286
287
288 public void setUuid(String uuid) {
289 this.uuid = uuid;
290 }
291
292 public String getPath() {
293 return this.path;
294 }
295
296
297
298
299
300
301 public void setPath(String path) {
302 this.path = path;
303 }
304
305 public Content getContentNode() {
306 return this.contentNode;
307 }
308
309
310
311
312
313 public void setContentNode(Content content) {
314 this.contentNode = content;
315 }
316
317 protected String getNodeDataName() {
318 return nodeDataName;
319 }
320
321 protected String getContentNodeName() {
322 return contentNodeName;
323 }
324
325 protected String getContentNodeCollectionName() {
326 return contentNodeCollectionName;
327 }
328 }