1 23 24 package org.infoglue.deliver.controllers.kernel.impl.simple; 25 26 import java.io.File ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.servlet.http.HttpServletRequest ; 31 32 import org.apache.log4j.Logger; 33 import org.infoglue.cms.entities.content.ContentVO; 34 import org.infoglue.cms.exception.SystemException; 35 import org.infoglue.cms.io.FileHelper; 36 import org.infoglue.cms.security.InfoGluePrincipal; 37 import org.infoglue.cms.util.CmsPropertyHandler; 38 import org.infoglue.deliver.applications.databeans.DatabaseWrapper; 39 import org.infoglue.deliver.applications.databeans.WebPage; 40 41 46 47 public class EditOnSiteBasicTemplateController extends BasicTemplateController 48 { 49 private final static Logger logger = Logger.getLogger(EditOnSiteBasicTemplateController.class.getName()); 50 51 public EditOnSiteBasicTemplateController(DatabaseWrapper databaseWrapper, InfoGluePrincipal infoGluePrincipal) 52 { 53 super(databaseWrapper, infoGluePrincipal); 54 } 55 56 59 60 private String decorateTag(Integer contentId, Integer languageId, String attributeName, String attributeValue) 61 { 62 if(attributeValue != null && !attributeValue.trim().equals("")) 63 { 64 String editOnSiteUrl = CmsPropertyHandler.getEditOnSiteUrl(); 65 StringBuffer requestDelim = new StringBuffer ( CmsPropertyHandler.getRequestArgumentDelimiter() ); 66 StringBuffer decoratedAttributeValue = new StringBuffer (); 67 decoratedAttributeValue.append("<span oncontextmenu=\"setContentItemParameters(" ); 68 decoratedAttributeValue.append( contentId ).append( "," ).append( languageId ); 69 decoratedAttributeValue.append( ",'").append( attributeName ).append( "'); setEditUrl('"); 70 decoratedAttributeValue.append( editOnSiteUrl ).append( "?contentId=" ).append( contentId ); 71 decoratedAttributeValue.append( requestDelim).append("languageId=").append( languageId ); 72 decoratedAttributeValue.append( requestDelim).append("attributeName=" ).append( attributeName ); 73 decoratedAttributeValue.append( requestDelim ).append( "forceWorkingChange=true');\">" ); 74 decoratedAttributeValue.append( attributeValue + "</span>"); 75 return decoratedAttributeValue.toString(); 76 } 77 else 78 { 79 return ""; 80 } 81 } 82 83 86 87 public String decoratePage(String page) 88 { 89 String decoratedTemplate = page; 90 91 try 92 { 93 String extraHeader = FileHelper.getFileAsString(new File (CmsPropertyHandler.getContextRootPath() + "preview/editOnSiteHeader.vm")); 94 String extraBody = FileHelper.getFileAsString(new File (CmsPropertyHandler.getContextRootPath() + "preview/editOnSiteBody.vm")); 95 96 String servletContext = request.getContextPath(); 97 extraHeader = extraHeader.replaceAll("\\{applicationContext\\}", servletContext); 98 100 StringBuffer modifiedTemplate = new StringBuffer (page); 101 102 int indexOfHeadEndTag = modifiedTemplate.indexOf("</head"); 104 if(indexOfHeadEndTag == -1) 105 indexOfHeadEndTag = modifiedTemplate.indexOf("</HEAD"); 106 107 if(indexOfHeadEndTag > -1) 108 { 109 modifiedTemplate = modifiedTemplate.replace(indexOfHeadEndTag, modifiedTemplate.indexOf(">", indexOfHeadEndTag) + 1, extraHeader); 110 } 111 else 112 { 113 int indexOfHTMLStartTag = modifiedTemplate.indexOf("<html"); 114 if(indexOfHTMLStartTag == -1) 115 indexOfHTMLStartTag = modifiedTemplate.indexOf("<HTML"); 116 117 if(indexOfHTMLStartTag > -1) 118 { 119 modifiedTemplate = modifiedTemplate.insert(modifiedTemplate.indexOf(">", indexOfHTMLStartTag) + 1, "<head>" + extraHeader); 120 } 121 else 122 { 123 logger.info("The current template is not a valid document. It does not comply with the simplest standards such as having a correct header."); 124 } 125 } 126 127 int indexOfBodyStartTag = modifiedTemplate.indexOf("<body"); 129 if(indexOfBodyStartTag == -1) 130 indexOfBodyStartTag = modifiedTemplate.indexOf("<BODY"); 131 132 if(indexOfBodyStartTag > -1) 133 { 134 modifiedTemplate = modifiedTemplate.insert(modifiedTemplate.indexOf(">", indexOfBodyStartTag) + 1, extraBody); 135 } 136 else 137 { 138 logger.info("The current template is not a valid document. It does not comply with the simplest standards such as having a correct body."); 139 } 140 141 decoratedTemplate = modifiedTemplate.toString(); 142 } 143 catch(Exception e) 144 { 145 logger.warn("An error occurred when deliver tried to decorate your template to enable onSiteEditing. Reason " + e.getMessage(), e); 146 } 147 148 return decoratedTemplate; 149 } 150 151 152 157 158 public String getContentAttribute(String attributeName) 159 { 160 return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttribute(attributeName)); 161 } 162 163 168 169 public String getContentAttribute(String contentBindningName, String attributeName) 170 { 171 return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttribute(contentBindningName, attributeName)); 172 191 } 192 193 194 198 public String getContentAttribute(String contentBindningName, String attributeName, boolean clean) 199 { 200 return super.getContentAttribute(contentBindningName, attributeName); 201 } 202 203 207 208 public String getContentAttribute(String attributeName, boolean clean) 209 { 210 return super.getContentAttribute(attributeName); 211 } 212 213 214 229 public String getContentAttribute(Integer contentId, String attributeName, boolean clean) 230 { 231 String attributeValue = super.getContentAttribute(contentId, attributeName); 232 233 if( clean == false ) 234 { 235 attributeValue = this.decorateTag( contentId, this.languageId, attributeName, attributeValue ); 236 } 237 return attributeValue; 238 } 239 240 244 245 public String getContentAttributeValue(Integer contentId, String attributeName, boolean clean, boolean escapeHTML) 246 { 247 String attributeValue = ""; 248 249 if(clean) 250 attributeValue = super.getContentAttributeValue(contentId, this.getLanguageId(), attributeName, escapeHTML); 251 else 252 return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getContentAttributeValue(contentId, this.getLanguageId(), attributeName, escapeHTML)); 253 254 return attributeValue; 255 } 256 257 261 262 public String getContentAttribute(Integer contentId, Integer languageId, String attributeName, boolean clean) 263 { 264 String attributeValue = ""; 266 267 if(clean) 268 { 269 attributeValue = super.getContentAttribute(contentId, languageId, attributeName); 270 } 271 else 272 { 273 attributeValue = super.getContentAttribute(contentId, languageId, attributeName); 274 attributeValue = decorateTag(contentId, languageId, attributeName, attributeValue); 275 } 276 277 return attributeValue; 278 } 279 280 285 286 public String getContentAttribute(Integer contentId, String attributeName) 287 { 288 if(attributeName.equalsIgnoreCase(this.getTemplateAttributeName())) 289 return super.getContentAttribute(contentId, attributeName); 290 else 292 return decorateTag(contentId, this.getLanguageId(), attributeName, super.getContentAttribute(contentId, attributeName)); 293 } 294 295 296 297 302 303 public String getParsedContentAttribute(String attributeName) 304 { 305 return decorateTag(this.getContentId(), this.getLanguageId(), attributeName, super.getParsedContentAttribute(attributeName)); 306 } 307 308 309 314 315 public String getParsedContentAttribute(String contentBindningName, String attributeName) 316 { 317 String attributeValue = ""; 318 319 try 320 { 321 ContentVO contentVO = this.nodeDeliveryController.getBoundContent(this.getDatabase(), this.getPrincipal(), this.siteNodeId, this.languageId, USE_LANGUAGE_FALLBACK, contentBindningName, this.deliveryContext); 322 if(contentVO != null) 323 { 324 attributeValue = getParsedContentAttribute(contentVO.getContentId(), attributeName); 325 attributeValue = decorateTag(contentVO.getContentId(), this.getLanguageId(), attributeName, attributeValue); 326 } 327 } 328 catch(Exception e) 329 { 330 logger.error("An error occurred trying to get attributeName=" + attributeName + " on contentBindning " + contentBindningName + ":" + e.getMessage(), e); 331 } 332 333 return attributeValue; 334 } 335 336 340 341 public String getParsedContentAttribute(String attributeName, boolean clean) 342 { 343 String attributeValue = ""; 345 346 if(clean) 347 { 348 attributeValue = super.getParsedContentAttribute(attributeName); 349 } 350 else 351 { 352 attributeValue = super.getParsedContentAttribute(attributeName); 353 attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue); 354 } 355 356 return attributeValue; 357 } 358 359 363 364 public String getParsedContentAttribute(String contentBindningName, String attributeName, boolean clean) 365 { 366 368 String attributeValue = ""; 369 370 if(clean) 371 { 372 attributeValue = super.getParsedContentAttribute(contentBindningName, attributeName); 373 } 374 else 375 { 376 attributeValue = super.getParsedContentAttribute(contentBindningName, attributeName); 377 attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue); 378 } 379 380 return attributeValue; 381 } 382 383 387 388 public String getParsedContentAttribute(Integer contentId, String attributeName, boolean clean) 389 { 390 392 String attributeValue = ""; 393 394 if(clean) 395 { 396 attributeValue = super.getParsedContentAttribute(contentId, attributeName); 397 } 398 else 399 { 400 attributeValue = super.getParsedContentAttribute(contentId, attributeName); 401 attributeValue = decorateTag(contentId, this.getLanguageId(), attributeName, attributeValue); 402 } 403 404 return attributeValue; 405 406 } 407 408 412 413 public String getParsedContentAttribute(Integer contentId, Integer languageId, String attributeName, boolean clean) 414 { 415 417 String attributeValue = ""; 418 419 if(clean) 420 { 421 attributeValue = super.getParsedContentAttribute(contentId, languageId, attributeName); 422 } 423 else 424 { 425 attributeValue = super.getParsedContentAttribute(contentId, languageId, attributeName); 426 attributeValue = decorateTag(contentId, languageId, attributeName, attributeValue); 427 } 428 429 return attributeValue; 430 431 } 432 433 437 438 public String getParsedContentAttribute(Integer contentId, String attributeName) 439 { 440 return decorateTag(contentId, this.getLanguageId(), attributeName, super.getParsedContentAttribute(contentId, attributeName)); 441 } 442 443 444 445 449 450 public List getBoundPages(String structureBindningName) 451 { 452 List boundPages = super.getBoundPages(structureBindningName); 453 Iterator i = boundPages.iterator(); 454 while(i.hasNext()) 455 { 456 WebPage webPage = (WebPage)i.next(); 457 Integer contentId = super.getContentId(webPage.getSiteNodeId(), META_INFO_BINDING_NAME); 458 String navigationTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, webPage.getNavigationTitle()); 459 webPage.setNavigationTitle(navigationTitle); 460 } 461 462 return boundPages; 463 } 464 465 466 470 471 public List getBoundPages(Integer siteNodeId, String structureBindningName) 472 { 473 List boundPages = super.getBoundPages(siteNodeId, structureBindningName); 474 Iterator i = boundPages.iterator(); 475 while(i.hasNext()) 476 { 477 WebPage webPage = (WebPage)i.next(); 478 Integer contentId = super.getContentId(webPage.getSiteNodeId(), META_INFO_BINDING_NAME); 479 String navigationTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, webPage.getNavigationTitle()); 480 webPage.setNavigationTitle(navigationTitle); 481 } 482 483 return boundPages; 484 } 485 486 492 493 public String getPageNavTitle(String structureBindningName) 494 { 495 Integer siteNodeId = super.getSiteNodeId(structureBindningName); 496 Integer contentId = super.getContentId(siteNodeId, META_INFO_BINDING_NAME); 497 String navTitle = decorateTag(contentId, this.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, super.getPageNavTitle(structureBindningName)); 498 499 return navTitle; 500 } 501 502 503 508 514 517 524 525 528 535 536 541 548 549 554 561 562 566 567 public TemplateController getTemplateController(Integer siteNodeId, Integer languageId, Integer contentId, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception  568 { 569 return getTemplateController(siteNodeId, languageId, contentId, this.request, infoGluePrincipal); 570 } 571 572 public TemplateController getTemplateController(Integer siteNodeId, Integer languageId, Integer contentId, HttpServletRequest request, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception  573 { 574 TemplateController templateController = null; 575 templateController = new EditOnSiteBasicTemplateController(this.databaseWrapper, infoGluePrincipal); 576 templateController.setStandardRequestParameters(siteNodeId, languageId, contentId); 577 templateController.setHttpRequest(request); 578 templateController.setBrowserBean(this.browserBean); 579 templateController.setDeliveryControllers(this.nodeDeliveryController, null, this.integrationDeliveryController); 580 return templateController; 581 } 582 583 584 } | Popular Tags |