KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > content > ContentByIdTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.content;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.ConvertUtil;
20 import com.blandware.atleap.common.util.StringUtil;
21 import com.blandware.atleap.model.core.ContentField;
22 import com.blandware.atleap.model.core.ContentFieldValue;
23 import com.blandware.atleap.service.core.ContentFieldManager;
24 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor;
25 import com.blandware.atleap.webapp.util.core.ApplicationResources;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.taglib.TagUtils;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.web.context.support.WebApplicationContextUtils;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.jsp.JspException JavaDoc;
37 import javax.servlet.jsp.JspTagException JavaDoc;
38 import javax.servlet.jsp.PageContext JavaDoc;
39 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
40 import java.util.Locale JavaDoc;
41
42 /**
43  * <p>Searches for content field value of content field with specified identifier
44  * that belongs to {@link com.blandware.atleap.model.core.Localizable} with specified ID. Result is saved
45  * to variable or written to page.
46  * </p>
47  * <p>
48  * If nothing was found (or value is empty string), then message like "The field
49  * value is empty, please insert the text" will be shown instead, if site is in
50  * edit mode OR <b>ignore</b> is <code>false</code>.
51  * </p>
52  * <p>
53  * Allowed attributes are:
54  * <ul>
55  * <li>
56  * <b>localizableId</b> - ID of localizable to which desired content field belongs
57  * </li>
58  * <li>
59  * <b>identifier</b> - identifier of content field which value is needed
60  * </li>
61  * <li>
62  * <b>locale</b> - identifier of content locale to use. If not given, then
63  * user content locale is tried. If not found, default one is used.
64  * </li>
65  * <li>
66  * <b>ignore</b> - if this attribute is set to <code>false</code> (or omitted)
67  * and no value is found (or it's empty string), then a message like "The field
68  * value is empty, please insert the text" will be treated as result. This can
69  * be "true" or "false", default is "false".
70  * </li>
71  * <li>
72  * <b>filter</b> - whether to replace characters, which are sensitive in HTML,
73  * with their entity equivalents.
74  * </li>
75  * <li>
76  * <b>var</b> - name of scope variable to store result to
77  * </li>
78  * <li>
79  * <b>scope</b> - scope of variable
80  * </li>
81  * <li>
82  * <b>editable</b> - whether a wrapper need to be generated that will allow
83  * user to edit content field value.
84  * </li>
85  * </ul>
86  * </p>
87  * <p><a HREF="ContentByIdTag.java.htm"><i>View Source</i></a></p>
88  *
89  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
90  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
91  * @version $Revision: 1.20 $ $Date: 2005/10/24 15:06:19 $
92  * @jsp.tag name="contentById"
93  * body-content="empty"
94  * @see org.apache.struts.Globals#LOCALE_KEY
95  */

