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.model.core.Page; 24 import com.blandware.atleap.service.core.ContentFieldManager; 25 import com.blandware.atleap.service.core.PageManager; 26 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor; 27 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition; 28 import com.blandware.atleap.webapp.taglib.core.util.ContextMenuItem; 29 import com.blandware.atleap.webapp.taglib.core.util.JavaScriptUtil; 30 import com.blandware.atleap.webapp.util.core.ApplicationResources; 31 import com.blandware.atleap.webapp.util.core.CacheUtil; 32 import com.blandware.atleap.webapp.util.core.WebappConstants; 33 import com.blandware.atleap.webapp.util.core.WebappUtil; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.commons.validator.GenericValidator; 37 import org.apache.struts.Globals; 38 import org.apache.struts.taglib.TagUtils; 39 import org.apache.struts.tiles.ComponentDefinition; 40 import org.apache.struts.tiles.TilesUtil; 41 import org.springframework.context.ApplicationContext; 42 import org.springframework.web.context.support.WebApplicationContextUtils; 43 44 import javax.servlet.ServletContext ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.jsp.JspTagException ; 47 import javax.servlet.jsp.PageContext ; 48 import javax.servlet.jsp.tagext.SimpleTagSupport ; 49 import java.util.Arrays ; 50 import java.util.Locale ; 51 52 54 145 public class ContentTag extends SimpleTagSupport { 146 147 protected transient final Log log = LogFactory.getLog(ContentTag.class); 148 protected ApplicationContext applicationCtx = null; 149 protected static final String NUMBER_KEY = "com.blandware.atleap.taglib.content.CONTENT_TAG_NUMBER"; 150 151 154 protected String var; 155 156 159 protected String scope; 160 163 protected String identifier = null; 164 167 protected String definition = null; 168 171 protected String uri = null; 172 175 protected String locale = null; 176 179 protected String index = null; 180 184 protected Boolean ignore = Boolean.FALSE; 185 189 protected String contextMenuJsp = null; 190 191 192 195 protected Boolean showEmpty; 196 197 200 protected Boolean filter = null; 201 202 206 protected Boolean editable = Boolean.TRUE; 207 208 217 public String getIdentifier() { 218 return identifier; 219 } 220 221 226 public void setIdentifier(String identifier) { 227 this.identifier = identifier; 228 } 229 230 239 public String getUri() { 240 return uri; 241 } 242 243 248 public void setUri(String uri) { 249 this.uri = uri; 250 } 251 252 261 public String getDefinition() { 262 return definition; 263 } 264 265 270 public void setDefinition(String definition) { 271 this.definition = definition; 272 } 273 274 283 public Boolean getIgnore() { 284 return ignore; 285 } 286 287 292 public void setIgnore(Boolean ignore) { 293 this.ignore = ignore; 294 } 295 296 306 public Boolean getShowEmpty() { 307 return showEmpty; 308 } 309 310 316 public void setShowEmpty(Boolean showEmpty) { 317 this.showEmpty = showEmpty; 318 } 319 320 330 public Boolean getFilter() { 331 return filter; 332 } 333 334 340 public void setFilter(Boolean filter) { 341 this.filter = filter; 342 } 343 344 353 public String getLocale() { 354 return locale; 355 } 356 357 362 public void setLocale(String locale) { 363 this.locale = locale; 364 } 365 366 375 public String getVar() { 376 return var; 377 } 378 379 384 public void setVar(String var) { 385 this.var = var; 386 } 387 388 397 public String getScope() { 398 return scope; 399 } 400 401 406 public void setScope(String scope) { 407 this.scope = scope; 408 } 409 410 419 public String getIndex() { 420 return index; 421 } 422 423 428 public void setIndex(String index) { 429 this.index = index; 430 } 431 432 442 public Boolean getEditable() { 443 return editable; 444 } 445 446 452 public void setEditable(Boolean editable) { 453 this.editable = editable; 454 } 455 456 465 public String getContextMenuJsp() { 466 return contextMenuJsp; 467 } 468 469 474 public void setContextMenuJsp(String contextMenuJsp) { 475 this.contextMenuJsp = contextMenuJsp; 476 } 477 478 483 public void doTag() throws JspTagException { 484 485 PageContext pageContext = (PageContext ) getJspContext(); 486 487 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 488 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 489 ServletContext servletContext = pageContext.getServletContext(); 490 491 if ( ignore == null ) { 492 ignore = Boolean.FALSE; 493 } 494 495 if ( applicationCtx == null ) { 496 applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()); 497 } 498 499 if ( index != null && index.trim().length() > 0 ) { 500 identifier = identifier + "[" + index + "]"; 501 } 502 503 Integer tagNumber = (Integer ) pageContext.getAttribute(NUMBER_KEY, PageContext.REQUEST_SCOPE); 505 if ( tagNumber == null ) { 506 tagNumber = new Integer (0); 507 } else { 508 tagNumber = new Integer (tagNumber.intValue() + 1); 509 } 510 511 pageContext.setAttribute(NUMBER_KEY, tagNumber, PageContext.REQUEST_SCOPE); 512 513 if ( locale == null ) { 515 Locale l = (Locale ) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE); 516 if ( l != null ) { 517 locale = l.getLanguage(); 518 } 519 } 520 if ( locale == null ) { 521 locale = Locale.getDefault().getLanguage(); 522 } 523 524 if ( definition != null ) { 526 ComponentDefinition tmpDef = null; 527 try { 528 tmpDef = TilesUtil.getDefinition(definition, request, servletContext); 529 } catch ( Exception ex ) { 530 } 532 if ( tmpDef == null ) { 533 if ( ignore.booleanValue() ) { 534 return; 535 } 536 String errorMessage = "Specified definition '" + definition + "' has not been found"; 537 JspTagException e = new JspTagException (errorMessage); 538 throw e; 539 } 540 } else { 541 definition = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.DEFINITION_NAME, PageContext.REQUEST_SCOPE); 542 } 543 if ( definition == null ) { 544 if ( ignore.booleanValue() ) { 545 return; 546 } 547 String errorMessage = "DEFINITION_NAME attribute has not been found in request. This can be used only on pages processed by action using tile definition."; 548 JspTagException e = new JspTagException (errorMessage); 549 throw e; 550 } 551 552 if ( uri != null ) { 554 PageManager pageManager = (PageManager) applicationCtx.getBean(Constants.PAGE_MANAGER_BEAN); 555 Page page = pageManager.findPageByUri(uri); 556 if ( page == null ) { 557 if ( ignore.booleanValue() ) { 558 return; 559 } 560 String errorMessage = "Specified page uri '" + uri + "' has not been found"; 561 JspTagException e = new JspTagException (errorMessage); 562 throw e; 563 } 564 } else { 565 uri = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.PROCESSED_URI, PageContext.REQUEST_SCOPE); 566 } 567 568 if ( uri == null ) { 569 if ( ignore.booleanValue() ) { 570 return; 571 } 572 String errorMessage = "PROCESSED_URI attribute has not been found in request. This can be used only on pages processed by action using tile definition."; 573 JspTagException e = new JspTagException (errorMessage); 574 throw e; 575 } 576 577 String savedDefinition = new String (definition); 579 String savedUri = new String (uri); 580 581 TagUtils tagUtils = TagUtils.getInstance(); 582 if ( GenericValidator.isBlankOrNull(contextMenuJsp) ) { 583 contextMenuJsp = WebappConstants.DEFAULT_CONTEXT_MENU_JSP; 584 } 585 try { 586 if ( log.isDebugEnabled() ) { 587 log.debug("Processing content; definition name: " + definition + ", locale: " + locale + ", uri: " + uri + ", identifier: " + identifier); 588 } 589 String content = null; 590 Long fieldId = null; 591 Long fieldValueId = null; 592 byte fieldType = 0; 593 594 CacheUtil cacheUtil = CacheUtil.getInstance(request); 595 CacheUtil.CFVData cfvData = null; 596 597 cfvData = cacheUtil.getPageFieldValueFromCache(uri, identifier, locale); 599 600 if ( cfvData != null ) { 601 content = cfvData.getData(); 602 fieldId = cfvData.getContentFieldId(); 603 fieldType = cfvData.getContentFieldType(); 604 } 605 606 boolean showEmpty = this.showEmpty == null ? fieldType != ContentField.LINE_TYPE : this.showEmpty.booleanValue(); 611 612 if ( content == null || (content.trim().length() == 0 && !showEmpty) ) { 614 if ( log.isDebugEnabled() ) { 616 log.debug("Trying to search in persistent storage by uri: " + uri); 617 } 618 ContentFieldManager contentFieldManager = (ContentFieldManager) applicationCtx.getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 619 620 boolean continueSearch = true; 622 ContentField field = contentFieldManager.findContentFieldByPageUri(identifier, uri); 623 content = null; 624 if ( field != null ) { 625 ContentFieldValue fieldValue = contentFieldManager.findContentFieldValueByUriAndIdentifierAndLocale(uri, identifier, locale); 626 if ( fieldValue == null ) { 627 continueSearch = false; 628 } else { 629 fieldValueId = fieldValue.getId(); 630 content = retrieveContent(fieldValue, field.getType()); 631 showEmpty = this.showEmpty == null ? field.getType() != ContentField.LINE_TYPE : this.showEmpty.booleanValue(); 632 continueSearch = (content == null || content.trim().length() == 0) && !showEmpty; 633 } 634 } 635 636 if ( continueSearch ) { 637 String tmpDefinition = null; 639 do { 640 tmpDefinition = new String (definition); 641 definition = ((HeritableComponentDefinition) TilesUtil.getDefinition(definition, request, servletContext)).getExtends(); 642 if ( definition != null ) { 643 if ( log.isDebugEnabled() ) { 644 log.debug("Trying to search in persistent storage by definition: " + definition); 645 } 646 field = contentFieldManager.findContentFieldByLayoutDefinition(identifier, definition); 647 if ( field != null ) { 648 ContentFieldValue fieldValue = contentFieldManager.findContentFieldValueByDefinitionAndIdentifierAndLocale(definition, identifier, locale); 650 if ( fieldValue == null ) { 651 continueSearch = false; 652 } else { 653 fieldValueId = fieldValue.getId(); 654 content = retrieveContent(fieldValue, field.getType()); 655 showEmpty = this.showEmpty == null ? field.getType() != ContentField.LINE_TYPE : this.showEmpty.booleanValue(); 656 continueSearch = (content == null || content.trim().length() == 0) && !showEmpty; 657 } 658 } else { 659 continueSearch = true; 660 } 661 } 662 } while ( definition != null && continueSearch ); 663 664 if ( field != null ) { 665 fieldId = field.getId(); 666 fieldType = field.getType(); 667 668 if ( content != null ) { 670 cfvData = new CacheUtil.CFVData(content, fieldId, fieldType, fieldValueId); 671 if ( definition == null ) { 672 cacheUtil.putLayoutFieldValueInCache(cfvData, tmpDefinition, uri, identifier, locale); 673 } else { 674 cacheUtil.putLayoutFieldValueInCache(cfvData, definition, uri, identifier, locale); 675 } 676 } 677 } 678 } else if ( field != null ) { 679 fieldId = field.getId(); 680 fieldType = field.getType(); 681 682 if ( content != null ) { 683 cfvData = new CacheUtil.CFVData(content, fieldId, fieldType, fieldValueId); 685 cacheUtil.putPageFieldValueInCache(cfvData, uri, identifier, locale); 686 } 687 } 688 if ( field == null ) { 689 if ( !ignore.booleanValue() ) { 690 String errorMessage = applicationResources.getMessage(request, "core.commons.contentTag.fieldNotFound", new Object []{identifier, savedUri, savedDefinition}); 691 tagUtils.write(pageContext, errorMessage); 692 } 693 return; 694 } 695 } 696 697 boolean contentIsEmpty = content == null || content.trim().length() == 0; 698 699 if ( !contentIsEmpty ) { 700 boolean encode = filter == null ? fieldType != ContentField.HTML_TYPE : filter.booleanValue(); 701 702 if ( encode ) { 703 content = StringUtil.htmlEncode(content); 704 } 705 } 706 707 if ( var != null ) { 708 if ( content != null ) { 709 int varScope = PageContext.PAGE_SCOPE; 711 if ( scope != null ) { 712 varScope = tagUtils.getScope(scope); 713 } 714 pageContext.setAttribute(var, content, varScope); 715 } 716 } else { 717 Object editModeEnabledAttr = request.getSession().getAttribute(WebappConstants.SITE_EDIT_MODE_ENABLED_KEY); 719 boolean editModeEnabled = editModeEnabledAttr != null && Boolean.TRUE.equals(editModeEnabledAttr); 720 if ( editModeEnabled && editable != null && editable.booleanValue() ) { 721 if ( contentIsEmpty ) { 722 content = applicationResources.getMessage(request, "core.commons.contentTag.insertText"); 723 } 724 StringBuffer divId = new StringBuffer ("__field__value__").append(fieldId).append("__wrapper__").append(tagNumber); 726 String requestUrl = (String ) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL); 727 if ( requestUrl == null ) { 728 requestUrl = WebappUtil.findGlobalForwardConfig("index", null, request).getPath(); 729 } 730 731 StringBuffer contextMenuId = new StringBuffer ("__contextMenu__content__").append(tagNumber); 733 734 739 StringBuffer contextMenuLayerVar = new StringBuffer (contextMenuId.toString()); 740 741 StringBuffer onmouseover = new StringBuffer ("doSelectLayer(this.id);"); 742 StringBuffer onmouseout = new StringBuffer ("doUnselectLayer(this.id);"); 743 StringBuffer ondblclick = new StringBuffer ("doCallFieldUpdateInCurrentLocale('").append(locale).append("', ").append(fieldId).append(", '").append(requestUrl).append("');"); 744 StringBuffer oncontextmenu = new StringBuffer ("return showContextMenu(").append(contextMenuLayerVar).append(", event);"); 745 746 String divDisplay = "inline"; 747 if ( fieldType == ContentField.HTML_TYPE ) { 748 divDisplay = "block"; 749 } 750 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("\" oncontextmenu=\"").append(oncontextmenu).append("\">").append(content).append("</div>").toString(); 751 tagUtils.write(pageContext, content); 752 753 String editInCurrentLocaleTitle = applicationResources.getMessage(request, "core.editMode.menu.context.editInCurrentLocale"); 756 String editInAllLocalesTitle = applicationResources.getMessage(request, "core.editMode.menu.context.editInAllLocales"); 757 String editInChosenLocalesTitle = applicationResources.getMessage(request, "core.editMode.menu.context.editInChosenLocales"); 758 759 ContextMenuItem menu = new ContextMenuItem(); 761 menu.setId(contextMenuId.toString()); 762 menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:" + ondblclick, 763 editInCurrentLocaleTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS))); 764 menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:doCallFieldUpdate(" + fieldId + ", '" + requestUrl + "');", 765 editInAllLocalesTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS))); 766 menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:doChooseLocalesForFieldUpdate(" + fieldId + ", '" + requestUrl + "');", 767 editInChosenLocalesTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS))); 768 769 pageContext.setAttribute(WebappConstants.CONTEXT_MENUS_KEY, Arrays.asList(new ContextMenuItem[]{menu}), PageContext.REQUEST_SCOPE); 771 772 pageContext.include(contextMenuJsp); 774 } else { 775 showEmpty = this.showEmpty == null ? fieldType != ContentField.LINE_TYPE : this.showEmpty.booleanValue(); 777 if ( content == null ) { 778 content = ""; 779 } 780 if ( !contentIsEmpty || showEmpty ) { 781 tagUtils.write(pageContext, content); 782 } 783 } 784 } 785 } catch ( Exception ex ) { 786 JspTagException e = new JspTagException (ex); 787 throw e; 788 } 789 790 } 791 792 793 800 protected String retrieveContent(ContentFieldValue fieldValue, byte fieldType) { 801 String content = null; 802 if ( fieldType == ContentField.LINE_TYPE ) { 803 content = fieldValue.getSimpleValue(); 804 } else { 805 content = ConvertUtil.convertToString(fieldValue.getValue()); 806 } 807 return content; 808 } 809 810 } 811 | Popular Tags |