KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > taglib > content > ContentAttributeTag


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.taglib.content;
25
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29
30 import org.infoglue.cms.entities.management.LanguageVO;
31 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController;
32 import org.infoglue.deliver.taglib.component.ComponentLogicTag;
33 import org.infoglue.deliver.util.Support;
34
35 /**
36  * This is an attempt to make an TagLib for attempts to get a ContentAttribute from a content referenced by a component
37  * in a JSP.
38  *
39  * <%@ taglib uri="infoglue" prefix="infoglue" %>
40  *
41  * <infoglue:component.ContentAttribute propertyName="Article" attributeName="Title"/>
42  *
43  * @author Mattias Bogeblad
44  *
45  * 2005-12-22 Added mapKeyName which extracts a value from a properties.file formated text content. / per.jonsson@it-huset.se
46  *
47  *
48  */

49
50 public class ContentAttributeTag extends ComponentLogicTag
51 {
52     private static final long serialVersionUID = 3257850991142318897L;
53     
54     private Integer JavaDoc contentId;
55     private String JavaDoc propertyName;
56     private String JavaDoc attributeName;
57     private Integer JavaDoc languageId;
58     private String JavaDoc mapKeyName;
59     private boolean disableEditOnSight = false;
60     private boolean useInheritance = true;
61     private boolean useAttributeLanguageFallback = false;
62     private boolean parse = false;
63     private boolean fullBaseUrl = false;
64     
65     public ContentAttributeTag()
66     {
67         super();
68     }
69     
70     /**
71      * Initializes the parameters to make it accessible for the children tags (if any).
72      *
73      * @return indication of whether to evaluate the body or not.
74      * @throws JspException if an error occurred while processing this tag.
75      */

76     public int doStartTag() throws JspException JavaDoc
77     {
78         return EVAL_BODY_INCLUDE;
79     }
80
81     private String JavaDoc getContentAttributeValue(Integer JavaDoc languageId) throws JspException JavaDoc
82     {
83         String JavaDoc result = null;
84         
85         if(contentId != null)
86         {
87             if(!parse)
88             {
89                 result = getController().getContentAttribute(contentId, languageId, attributeName, disableEditOnSight);
90             }
91             else
92             {
93                 result = getController().getParsedContentAttribute(contentId, languageId, attributeName, disableEditOnSight);
94             }
95         }
96         else if(propertyName != null)
97         {
98             if(!parse)
99             {
100                 result = getComponentLogic().getContentAttribute(propertyName, languageId, attributeName, disableEditOnSight, useInheritance);
101             }
102             else
103             {
104                 result = getComponentLogic().getParsedContentAttribute(propertyName, languageId, attributeName, disableEditOnSight, useInheritance);
105             }
106         }
107         else
108         {
109             throw new JspException JavaDoc("You must specify either contentId or propertyName");
110         }
111
112         return result;
113     }
114     
115     public int doEndTag() throws JspException JavaDoc
116     {
117         if(this.languageId == null)
118             this.languageId = getController().getLanguageId();
119
120         boolean previousSetting = getController().getDeliveryContext().getUseFullUrl();
121         String JavaDoc result = null;
122         if(previousSetting != fullBaseUrl)
123         {
124             getController().getDeliveryContext().setUseFullUrl(fullBaseUrl);
125         }
126         // Have to force a disable editon sight, not good with renderstuff
127
// when converting a attributeto a map.
128
// per.jonsson@it-huset.se
129
if ( mapKeyName != null )
130         {
131             disableEditOnSight = true;
132         }
133         
134         result = getContentAttributeValue(this.languageId);
135         
136         if((result == null || result.trim().equals("")) && useAttributeLanguageFallback)
137         {
138             try
139             {
140                 LanguageVO masteLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(this.getController().getDatabase(), this.getController().getSiteNodeId());
141                 result = getContentAttributeValue(masteLanguageVO.getLanguageId());
142             }
143             catch(Exception JavaDoc e)
144             {
145                 throw new JspException JavaDoc("Error getting the master language for this sitenode:" + this.getController().getSiteNodeId());
146             }
147         }
148         
149         if ( mapKeyName != null && result != null )
150         {
151             Map JavaDoc map = Support.convertTextToProperties( result.toString() );
152             if ( map != null && !map.isEmpty() )
153             {
154                 result = (String JavaDoc)map.get( mapKeyName );
155             }
156         }
157         produceResult( result );
158         //Resetting the full url to the previous state
159
getController().getDeliveryContext().setUseFullUrl(previousSetting);
160
161         contentId = null;
162         propertyName = null;
163         attributeName = null;;
164         mapKeyName = null;;
165         disableEditOnSight = false;
166         useInheritance = true;
167         useAttributeLanguageFallback = true;
168         parse = false;
169         fullBaseUrl = false;
170         languageId = null;
171
172         return EVAL_PAGE;
173     }
174
175     public void setPropertyName(String JavaDoc propertyName) throws JspException JavaDoc
176     {
177         this.propertyName = evaluateString("contentAttribute", "propertyName", propertyName);
178     }
179     
180     public void setAttributeName(String JavaDoc attributeName) throws JspException JavaDoc
181     {
182         this.attributeName = evaluateString("contentAttribute", "attributeName", attributeName);
183     }
184     
185     public void setDisableEditOnSight(boolean disableEditOnSight)
186     {
187         this.disableEditOnSight = disableEditOnSight;
188     }
189     
190     public void setUseInheritance(boolean useInheritance)
191     {
192         this.useInheritance = useInheritance;
193     }
194     
195     public void setParse(boolean parse)
196     {
197         this.parse = parse;
198     }
199     
200     public void setContentId(final String JavaDoc contentId) throws JspException JavaDoc
201     {
202         this.contentId = evaluateInteger("contentAttribute", "contentId", contentId);
203     }
204     
205     public void setLanguageId(final String JavaDoc languageId) throws JspException JavaDoc
206     {
207         this.languageId = evaluateInteger("contentAttribute", "languageId", languageId);
208     }
209
210     public void setFullBaseUrl(boolean fullBaseUrl)
211     {
212         this.fullBaseUrl = fullBaseUrl;
213     }
214
215     public void setMapKeyName( String JavaDoc mapKeyName )
216     {
217         this.mapKeyName = mapKeyName;
218     }
219     
220     public void setUseAttributeLanguageFallback(boolean useAttributeLanguageFallback)
221     {
222         this.useAttributeLanguageFallback = useAttributeLanguageFallback;
223     }
224 }
Popular Tags