1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.apache.log4j.Logger; 34 import org.exolab.castor.jdo.Database; 35 import org.exolab.castor.jdo.OQLQuery; 36 import org.exolab.castor.jdo.QueryResults; 37 import org.infoglue.cms.applications.contenttool.wizards.actions.CreateContentWizardInfoBean; 38 import org.infoglue.cms.entities.content.Content; 39 import org.infoglue.cms.entities.content.ContentVO; 40 import org.infoglue.cms.entities.content.ContentVersion; 41 import org.infoglue.cms.entities.content.ContentVersionVO; 42 import org.infoglue.cms.entities.content.impl.simple.ContentImpl; 43 import org.infoglue.cms.entities.content.impl.simple.MediumContentImpl; 44 import org.infoglue.cms.entities.content.impl.simple.SmallContentImpl; 45 import org.infoglue.cms.entities.kernel.BaseEntityVO; 46 import org.infoglue.cms.entities.management.ContentTypeDefinition; 47 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO; 48 import org.infoglue.cms.entities.management.Repository; 49 import org.infoglue.cms.entities.management.RepositoryLanguage; 50 import org.infoglue.cms.entities.management.RepositoryVO; 51 import org.infoglue.cms.entities.management.ServiceDefinition; 52 import org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl; 53 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl; 54 import org.infoglue.cms.entities.structure.Qualifyer; 55 import org.infoglue.cms.entities.structure.ServiceBinding; 56 import org.infoglue.cms.exception.Bug; 57 import org.infoglue.cms.exception.ConstraintException; 58 import org.infoglue.cms.exception.SystemException; 59 import org.infoglue.cms.security.InfoGluePrincipal; 60 import org.infoglue.cms.services.BaseService; 61 import org.infoglue.cms.util.ConstraintExceptionBuffer; 62 import org.infoglue.deliver.util.CacheController; 63 64 import com.opensymphony.module.propertyset.PropertySet; 65 import com.opensymphony.module.propertyset.PropertySetManager; 66 67 70 71 public class ContentController extends BaseController 72 { 73 private final static Logger logger = Logger.getLogger(ContentController.class.getName()); 74 75 78 79 public static ContentController getContentController() 80 { 81 return new ContentController(); 82 } 83 84 public ContentVO getContentVOWithId(Integer contentId) throws SystemException, Bug 85 { 86 return (ContentVO) getVOWithId(SmallContentImpl.class, contentId); 87 } 88 89 public ContentVO getContentVOWithId(Integer contentId, Database db) throws SystemException, Bug 90 { 91 return (ContentVO) getVOWithId(SmallContentImpl.class, contentId, db); 92 } 93 94 public ContentVO getSmallContentVOWithId(Integer contentId, Database db) throws SystemException, Bug 95 { 96 return (ContentVO) getVOWithId(SmallContentImpl.class, contentId, db); 97 } 98 99 public Content getContentWithId(Integer contentId, Database db) throws SystemException, Bug 100 { 101 return (Content) getObjectWithId(ContentImpl.class, contentId, db); 102 } 103 104 public Content getReadOnlyContentWithId(Integer contentId, Database db) throws SystemException, Bug 105 { 106 return (Content) getObjectWithIdAsReadOnly(ContentImpl.class, contentId, db); 107 } 108 109 110 111 public List getContentVOList() throws SystemException, Bug 112 { 113 return getAllVOObjects(ContentImpl.class, "contentId"); 114 } 115 116 119 120 public ContentVO create(CreateContentWizardInfoBean createContentWizardInfoBean) throws ConstraintException, SystemException 121 { 122 Database db = CastorDatabaseService.getDatabase(); 123 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 124 125 Content content = null; 126 127 beginTransaction(db); 128 129 try 130 { 131 content = create(db, createContentWizardInfoBean.getParentContentId(), createContentWizardInfoBean.getContentTypeDefinitionId(), createContentWizardInfoBean.getRepositoryId(), createContentWizardInfoBean.getContent().getValueObject()); 132 133 Iterator it = createContentWizardInfoBean.getContentVersions().keySet().iterator(); 134 while (it.hasNext()) 135 { 136 Integer languageId = (Integer )it.next(); 137 logger.info("languageId:" + languageId); 138 ContentVersionVO contentVersionVO = (ContentVersionVO)createContentWizardInfoBean.getContentVersions().get(languageId); 139 ContentVersionController.getContentVersionController().create(content.getContentId(), languageId, contentVersionVO, null, db); 140 } 141 142 144 ceb.throwIfNotEmpty(); 145 146 commitTransaction(db); 147 } 148 catch(ConstraintException ce) 149 { 150 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 151 rollbackTransaction(db); 152 throw ce; 153 } 154 catch(Exception e) 155 { 156 logger.error("An error occurred so we should not complete the transaction:" + e, e); 157 rollbackTransaction(db); 158 throw new SystemException(e.getMessage()); 159 } 160 161 return content.getValueObject(); 162 } 163 164 168 169 public ContentVO create(Integer parentContentId, Integer contentTypeDefinitionId, Integer repositoryId, ContentVO contentVO) throws ConstraintException, SystemException 170 { 171 Database db = CastorDatabaseService.getDatabase(); 172 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 173 174 Content content = null; 175 176 beginTransaction(db); 177 178 try 179 { 180 content = create(db, parentContentId, contentTypeDefinitionId, repositoryId, contentVO); 181 ceb.throwIfNotEmpty(); 182 183 commitTransaction(db); 184 } 185 catch(ConstraintException ce) 186 { 187 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 188 rollbackTransaction(db); 189 throw ce; 190 } 191 catch(Exception e) 192 { 193 logger.error("An error occurred so we should not complete the transaction:" + e, e); 194 rollbackTransaction(db); 195 throw new SystemException(e.getMessage()); 196 } 197 198 return content.getValueObject(); 199 } 200 201 205 206 public Content create(Database db, Integer parentContentId, Integer contentTypeDefinitionId, Integer repositoryId, ContentVO contentVO) throws ConstraintException, SystemException, Exception 207 { 208 Content content = null; 209 210 try 211 { 212 Content parentContent = null; 213 ContentTypeDefinition contentTypeDefinition = null; 214 215 if(parentContentId != null) 216 { 217 parentContent = getContentWithId(parentContentId, db); 218 219 if(repositoryId == null) 220 repositoryId = parentContent.getRepository().getRepositoryId(); 221 } 222 223 if(contentTypeDefinitionId != null) 224 contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db); 225 226 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 227 228 content = new ContentImpl(); 229 content.setValueObject(contentVO); 230 content.setParentContent((ContentImpl)parentContent); 231 content.setRepository((RepositoryImpl)repository); 232 content.setContentTypeDefinition((ContentTypeDefinitionImpl)contentTypeDefinition); 233 234 db.create(content); 235 236 if(parentContent != null) 238 { 239 parentContent.getChildren().add(content); 240 parentContent.setIsBranch(new Boolean (true)); 241 } 242 243 } 245 catch(Exception e) 246 { 247 logger.error("An error occurred so we should not complete the transaction:" + e, e); 248 e.printStackTrace(); 249 throw new SystemException(e.getMessage()); 251 } 252 253 return content; 254 } 255 256 257 260 261 public void delete(ContentVO contentVO) throws ConstraintException, SystemException 262 { 263 Database db = CastorDatabaseService.getDatabase(); 264 beginTransaction(db); 265 try 266 { 267 delete(contentVO, db, false, false, false); 268 269 commitTransaction(db); 270 271 } 272 catch(ConstraintException ce) 273 { 274 logger.error("An error occurred so we should not complete the transaction:" + ce, ce); 275 rollbackTransaction(db); 276 throw ce; 277 } 278 catch(Exception e) 279 { 280 logger.error("An error occurred so we should not complete the transaction:" + e, e); 281 rollbackTransaction(db); 282 throw new SystemException(e.getMessage()); 283 } 284 285 } 286 287 288 291 292 public void delete(ContentVO contentVO, Database db) throws ConstraintException, SystemException, Exception 293 { 294 delete(contentVO, db, false, false, false); 295 } 296 297 300 301 public void delete(ContentVO contentVO, Database db, boolean skipRelationCheck, boolean skipServiceBindings, boolean forceDelete) throws ConstraintException, SystemException, Exception 302 { 303 Content content = getContentWithId(contentVO.getContentId(), db); 304 Content parent = content.getParentContent(); 305 if(parent != null) 306 { 307 Iterator childContentIterator = parent.getChildren().iterator(); 308 while(childContentIterator.hasNext()) 309 { 310 Content candidate = (Content)childContentIterator.next(); 311 if(candidate.getId().equals(contentVO.getContentId())) 312 { 313 deleteRecursive(content, childContentIterator, db, skipRelationCheck, skipServiceBindings, forceDelete); 314 } 315 } 316 } 317 else 318 { 319 deleteRecursive(content, null, db, skipRelationCheck, skipServiceBindings, forceDelete); 320 } 321 } 322 323 326 327 private static void deleteRecursive(Content content, Iterator parentIterator, Database db, boolean skipRelationCheck, boolean skipServiceBindings, boolean forceDelete) throws ConstraintException, SystemException, Exception 328 { 329 if(!skipRelationCheck) 330 { 331 List referenceBeanList = RegistryController.getController().getReferencingObjectsForContent(content.getId(), db); 332 if(referenceBeanList != null && referenceBeanList.size() > 0) 333 throw new ConstraintException("ContentVersion.stateId", "3305"); 334 } 335 336 Collection children = content.getChildren(); 337 Iterator childrenIterator = children.iterator(); 338 while(childrenIterator.hasNext()) 339 { 340 Content childContent = (Content)childrenIterator.next(); 341 deleteRecursive(childContent, childrenIterator, db, skipRelationCheck, skipServiceBindings, forceDelete); 342 } 343 content.setChildren(new ArrayList ()); 344 345 if(forceDelete || getIsDeletable(content)) 346 { 347 ContentVersionController.getContentVersionController().deleteVersionsForContent(content, db, forceDelete); 348 349 if(!skipServiceBindings) 350 ServiceBindingController.deleteServiceBindingsReferencingContent(content, db); 351 352 if(parentIterator != null) 353 parentIterator.remove(); 354 355 db.remove(content); 356 357 Map args = new HashMap (); 358 args.put("globalKey", "infoglue"); 359 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 360 361 ps.remove( "content_" + content.getContentId() + "_allowedContentTypeNames"); 362 ps.remove( "content_" + content.getContentId() + "_defaultContentTypeName"); 363 ps.remove( "content_" + content.getContentId() + "_initialLanguageId"); 364 365 } 366 else 367 { 368 throw new ConstraintException("ContentVersion.stateId", "3300"); 369 } 370 } 371 372 373 377 378 private static boolean getIsDeletable(Content content) 379 { 380 boolean isDeletable = true; 381 382 Collection contentVersions = content.getContentVersions(); 383 Iterator versionIterator = contentVersions.iterator(); 384 while (versionIterator.hasNext()) 385 { 386 ContentVersion contentVersion = (ContentVersion)versionIterator.next(); 387 if(contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue() && contentVersion.getIsActive().booleanValue() == true) 388 { 389 logger.info("The content had a published version so we cannot delete it.."); 390 isDeletable = false; 391 break; 392 } 393 } 394 395 return isDeletable; 396 } 397 398 399 public ContentVO update(ContentVO contentVO) throws ConstraintException, SystemException 400 { 401 return update(contentVO, null); 402 } 403 404 405 public ContentVO update(ContentVO contentVO, Integer contentTypeDefinitionId) throws ConstraintException, SystemException 406 { 407 Database db = CastorDatabaseService.getDatabase(); 408 409 Content content = null; 410 411 beginTransaction(db); 412 413 try 414 { 415 content = (Content)getObjectWithId(ContentImpl.class, contentVO.getId(), db); 416 content.setVO(contentVO); 417 418 if(contentTypeDefinitionId != null) 419 { 420 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db); 421 content.setContentTypeDefinition((ContentTypeDefinitionImpl)contentTypeDefinition); 422 } 423 424 commitTransaction(db); 425 } 426 catch(Exception e) 427 { 428 logger.error("An error occurred so we should not complete the transaction:" + e, e); 429 rollbackTransaction(db); 430 throw new SystemException(e.getMessage()); 431 } 432 433 return content.getValueObject(); 434 } 435 436 public List getAvailableLanguagesForContentWithId(Integer contentId, Database db) throws ConstraintException, SystemException, Exception 437 { 438 List availableLanguageVOList = new ArrayList (); 439 440 Content content = getContentWithId(contentId, db); 441 if(content != null) 442 { 443 Repository repository = content.getRepository(); 444 if(repository != null) 445 { 446 List availableRepositoryLanguageList = RepositoryLanguageController.getController().getRepositoryLanguageListWithRepositoryId(repository.getId(), db); 447 Iterator i = availableRepositoryLanguageList.iterator(); 448 while(i.hasNext()) 449 { 450 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)i.next(); 451 availableLanguageVOList.add(repositoryLanguage.getLanguage().getValueObject()); 452 } 453 } 454 } 455 456 return availableLanguageVOList; 457 } 458 494 495 498 499 public static ContentVO getParentContent(Integer contentId) throws SystemException, Bug 500 { 501 logger.info("Coming in with:" + contentId); 502 Database db = CastorDatabaseService.getDatabase(); 503 ContentVO parentContentVO = null; 504 505 beginTransaction(db); 506 507 try 508 { 509 Content content = (Content) getObjectWithId(ContentImpl.class, contentId, db); 510 logger.info("CONTENT:" + content.getName()); 511 Content parent = content.getParentContent(); 512 if(parent != null) 513 parentContentVO = parent.getValueObject(); 514 515 commitTransaction(db); 516 } 517 catch(Exception e) 518 { 519 logger.error("An error occurred so we should not complete the transaction:" + e, e); 520 rollbackTransaction(db); 521 throw new SystemException(e.getMessage()); 522 } 523 return parentContentVO; 524 } 525 526 527 public static void addChildContent(ContentVO parentVO, ContentVO childVO) 528 throws ConstraintException, SystemException 529 { 530 531 Database db = CastorDatabaseService.getDatabase(); 532 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 533 534 beginTransaction(db); 535 536 try 537 { 538 Content parent = (Content) getObjectWithId(ContentImpl.class, parentVO.getContentId(), db); 539 Content child = (Content) getObjectWithId(ContentImpl.class, childVO.getContentId(), db); 540 parent.getChildren().add(child); 541 542 ceb.throwIfNotEmpty(); 543 commitTransaction(db); 544 } 545 catch(ConstraintException ce) 546 { 547 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 548 rollbackTransaction(db); 549 throw ce; 550 } 551 catch(Exception e) 552 { 553 logger.error("An error occurred so we should not complete the transaction:" + e, e); 554 rollbackTransaction(db); 555 throw new SystemException(e.getMessage()); 556 } 557 558 } 559 560 public static void removeChildContent(ContentVO parentVO, ContentVO childVO) 561 throws ConstraintException, SystemException 562 { 563 564 Database db = CastorDatabaseService.getDatabase(); 565 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 566 567 beginTransaction(db); 568 569 try 570 { 571 Content parent = (Content) getObjectWithId(ContentImpl.class, parentVO.getContentId(), db); 572 Content child = (Content) getObjectWithId(ContentImpl.class, childVO.getContentId(), db); 573 parent.getChildren().remove(child); 574 575 ceb.throwIfNotEmpty(); 576 commitTransaction(db); 577 } 578 catch(ConstraintException ce) 579 { 580 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 581 rollbackTransaction(db); 582 throw ce; 583 } 584 catch(Exception e) 585 { 586 logger.error("An error occurred so we should not complete the transaction:" + e, e); 587 rollbackTransaction(db); 588 throw new SystemException(e.getMessage()); 589 } 590 591 } 592 593 594 595 600 601 public void moveContent(ContentVO contentVO, Integer newParentContentId) throws ConstraintException, SystemException 602 { 603 Database db = CastorDatabaseService.getDatabase(); 604 605 beginTransaction(db); 606 607 try 608 { 609 moveContent(contentVO, newParentContentId, db); 610 611 commitTransaction(db); 612 } 613 catch(Exception e) 614 { 615 logger.error("An error occurred so we should not complete the transaction:" + e, e); 616 rollbackTransaction(db); 617 throw new SystemException(e.getMessage()); 618 } 619 620 } 621 622 627 628 public void moveContent(ContentVO contentVO, Integer newParentContentId, Database db) throws ConstraintException, SystemException 629 { 630 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 631 632 Content content = null; 633 Content newParentContent = null; 634 Content oldParentContent = null; 635 636 contentVO.validate(); 638 639 if(newParentContentId == null) 640 { 641 logger.warn("You must specify the new parent-content......"); 642 throw new ConstraintException("Content.parentContentId", "3303"); 643 } 644 645 if(contentVO.getId().intValue() == newParentContentId.intValue()) 646 { 647 logger.warn("You cannot have the content as it's own parent......"); 648 throw new ConstraintException("Content.parentContentId", "3301"); 649 } 650 651 content = getContentWithId(contentVO.getContentId(), db); 652 oldParentContent = content.getParentContent(); 653 newParentContent = getContentWithId(newParentContentId, db); 654 655 if(oldParentContent.getId().intValue() == newParentContentId.intValue()) 656 { 657 logger.warn("You cannot specify the same folder as it originally was located in......"); 658 throw new ConstraintException("Content.parentContentId", "3304"); 659 } 660 661 Content tempContent = newParentContent.getParentContent(); 662 while(tempContent != null) 663 { 664 if(tempContent.getId().intValue() == content.getId().intValue()) 665 { 666 logger.warn("You cannot move the content to a child under it......"); 667 throw new ConstraintException("Content.parentContentId", "3302"); 668 } 669 tempContent = tempContent.getParentContent(); 670 } 671 672 oldParentContent.getChildren().remove(content); 673 content.setParentContent((ContentImpl)newParentContent); 674 675 changeRepositoryRecursive(content, newParentContent.getRepository()); 676 newParentContent.getChildren().add(content); 678 679 ceb.throwIfNotEmpty(); 681 } 682 683 688 689 private void changeRepositoryRecursive(Content content, Repository newRepository) 690 { 691 if(content.getRepository().getId().intValue() != newRepository.getId().intValue()) 692 { 693 content.setRepository((RepositoryImpl)newRepository); 694 Iterator childContentsIterator = content.getChildren().iterator(); 695 while(childContentsIterator.hasNext()) 696 { 697 Content childContent = (Content)childContentsIterator.next(); 698 changeRepositoryRecursive(childContent, newRepository); 699 } 700 } 701 } 702 703 706 707 public List getContentVOWithContentTypeDefinition(String contentTypeDefinitionName) throws SystemException 708 { 709 Database db = CastorDatabaseService.getDatabase(); 710 beginTransaction(db); 711 try 712 { 713 List result = getContentVOWithContentTypeDefinition(contentTypeDefinitionName, db); 714 commitTransaction(db); 715 return result; 716 } 717 catch(Exception e) 718 { 719 logger.error("An error occurred so we should not complete the transaction:" + e, e); 720 rollbackTransaction(db); 721 throw new SystemException(e.getMessage()); 722 } 723 } 724 725 728 public List getContentVOWithContentTypeDefinition(String contentTypeDefinitionName, Database db) throws SystemException 729 { 730 HashMap arguments = new HashMap (); 731 arguments.put("method", "selectListOnContentTypeName"); 732 733 List argumentList = new ArrayList (); 734 HashMap argument = new HashMap (); 735 argument.put("contentTypeDefinitionName", contentTypeDefinitionName); 736 argumentList.add(argument); 737 arguments.put("arguments", argumentList); 738 try 739 { 740 return getContentVOList(arguments, db); 741 } 742 catch(SystemException e) 743 { 744 throw e; 745 } 746 catch(Exception e) 747 { 748 throw new SystemException(e.getMessage()); 749 } 750 } 751 752 756 757 public List getContentVOList(HashMap argumentHashMap) throws SystemException, Bug 758 { 759 List contents = null; 760 761 String method = (String )argumentHashMap.get("method"); 762 logger.info("method:" + method); 763 764 if(method.equalsIgnoreCase("selectContentListOnIdList")) 765 { 766 contents = new ArrayList (); 767 List arguments = (List )argumentHashMap.get("arguments"); 768 logger.info("Arguments:" + arguments.size()); 769 Iterator argumentIterator = arguments.iterator(); 770 while(argumentIterator.hasNext()) 771 { 772 HashMap argument = (HashMap )argumentIterator.next(); 773 Integer contentId = new Integer ((String )argument.get("contentId")); 774 logger.info("Getting the content with Id:" + contentId); 775 contents.add(getContentVOWithId(contentId)); 776 } 777 } 778 else if(method.equalsIgnoreCase("selectListOnContentTypeName")) 779 { 780 List arguments = (List )argumentHashMap.get("arguments"); 781 logger.info("Arguments:" + arguments.size()); 782 contents = getContentVOListByContentTypeNames(arguments); 783 } 784 return contents; 785 } 786 787 791 792 public List getContentVOList(HashMap argumentHashMap, Database db) throws SystemException, Exception 793 { 794 List contents = null; 795 796 String method = (String )argumentHashMap.get("method"); 797 logger.info("method:" + method); 798 799 if(method.equalsIgnoreCase("selectContentListOnIdList")) 800 { 801 contents = new ArrayList (); 802 List arguments = (List )argumentHashMap.get("arguments"); 803 logger.info("Arguments:" + arguments.size()); 804 Iterator argumentIterator = arguments.iterator(); 805 while(argumentIterator.hasNext()) 806 { 807 HashMap argument = (HashMap )argumentIterator.next(); 808 Integer contentId = new Integer ((String )argument.get("contentId")); 809 logger.info("Getting the content with Id:" + contentId); 810 contents.add(getSmallContentVOWithId(contentId, db)); 811 } 812 } 813 else if(method.equalsIgnoreCase("selectListOnContentTypeName")) 814 { 815 List arguments = (List )argumentHashMap.get("arguments"); 816 logger.info("Arguments:" + arguments.size()); 817 contents = getContentVOListByContentTypeNames(arguments, db); 818 } 819 return contents; 820 } 821 822 823 826 827 protected List getContentVOListByContentTypeNames(List arguments) throws SystemException, Bug 828 { 829 Database db = CastorDatabaseService.getDatabase(); 830 831 List contents = new ArrayList (); 832 833 beginTransaction(db); 834 835 try 836 { 837 Iterator i = arguments.iterator(); 838 while(i.hasNext()) 839 { 840 HashMap argument = (HashMap )i.next(); 841 String contentTypeDefinitionName = (String )argument.get("contentTypeDefinitionName"); 842 843 OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.contentTypeDefinition.name = $1 ORDER BY c.contentId"); 846 oql.bind(contentTypeDefinitionName); 847 848 QueryResults results = oql.execute(Database.ReadOnly); 849 850 while(results.hasMore()) 851 { 852 MediumContentImpl content = (MediumContentImpl)results.next(); 853 contents.add(content.getValueObject()); 854 } 855 856 results.close(); 857 oql.close(); 858 } 859 860 commitTransaction(db); 861 } 862 catch(Exception e) 863 { 864 logger.error("An error occurred so we should not complete the transaction:" + e, e); 865 rollbackTransaction(db); 866 throw new SystemException(e.getMessage()); 867 } 868 869 return contents; 870 } 871 872 873 876 877 protected List getContentVOListByContentTypeNames(List arguments, Database db) throws SystemException, Exception 878 { 879 List contents = new ArrayList (); 880 881 Iterator i = arguments.iterator(); 882 while(i.hasNext()) 883 { 884 HashMap argument = (HashMap )i.next(); 885 String contentTypeDefinitionName = (String )argument.get("contentTypeDefinitionName"); 886 OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.contentTypeDefinition.name = $1"); 889 oql.bind(contentTypeDefinitionName); 890 891 QueryResults results = oql.execute(Database.ReadOnly); 892 893 while(results.hasMore()) 894 { 895 MediumContentImpl content = (MediumContentImpl)results.next(); 896 contents.add(content.getValueObject()); 897 } 898 899 results.close(); 900 oql.close(); 901 } 902 903 return contents; 904 } 905 906 909 952 953 957 958 public ContentVO getRootContentVO(Integer repositoryId, String userName) throws ConstraintException, SystemException 959 { 960 Database db = CastorDatabaseService.getDatabase(); 961 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 962 963 Content content = null; 964 965 beginTransaction(db); 966 967 try 968 { 969 logger.info("Fetching the root content for the repository " + repositoryId); 970 OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.SmallContentImpl c WHERE is_undefined(c.parentContentId) AND c.repositoryId = $1"); 972 oql.bind(repositoryId); 973 974 QueryResults results = oql.execute(Database.ReadOnly); 975 if (results.hasMore()) 976 { 977 content = (Content)results.next(); 978 } 979 else 980 { 981 logger.info("Found no rootContent so we create a new...."); 983 ContentVO rootContentVO = new ContentVO(); 984 RepositoryVO repositoryVO = RepositoryController.getController().getRepositoryVOWithId(repositoryId); 985 rootContentVO.setCreatorName(userName); 986 rootContentVO.setName(repositoryVO.getName()); 987 rootContentVO.setIsBranch(new Boolean (true)); 988 content = create(db, null, null, repositoryId, rootContentVO); 989 } 990 991 results.close(); 992 oql.close(); 993 994 ceb.throwIfNotEmpty(); 996 commitTransaction(db); 997 998 } 999 catch(ConstraintException ce) 1000 { 1001 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 1002 rollbackTransaction(db); 1003 throw ce; 1004 } 1005 catch(Exception e) 1006 { 1007 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1008 rollbackTransaction(db); 1009 throw new SystemException(e.getMessage()); 1010 } 1011 1012 return (content == null) ? null : content.getValueObject(); 1013 } 1014 1015 1016 1017 1021 1022 public ContentVO getRootContentVO(Integer repositoryId, String userName, boolean createIfNonExisting) throws ConstraintException, SystemException 1023 { 1024 Database db = CastorDatabaseService.getDatabase(); 1025 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 1026 1027 Content content = null; 1028 1029 beginTransaction(db); 1030 1031 try 1032 { 1033 content = getRootContent(db, repositoryId, userName, createIfNonExisting); 1034 1035 ceb.throwIfNotEmpty(); 1037 commitTransaction(db); 1038 1039 } 1040 catch(ConstraintException ce) 1041 { 1042 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 1043 rollbackTransaction(db); 1044 throw ce; 1045 } 1046 catch(Exception e) 1047 { 1048 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1049 rollbackTransaction(db); 1050 throw new SystemException(e.getMessage()); 1051 } 1052 1053 return (content == null) ? null : content.getValueObject(); 1054 } 1055 1056 1057 1061 1062 public Content getRootContent(Database db, Integer repositoryId, String userName, boolean createIfNonExisting) throws ConstraintException, SystemException, Exception 1063 { 1064 Content content = null; 1065 1066 logger.info("Fetching the root content for the repository " + repositoryId); 1067 OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl c WHERE is_undefined(c.parentContent) AND c.repository.repositoryId = $1"); 1068 oql.bind(repositoryId); 1069 1070 QueryResults results = oql.execute(Database.ReadOnly); 1071 if (results.hasMore()) 1072 { 1073 content = (Content)results.next(); 1074 } 1075 else 1076 { 1077 if(createIfNonExisting) 1078 { 1079 logger.info("Found no rootContent so we create a new...."); 1081 ContentVO rootContentVO = new ContentVO(); 1082 RepositoryVO repositoryVO = RepositoryController.getController().getRepositoryVOWithId(repositoryId); 1083 rootContentVO.setCreatorName(userName); 1084 rootContentVO.setName(repositoryVO.getName()); 1085 rootContentVO.setIsBranch(new Boolean (true)); 1086 content = create(db, null, null, repositoryId, rootContentVO); 1087 } 1088 } 1089 1090 results.close(); 1091 oql.close(); 1092 1093 return content; 1094 } 1095 1096 1097 1101 1102 public Content getRootContent(Integer repositoryId, Database db) throws ConstraintException, SystemException, Exception 1103 { 1104 Content content = null; 1105 1106 OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl c WHERE is_undefined(c.parentContent) AND c.repository.repositoryId = $1"); 1107 oql.bind(repositoryId); 1108 1109 QueryResults results = oql.execute(); 1110 this.logger.info("Fetching entity in read/write mode" + repositoryId); 1111 1112 if (results.hasMore()) 1113 { 1114 content = (Content)results.next(); 1115 } 1116 1117 results.close(); 1118 oql.close(); 1119 1120 return content; 1121 } 1122 1123 1126 1127 public List getContentChildrenVOList(Integer parentContentId) throws ConstraintException, SystemException 1128 { 1129 String key = "" + parentContentId; 1130 logger.info("key:" + key); 1131 List cachedChildContentVOList = (List )CacheController.getCachedObject("childContentCache", key); 1132 if(cachedChildContentVOList != null) 1133 { 1134 logger.info("There was an cached childContentVOList:" + cachedChildContentVOList.size()); 1135 return cachedChildContentVOList; 1136 } 1137 1138 Database db = CastorDatabaseService.getDatabase(); 1139 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 1140 1141 List childrenVOList = null; 1142 1143 beginTransaction(db); 1144 1145 try 1146 { 1147 Content content = getContentWithId(parentContentId, db); 1148 Collection children = content.getChildren(); 1149 childrenVOList = ContentController.toVOList(children); 1150 1151 ceb.throwIfNotEmpty(); 1153 1154 commitTransaction(db); 1155 } 1156 catch(ConstraintException ce) 1157 { 1158 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 1159 rollbackTransaction(db); 1160 throw ce; 1161 } 1162 catch(Exception e) 1163 { 1164 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1165 rollbackTransaction(db); 1166 throw new SystemException(e.getMessage()); 1167 } 1168 1169 CacheController.cacheObject("childContentCache", key, childrenVOList); 1170 1171 return childrenVOList; 1172 } 1173 1174 1177 1178 public ContentTypeDefinitionVO getContentTypeDefinition(Integer contentId) throws ConstraintException, SystemException 1179 { 1180 Database db = CastorDatabaseService.getDatabase(); 1181 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 1182 1183 ContentTypeDefinitionVO contentTypeDefinitionVO = null; 1184 1185 beginTransaction(db); 1186 1187 try 1188 { 1189 Content content = getContentWithId(contentId, db); 1190 if(content != null && content.getContentTypeDefinition() != null) 1191 contentTypeDefinitionVO = content.getContentTypeDefinition().getValueObject(); 1192 1193 ceb.throwIfNotEmpty(); 1195 1196 commitTransaction(db); 1197 } 1198 catch(ConstraintException ce) 1199 { 1200 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 1201 rollbackTransaction(db); 1202 throw ce; 1203 } 1204 catch(Exception e) 1205 { 1206 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1207 rollbackTransaction(db); 1208 throw new SystemException(e.getMessage()); 1209 } 1210 1211 return contentTypeDefinitionVO; 1212 } 1213 1214 1217 1218 public List getRepositoryLanguages(Integer contentId) throws ConstraintException, SystemException 1219 { 1220 Database db = CastorDatabaseService.getDatabase(); 1221 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 1222 1223 List languages = null; 1224 1225 beginTransaction(db); 1226 1227 try 1228 { 1229 languages = getAvailableLanguagesForContentWithId(contentId, db); 1230 1231 ceb.throwIfNotEmpty(); 1233 1234 commitTransaction(db); 1235 } 1236 catch(ConstraintException ce) 1237 { 1238 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 1239 rollbackTransaction(db); 1240 throw ce; 1241 } 1242 catch(Exception e) 1243 { 1244 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1245 rollbackTransaction(db); 1246 throw new SystemException(e.getMessage()); 1247 } 1248 1249 return languages; 1250 } 1251 1252 1253 1256 1257 public static List getBoundContents(Integer serviceBindingId) throws SystemException, Exception 1258 { 1259 List result = new ArrayList (); 1260 1261 Database db = CastorDatabaseService.getDatabase(); 1262 1263 beginTransaction(db); 1264 1265 try 1266 { 1267 result = getBoundContents(db, serviceBindingId); 1268 1269 commitTransaction(db); 1270 } 1271 catch(Exception e) 1272 { 1273 logger.error("An error occurred so we should not complete the transaction:" + e, e); 1274 rollbackTransaction(db); 1275 throw new SystemException(e.getMessage()); 1276 } 1277 1278 return result; 1279 } 1280 1281 1282 1285 1286 public static List getBoundContents(Database db, Integer serviceBindingId) throws SystemException, Exception 1287 { 1288 List result = new ArrayList (); 1289 1290 ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db); 1291 1292 if(serviceBinding != null) 1293 { 1294 ServiceDefinition serviceDefinition = serviceBinding.getServiceDefinition(); 1295 if(serviceDefinition != null) 1296 { 1297 String serviceClassName = serviceDefinition.getClassName(); 1298 BaseService service = (BaseService)Class.forName(serviceClassName).newInstance(); 1299 1300 HashMap arguments = new HashMap (); 1301 arguments.put("method", "selectContentListOnIdList"); 1302 1303 List qualifyerList = new ArrayList (); 1304 Collection qualifyers = serviceBinding.getBindingQualifyers(); 1305 1306 qualifyers = sortQualifyers(qualifyers); 1307 1308 Iterator iterator = qualifyers.iterator(); 1309 while(iterator.hasNext()) 1310 { 1311 Qualifyer qualifyer = (Qualifyer)iterator.next(); 1312 HashMap argument = new HashMap (); 1313 argument.put(qualifyer.getName(), qualifyer.getValue()); 1314 qualifyerList.add(argument); 1315 } 1316 arguments.put("arguments", qualifyerList); 1317 1318 List contents = service.selectMatchingEntities(arguments); 1319 1320 if(contents != null) 1321 { 1322 Iterator i = contents.iterator(); 1323 while(i.hasNext()) 1324 { 1325 ContentVO candidate = (ContentVO)i.next(); 1326 result.add(candidate); 1327 } 1328 } 1329 } 1330 } 1331 1332 return result; 1333 } 1334 1335 1336 public static List getInTransactionBoundContents(Database db, Integer serviceBindingId) throws SystemException, Exception 1337 { 1338 List result = new ArrayList (); 1339 1340 ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db); 1341 1342 if(serviceBinding != null) 1343 { 1344 ServiceDefinition serviceDefinition = serviceBinding.getServiceDefinition(); 1345 if(serviceDefinition != null) 1346 { 1347 String serviceClassName = serviceDefinition.getClassName(); 1348 BaseService service = (BaseService)Class.forName(serviceClassName).newInstance(); 1349 1350 HashMap arguments = new HashMap (); 1351 arguments.put("method", "selectContentListOnIdList"); 1352 1353 List qualifyerList = new ArrayList (); 1354 Collection qualifyers = serviceBinding.getBindingQualifyers(); 1355 1356 qualifyers = sortQualifyers(qualifyers); 1357 1358 Iterator iterator = qualifyers.iterator(); 1359 while(iterator.hasNext()) 1360 { 1361 Qualifyer qualifyer = (Qualifyer)iterator.next(); 1362 HashMap argument = new HashMap (); 1363 argument.put(qualifyer.getName(), qualifyer.getValue()); 1364 qualifyerList.add(argument); 1365 } 1366 arguments.put("arguments", qualifyerList); 1367 1368 List contents = service.selectMatchingEntities(arguments, db); 1369 1370 if(contents != null) 1371 { 1372 Iterator i = contents.iterator(); 1373 while(i.hasNext()) 1374 { 1375 ContentVO candidate = (ContentVO)i.next(); 1376 result.add(candidate); 1377 } 1378 } 1379 } 1380 } 1381 1382 return result; 1383 } 1384 1385 1386 1389 1390 private static List sortQualifyers(Collection qualifyers) 1391 { 1392 List sortedQualifyers = new ArrayList (); 1393 1394 try 1395 { 1396 Iterator iterator = qualifyers.iterator(); 1397 while(iterator.hasNext()) 1398 { 1399 Qualifyer qualifyer = (Qualifyer)iterator.next(); 1400 int index = 0; 1401 Iterator sortedListIterator = sortedQualifyers.iterator(); 1402 while(sortedListIterator.hasNext()) 1403 { 1404 Qualifyer sortedQualifyer = (Qualifyer)sortedListIterator.next(); 1405 if(sortedQualifyer.getSortOrder().intValue() > qualifyer.getSortOrder().intValue()) 1406 { 1407 break; 1408 } 1409 index++; 1410 } 1411 sortedQualifyers.add(index, qualifyer); 1412 1413 } 1414 } 1415 catch(Exception e) 1416 { 1417 logger.warn("The sorting of qualifyers failed:" + e.getMessage(), e); 1418 } 1419 1420 return sortedQualifyers; 1421 } 1422 1423 1426 1427 public List getRepositoryContents(Integer repositoryId, Database db) throws SystemException, Exception 1428 { 1429 List contents = new ArrayList (); 1430 1431 OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.repositoryId = $1"); 1432 oql.bind(repositoryId); 1433 1434 QueryResults results = oql.execute(Database.ReadOnly); 1435 1436 while(results.hasMore()) 1437 { 1438 MediumContentImpl content = (MediumContentImpl)results.next(); 1439 contents.add(content); 1440 } 1441 1442 results.close(); 1443 oql.close(); 1444 1445 return contents; 1446 } 1447 1448 1471 public ContentVO getContentVOWithPath(Integer repositoryId, String path, boolean forceFolders, InfoGluePrincipal creator, Database db) throws SystemException, Exception 1472 { 1473 Content content = getRootContent(repositoryId, db); 1474 final String paths[] = path.split("/"); 1475 for(int i=0; i<paths.length; ++i) { 1476 final String name = paths[i]; 1477 final Content childContent = getChildWithName(content, name); 1478 if(childContent != null) 1479 content = childContent; 1480 else if(childContent == null && !forceFolders) 1481 throw new SystemException("There exists no content with the path [" + path + "]."); 1482 else 1483 { 1484 logger.info(" CREATE " + name); 1485 ContentVO contentVO = new ContentVO(); 1486 contentVO.setIsBranch(Boolean.TRUE); 1487 contentVO.setCreatorName(creator.getName()); 1488 contentVO.setName(name); 1489 content = create(db, content.getId(), null, repositoryId, contentVO); 1490 } 1491 } 1492 return content.getValueObject(); 1493 } 1494 1495 1498 private Content getChildWithName(Content content, String name) 1499 { 1500 for(Iterator i=content.getChildren().iterator(); i.hasNext(); ) 1501 { 1502 final Content childContent = (Content) i.next(); 1503 if(childContent.getName().equals(name)) 1504 return childContent; 1505 } 1506 return null; 1507 } 1508 1509 1510 1513 1514 public List getContentVOWithParentRecursive(Integer contentId) throws ConstraintException, SystemException 1515 { 1516 return getContentVOWithParentRecursive(contentId, new ArrayList ()); 1517 } 1518 1519 private List getContentVOWithParentRecursive(Integer contentId, List resultList) throws ConstraintException, SystemException 1520 { 1521 resultList.add(getContentVOWithId(contentId)); 1523 1524 List childContentList = ContentController.getContentController().getContentChildrenVOList(contentId); 1526 Iterator cit = childContentList.iterator(); 1527 while (cit.hasNext()) 1528 { 1529 ContentVO contentVO = (ContentVO) cit.next(); 1530 getContentVOWithParentRecursive(contentVO.getId(), resultList); 1531 } 1532 1533 return resultList; 1534 } 1535 1536 1540 1541 public BaseEntityVO getNewVO() 1542 { 1543 return new ContentVO(); 1544 } 1545 1546 1547} 1548 | Popular Tags |