1 23 24 package org.infoglue.deliver.invokers; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import org.apache.log4j.Logger; 36 import org.dom4j.Document; 37 import org.dom4j.Element; 38 import org.dom4j.io.OutputFormat; 39 import org.dom4j.io.XMLWriter; 40 import org.exolab.castor.jdo.Database; 41 import org.infoglue.cms.entities.content.ContentVO; 42 import org.infoglue.cms.entities.management.LanguageVO; 43 import org.infoglue.cms.entities.structure.SiteNodeVO; 44 import org.infoglue.cms.exception.SystemException; 45 import org.infoglue.cms.util.CmsPropertyHandler; 46 import org.infoglue.cms.util.XMLHelper; 47 import org.infoglue.cms.util.dom.DOMBuilder; 48 import org.infoglue.deliver.applications.actions.InfoGlueComponent; 49 import org.infoglue.deliver.applications.databeans.ComponentRestriction; 50 import org.infoglue.deliver.applications.databeans.Slot; 51 import org.infoglue.deliver.controllers.kernel.impl.simple.ComponentLogic; 52 import org.infoglue.deliver.controllers.kernel.impl.simple.ContentDeliveryController; 53 import org.infoglue.deliver.controllers.kernel.impl.simple.IntegrationDeliveryController; 54 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController; 55 import org.infoglue.deliver.controllers.kernel.impl.simple.NodeDeliveryController; 56 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController; 57 import org.infoglue.deliver.util.CacheController; 58 import org.infoglue.deliver.util.NullObject; 59 import org.infoglue.deliver.util.Timer; 60 import org.infoglue.deliver.util.VelocityTemplateProcessor; 61 62 67 68 public class ComponentBasedHTMLPageInvoker extends PageInvoker 69 { 70 71 private final static Logger logger = Logger.getLogger(ComponentBasedHTMLPageInvoker.class.getName()); 72 73 77 78 public PageInvoker getDecoratedPageInvoker() throws SystemException 79 { 80 return new DecoratedComponentBasedHTMLPageInvoker(); 81 } 82 83 86 87 public void invokePage() throws SystemException, Exception 88 { 89 String pageContent = ""; 90 91 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(this.getDeliveryContext()); 92 IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(this.getDeliveryContext()); 93 94 Integer repositoryId = nodeDeliveryController.getSiteNode(getDatabase(), this.getDeliveryContext().getSiteNodeId()).getRepository().getId(); 95 96 String componentXML = getPageComponentsString(getDatabase(), this.getTemplateController(), this.getDeliveryContext().getSiteNodeId(), this.getDeliveryContext().getLanguageId(), this.getDeliveryContext().getContentId()); 97 98 if(componentXML != null && componentXML.length() != 0) 99 { 100 Document document = new DOMBuilder().getDocument(componentXML); 101 102 List pageComponents = getPageComponents(getDatabase(), componentXML, document.getRootElement(), "base", this.getTemplateController(), null); 103 InfoGlueComponent baseComponent = null; 104 if(pageComponents.size() > 0) 105 { 106 baseComponent = (InfoGlueComponent)pageComponents.get(0); 107 } 108 109 if(baseComponent != null) 110 { 111 ContentVO metaInfoContentVO = nodeDeliveryController.getBoundContent(getDatabase(), this.getTemplateController().getPrincipal(), this.getDeliveryContext().getSiteNodeId(), this.getDeliveryContext().getLanguageId(), true, "Meta information", this.getDeliveryContext()); 112 pageContent = renderComponent(baseComponent, this.getTemplateController(), repositoryId, this.getDeliveryContext().getSiteNodeId(), this.getDeliveryContext().getLanguageId(), this.getDeliveryContext().getContentId(), metaInfoContentVO.getId()); 113 } 114 } 115 116 Map context = getDefaultContext(); 117 StringWriter cacheString = new StringWriter (); 118 PrintWriter cachedStream = new PrintWriter (cacheString); 119 new VelocityTemplateProcessor().renderTemplate(context, cachedStream, pageContent); 120 121 String pageString = pageContent; 122 if(this.getDeliveryContext().getEvaluateFullPage()) 123 pageString = cacheString.toString(); 124 125 pageString = this.getTemplateController().decoratePage(pageString); 126 127 this.setPageString(pageString); 128 129 } 130 131 132 135 136 protected String getPageComponentsString(Database db, TemplateController templateController, Integer siteNodeId, Integer languageId, Integer contentId) throws SystemException, Exception 137 { 138 SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId); 139 ContentVO contentVO = null; 140 if(siteNodeVO.getMetaInfoContentId() != null && siteNodeVO.getMetaInfoContentId().intValue() > -1) 141 contentVO = templateController.getContent(siteNodeVO.getMetaInfoContentId()); 142 else 143 contentVO = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getBoundContent(db, templateController.getPrincipal(), siteNodeId, languageId, true, "Meta information", this.getDeliveryContext()); 144 145 if(contentVO == null) 146 throw new SystemException("There was no Meta Information bound to this page which makes it impossible to render."); 147 148 String cacheName = "componentEditorCache"; 149 String cacheKey = "pageComponentString_" + siteNodeId + "_" + languageId + "_" + contentId; 150 151 String attributeName = "ComponentStructure"; 152 String attributeKey = "" + contentVO.getId() + "_" + languageId + "_" + attributeName + "_" + siteNodeId + "_" + true; 153 154 String versionKey = attributeKey + "_contentVersionId"; 155 156 String cachedPageComponentsString = (String )CacheController.getCachedObject(cacheName, cacheKey); 157 Integer contentVersionId = (Integer )CacheController.getCachedObjectFromAdvancedCache("contentAttributeCache", versionKey); 159 160 161 if(cachedPageComponentsString != null) 162 { 163 templateController.getDeliveryContext().addUsedContentVersion("contentVersion_" + contentVersionId); 166 return cachedPageComponentsString; 167 } 168 169 String pageComponentsString = null; 170 171 Integer masterLanguageId = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(db, siteNodeId).getId(); 173 pageComponentsString = templateController.getContentAttribute(contentVO.getContentId(), masterLanguageId, "ComponentStructure", true); 174 175 if(pageComponentsString == null) 176 throw new SystemException("There was no Meta Information bound to this page which makes it impossible to render."); 177 178 logger.info("pageComponentsString: " + pageComponentsString); 179 180 CacheController.cacheObject(cacheName, cacheKey, pageComponentsString); 181 182 return pageComponentsString; 183 } 184 185 186 189 190 protected org.w3c.dom.Document getPageComponentsDocument(Database db, TemplateController templateController, Integer siteNodeId, Integer languageId, Integer contentId) throws SystemException, Exception 191 { 192 String cacheName = "componentEditorCache"; 193 String cacheKey = "pageComponentDocument_" + siteNodeId + "_" + languageId + "_" + contentId; 194 org.w3c.dom.Document cachedPageComponentsDocument = (org.w3c.dom.Document )CacheController.getCachedObject(cacheName, cacheKey); 195 if(cachedPageComponentsDocument != null) 196 return cachedPageComponentsDocument; 197 198 org.w3c.dom.Document pageComponentsDocument = null; 199 200 try 201 { 202 String xml = this.getPageComponentsString(db, templateController, siteNodeId, languageId, contentId); 203 pageComponentsDocument = XMLHelper.readDocumentFromByteArray(xml.getBytes("UTF-8")); 204 205 CacheController.cacheObject(cacheName, cacheKey, pageComponentsDocument); 206 } 207 catch(Exception e) 208 { 209 logger.error(e.getMessage(), e); 210 throw e; 211 } 212 213 return pageComponentsDocument; 214 } 215 216 217 220 221 protected Map getComponents(Database db, Element element, TemplateController templateController, InfoGlueComponent parentComponent) throws Exception 222 { 223 InfoGlueComponent component = null; 224 225 Locale locale = LanguageDeliveryController.getLanguageDeliveryController().getLocaleWithId(db, templateController.getLanguageId()); 226 227 Map components = new HashMap (); 228 229 String componentXPath = "component"; 230 List componentNodeList = element.selectNodes(componentXPath); 231 Iterator componentNodeListIterator = componentNodeList.iterator(); 232 while(componentNodeListIterator.hasNext()) 233 { 234 Element child = (Element)componentNodeListIterator.next(); 235 Integer id = new Integer (child.attributeValue("id")); 236 Integer contentId = new Integer (child.attributeValue("contentId")); 237 String name = child.attributeValue("name"); 238 239 ContentVO contentVO = ContentDeliveryController.getContentDeliveryController().getContentVO(contentId, db); 240 241 component = new InfoGlueComponent(); 242 component.setId(id); 243 component.setContentId(contentId); 244 component.setName(contentVO.getName()); 245 component.setSlotName(name); 247 component.setParentComponent(parentComponent); 248 249 List propertiesNodeList = child.selectNodes("properties"); 252 if(propertiesNodeList.size() > 0) 254 { 255 Element propertiesElement = (Element)propertiesNodeList.get(0); 256 257 List propertyNodeList = propertiesElement.selectNodes("property"); 258 Iterator propertyNodeListIterator = propertyNodeList.iterator(); 260 while(propertyNodeListIterator.hasNext()) 261 { 262 Element propertyElement = (Element)propertyNodeListIterator.next(); 263 264 String propertyName = propertyElement.attributeValue("name"); 265 String type = propertyElement.attributeValue("type"); 266 String path = propertyElement.attributeValue("path"); 267 268 if(path == null) 269 { 270 LanguageVO langaugeVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(getDatabase(), templateController.getSiteNodeId()); 271 if(propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()) != null) 272 path = propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()); 273 } 274 275 if(propertyElement.attributeValue("path_" + locale.getLanguage()) != null) 277 path = propertyElement.attributeValue("path_" + locale.getLanguage()); 278 280 Map property = new HashMap (); 281 property.put("name", propertyName); 282 property.put("path", path); 283 property.put("type", type); 284 285 List bindings = new ArrayList (); 286 List bindingNodeList = propertyElement.selectNodes("binding"); 287 Iterator bindingNodeListIterator = bindingNodeList.iterator(); 289 while(bindingNodeListIterator.hasNext()) 290 { 291 Element bindingElement = (Element)bindingNodeListIterator.next(); 292 String entity = bindingElement.attributeValue("entity"); 293 String entityId = bindingElement.attributeValue("entityId"); 294 if(entity.equalsIgnoreCase("Content")) 296 { 297 bindings.add(entityId); 298 } 299 else 300 { 301 bindings.add(entityId); 302 } 303 } 304 305 property.put("bindings", bindings); 306 307 component.getProperties().put(propertyName, property); 308 } 309 } 310 311 312 getComponentRestrictions(child, component, locale, templateController); 313 314 315 String componentString = this.getComponentString(templateController, contentId, component); 317 int offset = 0; 320 int slotStartIndex = componentString.indexOf("<ig:slot", offset); 321 while(slotStartIndex > -1) 322 { 323 int slotStopIndex = componentString.indexOf("</ig:slot>", slotStartIndex); 324 String slotString = componentString.substring(slotStartIndex, slotStopIndex + 10); 325 String slotId = slotString.substring(slotString.indexOf("id") + 4, slotString.indexOf("\"", slotString.indexOf("id") + 4)); 326 327 boolean inherit = true; 328 int inheritIndex = slotString.indexOf("inherit"); 329 if(inheritIndex > -1) 330 { 331 String inheritString = slotString.substring(inheritIndex + 9, slotString.indexOf("\"", inheritIndex + 9)); 332 inherit = Boolean.getBoolean(inheritString); 333 } 334 335 String [] allowedComponentNamesArray = null; 336 int allowedComponentNamesIndex = slotString.indexOf("allowedComponentNames"); 337 if(allowedComponentNamesIndex > -1) 338 { 339 String allowedComponentNames = slotString.substring(allowedComponentNamesIndex + 23, slotString.indexOf("\"", allowedComponentNamesIndex + 23)); 340 allowedComponentNamesArray = allowedComponentNames.split(","); 341 } 342 343 Slot slot = new Slot(); 344 slot.setId(slotId); 345 slot.setInherit(inherit); 346 slot.setAllowedComponentsArray(allowedComponentNamesArray); 347 348 List subComponents = getComponents(db, templateController, component, templateController.getSiteNodeId(), slotId); 349 slot.setComponents(subComponents); 350 351 component.getSlotList().add(slot); 352 353 offset = slotStopIndex; slotStartIndex = componentString.indexOf("<ig:slot", offset); 355 } 356 357 358 List anl = child.selectNodes("components"); 359 if(anl.size() > 0) 360 { 361 Element componentsElement = (Element)anl.get(0); 362 component.setComponents(getComponents(db, componentsElement, templateController, component)); 363 } 364 365 components.put(name, component); 366 } 367 368 369 return components; 370 } 371 372 375 376 protected Map getComponent(Database db, Element element, String componentName, TemplateController templateController, InfoGlueComponent parentComponent) throws Exception 377 { 378 InfoGlueComponent component = null; 380 381 Locale locale = LanguageDeliveryController.getLanguageDeliveryController().getLocaleWithId(db, templateController.getLanguageId()); 382 383 Map components = new HashMap (); 384 385 String componentXPath = getComponentXPath(parentComponent) + "/components/component[@name='" + componentName + "']"; 386 387 List componentNodeList = element.selectNodes(componentXPath); 389 Iterator componentNodeListIterator = componentNodeList.iterator(); 390 while(componentNodeListIterator.hasNext()) 391 { 392 Element child = (Element)componentNodeListIterator.next(); 393 Integer id = new Integer (child.attributeValue("id")); 394 Integer contentId = new Integer (child.attributeValue("contentId")); 395 String name = child.attributeValue("name"); 396 397 ContentVO contentVO = ContentDeliveryController.getContentDeliveryController().getContentVO(contentId, db); 398 399 component = new InfoGlueComponent(); 400 component.setId(id); 401 component.setContentId(contentId); 402 component.setName(contentVO.getName()); 403 component.setSlotName(name); 405 component.setParentComponent(parentComponent); 406 408 List propertiesNodeList = child.selectNodes("properties"); 411 if(propertiesNodeList.size() > 0) 413 { 414 Element propertiesElement = (Element)propertiesNodeList.get(0); 415 416 List propertyNodeList = propertiesElement.selectNodes("property"); 417 Iterator propertyNodeListIterator = propertyNodeList.iterator(); 419 while(propertyNodeListIterator.hasNext()) 420 { 421 Element propertyElement = (Element)propertyNodeListIterator.next(); 422 423 String propertyName = propertyElement.attributeValue("name"); 424 String type = propertyElement.attributeValue("type"); 425 String path = propertyElement.attributeValue("path"); 426 427 if(path == null) 428 { 429 LanguageVO langaugeVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(getDatabase(), templateController.getSiteNodeId()); 430 if(propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()) != null) 431 path = propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()); 432 } 433 434 if(propertyElement.attributeValue("path_" + locale.getLanguage()) != null) 436 path = propertyElement.attributeValue("path_" + locale.getLanguage()); 437 439 Map property = new HashMap (); 440 property.put("name", propertyName); 441 property.put("path", path); 442 property.put("type", type); 443 444 List bindings = new ArrayList (); 445 List bindingNodeList = propertyElement.selectNodes("binding"); 446 Iterator bindingNodeListIterator = bindingNodeList.iterator(); 448 while(bindingNodeListIterator.hasNext()) 449 { 450 Element bindingElement = (Element)bindingNodeListIterator.next(); 451 String entity = bindingElement.attributeValue("entity"); 452 String entityId = bindingElement.attributeValue("entityId"); 453 if(entity.equalsIgnoreCase("Content")) 455 { 456 bindings.add(entityId); 458 } 459 else 460 { 461 bindings.add(entityId); 463 } 464 } 465 466 property.put("bindings", bindings); 467 468 component.getProperties().put(propertyName, property); 469 } 470 } 471 472 getComponentRestrictions(child, component, locale, templateController); 473 474 List anl = child.selectNodes("components"); 475 if(anl.size() > 0) 477 { 478 Element componentsElement = (Element)anl.get(0); 479 component.setComponents(getComponents(db, componentsElement, templateController, component)); 480 } 481 482 List componentList = new ArrayList (); 483 if(components.containsKey(name)) 484 componentList = (List )components.get(name); 485 486 componentList.add(component); 487 488 components.put(name, componentList); 489 } 490 491 return components; 492 } 493 494 495 498 499 private String renderComponent(InfoGlueComponent component, TemplateController templateController, Integer repositoryId, Integer siteNodeId, Integer languageId, Integer contentId, Integer metainfoContentId) throws Exception 500 { 501 507 String decoratedComponent = ""; 508 509 String componentEditorUrl = CmsPropertyHandler.getComponentEditorUrl(); 510 templateController.setComponentLogic(new ComponentLogic(templateController, component)); 511 templateController.getDeliveryContext().getUsageListeners().add(templateController.getComponentLogic().getComponentDeliveryContext()); 512 513 boolean renderComponent = false; 514 boolean cacheComponent = false; 515 516 String cacheResult = templateController.getComponentLogic().getPropertyValue("CacheResult", true, false); 517 String updateInterval = templateController.getComponentLogic().getPropertyValue("UpdateInterval", true, false); 518 String componentCacheKey = templateController.getComponentLogic().getComponentDeliveryContext().getPageKey() + "_" + component.getId() + "_" + component.getSlotName() + "_" + component.getContentId() + "_" + component.getIsInherited(); 519 520 if(cacheResult == null || !cacheResult.equalsIgnoreCase("true")) 521 { 522 renderComponent = true; 523 } 524 else 525 { 526 cacheComponent = true; 527 String refresh = this.getRequest().getParameter("refresh"); 528 if(refresh != null && refresh.equalsIgnoreCase("true")) 529 renderComponent = true; 530 } 531 532 534 if(!renderComponent) 535 { 536 if(updateInterval != null && !updateInterval.equals("") && !updateInterval.equals("-1")) 538 { 539 decoratedComponent = (String )CacheController.getCachedObjectFromAdvancedCache("componentCache", componentCacheKey, new Integer (updateInterval).intValue()); 540 } 541 else 542 decoratedComponent = (String )CacheController.getCachedObjectFromAdvancedCache("componentCache", componentCacheKey); 543 544 if(decoratedComponent == null) 545 renderComponent = true; 546 } 547 548 550 if(renderComponent) 551 { 552 decoratedComponent = ""; 553 554 try 555 { 556 String componentString = getComponentString(templateController, component.getContentId(), component); 557 558 Map context = getDefaultContext(); 559 context.put("templateLogic", templateController); 560 StringWriter cacheString = new StringWriter (); 561 PrintWriter cachedStream = new PrintWriter (cacheString); 562 new VelocityTemplateProcessor().renderTemplate(context, cachedStream, componentString); 564 componentString = cacheString.toString(); 566 567 int offset = 0; 568 int slotStartIndex = componentString.indexOf("<ig:slot", offset); 569 int slotStopIndex = 0; 570 571 while(slotStartIndex > -1) 572 { 573 if(offset > 0) 574 decoratedComponent += componentString.substring(offset + 10, slotStartIndex); 575 else 576 decoratedComponent += componentString.substring(offset, slotStartIndex); 577 578 slotStopIndex = componentString.indexOf("</ig:slot>", slotStartIndex); 579 580 String slot = componentString.substring(slotStartIndex, slotStopIndex + 10); 581 String id = slot.substring(slot.indexOf("id") + 4, slot.indexOf("\"", slot.indexOf("id") + 4)); 582 583 boolean inherit = true; 584 int inheritIndex = slot.indexOf("inherit"); 585 if(inheritIndex > -1) 586 { 587 String inheritString = slot.substring(inheritIndex + 9, slot.indexOf("\"", inheritIndex + 9)); 588 inherit = Boolean.getBoolean(inheritString); 589 } 590 591 List subComponents = getInheritedComponents(templateController.getDatabase(), templateController, component, templateController.getSiteNodeId(), id, inherit); 592 Iterator subComponentsIterator = subComponents.iterator(); 593 while(subComponentsIterator.hasNext()) 594 { 595 InfoGlueComponent subComponent = (InfoGlueComponent)subComponentsIterator.next(); 596 logger.info(component.getName() + " had subcomponent " + subComponent.getName() + ":" + subComponent.getId()); 597 String subComponentString = ""; 598 if(subComponent != null) 599 { 600 subComponentString = renderComponent(subComponent, templateController, repositoryId, siteNodeId, languageId, contentId, metainfoContentId); 601 } 602 decoratedComponent += subComponentString.trim(); 603 } 604 605 offset = slotStopIndex; 606 slotStartIndex = componentString.indexOf("<ig:slot", offset); 607 } 608 609 if(offset > 0) 610 { 611 decoratedComponent += componentString.substring(offset + 10); 612 } 613 else 614 { 615 decoratedComponent += componentString.substring(offset); 616 } 617 618 if(cacheComponent) 619 { 620 if(this.getTemplateController().getOperatingMode().intValue() == 3) 621 CacheController.cacheObjectInAdvancedCache("componentCache", componentCacheKey, decoratedComponent, templateController.getComponentLogic().getComponentDeliveryContext().getAllUsedEntities(), false); 622 else 623 CacheController.cacheObjectInAdvancedCache("componentCache", componentCacheKey, decoratedComponent, templateController.getComponentLogic().getComponentDeliveryContext().getAllUsedEntities(), true); 624 } 625 626 } 627 catch(Exception e) 628 { 629 logger.warn("An component with either an empty template or with no template in the sitelanguages was found:" + e.getMessage(), e); 630 } 631 632 } 633 634 templateController.getDeliveryContext().getUsageListeners().remove(templateController.getComponentLogic().getComponentDeliveryContext()); 635 637 return decoratedComponent; 638 } 639 640 641 644 645 protected String getComponentString(TemplateController templateController, Integer contentId, InfoGlueComponent component) throws SystemException, Exception 646 { 647 String template = null; 648 649 try 650 { 651 if(templateController.getDeliveryContext().getShowSimple() == true) 652 { 653 String componentString = templateController.getContentAttribute(contentId, templateController.getTemplateAttributeName(), true); 654 String slots = ""; 655 int offset = 0; 656 int index = componentString.indexOf("<ig:slot"); 657 int end = componentString.indexOf("</ig:slot>", offset); 658 while(index > -1 && end > -1) 659 { 660 offset = end; 661 slots += componentString.substring(index, end + 10); 662 index = componentString.indexOf("<ig:slot", offset + 1); 663 end = componentString.indexOf("</ig:slot>", index); 664 } 665 template = "<div style=\"position:relative; awidth:90%; padding: 5px 5px 5px 5px; font-family:verdana, sans-serif; font-size:10px; border: 1px solid black;\">" + component.getName() + slots + "</div>"; 666 } 667 else 668 template = templateController.getContentAttribute(contentId, templateController.getTemplateAttributeName(), true); 669 670 if(template == null) 671 throw new SystemException("There was no template available on the content with id " + contentId + ". Check so that the templates language are active on your site."); 672 } 673 catch(Exception e) 674 { 675 logger.error(e.getMessage(), e); 676 throw e; 677 } 678 679 return template; 680 } 681 682 685 686 protected List getInheritedComponents(Database db, TemplateController templateController, InfoGlueComponent component, Integer siteNodeId, String id, boolean inherit) throws Exception 687 { 688 List inheritedComponents = new ArrayList (); 691 692 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(templateController.getSiteNodeId(), templateController.getLanguageId(), templateController.getContentId()); 693 694 Iterator slotIterator = component.getSlotList().iterator(); 695 while(slotIterator.hasNext()) 696 { 697 Slot slot = (Slot)slotIterator.next(); 698 if(slot.getId().equalsIgnoreCase(id)) 701 { 702 Iterator subComponentIterator = slot.getComponents().iterator(); 703 while(subComponentIterator.hasNext()) 704 { 705 InfoGlueComponent infoGlueComponent = (InfoGlueComponent)subComponentIterator.next(); 706 inheritedComponents.add(infoGlueComponent); 708 } 709 } 710 } 711 712 SiteNodeVO parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, siteNodeId); 713 714 boolean restrictAll = false; 715 Iterator restrictionsIterator = component.getRestrictions().iterator(); 716 while(restrictionsIterator.hasNext()) 717 { 718 ComponentRestriction restriction = (ComponentRestriction)restrictionsIterator.next(); 719 if(restriction.getType().equalsIgnoreCase("blockComponents")) 720 { 721 if(restriction.getSlotId().equalsIgnoreCase(id) && restriction.getArguments().equalsIgnoreCase("*")) 722 { 723 restrictAll = true; 724 } 725 } 726 } 727 while(inheritedComponents.size() == 0 && parentSiteNodeVO != null && inherit && !restrictAll) 728 { 729 String componentXML = this.getPageComponentsString(db, templateController, parentSiteNodeVO.getId(), templateController.getLanguageId(), component.getContentId()); 733 736 String key = "" + parentSiteNodeVO.getId() + "_" + componentXML.hashCode(); 737 Object componentsCandidate = CacheController.getCachedObjectFromAdvancedCache("componentPropertyCache", key); 738 Document document = null; 739 if(componentsCandidate == null) 740 { 741 document = new DOMBuilder().getDocument(componentXML); 743 } 746 else 747 { 748 document = (Document)componentsCandidate; 749 } 750 751 Map components = getComponent(db, document.getRootElement(), id, templateController, component); 752 754 if(components.containsKey(id)) 755 { 756 inheritedComponents = (List )components.get(id); 757 Iterator inheritedComponentIterator = inheritedComponents.iterator(); 758 while(inheritedComponentIterator.hasNext()) 759 { 760 InfoGlueComponent infoGlueComponent = (InfoGlueComponent)inheritedComponentIterator.next(); 761 infoGlueComponent.setIsInherited(true); 762 } 763 } 764 765 parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, parentSiteNodeVO.getId()); 766 } 767 768 return inheritedComponents; 769 } 770 771 774 775 private String getComponentXPath(InfoGlueComponent infoGlueComponent) 776 { 777 String path = ""; 778 String parentPath = ""; 779 780 InfoGlueComponent parentInfoGlueComponent = infoGlueComponent.getParentComponent(); 781 if(parentInfoGlueComponent != null && parentInfoGlueComponent.getId().intValue() != infoGlueComponent.getId().intValue()) 783 { 784 parentPath = getComponentXPath(parentInfoGlueComponent); 786 } 788 789 path = parentPath + "/components/component[@name='" + infoGlueComponent.getSlotName() + "']"; 791 793 return path; 794 } 795 796 799 800 protected InfoGlueComponent getComponent(Database db, TemplateController templateController, InfoGlueComponent component, Integer siteNodeId, String id) throws Exception 801 { 802 807 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(templateController.getSiteNodeId(), templateController.getLanguageId(), templateController.getContentId()); 808 809 String componentXML = this.getPageComponentsString(db, templateController, siteNodeId, templateController.getLanguageId(), component.getContentId()); 810 812 Document document = new DOMBuilder().getDocument(componentXML); 813 814 Map components = getComponent(db, document.getRootElement(), id, templateController, component); 815 816 InfoGlueComponent infoGlueComponent = (InfoGlueComponent)components.get(id); 817 819 SiteNodeVO parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, siteNodeId); 820 822 while(infoGlueComponent == null && parentSiteNodeVO != null) 823 { 824 componentXML = this.getPageComponentsString(db, templateController, parentSiteNodeVO.getId(), templateController.getLanguageId(), component.getContentId()); 825 827 document = new DOMBuilder().getDocument(componentXML); 828 829 components = getComponent(db, document.getRootElement(), id, templateController, component); 830 831 infoGlueComponent = (InfoGlueComponent)components.get(id); 832 if(infoGlueComponent != null) 834 infoGlueComponent.setIsInherited(true); 835 836 parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, parentSiteNodeVO.getId()); 837 } 839 840 842 return infoGlueComponent; 843 } 844 845 846 849 850 protected List getComponents(Database db, TemplateController templateController, InfoGlueComponent component, Integer siteNodeId, String id) throws Exception 851 { 852 857 List subComponents = new ArrayList (); 858 859 try 860 { 861 862 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(templateController.getSiteNodeId(), templateController.getLanguageId(), templateController.getContentId()); 863 864 String componentStructureXML = this.getPageComponentsString(db, templateController, siteNodeId, templateController.getLanguageId(), component.getContentId()); 865 867 Document document = new DOMBuilder().getDocument(componentStructureXML); 868 869 Map components = getComponent(db, document.getRootElement(), id, templateController, component); 870 871 if(components.containsKey(id)) 872 subComponents = (List )components.get(id); 873 874 SiteNodeVO parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, siteNodeId); 875 877 while((subComponents == null || subComponents.size() == 0) && parentSiteNodeVO != null) 878 { 879 componentStructureXML = this.getPageComponentsString(db, templateController, parentSiteNodeVO.getId(), templateController.getLanguageId(), component.getContentId()); 882 884 document = new DOMBuilder().getDocument(componentStructureXML); 885 886 components = getComponent(db, document.getRootElement(), id, templateController, component); 887 888 if(components.containsKey(id)) 889 subComponents = (List )components.get(id); 890 891 if(subComponents != null) 892 { 893 Iterator inheritedComponentsIterator = subComponents.iterator(); 895 while(inheritedComponentsIterator.hasNext()) 896 { 897 InfoGlueComponent infoGlueComponent = (InfoGlueComponent)inheritedComponentsIterator.next(); 898 infoGlueComponent.setIsInherited(true); 899 } 900 } 901 902 parentSiteNodeVO = nodeDeliveryController.getParentSiteNode(db, parentSiteNodeVO.getId()); 903 } 905 906 } 908 catch(Exception e) 909 { 910 logger.warn("An error occurred: " + e.getMessage(), e); 911 throw e; 912 } 913 914 return subComponents; 915 } 916 917 918 923 924 protected List getPageComponents(Database db, String componentXML, Element element, String slotName, TemplateController templateController, InfoGlueComponent parentComponent) throws Exception 925 { 926 928 Locale locale = LanguageDeliveryController.getLanguageDeliveryController().getLocaleWithId(db, templateController.getLanguageId()); 929 930 String key = "" + componentXML.hashCode() + "_" + templateController.getLanguageId() + "_" + slotName; 931 if(parentComponent != null) 932 key = "" + componentXML.hashCode() + "_" + templateController.getLanguageId() + "_" + slotName + "_" + parentComponent.getId() + "_" + parentComponent.getName() + "_" + parentComponent.getIsInherited(); 933 934 Object componentsCandidate = CacheController.getCachedObjectFromAdvancedCache("componentPropertyCache", key); 935 List components = new ArrayList (); 937 938 if(componentsCandidate != null) 939 { 940 if(componentsCandidate instanceof NullObject) 941 components = null; 942 else 943 components = (List )componentsCandidate; 944 } 945 else 946 { 947 String componentXPath = "component[@name='" + slotName + "']"; 948 List componentElements = element.selectNodes(componentXPath); 949 Iterator componentIterator = componentElements.iterator(); 951 while(componentIterator.hasNext()) 952 { 953 Element componentElement = (Element)componentIterator.next(); 954 955 Integer id = new Integer (componentElement.attributeValue("id")); 956 Integer contentId = new Integer (componentElement.attributeValue("contentId")); 957 String name = componentElement.attributeValue("name"); 958 959 try 960 { 961 ContentVO contentVO = ContentDeliveryController.getContentDeliveryController().getContentVO(contentId, db); 962 963 InfoGlueComponent component = new InfoGlueComponent(); 964 component.setId(id); 965 component.setContentId(contentId); 966 component.setName(contentVO.getName()); 967 component.setSlotName(name); 968 component.setParentComponent(parentComponent); 969 970 List propertiesNodeList = componentElement.selectNodes("properties"); 973 if(propertiesNodeList.size() > 0) 974 { 975 Element propertiesElement = (Element)propertiesNodeList.get(0); 976 977 List propertyNodeList = propertiesElement.selectNodes("property"); 978 Iterator propertyNodeListIterator = propertyNodeList.iterator(); 979 while(propertyNodeListIterator.hasNext()) 980 { 981 Element propertyElement = (Element)propertyNodeListIterator.next(); 982 983 String propertyName = propertyElement.attributeValue("name"); 984 String type = propertyElement.attributeValue("type"); 985 String path = propertyElement.attributeValue("path"); 986 987 if(path == null) 988 { 989 LanguageVO langaugeVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(getDatabase(), templateController.getSiteNodeId()); 990 if(propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()) != null) 991 path = propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()); 992 } 993 994 if(propertyElement.attributeValue("path_" + locale.getLanguage()) != null) 995 path = propertyElement.attributeValue("path_" + locale.getLanguage()); 996 997 Map property = new HashMap (); 998 property.put("name", propertyName); 999 property.put("path", path); 1000 property.put("type", type); 1001 1002 List bindings = new ArrayList (); 1003 List bindingNodeList = propertyElement.selectNodes("binding"); 1004 Iterator bindingNodeListIterator = bindingNodeList.iterator(); 1005 while(bindingNodeListIterator.hasNext()) 1006 { 1007 Element bindingElement = (Element)bindingNodeListIterator.next(); 1008 String entity = bindingElement.attributeValue("entity"); 1009 String entityId = bindingElement.attributeValue("entityId"); 1010 if(entity.equalsIgnoreCase("Content")) 1011 { 1012 bindings.add(entityId); 1013 } 1014 else 1015 { 1016 bindings.add(entityId); 1017 } 1018 } 1019 1020 property.put("bindings", bindings); 1021 1022 component.getProperties().put(propertyName, property); 1023 } 1024 } 1025 1026 1027 getComponentRestrictions(componentElement, component, locale, templateController); 1028 1029 try 1031 { 1032 String componentString = this.getComponentString(templateController, contentId, component); 1033 int offset = 0; 1034 int slotStartIndex = componentString.indexOf("<ig:slot", offset); 1035 while(slotStartIndex > -1) 1036 { 1037 int slotStopIndex = componentString.indexOf("</ig:slot>", slotStartIndex); 1038 String slotString = componentString.substring(slotStartIndex, slotStopIndex + 10); 1039 String slotId = slotString.substring(slotString.indexOf("id") + 4, slotString.indexOf("\"", slotString.indexOf("id") + 4)); 1040 1041 boolean inherit = true; 1042 int inheritIndex = slotString.indexOf("inherit"); 1043 if(inheritIndex > -1) 1044 { 1045 String inheritString = slotString.substring(inheritIndex + 9, slotString.indexOf("\"", inheritIndex + 9)); 1046 inherit = Boolean.getBoolean(inheritString); 1048 } 1049 1050 String [] allowedComponentNamesArray = null; 1051 int allowedComponentNamesIndex = slotString.indexOf("allowedComponentNames"); 1052 if(allowedComponentNamesIndex > -1) 1053 { 1054 String allowedComponentNames = slotString.substring(allowedComponentNamesIndex + 23, slotString.indexOf("\"", allowedComponentNamesIndex + 23)); 1055 allowedComponentNamesArray = allowedComponentNames.split(","); 1057 } 1058 1059 Slot slot = new Slot(); 1060 slot.setId(slotId); 1061 slot.setInherit(inherit); 1062 slot.setAllowedComponentsArray(allowedComponentNamesArray); 1063 1064 Element componentsElement = (Element)componentElement.selectSingleNode("components"); 1065 1066 List subComponents = getPageComponents(db, componentXML, componentsElement, slotId, templateController, component); 1067 slot.setComponents(subComponents); 1069 1070 component.getSlotList().add(slot); 1071 1072 offset = slotStopIndex; 1073 slotStartIndex = componentString.indexOf("<ig:slot", offset); 1074 } 1075 } 1076 catch(Exception e) 1077 { 1078 logger.warn("An component with either an empty template or with no template in the sitelanguages was found:" + e.getMessage(), e); 1079 } 1080 1081 components.add(component); 1082 } 1083 catch(Exception e) 1084 { 1085 logger.warn("There was deleted referenced component or some other problem when rendering siteNode: " + templateController.getCurrentPagePath() + "(" + templateController.getSiteNodeId() + ") in language " + templateController.getLanguageId() + ":" + e.getMessage(), e); 1086 } 1087 } 1088 } 1089 1090 if(components != null) 1091 CacheController.cacheObjectInAdvancedCache("componentPropertyCache", key, components, new String []{}, false); 1092 else 1093 CacheController.cacheObjectInAdvancedCache("componentPropertyCache", key, new NullObject(), new String []{}, false); 1094 1095 return components; 1096 } 1097 1098 1101 private void getComponentProperties(Element child, InfoGlueComponent component, Locale locale, TemplateController templateController) throws Exception 1102 { 1103 List propertiesNodeList = child.selectNodes("properties"); 1104 if(propertiesNodeList.size() > 0) 1106 { 1107 Element propertiesElement = (Element)propertiesNodeList.get(0); 1108 1109 List propertyNodeList = propertiesElement.selectNodes("property"); 1110 Iterator propertyNodeListIterator = propertyNodeList.iterator(); 1112 while(propertyNodeListIterator.hasNext()) 1113 { 1114 Element propertyElement = (Element)propertyNodeListIterator.next(); 1115 1116 String propertyName = propertyElement.attributeValue("name"); 1117 String type = propertyElement.attributeValue("type"); 1118 String path = propertyElement.attributeValue("path"); 1119 1120 if(path == null) 1121 { 1122 LanguageVO langaugeVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(getDatabase(), templateController.getSiteNodeId()); 1123 if(propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()) != null) 1124 path = propertyElement.attributeValue("path_" + langaugeVO.getLanguageCode()); 1125 } 1126 1127 if(propertyElement.attributeValue("path_" + locale.getLanguage()) != null) 1129 path = propertyElement.attributeValue("path_" + locale.getLanguage()); 1130 1132 Map property = new HashMap (); 1133 property.put("name", propertyName); 1134 property.put("path", path); 1135 property.put("type", type); 1136 1137 List bindings = new ArrayList (); 1138 List bindingNodeList = propertyElement.selectNodes("binding"); 1139 Iterator bindingNodeListIterator = bindingNodeList.iterator(); 1141 while(bindingNodeListIterator.hasNext()) 1142 { 1143 Element bindingElement = (Element)bindingNodeListIterator.next(); 1144 String entity = bindingElement.attributeValue("entity"); 1145 String entityId = bindingElement.attributeValue("entityId"); 1146 if(entity.equalsIgnoreCase("Content")) 1148 { 1149 bindings.add(entityId); 1150 } 1151 else 1152 { 1153 bindings.add(entityId); 1154 } 1155 } 1156 1157 property.put("bindings", bindings); 1158 1159 component.getProperties().put(propertyName, property); 1160 } 1161 } 1162 } 1163 1164 1165 1168 private void getComponentRestrictions(Element child, InfoGlueComponent component, Locale locale, TemplateController templateController) throws Exception 1169 { 1170 List restrictionsNodeList = child.selectNodes("restrictions"); 1172 if(restrictionsNodeList.size() > 0) 1174 { 1175 Element restrictionsElement = (Element)restrictionsNodeList.get(0); 1176 1177 List restrictionNodeList = restrictionsElement.selectNodes("restriction"); 1178 Iterator restrictionNodeListIterator = restrictionNodeList.iterator(); 1180 while(restrictionNodeListIterator.hasNext()) 1181 { 1182 Element restrictionElement = (Element)restrictionNodeListIterator.next(); 1183 1184 ComponentRestriction restriction = new ComponentRestriction(); 1185 1186 String type = restrictionElement.attributeValue("type"); 1187 if(type.equals("blockComponents")) 1188 { 1189 String slotId = restrictionElement.attributeValue("slotId"); 1190 String arguments = restrictionElement.attributeValue("arguments"); 1191 1192 restriction.setType(type); 1193 restriction.setSlotId(slotId); 1194 restriction.setArguments(arguments); 1195 } 1196 1197 component.getRestrictions().add(restriction); 1198 } 1199 } 1200 } 1201 1202} 1203 | Popular Tags |