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