96 public class ContentByIdTag extends SimpleTagSupport JavaDoc {
97
98     protected transient final Log log = LogFactory.getLog(ContentByIdTag.class);
99     protected static final String JavaDoc NUMBER_KEY = "com.blandware.atleap.taglib.content.CONTENT_BY_ID_TAG_NUMBER";
100
101     protected ApplicationContext applicationCtx = null;
102
103     /**
104      * ID of localizable to retrieve content field value for
105      */

106     protected Long JavaDoc localizableId;
107
108     /**
109      * Identifier of content field to retrieve value of
110      */

111     protected String JavaDoc identifier;
112
113     /**
114      * Locale to get value for. By default it is taken from session
115      */

116     protected String JavaDoc locale;
117
118     /**
119      * If this attribute is set to <code>false</code> (or omitted) and no value
120      * is found (or it's empty string), then a message like "The field value is
121      * empty, please insert the text" will be treated as result.
122      */

123     protected Boolean JavaDoc ignore = Boolean.FALSE;
124
125     /**
126      * Whether or not to replace characters, which are sensitive in HTML, with their entity equivalents.<br />
127      */

128     protected Boolean JavaDoc filter = null;
129
130     /**
131      * Name of variable to export content field value
132      */

133     protected String JavaDoc var;
134
135     /**
136      * Scope to export content field value to
137      */

138     protected String JavaDoc scope;
139
140     /**
141      * If this value is <code>Boolean.TRUE</code> content will be wrapped in &lt;span&gt;&lt;/span&gt; tags
142      * to provide access to editing content while in edit mode.
143      */

144     protected Boolean JavaDoc editable = Boolean.TRUE;
145
146
147     /**
148      * Returns localizable ID
149      *
150      * @return localizable ID
151      * @jsp.attribute required="true"
152      * rtexprvalue="true"
153      * type="java.lang.Long"
154      * description="ID of localizable to retrieve content for"
155      */

156     public Long JavaDoc getLocalizableId() {
157         return localizableId;
158     }
159
160     /**
161      * Sets localizable ID
162      *
163      * @param localizableId localizable ID to set
164      */

165     public void setLocalizableId(Long JavaDoc localizableId) {
166         this.localizableId = localizableId;
167     }
168
169     /**
170      * Returns identifier of field
171      *
172      * @return identifier of field
173      * @jsp.attribute required="true"
174      * rtexprvalue="true"
175      * type="java.lang.String"
176      * description="Identifier of field to retrieve content of"
177      */

178     public String JavaDoc getIdentifier() {
179         return identifier;
180     }
181
182     /**
183      * Sets identifier of field
184      *
185      * @param identifier identifier of field to set
186      */

187     public void setIdentifier(String JavaDoc identifier) {
188         this.identifier = identifier;
189     }
190
191     /**
192      * Returns locale
193      *
194      * @return locale
195      * @jsp.attribute required="false"
196      * rtexprvalue="true"
197      * type="java.lang.String"
198      * description="Locale to get value for"
199      */

200     public String JavaDoc getLocale() {
201         return locale;
202     }
203
204     /**
205      * Sets locale
206      *
207      * @param locale locale to set
208      */

209     public void setLocale(String JavaDoc locale) {
210         this.locale = locale;
211     }
212
213     /**
214      * Returns whether to suppress output of message like "The field value is
215      * empty, please insert the text" for empty or absent CFV while not in edit
216      * mode
217      *
218      * @return whether to suppress 'empty CFV' message while not in edit mode
219      * @see #ignore
220      * @jsp.attribute required="false"
221      * rtexprvalue="true"
222      * type="java.lang.Boolean"
223      * description="Whether to suppress 'empty CFV' message while not in edit mode"
224      */

225     public Boolean JavaDoc getIgnore() {
226         return ignore;
227     }
228
229     /**
230      * Sets whether to suppress output of message like "The field value is
231      * empty, please insert the text" for empty or absent CFV while not in edit
232      * mode
233      *
234      * @param ignore whether to suppress 'empty CFV' message while not in edit mode
235      * @see #ignore
236      */

237     public void setIgnore(Boolean JavaDoc ignore) {
238         this.ignore = ignore;
239     }
240
241     /**
242      * Returns whether or not to replace characters, which are sensitive in
243      * HTML, with their entity equivalents.
244      *
245      * @return whether or not to replace characters, which are sensitive in HTML
246      * @jsp.attribute required="false"
247      * rtexprvalue="true"
248      * type="java.lang.Boolean"
249      * description="Whether or not to replace characters, which are sensitive in HTML, with their entity equivalents"
250      */

251     public Boolean JavaDoc getFilter() {
252         return filter;
253     }
254
255     /**
256      * Sets whether or not to replace characters, which are sensitive in HTML,
257      * with their entity equivalents.
258      *
259      * @param filter whether or not to replace characters, which are sensitive in HTML
260      */

261     public void setFilter(Boolean JavaDoc filter) {
262         this.filter = filter;
263     }
264
265     /**
266      * Returns name of variable that will accept found value
267      *
268      * @return name of variable
269      * @jsp.attribute required="false"
270      * rtexprvalue="true"
271      * type="java.lang.String"
272      * description="Name of variable to export message"
273      */

274     public String JavaDoc getVar() {
275         return var;
276     }
277
278     /**
279      * Sets name of variable that will accept found value
280      *
281      * @param var name of variable to set
282      */

283     public void setVar(String JavaDoc var) {
284         this.var = var;
285     }
286
287     /**
288      * Returns variable scope
289      *
290      * @return variable scope
291      * @jsp.attribute required="false"
292      * rtexprvalue="true"
293      * type="java.lang.String"
294      * description="Scope to export variable to"
295      */

296     public String JavaDoc getScope() {
297         return scope;
298     }
299
300     /**
301      * Sets variable scope
302      *
303      * @param scope variable scope to scope
304      */

305     public void setScope(String JavaDoc scope) {
306         this.scope = scope;
307     }
308
309     /**
310      * Returns whether this value is editable
311      *
312      * @return whether this value is editable
313      * @see #editable
314      * @jsp.attribute required="false"
315      * rtexprvalue="true"
316      * type="java.lang.Boolean"
317      * description="Is content editable or not"
318      */

319     public Boolean JavaDoc getEditable() {
320         return editable;
321     }
322
323     /**
324      * Sets whether this value is editable
325      *
326      * @param editable whether this value is editable
327      * @see #editable
328      */

329     public void setEditable(Boolean JavaDoc editable) {
330         this.editable = editable;
331     }
332
333     /**
334      * Processes the tag
335      *
336      * @throws JspException
337      */

338     public void doTag() throws JspException JavaDoc {
339         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
340         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
341         ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext());
342
343         if ( applicationCtx == null ) {
344             applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
345         }
346
347         // calculate tag number
348
Integer JavaDoc tagNumber = (Integer JavaDoc) pageContext.getAttribute(NUMBER_KEY, PageContext.REQUEST_SCOPE);
349         if ( tagNumber == null ) {
350             tagNumber = new Integer JavaDoc(0);
351         } else {
352             tagNumber = new Integer JavaDoc(tagNumber.intValue() + 1);
353         }
354
355         pageContext.setAttribute(NUMBER_KEY, tagNumber, PageContext.REQUEST_SCOPE);
356
357         //get locale
358
if ( locale == null ) {
359             Locale JavaDoc l = (Locale JavaDoc) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE);
360             if ( l != null ) {
361                 locale = l.getLanguage();
362             }
363         }
364         if ( locale == null ) {
365             locale = Locale.getDefault().getLanguage();
366         }
367
368         TagUtils tagUtils = TagUtils.getInstance();
369         try {
370             if ( log.isDebugEnabled() ) {
371                 log.debug("Processing content localizableId: " + localizableId + ", locale: " + locale + ", identifier: " + identifier);
372             }
373
374             String JavaDoc content = null;
375             Long JavaDoc contentFieldId = null;
376             byte contentFieldType = 0;
377
378             CacheUtil cacheUtil = CacheUtil.getInstance(request);
379             CacheUtil.CFVData cfvData = null;
380
381             //try to search in cache
382
cfvData = cacheUtil.getLocalizableFieldValueFromCache(localizableId, identifier, locale);
383
384             if ( cfvData != null ) {
385                 content = cfvData.getData();
386                 contentFieldId = cfvData.getContentFieldId();
387                 contentFieldType = cfvData.getContentFieldType();
388             }
389
390             //not in cache, search in database
391
if ( content == null ) {
392                 // try to search content by uri
393
if ( log.isDebugEnabled() ) {
394                     log.debug("Trying to search in database by localizableId: " + localizableId);
395                 }
396                 ContentFieldManager contentFieldManager = (ContentFieldManager) applicationCtx.getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
397
398                 ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByLocalizableIdAndIdentifierAndLocale(localizableId, identifier, locale);
399                 ContentField contentField = null;
400                 if ( contentFieldValue != null ) {
401                     contentField = contentFieldValue.getContentField();
402                     if ( contentField.getType() == ContentField.LINE_TYPE ) {
403                         content = contentFieldValue.getSimpleValue();
404                     } else if ( contentField.getType() == ContentField.MULTILINE_TYPE ) {
405                         content = ConvertUtil.convertToString(contentFieldValue.getValue(), request.getCharacterEncoding());
406                     } else /* if HTML_TYPE */ {
407                         content = ConvertUtil.convertToString(contentFieldValue.getValue(), request.getCharacterEncoding());
408                     }
409                     contentFieldId = contentField.getId();
410                     contentFieldType = contentField.getType();
411                     //add to cache
412
cfvData = new CacheUtil.CFVData(content, contentFieldId, contentFieldType, contentFieldValue.getId());
413                     cacheUtil.putLocalizableFieldValueInCache(cfvData, localizableId, identifier, contentFieldValue.getContentLocale().getIdentifier());
414                 }
415             }
416
417             boolean encode;
418             if ( filter != null ) {
419                 encode = filter.booleanValue();
420             } else {
421                 if ( contentFieldType == ContentField.HTML_TYPE ) {
422                     encode = false;
423                 } else {
424                     encode = true;
425                 }
426             }
427
428             if ( encode ) {
429                 content = StringUtil.htmlEncode(content);
430             }
431
432             if ( var != null ) {
433                 if ( content != null ) {
434                     // save content in specified scope
435
int varScope = PageContext.PAGE_SCOPE;
436                     if ( scope != null ) {
437                         varScope = tagUtils.getScope(scope);
438                     }
439                     pageContext.setAttribute(var, content, varScope);
440                 }
441             } else {
442                 // write content directly to page
443
Object JavaDoc editModeEnabledAttr = request.getSession().getAttribute(WebappConstants.SITE_EDIT_MODE_ENABLED_KEY);
444                 boolean editModeEnabled = editModeEnabledAttr != null && Boolean.TRUE.equals(editModeEnabledAttr);
445                 if ( (content == null || content.trim().length() == 0) && (editModeEnabled || !ignore.booleanValue()) ) {
446                     content = applicationResources.getMessage(request, "core.commons.contentTag.insertText");
447                 }
448
449                 if ( content != null && content.trim().length() > 0 ) {
450
451                     boolean createWrapper = editModeEnabled && editable != null && editable.booleanValue();
452                     if ( createWrapper ) {
453                         // if content field value has been found (normally it will always be) wrap content with <div></div> tags
454
StringBuffer JavaDoc divId = new StringBuffer JavaDoc("__field__value__").append(contentFieldId).append("__wrapper__").append(tagNumber);
455                         String JavaDoc requestUrl = (String JavaDoc) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL);
456
457                         StringBuffer JavaDoc onmouseover = new StringBuffer JavaDoc("doSelectLayer(this.id);");
458                         StringBuffer JavaDoc onmouseout = new StringBuffer JavaDoc("doUnselectLayer(this.id);");
459                         StringBuffer JavaDoc ondblclick = new StringBuffer JavaDoc("doCallFieldUpdate(").append(contentFieldId).append(", '").append(requestUrl).append("');");
460
461                         String JavaDoc divDisplay = "inline";
462                         if ( contentFieldType == ContentField.HTML_TYPE ) {
463                             divDisplay = "block";
464                         }
465                         if ( content.trim().length() == 0 ) {
466                             content = applicationResources.getMessage(request, "core.commons.contentTag.insertText");
467                         }
468                         content = new StringBuffer JavaDoc("<div id=\"").append(divId).append("\" name=\"").append(divId).append("\" class=\"fieldValueWrapper\" style=\"display: ").append(divDisplay).append(";\" ").append("onmouseover=\"").append(onmouseover).append("\" onmouseout=\"").append(onmouseout).append("\" ondblclick=\"").append(ondblclick).append("\">").append(content).append("</div>").toString();
469                     }
470                     tagUtils.write(pageContext, content);
471                 }
472             }
473
474         } catch ( Exception JavaDoc ex ) {
475             JspTagException JavaDoc e = new JspTagException JavaDoc(ex);
476             tagUtils.saveException(pageContext, e);
477             throw e;
478         }
479     }
480
481 }
482
Popular Tags