1 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 ; 36 import javax.servlet.jsp.JspException ; 37 import javax.servlet.jsp.JspTagException ; 38 import javax.servlet.jsp.PageContext ; 39 import javax.servlet.jsp.tagext.SimpleTagSupport ; 40 import java.util.Locale ; 41 42 96 public class ContentByIdTag extends SimpleTagSupport { 97 98 protected transient final Log log = LogFactory.getLog(ContentByIdTag.class); 99 protected static final String NUMBER_KEY = "com.blandware.atleap.taglib.content.CONTENT_BY_ID_TAG_NUMBER"; 100 101 protected ApplicationContext applicationCtx = null; 102 103 106 protected Long localizableId; 107 108 111 protected String identifier; 112 113 116 protected String locale; 117 118 123 protected Boolean ignore = Boolean.FALSE; 124 125 128 protected Boolean filter = null; 129 130 133 protected String var; 134 135 138 protected String scope; 139 140 144 protected Boolean editable = Boolean.TRUE; 145 146 147 156 public Long getLocalizableId() { 157 return localizableId; 158 } 159 160 165 public void setLocalizableId(Long localizableId) { 166 this.localizableId = localizableId; 167 } 168 169 178 public String getIdentifier() { 179 return identifier; 180 } 181 182 187 public void setIdentifier(String identifier) { 188 this.identifier = identifier; 189 } 190 191 200 public String getLocale() { 201 return locale; 202 } 203 204 209 public void setLocale(String locale) { 210 this.locale = locale; 211 } 212 213 225 public Boolean getIgnore() { 226 return ignore; 227 } 228 229 237 public void setIgnore(Boolean ignore) { 238 this.ignore = ignore; 239 } 240 241 251 public Boolean getFilter() { 252 return filter; 253 } 254 255 261 public void setFilter(Boolean filter) { 262 this.filter = filter; 263 } 264 265 274 public String getVar() { 275 return var; 276 } 277 278 283 public void setVar(String var) { 284 this.var = var; 285 } 286 287 296 public String getScope() { 297 return scope; 298 } 299 300 305 public void setScope(String scope) { 306 this.scope = scope; 307 } 308 309 319 public Boolean getEditable() { 320 return editable; 321 } 322 323 329 public void setEditable(Boolean editable) { 330 this.editable = editable; 331 } 332 333 338 public void doTag() throws JspException { 339 PageContext pageContext = (PageContext ) getJspContext(); 340 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 341 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 342 343 if ( applicationCtx == null ) { 344 applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()); 345 } 346 347 Integer tagNumber = (Integer ) pageContext.getAttribute(NUMBER_KEY, PageContext.REQUEST_SCOPE); 349 if ( tagNumber == null ) { 350 tagNumber = new Integer (0); 351 } else { 352 tagNumber = new Integer (tagNumber.intValue() + 1); 353 } 354 355 pageContext.setAttribute(NUMBER_KEY, tagNumber, PageContext.REQUEST_SCOPE); 356 357 if ( locale == null ) { 359 Locale l = (Locale ) 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 content = null; 375 Long contentFieldId = null; 376 byte contentFieldType = 0; 377 378 CacheUtil cacheUtil = CacheUtil.getInstance(request); 379 CacheUtil.CFVData cfvData = null; 380 381 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 if ( content == null ) { 392 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 { 407 content = ConvertUtil.convertToString(contentFieldValue.getValue(), request.getCharacterEncoding()); 408 } 409 contentFieldId = contentField.getId(); 410 contentFieldType = contentField.getType(); 411 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 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 Object 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 StringBuffer divId = new StringBuffer ("__field__value__").append(contentFieldId).append("__wrapper__").append(tagNumber); 455 String requestUrl = (String ) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL); 456 457 StringBuffer onmouseover = new StringBuffer ("doSelectLayer(this.id);"); 458 StringBuffer onmouseout = new StringBuffer ("doUnselectLayer(this.id);"); 459 StringBuffer ondblclick = new StringBuffer ("doCallFieldUpdate(").append(contentFieldId).append(", '").append(requestUrl).append("');"); 460 461 String 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 ("<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 ex ) { 475 JspTagException e = new JspTagException (ex); 476 tagUtils.saveException(pageContext, e); 477 throw e; 478 } 479 } 480 481 } 482 | Popular Tags |