View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Base class for implementing content tags (e.g. {@link ContentNodeIterator}).
54   * @author fgiust
55   * @version $Revision: 41137 $ ($Author: gjoseph $)
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       * This is historically. Meaning that we work on the page node itself even when we are in an iterator.
78       */
79      private boolean actpage;
80  
81      /**
82       * Set the node data name, e.g. "mainText".
83       * @jsp.attribute required="false" rtexprvalue="true"
84       */
85      public void setNodeDataName(String name) {
86          this.nodeDataName = name;
87      }
88  
89      /**
90       * Inside a "contentNodeIterator": if not set, the current content node is taken. If set empty
91       * (contentNodeName=""), the top level content is taken. If specified, the named content node is taken. Outside
92       * a "contentNodeIterator": if not set or empty: the top level content is taken. If specified, the named content
93       * node is taken.
94       *
95       * @jsp.attribute required="false" rtexprvalue="true"
96       */
97      public void setContentNodeName(String name) {
98          this.contentNodeName = name;
99      }
100 
101     /**
102      * Name of the collection holding the content node, e.g. "mainColumnParagraphs".
103      * @jsp.attribute required="false" rtexprvalue="true"
104      */
105     public void setContentNodeCollectionName(String name) {
106         this.contentNodeCollectionName = name;
107     }
108 
109     /**
110      * Inherit the value from parent pages, if not set in the current one.
111      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
112      */
113     public void setInherit(boolean inherit) {
114         this.inherit = inherit;
115     }
116 
117     /**
118      * @deprecated Use {@link #getFirstMatchingNode()} instead
119      */
120     protected Content getFirtMatchingNode() {
121         return getFirstMatchingNode();
122     }
123 
124     /**
125      * Get the first matching node containing a NodeData named <code>nodeDataName</code>.
126      * @return the active node, or the first matching one if nodedata is null and inherit is set.
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                 // contentNodeName is defined
195                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
196                     // e.g. <cms:out nodeDataName="title" contentNodeName="footer"/>
197                     return currentPage.getContent(contentNodeName);
198                 }
199 
200                 // e.g. <cms:out nodeDataName="title" contentNodeName="01" contentNodeCollectionName="mainPars"/>
201                 // e.g. <cms:out nodeDataName="title" contentNodeName="footer" contentNodeCollectionName=""/>
202                 return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName);
203             } else  if (currentParagraph == null || currentParagraph.getHandle().equals(MgnlContext.getAggregationState().getMainContent().getHandle())) {
204                 // outside collection iterator
205                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
206                     // e.g. <cms:out nodeDataName="title"/>
207                     // e.g. <cms:out nodeDataName="title" contentNodeName=""/>
208                     // e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
209                     return currentPage;
210                 }
211                 // ERROR: no content node assignable because contentNodeName is empty
212                 // e.g. <cms:out nodeDataName="title" contentNodeCollectionName="mainPars"/>
213 
214                 // but in this case we return contentNodeCollection if existent
215                 if (currentPage.hasContent(contentNodeCollectionName)) {
216                     return currentPage.getContent(contentNodeCollectionName);
217                 }
218 
219             } else {
220                 // inside collection iterator
221                 if (contentNodeName == null && contentNodeCollectionName == null) {
222                     // e.g. <cms:out nodeDataName="title"/>
223                     return currentParagraph;
224                 } else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
225                     || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
226                     // empty collection name -> use actpage
227                     // e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
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      * If true we work on the current active page instead of any other node.
260      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
261      * @deprecated
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      * Used if the uuid or path attribute is set. Defaults to "website".
273      * @jsp.attribute required="false" rtexprvalue="true"
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      * The uuid to use for finding the content.
285      * You must define the repository attribute if the content is not stored in the website repository.
286      * @jsp.attribute required="false" rtexprvalue="true"
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      * The absolute path to the content.
298      * You must define the repository attribute if the content is not stored in the website repository.
299      * @jsp.attribute required="false" rtexprvalue="true"
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      * The content object to use.
311      * @jsp.attribute required="false" rtexprvalue="true"
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 }