1 25 26 package org.infoglue.cms.controllers.kernel.impl.simple; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import org.apache.log4j.Logger; 38 import org.exolab.castor.jdo.Database; 39 import org.exolab.castor.jdo.OQLQuery; 40 import org.exolab.castor.jdo.QueryResults; 41 import org.infoglue.cms.applications.databeans.ReferenceBean; 42 import org.infoglue.cms.applications.databeans.ReferenceVersionBean; 43 import org.infoglue.cms.entities.content.Content; 44 import org.infoglue.cms.entities.content.ContentVersion; 45 import org.infoglue.cms.entities.content.ContentVersionVO; 46 import org.infoglue.cms.entities.kernel.BaseEntityVO; 47 import org.infoglue.cms.entities.management.CategoryVO; 48 import org.infoglue.cms.entities.management.Language; 49 import org.infoglue.cms.entities.management.Registry; 50 import org.infoglue.cms.entities.management.RegistryVO; 51 import org.infoglue.cms.entities.management.impl.simple.RegistryImpl; 52 import org.infoglue.cms.entities.structure.Qualifyer; 53 import org.infoglue.cms.entities.structure.ServiceBinding; 54 import org.infoglue.cms.entities.structure.SiteNode; 55 import org.infoglue.cms.entities.structure.SiteNodeVersion; 56 import org.infoglue.cms.entities.structure.SiteNodeVersionVO; 57 import org.infoglue.cms.exception.ConstraintException; 58 import org.infoglue.cms.exception.SystemException; 59 60 61 68 69 public class RegistryController extends BaseController 70 { 71 private final static Logger logger = Logger.getLogger(RegistryController.class.getName()); 72 73 private static final RegistryController instance = new RegistryController(); 74 75 public static RegistryController getController() 76 { 77 return instance; 78 } 79 80 private RegistryController() 81 { 82 } 83 84 91 92 public RegistryVO create(RegistryVO valueObject, Database db) throws ConstraintException, SystemException, Exception 93 { 94 Registry registry = new RegistryImpl(); 95 registry.setValueObject(valueObject); 96 db.create(registry); 97 return registry.getValueObject(); 98 } 99 100 107 108 public RegistryVO update(RegistryVO valueObject, Database db) throws ConstraintException, SystemException 109 { 110 return (RegistryVO) updateEntity(RegistryImpl.class, (BaseEntityVO) valueObject, db); 111 } 112 113 114 120 121 public void delete(Integer registryId) throws ConstraintException, SystemException 122 { 123 this.deleteEntity(RegistryImpl.class, registryId); 124 } 125 126 134 135 public void updateContentVersion(ContentVersionVO contentVersionVO) throws ConstraintException, SystemException 136 { 137 String versionValue = contentVersionVO.getVersionValue(); 138 139 Database db = CastorDatabaseService.getDatabase(); 140 141 try 142 { 143 beginTransaction(db); 144 145 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getContentVersionId(), db); 146 updateContentVersion(contentVersion, db); 147 148 commitTransaction(db); 149 } 150 catch (Exception e) 151 { 152 rollbackTransaction(db); 153 throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e); 154 } 155 } 156 157 158 159 167 168 public void updateContentVersion(ContentVersion contentVersion, Database db) throws ConstraintException, SystemException, Exception 169 { 170 String versionValue = contentVersion.getVersionValue(); 171 172 ContentVersion oldContentVersion = contentVersion; Content oldContent = oldContentVersion.getOwningContent(); 174 175 if(oldContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info")) 176 { 177 logger.info("It was a meta info so lets check it for other stuff as well"); 178 179 SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersionWhichUsesContentVersionAsMetaInfo(oldContentVersion, db); 180 if(siteNodeVersion != null) 181 { 182 logger.info("Going to use " + siteNodeVersion.getId() + " as reference"); 183 clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db); 184 185 getComponents(siteNodeVersion, versionValue, db); 186 getComponentBindings(siteNodeVersion, versionValue, db); 187 getPageBindings(siteNodeVersion, db); 188 } 189 190 getInlineSiteNodes(oldContentVersion, versionValue, db); 191 getInlineContents(oldContentVersion, versionValue, db); 192 getRelationSiteNodes(oldContentVersion, versionValue, db); 193 getRelationContents(oldContentVersion, versionValue, db); 194 } 195 else 196 { 197 clearRegistryVOList(ContentVersion.class.getName(), oldContentVersion.getContentVersionId().toString(), db); 198 getInlineSiteNodes(oldContentVersion, versionValue, db); 199 getInlineContents(oldContentVersion, versionValue, db); 200 getRelationSiteNodes(oldContentVersion, versionValue, db); 201 getRelationContents(oldContentVersion, versionValue, db); 202 } 203 } 204 205 206 214 215 public void updateContentVersion(ContentVersion contentVersion, SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception 216 { 217 String versionValue = contentVersion.getVersionValue(); 218 219 ContentVersion oldContentVersion = contentVersion; Content oldContent = oldContentVersion.getOwningContent(); 221 222 if(oldContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info")) 223 { 224 logger.info("It was a meta info so lets check it for other stuff as well"); 225 226 if(siteNodeVersion != null) 227 { 228 logger.info("Going to use " + siteNodeVersion.getId() + " as reference"); 229 clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db); 230 231 getComponents(siteNodeVersion, versionValue, db); 232 getComponentBindings(siteNodeVersion, versionValue, db); 233 getPageBindings(siteNodeVersion, db); 234 } 235 236 getInlineSiteNodes(oldContentVersion, versionValue, db); 237 getInlineContents(oldContentVersion, versionValue, db); 238 getRelationSiteNodes(oldContentVersion, versionValue, db); 239 getRelationContents(oldContentVersion, versionValue, db); 240 } 241 else 242 { 243 clearRegistryVOList(ContentVersion.class.getName(), oldContentVersion.getContentVersionId().toString(), db); 244 if(siteNodeVersion != null) 245 getPageBindings(siteNodeVersion, db); 246 getInlineSiteNodes(oldContentVersion, versionValue, db); 247 getInlineContents(oldContentVersion, versionValue, db); 248 getRelationSiteNodes(oldContentVersion, versionValue, db); 249 getRelationContents(oldContentVersion, versionValue, db); 250 } 251 } 252 253 260 261 public void updateSiteNodeVersion(SiteNodeVersionVO siteNodeVersionVO) throws ConstraintException, SystemException 262 { 263 Database db = CastorDatabaseService.getDatabase(); 264 265 try 266 { 267 beginTransaction(db); 268 269 logger.info("Starting RegistryController.updateSiteNodeVersion..."); 270 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionVO.getId(), db); 271 logger.info("Before RegistryController.updateSiteNodeVersion..."); 272 updateSiteNodeVersion(siteNodeVersion, db); 273 logger.info("Before commit RegistryController.updateSiteNodeVersion..."); 274 275 commitTransaction(db); 276 } 277 catch (Exception e) 278 { 279 rollbackTransaction(db); 280 throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e); 281 } 282 } 283 284 291 292 public void updateSiteNodeVersion(SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception 293 { 294 SiteNodeVersion oldSiteNodeVersion = siteNodeVersion; 295 SiteNode oldSiteNode = oldSiteNodeVersion.getOwningSiteNode(); 296 297 logger.info("Before clearing old registry..."); 298 clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db); 299 logger.info("After clearing old registry..."); 300 301 Collection serviceBindings = siteNodeVersion.getServiceBindings(); 302 Iterator serviceBindingIterator = serviceBindings.iterator(); 303 while(serviceBindingIterator.hasNext()) 304 { 305 ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next(); 306 if(serviceBinding.getBindingQualifyers() != null) 307 { 308 Iterator qualifyersIterator = serviceBinding.getBindingQualifyers().iterator(); 309 while(qualifyersIterator.hasNext()) 310 { 311 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next(); 312 String name = qualifyer.getName(); 313 String value = qualifyer.getValue(); 314 315 try 316 { 317 RegistryVO registryVO = new RegistryVO(); 318 registryVO.setReferenceType(RegistryVO.PAGE_BINDING); 319 if(name.equalsIgnoreCase("contentId")) 320 { 321 Content content = ContentController.getContentController().getContentWithId(new Integer (value), db); 322 323 registryVO.setEntityId(value); 324 registryVO.setEntityName(Content.class.getName()); 325 326 registryVO.setReferencingEntityId(siteNodeVersion.getId().toString()); 327 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 328 registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString()); 329 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 330 331 Language masterLanguage = LanguageController.getController().getMasterLanguage(db, siteNodeVersion.getOwningSiteNode().getRepository().getId()); 332 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(content.getContentId(), masterLanguage.getId(), db); 333 getComponents(siteNodeVersion, contentVersion.getVersionValue(), db); 334 getComponentBindings(siteNodeVersion, contentVersion.getVersionValue(), db); 335 336 347 } 348 else if(name.equalsIgnoreCase("siteNodeId")) 349 { 350 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (value), db); 351 352 registryVO.setEntityId(value); 353 registryVO.setEntityName(SiteNode.class.getName()); 354 355 registryVO.setReferencingEntityId(siteNodeVersion.getId().toString()); 356 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 357 registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString()); 358 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 359 } 360 361 logger.info("Before creating registry entry..."); 362 363 this.create(registryVO, db); 364 } 365 catch(Exception e) 366 { 367 e.printStackTrace(); 368 } 369 } 370 } 371 } 372 } 373 374 375 382 383 public void getPageBindings(SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception 384 { 385 SiteNode oldSiteNode = siteNodeVersion.getOwningSiteNode(); 386 387 Collection serviceBindings = siteNodeVersion.getServiceBindings(); 388 Iterator serviceBindingIterator = serviceBindings.iterator(); 389 while(serviceBindingIterator.hasNext()) 390 { 391 ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next(); 392 if(serviceBinding.getBindingQualifyers() != null) 393 { 394 Iterator qualifyersIterator = serviceBinding.getBindingQualifyers().iterator(); 395 while(qualifyersIterator.hasNext()) 396 { 397 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next(); 398 String name = qualifyer.getName(); 399 String value = qualifyer.getValue(); 400 401 try 402 { 403 RegistryVO registryVO = new RegistryVO(); 404 registryVO.setReferenceType(RegistryVO.PAGE_BINDING); 405 if(name.equalsIgnoreCase("contentId")) 406 { 407 Content content = ContentController.getContentController().getContentWithId(new Integer (value), db); 408 409 registryVO.setEntityId(value); 410 registryVO.setEntityName(Content.class.getName()); 411 412 registryVO.setReferencingEntityId(siteNodeVersion.getId().toString()); 413 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 414 registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString()); 415 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 416 417 427 } 428 else if(name.equalsIgnoreCase("siteNodeId")) 429 { 430 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (value), db); 431 432 registryVO.setEntityId(value); 433 registryVO.setEntityName(SiteNode.class.getName()); 434 435 registryVO.setReferencingEntityId(siteNodeVersion.getId().toString()); 436 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 437 registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString()); 438 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 439 } 440 441 logger.info("Before creating registry entry..."); 442 443 this.create(registryVO, db); 444 } 445 catch(Exception e) 446 { 447 e.printStackTrace(); 448 } 449 } 450 } 451 } 452 } 453 454 457 458 public void getInlineSiteNodes(ContentVersion contentVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 459 { 460 Pattern pattern = Pattern.compile("\\$templateLogic\\.getPageUrl\\(.*?\\)"); 461 Matcher matcher = pattern.matcher(versionValue); 462 while ( matcher.find() ) 463 { 464 String match = matcher.group(); 465 logger.info("Adding match to registry after some processing: " + match); 466 Integer siteNodeId; 467 468 int siteNodeStartIndex = match.indexOf("("); 469 int siteNodeEndIndex = match.indexOf(","); 470 if(siteNodeStartIndex > 0 && siteNodeEndIndex > 0 && siteNodeEndIndex > siteNodeStartIndex) 471 { 472 String siteNodeIdString = match.substring(siteNodeStartIndex + 1, siteNodeEndIndex); 473 try 474 { 475 if(siteNodeIdString.indexOf("templateLogic.siteNodeId") == -1) 476 { 477 siteNodeId = new Integer (siteNodeIdString); 478 logger.info("siteNodeId:" + siteNodeId); 479 RegistryVO registryVO = new RegistryVO(); 480 registryVO.setEntityId(siteNodeId.toString()); 481 registryVO.setEntityName(SiteNode.class.getName()); 482 registryVO.setReferenceType(RegistryVO.INLINE_LINK); 483 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString()); 484 registryVO.setReferencingEntityName(ContentVersion.class.getName()); 485 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString()); 486 registryVO.setReferencingEntityCompletingName(Content.class.getName()); 487 488 this.create(registryVO, db); 489 } 490 } 491 catch(Exception e) 492 { 493 logger.warn("Tried to register inline sitenodes with exception as result:" + e.getMessage(), e); 494 } 495 } 496 } 497 } 498 499 502 503 public void getInlineContents(ContentVersion contentVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 504 { 505 Pattern pattern = Pattern.compile("\\$templateLogic\\.getInlineAssetUrl\\(.*?\\)"); 506 Matcher matcher = pattern.matcher(versionValue); 507 while ( matcher.find() ) 508 { 509 String match = matcher.group(); 510 logger.info("Adding match to registry after some processing: " + match); 511 Integer contentId; 512 513 int contentStartIndex = match.indexOf("("); 514 int contentEndIndex = match.indexOf(","); 515 if(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex) 516 { 517 contentId = new Integer (match.substring(contentStartIndex + 1, contentEndIndex)); 518 logger.info("contentId:" + contentId); 519 520 RegistryVO registryVO = new RegistryVO(); 521 registryVO.setEntityId(contentId.toString()); 522 registryVO.setEntityName(Content.class.getName()); 523 registryVO.setReferenceType(RegistryVO.INLINE_ASSET); 524 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString()); 525 registryVO.setReferencingEntityName(ContentVersion.class.getName()); 526 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString()); 527 registryVO.setReferencingEntityCompletingName(Content.class.getName()); 528 529 this.create(registryVO, db); 530 } 531 } 532 } 533 534 535 538 539 public void getRelationSiteNodes(ContentVersion contentVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 540 { 541 Pattern pattern = Pattern.compile("<qualifyer entity='SiteNode'>.*?</qualifyer>"); 542 Matcher matcher = pattern.matcher(versionValue); 543 while ( matcher.find() ) 544 { 545 String match = matcher.group(); 546 logger.info("Adding match to registry after some processing: " + match); 547 Integer siteNodeId; 548 549 int siteNodeStartIndex = match.indexOf("<id>"); 550 int siteNodeEndIndex = match.indexOf("</id>"); 551 while(siteNodeStartIndex > 0 && siteNodeEndIndex > 0 && siteNodeEndIndex > siteNodeStartIndex) 552 { 553 siteNodeId = new Integer (match.substring(siteNodeStartIndex + 4, siteNodeEndIndex)); 554 logger.info("siteNodeId:" + siteNodeId); 555 RegistryVO registryVO = new RegistryVO(); 556 registryVO.setEntityId(siteNodeId.toString()); 557 registryVO.setEntityName(SiteNode.class.getName()); 558 registryVO.setReferenceType(RegistryVO.INLINE_SITE_NODE_RELATION); 559 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString()); 560 registryVO.setReferencingEntityName(ContentVersion.class.getName()); 561 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString()); 562 registryVO.setReferencingEntityCompletingName(Content.class.getName()); 563 564 this.create(registryVO, db); 565 566 siteNodeStartIndex = match.indexOf("<id>", siteNodeEndIndex); 567 siteNodeEndIndex = match.indexOf("</id>", siteNodeStartIndex); 568 } 569 } 570 } 571 572 575 576 public void getRelationContents(ContentVersion contentVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 577 { 578 Pattern pattern = Pattern.compile("<qualifyer entity='Content'>.*?</qualifyer>"); 579 Matcher matcher = pattern.matcher(versionValue); 580 while ( matcher.find() ) 581 { 582 String match = matcher.group(); 583 logger.info("Adding match to registry after some processing: " + match); 584 Integer contentId; 585 586 int contentStartIndex = match.indexOf("<id>"); 587 int contentEndIndex = match.indexOf("</id>"); 588 while(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex) 589 { 590 contentId = new Integer (match.substring(contentStartIndex + 4, contentEndIndex)); 591 logger.info("contentId:" + contentId); 592 593 RegistryVO registryVO = new RegistryVO(); 594 registryVO.setEntityId(contentId.toString()); 595 registryVO.setEntityName(Content.class.getName()); 596 registryVO.setReferenceType(RegistryVO.INLINE_CONTENT_RELATION); 597 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString()); 598 registryVO.setReferencingEntityName(ContentVersion.class.getName()); 599 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString()); 600 registryVO.setReferencingEntityCompletingName(Content.class.getName()); 601 602 this.create(registryVO, db); 603 604 contentStartIndex = match.indexOf("<id>", contentEndIndex); 605 contentEndIndex = match.indexOf("</id>", contentStartIndex); 606 } 607 } 608 } 609 610 611 614 615 public void getComponents(SiteNodeVersion siteNodeVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 616 { 617 List foundComponents = new ArrayList (); 618 619 Pattern pattern = Pattern.compile("contentId=\".*?\""); 620 Matcher matcher = pattern.matcher(versionValue); 621 while ( matcher.find() ) 622 { 623 String match = matcher.group(); 624 logger.info("Adding match to registry after some processing: " + match); 625 Integer contentId; 626 627 int contentStartIndex = match.indexOf("\""); 628 int contentEndIndex = match.lastIndexOf("\""); 629 if(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex) 630 { 631 contentId = new Integer (match.substring(contentStartIndex + 1, contentEndIndex)); 632 logger.info("contentId:" + contentId); 633 634 if(!foundComponents.contains(contentId)) 635 { 636 RegistryVO registryVO = new RegistryVO(); 637 registryVO.setEntityId(contentId.toString()); 638 registryVO.setEntityName(Content.class.getName()); 639 registryVO.setReferenceType(RegistryVO.PAGE_COMPONENT); 640 registryVO.setReferencingEntityId(siteNodeVersion.getSiteNodeVersionId().toString()); 641 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 642 registryVO.setReferencingEntityCompletingId(siteNodeVersion.getOwningSiteNode().getSiteNodeId().toString()); 643 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 644 645 this.create(registryVO, db); 646 647 foundComponents.add(contentId); 648 } 649 } 650 } 651 } 652 653 656 657 public void getComponentBindings(SiteNodeVersion siteNodeVersion, String versionValue, Database db) throws ConstraintException, SystemException, Exception 658 { 659 List foundComponents = new ArrayList (); 660 661 Pattern pattern = Pattern.compile("<binding entity=\".*?\" entityId=\".*?\">"); 662 Matcher matcher = pattern.matcher(versionValue); 663 while ( matcher.find() ) 664 { 665 String match = matcher.group(); 666 logger.info("Adding match to registry after some processing: " + match); 667 String entityName; 668 String entityId; 669 670 int entityNameStartIndex = match.indexOf("\""); 671 int entityNameEndIndex = match.indexOf("\"", entityNameStartIndex + 1); 672 logger.info("entityNameStartIndex:" + entityNameStartIndex); 673 logger.info("entityNameEndIndex:" + entityNameEndIndex); 674 if(entityNameStartIndex > 0 && entityNameEndIndex > 0 && entityNameEndIndex > entityNameStartIndex) 675 { 676 entityName = match.substring(entityNameStartIndex + 1, entityNameEndIndex); 677 logger.info("entityName:" + entityName); 678 679 int entityIdStartIndex = match.indexOf("\"", entityNameEndIndex + 1); 680 int entityIdEndIndex = match.indexOf("\"", entityIdStartIndex + 1); 681 logger.info("entityIdStartIndex:" + entityIdStartIndex); 682 logger.info("entityIdEndIndex:" + entityIdEndIndex); 683 if(entityIdStartIndex > 0 && entityIdEndIndex > 0 && entityIdEndIndex > entityIdStartIndex) 684 { 685 entityId = match.substring(entityIdStartIndex + 1, entityIdEndIndex); 686 logger.info("entityId:" + entityId); 687 688 String key = entityName + ":" + entityId; 689 if(!foundComponents.contains(key)) 690 { 691 RegistryVO registryVO = new RegistryVO(); 692 if(entityName.indexOf("Content") > -1) 693 registryVO.setEntityName(Content.class.getName()); 694 else 695 registryVO.setEntityName(SiteNode.class.getName()); 696 697 registryVO.setEntityId(entityId); 698 registryVO.setReferenceType(RegistryVO.PAGE_COMPONENT_BINDING); 699 registryVO.setReferencingEntityId(siteNodeVersion.getSiteNodeVersionId().toString()); 700 registryVO.setReferencingEntityName(SiteNodeVersion.class.getName()); 701 registryVO.setReferencingEntityCompletingId(siteNodeVersion.getOwningSiteNode().getSiteNodeId().toString()); 702 registryVO.setReferencingEntityCompletingName(SiteNode.class.getName()); 703 704 this.create(registryVO, db); 705 706 foundComponents.add(key); 707 } 708 } 709 } 710 } 711 } 712 713 716 public BaseEntityVO getNewVO() 717 { 718 return new CategoryVO(); 719 } 720 721 727 793 794 public List getReferencingObjectsForContent(Integer contentId) throws SystemException 795 { 796 797 List referenceBeanList = new ArrayList (); 798 799 Database db = CastorDatabaseService.getDatabase(); 800 801 try 802 { 803 beginTransaction(db); 804 805 referenceBeanList = getReferencingObjectsForContent(contentId, db); 806 807 901 902 commitTransaction(db); 903 } 904 catch (Exception e) 905 { 906 e.printStackTrace(); 907 logger.warn("One of the references was not found which is bad but not critical:" + e.getMessage(), e); 908 rollbackTransaction(db); 909 } 911 912 logger.info("referenceBeanList:" + referenceBeanList.size()); 913 914 return referenceBeanList; 915 } 916 917 public List getReferencingObjectsForContent(Integer contentId, Database db) throws SystemException, Exception 918 { 919 920 List referenceBeanList = new ArrayList (); 921 922 Map entries = new HashMap (); 923 924 List registryEntires = getMatchingRegistryVOList(Content.class.getName(), contentId.toString(), db); 925 logger.info("registryEntires:" + registryEntires.size()); 926 Iterator registryEntiresIterator = registryEntires.iterator(); 927 while(registryEntiresIterator.hasNext()) 928 { 929 RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next(); 930 logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId()); 931 boolean add = true; 932 933 String key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId(); 934 ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key); 936 if(existingReferenceBean == null) 937 { 938 939 existingReferenceBean = new ReferenceBean(); 940 logger.info("Adding referenceBean to entries with key:" + key); 941 entries.put(key, existingReferenceBean); 942 referenceBeanList.add(existingReferenceBean); 943 } 944 945 ReferenceVersionBean referenceVersionBean = new ReferenceVersionBean(); 946 if(registryVO.getReferencingEntityName().indexOf("Content") > -1) 947 { 948 try 949 { 950 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer (registryVO.getReferencingEntityId()), db); 951 logger.info("contentVersion:" + contentVersion.getContentVersionId()); 952 existingReferenceBean.setName(contentVersion.getOwningContent().getName()); 953 existingReferenceBean.setReferencingCompletingObject(contentVersion.getOwningContent().getValueObject()); 954 955 referenceVersionBean.setReferencingObject(contentVersion.getValueObject()); 956 referenceVersionBean.getRegistryVOList().add(registryVO); 957 } 958 catch(Exception e) 959 { 960 add = false; 961 logger.info("content:" + registryVO.getReferencingEntityId() + " did not exist - skipping.."); 962 } 963 } 964 else 965 { 966 try 967 { 968 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer (registryVO.getReferencingEntityId()), db); 969 logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId()); 970 logger.info("siteNode:" + siteNodeVersion.getOwningSiteNode().getId()); 971 existingReferenceBean.setName(siteNodeVersion.getOwningSiteNode().getName()); 972 existingReferenceBean.setReferencingCompletingObject(siteNodeVersion.getOwningSiteNode().getValueObject()); 973 974 referenceVersionBean.setReferencingObject(siteNodeVersion.getValueObject()); 975 referenceVersionBean.getRegistryVOList().add(registryVO); 976 } 977 catch(Exception e) 978 { 979 add = false; 980 logger.info("siteNode:" + registryVO.getReferencingEntityId() + " did not exist - skipping.."); 981 } 982 } 983 984 if(add) 985 { 986 boolean exists = false; 987 ReferenceVersionBean existingReferenceVersionBean = null; 988 Iterator versionsIterator = existingReferenceBean.getVersions().iterator(); 989 while(versionsIterator.hasNext()) 990 { 991 existingReferenceVersionBean = (ReferenceVersionBean)versionsIterator.next(); 992 if(existingReferenceVersionBean.getReferencingObject().equals(referenceVersionBean.getReferencingObject())) 993 { 994 exists = true; 995 break; 996 } 997 } 998 999 if(!exists) 1000 existingReferenceBean.getVersions().add(referenceVersionBean); 1001 else 1002 existingReferenceVersionBean.getRegistryVOList().add(registryVO); 1003 1004 } 1005 1006 } 1007 1008 Iterator i = referenceBeanList.iterator(); 1009 while(i.hasNext()) 1010 { 1011 ReferenceBean referenceBean = (ReferenceBean)i.next(); 1012 if(referenceBean.getVersions().size() == 0) 1013 i.remove(); 1014 } 1015 1016 logger.info("referenceBeanList:" + referenceBeanList.size()); 1017 1018 return referenceBeanList; 1019 } 1020 1021 1022 1028 1096 1097 public List getReferencingObjectsForSiteNode(Integer siteNodeId) throws SystemException, Exception 1098 { 1099 List referenceBeanList = new ArrayList (); 1100 1101 Database db = CastorDatabaseService.getDatabase(); 1102 1103 try 1104 { 1105 beginTransaction(db); 1106 1107 referenceBeanList = getReferencingObjectsForSiteNode(siteNodeId, db); 1108 1109 commitTransaction(db); 1110 } 1111 catch (Exception e) 1112 { 1113 e.printStackTrace(); 1114 logger.warn("One of the references was not found which is bad but not critical:" + e.getMessage(), e); 1115 rollbackTransaction(db); 1116 } 1117 1118 return referenceBeanList; 1119 } 1120 1121 1122 public List getReferencingObjectsForSiteNode(Integer siteNodeId, Database db) throws SystemException, Exception 1123 { 1124 List referenceBeanList = new ArrayList (); 1125 1126 Map entries = new HashMap (); 1127 1128 List registryEntires = getMatchingRegistryVOList(SiteNode.class.getName(), siteNodeId.toString(), db); 1129 Iterator registryEntiresIterator = registryEntires.iterator(); 1130 while(registryEntiresIterator.hasNext()) 1131 { 1132 RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next(); 1133 logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId()); 1134 boolean add = true; 1135 1136 String key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId(); 1137 ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key); 1139 if(existingReferenceBean == null) 1140 { 1141 existingReferenceBean = new ReferenceBean(); 1142 logger.info("Adding referenceBean to entries with key:" + key); 1143 entries.put(key, existingReferenceBean); 1144 referenceBeanList.add(existingReferenceBean); 1145 } 1146 1147 ReferenceVersionBean referenceVersionBean = new ReferenceVersionBean(); 1148 1149 if(registryVO.getReferencingEntityName().indexOf("Content") > -1) 1150 { 1151 try 1152 { 1153 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer (registryVO.getReferencingEntityId()), db); 1154 logger.info("contentVersion:" + contentVersion.getContentVersionId()); 1155 existingReferenceBean.setName(contentVersion.getOwningContent().getName()); 1156 existingReferenceBean.setReferencingCompletingObject(contentVersion.getOwningContent().getValueObject()); 1157 1158 referenceVersionBean.setReferencingObject(contentVersion.getValueObject()); 1159 referenceVersionBean.getRegistryVOList().add(registryVO); 1160 } 1161 catch(Exception e) 1162 { 1163 add = false; 1164 logger.info("content:" + registryVO.getReferencingEntityId() + " did not exist - skipping.."); 1165 } 1166 } 1167 else 1168 { 1169 try 1170 { 1171 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer (registryVO.getReferencingEntityId()), db); 1172 logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId()); 1173 logger.info("siteNode:" + siteNodeVersion.getOwningSiteNode().getId()); 1174 existingReferenceBean.setName(siteNodeVersion.getOwningSiteNode().getName()); 1175 existingReferenceBean.setReferencingCompletingObject(siteNodeVersion.getOwningSiteNode().getValueObject()); 1176 1177 referenceVersionBean.setReferencingObject(siteNodeVersion.getValueObject()); 1178 referenceVersionBean.getRegistryVOList().add(registryVO); 1179 } 1180 catch(Exception e) 1181 { 1182 add = false; 1183 logger.info("siteNode:" + registryVO.getReferencingEntityId() + " did not exist - skipping.."); 1184 } 1185 } 1186 1187 if(add) 1188 { 1189 boolean exists = false; 1190 ReferenceVersionBean existingReferenceVersionBean = null; 1191 Iterator versionsIterator = existingReferenceBean.getVersions().iterator(); 1192 while(versionsIterator.hasNext()) 1193 { 1194 existingReferenceVersionBean = (ReferenceVersionBean)versionsIterator.next(); 1195 if(existingReferenceVersionBean.getReferencingObject().equals(referenceVersionBean.getReferencingObject())) 1196 { 1197 exists = true; 1198 break; 1199 } 1200 } 1201 1202 if(!exists) 1203 existingReferenceBean.getVersions().add(referenceVersionBean); 1204 else 1205 existingReferenceVersionBean.getRegistryVOList().add(registryVO); 1206 } 1207 } 1208 1209 Iterator i = referenceBeanList.iterator(); 1210 while(i.hasNext()) 1211 { 1212 ReferenceBean referenceBean = (ReferenceBean)i.next(); 1213 if(referenceBean.getVersions().size() == 0) 1214 i.remove(); 1215 } 1216 1217 return referenceBeanList; 1218 } 1219 1220 1223 1224 public List getMatchingRegistryVOList(String entityName, String entityId, Database db) throws SystemException, Exception 1225 { 1226 List matchingRegistryVOList = new ArrayList (); 1227 1228 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.entityName = $1 AND r.entityId = $2 ORDER BY r.registryId"); 1229 oql.bind(entityName); 1230 oql.bind(entityId); 1231 1232 QueryResults results = oql.execute(Database.ReadOnly); 1233 1234 while (results.hasMore()) 1235 { 1236 Registry registry = (Registry)results.next(); 1237 RegistryVO registryVO = registry.getValueObject(); 1238 1239 matchingRegistryVOList.add(registryVO); 1240 } 1241 1242 results.close(); 1243 oql.close(); 1244 1245 return matchingRegistryVOList; 1246 } 1247 1248 1249 public List getReferencedObjects(String referencingEntityName, String referencingEntityId) throws SystemException, Exception 1250 { 1251 List result = new ArrayList (); 1252 1253 Database db = CastorDatabaseService.getDatabase(); 1254 1255 try 1256 { 1257 beginTransaction(db); 1258 1259 List registryVOList = getMatchingRegistryVOListForReferencingEntity(referencingEntityName, referencingEntityId, db); 1260 1261 Iterator i = registryVOList.iterator(); 1262 while(i.hasNext()) 1263 { 1264 RegistryVO registryVO = (RegistryVO)i.next(); 1265 if(registryVO.getEntityName().indexOf("Content") > -1) 1266 { 1267 try 1268 { 1269 Content content = ContentController.getContentController().getContentWithId(new Integer (registryVO.getEntityId()), db); 1270 logger.info("contentVersion:" + content.getContentId()); 1271 result.add(content.getValueObject()); 1272 } 1273 catch(Exception e) 1274 { 1275 logger.info("content:" + registryVO.getEntityId() + " did not exist - skipping.."); 1276 } 1277 } 1278 else if(registryVO.getEntityName().indexOf("SiteNode") > -1) 1279 { 1280 try 1281 { 1282 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (registryVO.getEntityId()), db); 1283 logger.info("siteNode:" + siteNode.getId()); 1284 result.add(siteNode.getValueObject()); 1285 } 1286 catch(Exception e) 1287 { 1288 logger.info("siteNode:" + registryVO.getEntityId() + " did not exist - skipping.."); 1289 } 1290 } 1291 } 1292 1293 commitTransaction(db); 1294 } 1295 catch (Exception e) 1296 { 1297 rollbackTransaction(db); 1298 throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e); 1299 } 1300 1301 return result; 1302 } 1303 1304 public List getReferencedObjects(String referencingEntityName, String referencingEntityId, Database db) throws SystemException, Exception 1305 { 1306 List result = new ArrayList (); 1307 1308 List registryVOList = getMatchingRegistryVOListForReferencingEntity(referencingEntityName, referencingEntityId, db); 1309 1310 Iterator i = registryVOList.iterator(); 1311 while(i.hasNext()) 1312 { 1313 RegistryVO registryVO = (RegistryVO)i.next(); 1314 if(registryVO.getEntityName().indexOf("Content") > -1) 1315 { 1316 try 1317 { 1318 Content content = ContentController.getContentController().getContentWithId(new Integer (registryVO.getEntityId()), db); 1319 logger.info("contentVersion:" + content.getContentId()); 1320 result.add(content.getValueObject()); 1321 } 1322 catch(Exception e) 1323 { 1324 logger.info("content:" + registryVO.getEntityId() + " did not exist - skipping.."); 1325 } 1326 } 1327 else if(registryVO.getEntityName().indexOf("SiteNode") > -1) 1328 { 1329 try 1330 { 1331 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (registryVO.getEntityId()), db); 1332 logger.info("siteNode:" + siteNode.getId()); 1333 result.add(siteNode.getValueObject()); 1334 } 1335 catch(Exception e) 1336 { 1337 logger.info("siteNode:" + registryVO.getEntityId() + " did not exist - skipping.."); 1338 } 1339 } 1340 } 1341 1342 return result; 1343 } 1344 1345 1348 1349 public List getMatchingRegistryVOListForReferencingEntity(String referencingEntityName, String referencingEntityId, Database db) throws SystemException, Exception 1350 { 1351 List matchingRegistryVOList = new ArrayList (); 1352 1353 logger.info("referencingEntityName:" + referencingEntityName); 1354 logger.info("referencingEntityId:" + referencingEntityId); 1355 1356 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId"); 1357 oql.bind(referencingEntityName); 1358 oql.bind(referencingEntityId); 1359 1360 QueryResults results = oql.execute(Database.ReadOnly); 1361 1362 while (results.hasMore()) 1363 { 1364 Registry registry = (Registry)results.next(); 1365 RegistryVO registryVO = registry.getValueObject(); 1366 logger.info("found match:" + registryVO.getEntityName() + ":" + registryVO.getEntityId()); 1367 1368 matchingRegistryVOList.add(registryVO); 1369 } 1370 1371 results.close(); 1372 oql.close(); 1373 1374 return matchingRegistryVOList; 1375 } 1376 1377 1380 1381 public List clearRegistryVOList(String referencingEntityName, String referencingEntityId, Database db) throws SystemException, Exception 1382 { 1383 List matchingRegistryVOList = new ArrayList (); 1384 1385 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId"); 1386 oql.bind(referencingEntityName); 1387 oql.bind(referencingEntityId); 1388 1389 QueryResults results = oql.execute(); 1390 1391 while (results.hasMore()) 1392 { 1393 Registry registry = (Registry)results.next(); 1394 db.remove(registry); 1395 } 1396 1397 results.close(); 1398 oql.close(); 1399 1400 return matchingRegistryVOList; 1401 } 1402 1403 1404 1407 1408 public void clearRegistryForReferencedEntity(String entityName, String entityId) throws SystemException, Exception 1409 { 1410 Database db = CastorDatabaseService.getDatabase(); 1411 1412 try 1413 { 1414 beginTransaction(db); 1415 1416 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.entityName = $1 AND r.entityId = $2 ORDER BY r.registryId"); 1417 oql.bind(entityName); 1418 oql.bind(entityId); 1419 1420 QueryResults results = oql.execute(); 1421 1422 while (results.hasMore()) 1423 { 1424 Registry registry = (Registry)results.next(); 1425 db.remove(registry); 1426 } 1427 1428 results.close(); 1429 oql.close(); 1430 1431 commitTransaction(db); 1432 } 1433 catch (Exception e) 1434 { 1435 logger.warn("An error occurred so we should not complete the transaction:" + e); 1436 rollbackTransaction(db); 1437 } 1438 } 1439 1440 1443 1444 public void clearRegistryForReferencingEntityCompletingName(String entityCompletingName, String entityCompletingId) throws SystemException, Exception 1445 { 1446 Database db = CastorDatabaseService.getDatabase(); 1447 1448 try 1449 { 1450 beginTransaction(db); 1451 1452 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityCompletingName = $1 AND r.referencingEntityCompletingId = $2 ORDER BY r.registryId"); 1453 oql.bind(entityCompletingName); 1454 oql.bind(entityCompletingId); 1455 1456 QueryResults results = oql.execute(); 1457 1458 while (results.hasMore()) 1459 { 1460 Registry registry = (Registry)results.next(); 1461 db.remove(registry); 1462 } 1463 1464 results.close(); 1465 oql.close(); 1466 1467 commitTransaction(db); 1468 } 1469 catch (Exception e) 1470 { 1471 logger.warn("An error occurred so we should not complete the transaction:" + e); 1472 rollbackTransaction(db); 1473 } 1474 } 1475 1476 1479 1480 public void clearRegistryForReferencingEntityName(String entityName, String entityId) throws SystemException, Exception 1481 { 1482 Database db = CastorDatabaseService.getDatabase(); 1483 1484 try 1485 { 1486 beginTransaction(db); 1487 1488 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId"); 1489 oql.bind(entityName); 1490 oql.bind(entityId); 1491 1492 QueryResults results = oql.execute(); 1493 1494 while (results.hasMore()) 1495 { 1496 Registry registry = (Registry)results.next(); 1497 db.remove(registry); 1498 } 1499 1500 results.close(); 1501 oql.close(); 1502 1503 commitTransaction(db); 1504 } 1505 catch (Exception e) 1506 { 1507 logger.warn("An error occurred so we should not complete the transaction:" + e); 1508 rollbackTransaction(db); 1509 } 1510 } 1511 1512 1515 1538 1539 1542 1543 public List getSiteNodeVersionsWhichUsesContentVersionAsMetaInfo(ContentVersion contentVersion, Database db) throws SystemException, Exception 1544 { 1545 List siteNodeVersions = new ArrayList (); 1546 1547 OQLQuery oql = db.getOQLQuery("SELECT snv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl snv WHERE snv.serviceBindings.availableServiceBinding.name = $1 AND snv.serviceBindings.bindingQualifyers.name = $2 AND snv.serviceBindings.bindingQualifyers.value = $3"); 1548 oql.bind("Meta information"); 1549 oql.bind("contentId"); 1550 oql.bind(contentVersion.getOwningContent().getId()); 1551 1552 QueryResults results = oql.execute(); 1553 this.logger.info("Fetching entity in read/write mode"); 1554 1555 while (results.hasMore()) 1556 { 1557 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 1558 siteNodeVersions.add(siteNodeVersion); 1559 } 1561 1562 results.close(); 1563 oql.close(); 1564 1565 return siteNodeVersions; 1566 } 1567 1568 1571 1572 public SiteNodeVersion getLatestActiveSiteNodeVersionWhichUsesContentVersionAsMetaInfo(ContentVersion contentVersion, Database db) throws SystemException, Exception 1573 { 1574 SiteNodeVersion siteNodeVersion = null; 1575 1576 OQLQuery oql = db.getOQLQuery("SELECT snv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl snv WHERE snv.serviceBindings.availableServiceBinding.name = $1 AND snv.serviceBindings.bindingQualifyers.name = $2 AND snv.serviceBindings.bindingQualifyers.value = $3 AND snv.isActive = $4 ORDER BY snv.siteNodeVersionId desc"); 1577 oql.bind("Meta information"); 1578 oql.bind("contentId"); 1579 oql.bind(contentVersion.getOwningContent().getId()); 1580 oql.bind(new Boolean (true)); 1581 1582 QueryResults results = oql.execute(); 1583 this.logger.info("Fetching entity in read/write mode"); 1584 1585 if (results.hasMore()) 1586 { 1587 siteNodeVersion = (SiteNodeVersion)results.next(); 1588 } 1589 1590 results.close(); 1591 oql.close(); 1592 1593 return siteNodeVersion; 1594 } 1595 1596} 1597 | Popular Tags |