1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.log4j.Logger; 36 import org.apache.xerces.parsers.DOMParser; 37 import org.exolab.castor.jdo.Database; 38 import org.exolab.castor.jdo.OQLQuery; 39 import org.exolab.castor.jdo.QueryResults; 40 import org.infoglue.cms.applications.common.VisualFormatter; 41 import org.infoglue.cms.entities.content.Content; 42 import org.infoglue.cms.entities.content.ContentVO; 43 import org.infoglue.cms.entities.content.ContentVersion; 44 import org.infoglue.cms.entities.content.ContentVersionVO; 45 import org.infoglue.cms.entities.content.DigitalAsset; 46 import org.infoglue.cms.entities.content.DigitalAssetVO; 47 import org.infoglue.cms.entities.content.impl.simple.ContentImpl; 48 import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl; 49 import org.infoglue.cms.entities.kernel.BaseEntityVO; 50 import org.infoglue.cms.entities.management.ContentTypeDefinition; 51 import org.infoglue.cms.entities.management.Language; 52 import org.infoglue.cms.entities.management.RegistryVO; 53 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl; 54 import org.infoglue.cms.entities.structure.SiteNode; 55 import org.infoglue.cms.entities.structure.SiteNodeVersion; 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.util.ConstraintExceptionBuffer; 61 import org.w3c.dom.CDATASection ; 62 import org.w3c.dom.Document ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.NodeList ; 65 import org.xml.sax.InputSource ; 66 67 71 72 public class ContentVersionController extends BaseController 73 { 74 private final static Logger logger = Logger.getLogger(ContentVersionController.class.getName()); 75 76 private static final ContentCategoryController contentCategoryController = ContentCategoryController.getController(); 77 private final RegistryController registryController = RegistryController.getController(); 78 79 82 83 public static ContentVersionController getContentVersionController() 84 { 85 return new ContentVersionController(); 86 } 87 88 private static Map contentMap = Collections.synchronizedMap(new HashMap ()); 89 90 public Integer getContentIdForContentVersion(Integer contentVersionId) throws SystemException, Bug 91 { 92 Integer contentId = (Integer )contentMap.get(contentVersionId); 93 if(contentId == null) 94 { 95 ContentVersionVO ContentVersionVO = (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId); 96 contentId = ContentVersionVO.getContentId(); 97 contentMap.put(contentVersionId, contentId); 98 } 99 100 return contentId; 101 } 102 103 public Integer getContentIdForContentVersion(Integer contentVersionId, Database db) throws SystemException, Bug 104 { 105 Integer contentId = (Integer )contentMap.get(contentVersionId); 106 if(contentId == null) 107 { 108 System.out.println("Not cached the first time:" + contentVersionId); 109 ContentVersionVO ContentVersionVO = (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId, db); 110 contentId = ContentVersionVO.getContentId(); 111 contentMap.put(contentVersionId, contentId); 112 } 113 114 return contentId; 115 } 116 117 public ContentVersionVO getContentVersionVOWithId(Integer contentVersionId) throws SystemException, Bug 118 { 119 return (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId); 120 } 121 122 public ContentVersion getContentVersionWithId(Integer contentVersionId, Database db) throws SystemException, Bug 123 { 124 return (ContentVersion) getObjectWithId(ContentVersionImpl.class, contentVersionId, db); 125 } 126 127 public ContentVersion getReadOnlyContentVersionWithId(Integer contentVersionId, Database db) throws SystemException, Bug 128 { 129 return (ContentVersion) getObjectWithIdAsReadOnly(ContentVersionImpl.class, contentVersionId, db); 130 } 131 132 public List getContentVersionVOList() throws SystemException, Bug 133 { 134 return getAllVOObjects(ContentVersionImpl.class, "contentVersionId"); 135 } 136 137 140 141 public List getContentVersionVOWithParentRecursive(Integer contentId, Integer stateId, boolean mustBeFirst) throws ConstraintException, SystemException 142 { 143 return getContentVersionVOWithParentRecursive(contentId, stateId, new ArrayList (), mustBeFirst); 144 } 145 146 private List getContentVersionVOWithParentRecursive(Integer contentId, Integer stateId, List resultList, boolean mustBeFirst) throws ConstraintException, SystemException 147 { 148 resultList.addAll(getLatestContentVersionVOWithParent(contentId, stateId, mustBeFirst)); 150 List childContentList = ContentController.getContentController().getContentChildrenVOList(contentId); 152 Iterator cit = childContentList.iterator(); 153 while (cit.hasNext()) 154 { 155 ContentVO contentVO = (ContentVO) cit.next(); 156 getContentVersionVOWithParentRecursive(contentVO.getId(), stateId, resultList, mustBeFirst); 157 } 158 159 return resultList; 160 } 161 162 163 166 167 public List getContentVersionVOWithParentRecursiveAndRelated(Integer contentId, Integer stateId, boolean mustBeFirst) throws ConstraintException, SystemException 168 { 169 List contentVersionVOList = new ArrayList (); 170 171 Database db = CastorDatabaseService.getDatabase(); 172 173 beginTransaction(db); 174 175 try 176 { 177 List contentVersionList = getContentVersionWithParentRecursiveAndRelated(contentId, stateId, new ArrayList (), new ArrayList (), db, mustBeFirst); 178 contentVersionVOList = toVOList(contentVersionList); 179 180 commitTransaction(db); 181 } 182 catch(Exception e) 183 { 184 logger.error("An error occurred so we should not completes the transaction:" + e, e); 185 rollbackTransaction(db); 186 throw new SystemException(e.getMessage()); 187 } 188 189 return contentVersionVOList; 190 } 191 192 private List getContentVersionWithParentRecursiveAndRelated(Integer contentId, Integer stateId, List resultList, List checkedContents, Database db, boolean mustBeFirst) throws ConstraintException, SystemException, Exception 193 { 194 checkedContents.add(contentId); 195 196 List contentVersions = getLatestContentVersionWithParent(contentId, stateId, db, mustBeFirst); 198 resultList.addAll(contentVersions); 199 200 Iterator contentVersionsIterator = contentVersions.iterator(); 201 while(contentVersionsIterator.hasNext()) 202 { 203 ContentVersion contentVersion = (ContentVersion)contentVersionsIterator.next(); 204 List relatedEntities = RegistryController.getController().getMatchingRegistryVOListForReferencingEntity(ContentVersion.class.getName(), contentVersion.getId().toString(), db); 205 Iterator relatedEntitiesIterator = relatedEntities.iterator(); 206 207 while(relatedEntitiesIterator.hasNext()) 208 { 209 RegistryVO registryVO = (RegistryVO)relatedEntitiesIterator.next(); 210 logger.info("registryVO:" + registryVO.getEntityName() + ":" + registryVO.getEntityId()); 211 if(registryVO.getEntityName().equals(Content.class.getName()) && !checkedContents.contains(new Integer (registryVO.getEntityId()))) 212 { 213 List relatedContentVersions = getLatestContentVersionWithParent(new Integer (registryVO.getEntityId()), stateId, db, mustBeFirst); 214 resultList.addAll(relatedContentVersions); 215 checkedContents.add(new Integer (registryVO.getEntityId())); 216 } 217 } 218 } 219 220 221 List childContentList = ContentController.getContentController().getContentChildrenVOList(contentId); 223 Iterator cit = childContentList.iterator(); 224 while (cit.hasNext()) 225 { 226 ContentVO contentVO = (ContentVO) cit.next(); 227 getContentVersionWithParentRecursiveAndRelated(contentVO.getId(), stateId, resultList, checkedContents, db, mustBeFirst); 228 } 229 230 return resultList; 231 } 232 233 234 public List getContentVersionVOWithParent(Integer contentId) throws SystemException, Bug 235 { 236 List resultList = new ArrayList (); 237 Database db = CastorDatabaseService.getDatabase(); 238 ContentVersionVO contentVersionVO = null; 239 240 beginTransaction(db); 241 242 try 243 { 244 List contentVersions = getContentVersionWithParent(contentId, db); 245 resultList = toVOList(contentVersions); 246 260 261 commitTransaction(db); 262 } 263 catch(Exception e) 264 { 265 logger.error("An error occurred so we should not completes the transaction:" + e, e); 266 rollbackTransaction(db); 267 throw new SystemException(e.getMessage()); 268 } 269 270 return resultList; 271 } 272 273 public List getContentVersionWithParent(Integer contentId, Database db) throws SystemException, Bug, Exception 274 { 275 ArrayList resultList = new ArrayList (); 276 ContentVersionVO contentVersionVO = null; 277 278 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 ORDER BY cv.contentVersionId desc"); 279 oql.bind(contentId); 280 281 QueryResults results = oql.execute(Database.ReadOnly); 282 283 while (results.hasMore()) 284 { 285 ContentVersion contentVersion = (ContentVersion)results.next(); 286 resultList.add(contentVersion); 287 } 288 289 results.close(); 290 oql.close(); 291 292 return resultList; 293 } 294 295 304 305 public List getLatestContentVersionVOWithParent(Integer contentId, Integer stateId, boolean mustBeFirst) throws SystemException, Bug 306 { 307 List resultList = new ArrayList (); 308 309 Database db = CastorDatabaseService.getDatabase(); 310 311 beginTransaction(db); 312 313 try 314 { 315 resultList = getLatestContentVersionWithParent(contentId, stateId, db, mustBeFirst); 316 resultList = toVOList(resultList); 317 318 commitTransaction(db); 319 } 320 catch(Exception e) 321 { 322 logger.error("An error occurred so we should not completes the transaction:" + e, e); 323 rollbackTransaction(db); 324 throw new SystemException(e.getMessage()); 325 } 326 327 return resultList; 328 } 329 330 339 340 public List getLatestContentVersionWithParent(Integer contentId, Integer stateId, Database db, boolean mustBeFirst) throws SystemException, Bug, Exception 341 { 342 ArrayList resultList = new ArrayList (); 343 ArrayList langCheck = new ArrayList (); 344 345 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 ORDER BY cv.contentVersionId desc"); 346 oql.bind(contentId); 347 349 QueryResults results = oql.execute(Database.ReadOnly); 350 351 while (results.hasMore()) 353 { 354 ContentVersion contentVersion = (ContentVersion)results.next(); 355 logger.info("contentVersion:" + contentVersion.getValueObject().getContentName()); 356 if(contentVersion.getIsActive().booleanValue()) 357 { 358 if ( (contentVersion.getStateId().compareTo(stateId)==0) && (!langCheck.contains(contentVersion.getLanguage().getLanguageId()))) 359 { 360 logger.info("Added contentVersion:" + contentVersion.getValueObject().getContentName() + ":" + contentVersion.getId() + ":" + contentVersion.getIsActive() + ":" + contentVersion.getStateId()); 361 resultList.add(contentVersion); 362 langCheck.add(contentVersion.getLanguage().getLanguageId()); 363 } 364 365 if(mustBeFirst) 366 langCheck.add(contentVersion.getLanguage().getLanguageId()); 367 } 368 } 369 370 results.close(); 371 oql.close(); 372 373 logger.info("getLatestContentVersionWithParent done..."); 374 375 return resultList; 376 } 377 378 379 382 383 public List getLatestActiveContentVersionIfInState(Content content, Integer stateId, Database db) throws SystemException, Exception 384 { 385 List resultList = new ArrayList (); 386 Map lastLanguageVersions = new HashMap (); 387 Map languageVersions = new HashMap (); 388 389 Collection contentVersions = content.getContentVersions(); 390 391 Iterator versionIterator = contentVersions.iterator(); 392 while(versionIterator.hasNext()) 393 { 394 ContentVersion contentVersionCandidate = (ContentVersion)versionIterator.next(); 395 396 ContentVersion lastVersionInThatLanguage = (ContentVersion)lastLanguageVersions.get(contentVersionCandidate.getLanguage().getId()); 397 if(lastVersionInThatLanguage == null || (lastVersionInThatLanguage.getId().intValue() < contentVersionCandidate.getId().intValue() && contentVersionCandidate.getIsActive().booleanValue())) 398 lastLanguageVersions.put(contentVersionCandidate.getLanguage().getId(), contentVersionCandidate); 399 400 if(contentVersionCandidate.getIsActive().booleanValue() && contentVersionCandidate.getStateId().intValue() == stateId.intValue()) 401 { 402 if(contentVersionCandidate.getOwningContent().getContentId().intValue() == content.getId().intValue()) 403 { 404 ContentVersion versionInThatLanguage = (ContentVersion)languageVersions.get(contentVersionCandidate.getLanguage().getId()); 405 if(versionInThatLanguage == null || versionInThatLanguage.getContentVersionId().intValue() < contentVersionCandidate.getId().intValue()) 406 { 407 languageVersions.put(contentVersionCandidate.getLanguage().getId(), contentVersionCandidate); 408 } 409 } 410 } 411 } 412 413 logger.info("Found languageVersions:" + languageVersions.size()); 414 logger.info("Found lastLanguageVersions:" + lastLanguageVersions.size()); 415 Iterator i = languageVersions.values().iterator(); 416 while(i.hasNext()) 417 { 418 ContentVersion contentVersion = (ContentVersion)i.next(); 419 ContentVersion lastVersionInThatLanguage = (ContentVersion)lastLanguageVersions.get(contentVersion.getLanguage().getId()); 420 421 logger.info("contentVersion:" + contentVersion.getId()); 422 logger.info("lastVersionInThatLanguage:" + lastVersionInThatLanguage.getId()); 423 424 if(contentVersion == lastVersionInThatLanguage) 425 resultList.add(contentVersion); 426 } 427 428 return resultList; 429 } 430 431 432 435 436 public ContentVersionVO getLatestActiveContentVersionVO(Integer contentId, Integer languageId) throws SystemException, Bug 437 { 438 Database db = CastorDatabaseService.getDatabase(); 439 ContentVersionVO contentVersionVO = null; 440 441 beginTransaction(db); 442 443 try 444 { 445 ContentVersion contentVersion = null; 446 447 contentVersion = getLatestActiveContentVersion(contentId, languageId, db); 448 464 465 if(contentVersion != null) 466 contentVersionVO = contentVersion.getValueObject(); 467 468 commitTransaction(db); 469 } 470 catch(Exception e) 471 { 472 logger.error("An error occurred so we should not completes the transaction:" + e, e); 473 rollbackTransaction(db); 474 throw new SystemException(e.getMessage()); 475 } 476 477 return contentVersionVO; 478 } 479 480 483 484 public ContentVersionVO getLatestActiveContentVersionVO(Integer contentId, Integer languageId, Database db) throws SystemException, Bug 485 { 486 ContentVersionVO contentVersionVO = null; 487 488 ContentVersion contentVersion = getLatestActiveContentVersion(contentId, languageId, db); 489 490 if(contentVersion != null) 491 contentVersionVO = contentVersion.getValueObject(); 492 493 return contentVersionVO; 494 } 495 496 499 500 public ContentVersion getLatestActiveContentVersion(Integer contentId, Integer languageId, Database db) throws SystemException, Bug 501 { 502 ContentVersion contentVersion = null; 503 504 Content content = ContentController.getContentController().getContentWithId(contentId, db); 505 logger.info("contentId:" + contentId); 506 logger.info("languageId:" + languageId); 507 logger.info("content:" + content.getName()); 508 Collection contentVersions = content.getContentVersions(); 509 logger.info("contentVersions:" + contentVersions.size()); 510 511 Iterator i = contentVersions.iterator(); 512 while(i.hasNext()) 513 { 514 ContentVersion currentContentVersion = (ContentVersion)i.next(); 515 logger.info("found one candidate:" + currentContentVersion.getValueObject()); 516 if(contentVersion == null || (currentContentVersion.getId().intValue() > contentVersion.getId().intValue())) 517 { 518 logger.info("currentContentVersion:" + currentContentVersion.getIsActive()); 519 logger.info("currentContentVersion:" + currentContentVersion.getLanguage().getId()); 520 if(currentContentVersion.getIsActive().booleanValue() && currentContentVersion.getLanguage().getId().intValue() == languageId.intValue()) 521 contentVersion = currentContentVersion; 522 } 523 } 524 525 return contentVersion; 526 } 527 528 529 public ContentVersionVO getLatestContentVersionVO(Integer contentId, Integer languageId) throws SystemException, Bug 530 { 531 Database db = CastorDatabaseService.getDatabase(); 532 ContentVersionVO contentVersionVO = null; 533 534 beginTransaction(db); 535 536 try 537 { 538 539 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 ORDER BY cv.contentVersionId desc"); 540 oql.bind(contentId); 541 oql.bind(languageId); 542 543 QueryResults results = oql.execute(Database.ReadOnly); 544 545 if (results.hasMore()) 546 { 547 ContentVersion contentVersion = (ContentVersion)results.next(); 548 logger.info("found one:" + contentVersion.getValueObject()); 549 contentVersionVO = contentVersion.getValueObject(); 550 } 551 552 results.close(); 553 oql.close(); 554 555 commitTransaction(db); 556 } 557 catch(Exception e) 558 { 559 logger.error("An error occurred so we should not completes the transaction:" + e, e); 560 rollbackTransaction(db); 561 throw new SystemException(e.getMessage()); 562 } 563 564 565 return contentVersionVO; 566 } 567 568 569 public ContentVersion getContentVersionWithId(Integer contentVersionId) throws SystemException, Bug 570 { 571 Database db = CastorDatabaseService.getDatabase(); 572 ContentVersion contentVersion = null; 573 574 beginTransaction(db); 575 576 try 577 { 578 contentVersion = getContentVersionWithId(contentVersionId, db); 579 580 commitTransaction(db); 581 } 582 catch(Exception e) 583 { 584 logger.error("An error occurred so we should not completes the transaction:" + e, e); 585 rollbackTransaction(db); 586 throw new SystemException(e.getMessage()); 587 } 588 589 return contentVersion; 590 } 591 592 593 public ContentVersion getLatestContentVersion(Integer contentId, Integer languageId, Database db) throws SystemException, Bug, Exception 594 { 595 ContentVersion contentVersion = null; 596 597 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 ORDER BY cv.contentVersionId desc"); 598 oql.bind(contentId); 599 oql.bind(languageId); 600 601 QueryResults results = oql.execute(Database.ReadOnly); 602 603 if (results.hasMore()) 604 { 605 contentVersion = (ContentVersion)results.next(); 606 } 607 608 results.close(); 609 oql.close(); 610 611 return contentVersion; 612 } 613 614 615 618 619 public ContentVersionVO create(Integer contentId, Integer languageId, ContentVersionVO contentVersionVO, Integer oldContentVersionId) throws ConstraintException, SystemException 620 { 621 Database db = CastorDatabaseService.getDatabase(); 622 ContentVersion contentVersion = null; 623 624 beginTransaction(db); 625 try 626 { 627 contentVersion = create(contentId, languageId, contentVersionVO, oldContentVersionId, db); 628 commitTransaction(db); 629 } 630 catch(Exception e) 631 { 632 logger.error("An error occurred so we should not completes the transaction:" + e, e); 633 rollbackTransaction(db); 634 throw new SystemException(e.getMessage()); 635 } 636 637 return contentVersion.getValueObject(); 638 } 639 640 644 645 public ContentVersion create(Integer contentId, Integer languageId, ContentVersionVO contentVersionVO, Integer oldContentVersionId, Database db) throws ConstraintException, SystemException, Exception 646 { 647 Content content = ContentController.getContentController().getContentWithId(contentId, db); 648 Language language = LanguageController.getController().getLanguageWithId(languageId, db); 649 return create(content, language, contentVersionVO, oldContentVersionId, db); 650 } 651 652 656 657 public ContentVersion create(Content content, Language language, ContentVersionVO contentVersionVO, Integer oldContentVersionId, Database db) throws ConstraintException, SystemException, Exception 658 { 659 ContentVersion contentVersion = new ContentVersionImpl(); 660 contentVersion.setLanguage((LanguageImpl)language); 661 logger.info("Content:" + content.getContentId() + ":" + db.isPersistent(content)); 662 contentVersion.setOwningContent((ContentImpl)content); 663 664 contentVersion.setValueObject(contentVersionVO); 665 db.create(contentVersion); 666 667 content.getContentVersions().add(contentVersion); 668 669 if(oldContentVersionId != null && oldContentVersionId.intValue() != -1) 670 copyDigitalAssets(getContentVersionWithId(oldContentVersionId, db), contentVersion, db); 671 673 return contentVersion; 674 } 675 676 679 680 public void delete(ContentVersionVO contentVersionVO) throws ConstraintException, SystemException 681 { 682 Database db = CastorDatabaseService.getDatabase(); 683 beginTransaction(db); 684 try 685 { 686 delete(contentVersionVO, db); 687 commitTransaction(db); 688 } 689 catch(Exception e) 690 { 691 logger.error("An error occurred so we should not completes the transaction:" + e, e); 692 rollbackTransaction(db); 693 throw new SystemException(e.getMessage()); 694 } 695 } 696 697 700 701 public void delete(ContentVersionVO contentVersionVO, Database db) throws ConstraintException, SystemException, Exception 702 { 703 ContentVersion contentVersion = getContentVersionWithId(contentVersionVO.getContentVersionId(), db); 704 delete(contentVersion, db, false); 705 } 706 707 710 711 public void delete(ContentVersion contentVersion, Database db) throws ConstraintException, SystemException, Exception 712 { 713 delete(contentVersion, db, false); 714 } 715 716 719 720 public void delete(ContentVersion contentVersion, Database db, boolean forceDelete) throws ConstraintException, SystemException, Exception 721 { 722 if (!forceDelete && contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue() && contentVersion.getIsActive().booleanValue() == true) 723 throw new ConstraintException("ContentVersion.stateId", "3300"); 724 725 contentCategoryController.deleteByContentVersion(contentVersion, db); 726 727 Content content = contentVersion.getOwningContent(); 728 729 if(content != null) 730 content.getContentVersions().remove(contentVersion); 731 732 db.remove(contentVersion); 733 } 734 735 736 737 742 743 public void deleteVersionsForContent(Content content, Database db) throws ConstraintException, SystemException, Bug, Exception 744 { 745 deleteVersionsForContent(content, db, false); 746 } 747 748 753 754 public void deleteVersionsForContent(Content content, Database db, boolean forceDelete) throws ConstraintException, SystemException, Bug, Exception 755 { 756 Collection contentVersions = Collections.synchronizedCollection(content.getContentVersions()); 757 Iterator contentVersionIterator = contentVersions.iterator(); 758 759 while (contentVersionIterator.hasNext()) 760 { 761 ContentVersion contentVersion = (ContentVersion)contentVersionIterator.next(); 762 763 Collection digitalAssetList = contentVersion.getDigitalAssets(); 764 Iterator assets = digitalAssetList.iterator(); 765 while (assets.hasNext()) 766 { 767 DigitalAsset digitalAsset = (DigitalAsset)assets.next(); 768 assets.remove(); 769 db.remove(digitalAsset); 770 } 771 772 logger.info("Deleting contentVersion:" + contentVersion.getContentVersionId()); 773 contentVersionIterator.remove(); 774 delete(contentVersion, db, forceDelete); 775 } 776 content.setContentVersions(new ArrayList ()); 777 } 778 779 782 783 public void deleteDigitalAsset(Integer contentId, Integer languageId, String assetKey) throws ConstraintException, SystemException 784 { 785 Database db = CastorDatabaseService.getDatabase(); 786 beginTransaction(db); 787 try 788 { 789 ContentVersion contentVersion = this.getLatestActiveContentVersion(contentId, languageId, db); 790 791 Collection digitalAssets = contentVersion.getDigitalAssets(); 792 Iterator assetIterator = digitalAssets.iterator(); 793 while(assetIterator.hasNext()) 794 { 795 DigitalAsset currentDigitalAsset = (DigitalAsset)assetIterator.next(); 796 if(currentDigitalAsset.getAssetKey().equals(assetKey)) 797 { 798 assetIterator.remove(); 799 db.remove(currentDigitalAsset); 800 break; 801 } 802 } 803 804 commitTransaction(db); 805 } 806 catch(Exception e) 807 { 808 logger.error("An error occurred so we should not completes the transaction:" + e, e); 809 rollbackTransaction(db); 810 throw new SystemException(e.getMessage()); 811 } 812 } 813 814 815 818 819 public ContentVersionVO update(Integer contentId, Integer languageId, ContentVersionVO contentVersionVO) throws ConstraintException, SystemException 820 { 821 ContentVersionVO updatedContentVersionVO; 822 823 Database db = CastorDatabaseService.getDatabase(); 824 825 beginTransaction(db); 826 827 try 828 { 829 Content content = ContentController.getContentController().getContentWithId(contentId, db); 830 ContentTypeDefinition contentTypeDefinition = content.getContentTypeDefinition(); 831 ConstraintExceptionBuffer ceb = contentVersionVO.validateAdvanced(contentTypeDefinition.getValueObject()); 832 ceb.throwIfNotEmpty(); 833 834 ContentVersion contentVersion = null; 835 836 if(contentVersionVO.getId() == null) 837 { 838 logger.info("Creating the entity because there was no version at all for: " + contentId + " " + languageId); 839 contentVersion = create(contentId, languageId, contentVersionVO, null, db); 840 } 841 else 842 { 843 contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getId(), db); 844 contentVersion.setValueObject(contentVersionVO); 845 } 846 847 registryController.updateContentVersion(contentVersion, db); 848 849 updatedContentVersionVO = contentVersion.getValueObject(); 850 851 commitTransaction(db); 852 } 853 catch(ConstraintException ce) 854 { 855 logger.error("Validation error:" + ce, ce); 856 rollbackTransaction(db); 857 throw ce; 858 } 859 catch(Exception e) 860 { 861 logger.error("An error occurred so we should not completes the transaction:" + e, e); 862 rollbackTransaction(db); 863 throw new SystemException(e.getMessage()); 864 } 865 866 return updatedContentVersionVO; } 868 869 870 public List getPublishedActiveContentVersionVOList(Integer contentId) throws SystemException, Bug, Exception 871 { 872 List contentVersionVOList = new ArrayList (); 873 874 Database db = CastorDatabaseService.getDatabase(); 875 beginTransaction(db); 876 try 877 { 878 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.contentVersionId desc"); 879 oql.bind(contentId); 880 oql.bind(ContentVersionVO.PUBLISHED_STATE); 881 oql.bind(true); 882 883 QueryResults results = oql.execute(Database.ReadOnly); 884 885 while (results.hasMore()) 886 { 887 ContentVersion contentVersion = (ContentVersion)results.next(); 888 contentVersionVOList.add(contentVersion.getValueObject()); 889 } 890 891 results.close(); 892 oql.close(); 893 894 commitTransaction(db); 895 } 896 catch(Exception e) 897 { 898 logger.error("An error occurred so we should not completes the transaction:" + e, e); 899 rollbackTransaction(db); 900 throw new SystemException(e.getMessage()); 901 } 902 903 return contentVersionVOList; 904 } 905 906 907 public ContentVersion getLatestPublishedContentVersion(Integer contentId) throws SystemException, Bug, Exception 908 { 909 ContentVersion contentVersion = null; 910 911 Database db = CastorDatabaseService.getDatabase(); 912 beginTransaction(db); 913 try 914 { 915 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.contentVersionId desc"); 916 oql.bind(contentId); 917 oql.bind(ContentVersionVO.PUBLISHED_STATE); 918 oql.bind(true); 919 920 QueryResults results = oql.execute(Database.ReadOnly); 921 922 if (results.hasMore()) 923 { 924 contentVersion = (ContentVersion)results.next(); 925 } 926 927 results.close(); 928 oql.close(); 929 930 commitTransaction(db); 931 } 932 catch(Exception e) 933 { 934 logger.error("An error occurred so we should not completes the transaction:" + e, e); 935 rollbackTransaction(db); 936 throw new SystemException(e.getMessage()); 937 } 938 939 return contentVersion; 940 } 941 942 943 public ContentVersion getLatestPublishedContentVersion(Integer contentId, Integer languageId) throws SystemException, Bug, Exception 944 { 945 ContentVersion contentVersion = null; 946 947 Database db = CastorDatabaseService.getDatabase(); 948 beginTransaction(db); 949 try 950 { 951 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.stateId = $3 AND cv.isActive = $4 ORDER BY cv.contentVersionId desc"); 952 oql.bind(contentId); 953 oql.bind(languageId); 954 oql.bind(ContentVersionVO.PUBLISHED_STATE); 955 oql.bind(true); 956 957 QueryResults results = oql.execute(Database.ReadOnly); 958 959 if (results.hasMore()) 960 { 961 contentVersion = (ContentVersion)results.next(); 962 } 963 964 results.close(); 965 oql.close(); 966 967 commitTransaction(db); 968 } 969 catch(Exception e) 970 { 971 logger.error("An error occurred so we should not completes the transaction:" + e, e); 972 rollbackTransaction(db); 973 throw new SystemException(e.getMessage()); 974 } 975 976 return contentVersion; 977 } 978 979 980 public ContentVersion getLatestPublishedContentVersion(Integer contentId, Integer languageId, Database db) throws SystemException, Bug, Exception 981 { 982 ContentVersion contentVersion = null; 983 984 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.stateId = $3 AND cv.isActive = $4 ORDER BY cv.contentVersionId desc"); 985 oql.bind(contentId); 986 oql.bind(languageId); 987 oql.bind(ContentVersionVO.PUBLISHED_STATE); 988 oql.bind(true); 989 990 QueryResults results = oql.execute(); 991 this.logger.info("Fetching entity in read/write mode"); 992 993 if (results.hasMore()) 994 { 995 contentVersion = (ContentVersion)results.next(); 996 } 997 998 results.close(); 999 oql.close(); 1000 1001 return contentVersion; 1002 } 1003 1004 1005 1008 1009 public ContentVersionVO getPreviousContentVersionVO(Integer contentId, Integer languageId, Integer contentVersionId) throws SystemException, Bug 1010 { 1011 Database db = CastorDatabaseService.getDatabase(); 1012 ContentVersionVO contentVersionVO = null; 1013 1014 beginTransaction(db); 1015 1016 try 1017 { 1018 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.contentVersionId < $3 ORDER BY cv.contentVersionId desc"); 1019 oql.bind(contentId); 1020 oql.bind(languageId); 1021 oql.bind(contentVersionId); 1022 1023 QueryResults results = oql.execute(Database.ReadOnly); 1024 1025 if (results.hasMore()) 1026 { 1027 ContentVersion contentVersion = (ContentVersion)results.next(); 1028 logger.info("found one:" + contentVersion.getValueObject()); 1029 contentVersionVO = contentVersion.getValueObject(); 1030 } 1031 1032 results.close(); 1033 oql.close(); 1034 1035 commitTransaction(db); 1036 } 1037 catch(Exception e) 1038 { 1039 logger.error("An error occurred so we should not completes the transaction:" + e, e); 1040 rollbackTransaction(db); 1041 throw new SystemException(e.getMessage()); 1042 } 1043 1044 return contentVersionVO; 1045 } 1046 1047 1048 1051 1052 public ContentVersionVO getPreviousActiveContentVersionVO(Integer contentId, Integer languageId, Integer contentVersionId, Database db) throws SystemException, Bug, Exception 1053 { 1054 ContentVersionVO contentVersionVO = null; 1055 1056 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.isActive = $3 AND cv.contentVersionId < $4 ORDER BY cv.contentVersionId desc"); 1057 oql.bind(contentId); 1058 oql.bind(languageId); 1059 oql.bind(new Boolean (true)); 1060 oql.bind(contentVersionId); 1061 1062 QueryResults results = oql.execute(Database.ReadOnly); 1063 1064 if (results.hasMore()) 1065 { 1066 ContentVersion contentVersion = (ContentVersion)results.next(); 1067 logger.info("found one:" + contentVersion.getValueObject()); 1068 contentVersionVO = contentVersion.getValueObject(); 1069 } 1070 1071 results.close(); 1072 oql.close(); 1073 1074 return contentVersionVO; 1075 } 1076 1077 1078 1081 public void deleteDigitalAssetRelation(Integer contentVersionId, Integer digitalAssetId) throws SystemException, Bug 1082 { 1083 Database db = CastorDatabaseService.getDatabase(); 1084 beginTransaction(db); 1085 1086 try 1087 { 1088 ContentVersion contentVersion = getContentVersionWithId(contentVersionId, db); 1089 DigitalAsset digitalAsset = DigitalAssetController.getDigitalAssetWithId(digitalAssetId, db); 1090 contentVersion.getDigitalAssets().remove(digitalAsset); 1091 digitalAsset.getContentVersions().remove(contentVersion); 1092 commitTransaction(db); 1093 } 1094 catch(Exception e) 1095 { 1096 logger.error("An error occurred so we should not completes the transaction:" + e, e); 1097 rollbackTransaction(db); 1098 throw new SystemException(e.getMessage()); 1099 } 1100 } 1101 1102 1103 1106 public void deleteDigitalAssetRelation(Integer contentVersionId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug 1107 { 1108 ContentVersion contentVersion = getContentVersionWithId(contentVersionId, db); 1109 contentVersion.getDigitalAssets().remove(digitalAsset); 1110 digitalAsset.getContentVersions().remove(contentVersion); 1111 } 1112 1113 1114 1118 1119 public void copyDigitalAssets(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws ConstraintException, SystemException, Exception 1120 { 1121 Collection digitalAssets = originalContentVersion.getDigitalAssets(); 1122 1123 Iterator digitalAssetsIterator = digitalAssets.iterator(); 1125 while(digitalAssetsIterator.hasNext()) 1126 { 1127 DigitalAsset digitalAsset = (DigitalAsset)digitalAssetsIterator.next(); 1128 logger.info("Copying digitalAssets " + digitalAsset.getAssetKey()); 1129 DigitalAssetVO digitalAssetVO = digitalAsset.getValueObject(); 1130 1131 DigitalAssetController.create(digitalAssetVO, DigitalAssetController.getController().getAssetInputStream(digitalAsset), newContentVersion, db); 1132 logger.info("digitalAssets:" + digitalAssets.size()); 1134 } 1135 } 1137 1138 1139 1143 public String getAttributeValue(Integer contentVersionId, String attributeName, boolean escapeHTML) throws SystemException 1144 { 1145 String value = ""; 1146 ContentVersionVO contentVersionVO = getContentVersionVOWithId(contentVersionId); 1147 1148 if(contentVersionVO != null) 1149 { 1150 try 1151 { 1152 logger.info("attributeName:" + attributeName); 1153 logger.info("VersionValue:" + contentVersionVO.getVersionValue()); 1154 value = getAttributeValue(contentVersionVO, attributeName, escapeHTML); 1155 } 1156 catch (Exception e) 1157 { 1158 e.printStackTrace(); 1159 } 1160 } 1161 return value; 1163 } 1164 1165 1173 public String getAttributeValue(ContentVersionVO contentVersionVO, String attributeName, boolean escapeHTML) 1174 { 1175 String value = ""; 1176 String xml = contentVersionVO.getVersionValue(); 1177 1178 int startTagIndex = xml.indexOf("<" + attributeName + ">"); 1179 int endTagIndex = xml.indexOf("]]></" + attributeName + ">"); 1180 1181 if(startTagIndex > 0 && startTagIndex < xml.length() && endTagIndex > startTagIndex && endTagIndex < xml.length()) 1182 { 1183 value = xml.substring(startTagIndex + attributeName.length() + 11, endTagIndex); 1184 if(escapeHTML) 1185 value = new VisualFormatter().escapeHTML(value); 1186 } 1187 1188 return value; 1189 } 1190 1191 1192 1196 1197 public void updateAttributeValue(Integer contentVersionId, String attributeName, String attributeValue, InfoGluePrincipal infogluePrincipal) throws SystemException, Bug 1198 { 1199 ContentVersionVO contentVersionVO = getContentVersionVOWithId(contentVersionId); 1200 1201 if(contentVersionVO != null) 1202 { 1203 try 1204 { 1205 logger.info("attributeName:" + attributeName); 1206 logger.info("versionValue:" + contentVersionVO.getVersionValue()); 1207 logger.info("attributeValue:" + attributeValue); 1208 InputSource inputSource = new InputSource (new StringReader (contentVersionVO.getVersionValue())); 1209 1210 DOMParser parser = new DOMParser(); 1211 parser.parse(inputSource); 1212 Document document = parser.getDocument(); 1213 1214 NodeList nl = document.getDocumentElement().getChildNodes(); 1215 Node attributesNode = nl.item(0); 1216 1217 boolean existed = false; 1218 nl = attributesNode.getChildNodes(); 1219 for(int i=0; i<nl.getLength(); i++) 1220 { 1221 Node n = nl.item(i); 1222 if(n.getNodeName().equalsIgnoreCase(attributeName)) 1223 { 1224 if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null) 1225 { 1226 n.getFirstChild().setNodeValue(attributeValue); 1227 existed = true; 1228 break; 1229 } 1230 else 1231 { 1232 CDATASection cdata = document.createCDATASection(attributeValue); 1233 n.appendChild(cdata); 1234 existed = true; 1235 break; 1236 } 1237 } 1238 } 1239 1240 if(existed == false) 1241 { 1242 org.w3c.dom.Element attributeElement = document.createElement(attributeName); 1243 attributesNode.appendChild(attributeElement); 1244 CDATASection cdata = document.createCDATASection(attributeValue); 1245 attributeElement.appendChild(cdata); 1246 } 1247 1248 StringBuffer sb = new StringBuffer (); 1249 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 1250 logger.info("sb:" + sb); 1251 contentVersionVO.setVersionValue(sb.toString()); 1252 contentVersionVO.setVersionModifier(infogluePrincipal.getName()); 1253 update(contentVersionVO.getContentId(), contentVersionVO.getLanguageId(), contentVersionVO); 1254 } 1255 catch(Exception e) 1256 { 1257 e.printStackTrace(); 1258 } 1259 } 1260 } 1261 1262 1266 1267 public BaseEntityVO getNewVO() 1268 { 1269 return new ContentVersionVO(); 1270 } 1271 1272 1275 1276 public void getContentAndAffectedItemsRecursive(Integer contentId, Integer stateId, List siteNodeVersionVOList, List contenteVersionVOList, boolean mustBeFirst, boolean includeMetaInfo) throws ConstraintException, SystemException 1277 { 1278 Database db = CastorDatabaseService.getDatabase(); 1279 1280 beginTransaction(db); 1281 1282 try 1283 { 1284 Content content = ContentController.getContentController().getContentWithId(contentId, db); 1285 1286 getContentAndAffectedItemsRecursive(content, stateId, new ArrayList (), new ArrayList (), db, siteNodeVersionVOList, contenteVersionVOList, mustBeFirst, includeMetaInfo); 1287 1288 commitTransaction(db); 1289 } 1290 catch(Exception e) 1291 { 1292 logger.error("An error occurred so we should not completes the transaction:" + e, e); 1293 rollbackTransaction(db); 1294 throw new SystemException(e.getMessage()); 1295 } 1296 } 1297 1298 private void getContentAndAffectedItemsRecursive(Content content, Integer stateId, List checkedSiteNodes, List checkedContents, Database db, List siteNodeVersionVOList, List contentVersionVOList, boolean mustBeFirst, boolean includeMetaInfo) throws ConstraintException, SystemException, Exception 1299 { 1300 checkedSiteNodes.add(content.getId()); 1301 1302 List contentVersions = getLatestContentVersionWithParent(content.getId(), stateId, db, mustBeFirst); 1303 1304 Iterator contentVersionsIterator = contentVersions.iterator(); 1305 while(contentVersionsIterator.hasNext()) 1306 { 1307 ContentVersion contentVersion = (ContentVersion)contentVersionsIterator.next(); 1308 contentVersionVOList.add(contentVersion.getValueObject()); 1309 1310 List relatedEntities = RegistryController.getController().getMatchingRegistryVOListForReferencingEntity(ContentVersion.class.getName(), contentVersion.getId().toString(), db); 1311 logger.info("relatedEntities:" + relatedEntities); 1312 Iterator relatedEntitiesIterator = relatedEntities.iterator(); 1313 1314 while(relatedEntitiesIterator.hasNext()) 1315 { 1316 RegistryVO registryVO = (RegistryVO)relatedEntitiesIterator.next(); 1317 logger.info("registryVO:" + registryVO.getEntityName() + ":" + registryVO.getEntityId()); 1318 if(registryVO.getEntityName().equals(SiteNode.class.getName()) && !checkedSiteNodes.contains(new Integer (registryVO.getEntityId()))) 1319 { 1320 try 1321 { 1322 SiteNode relatedSiteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (registryVO.getEntityId()), db); 1323 SiteNodeVersion relatedSiteNodeVersion = SiteNodeVersionController.getController().getLatestActiveSiteNodeVersionIfInState(relatedSiteNode, stateId, db); 1324 if(relatedSiteNodeVersion != null && content.getRepository().getId().intValue() == relatedSiteNodeVersion.getOwningSiteNode().getRepository().getId().intValue()) 1325 { 1326 siteNodeVersionVOList.add(relatedSiteNodeVersion.getValueObject()); 1327 } 1328 } 1329 catch(Exception e) 1330 { 1331 logger.warn("The related siteNode with id:" + registryVO.getEntityId() + " could not be loaded.", e); 1332 } 1333 1334 checkedSiteNodes.add(new Integer (registryVO.getEntityId())); 1335 } 1336 else if(registryVO.getEntityName().equals(Content.class.getName()) && !checkedContents.contains(new Integer (registryVO.getEntityId()))) 1337 { 1338 try 1339 { 1340 Content relatedContent = ContentController.getContentController().getContentWithId(new Integer (registryVO.getEntityId()), db); 1341 if(includeMetaInfo || (!includeMetaInfo && (relatedContent.getContentTypeDefinition() == null || !relatedContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info")))) 1342 { 1343 List relatedContentVersions = ContentVersionController.getContentVersionController().getLatestActiveContentVersionIfInState(relatedContent, stateId, db); 1344 logger.info("relatedContentVersions:" + relatedContentVersions.size()); 1345 1346 Iterator relatedContentVersionsIterator = relatedContentVersions.iterator(); 1347 while(relatedContentVersionsIterator.hasNext()) 1348 { 1349 ContentVersion relatedContentVersion = (ContentVersion)relatedContentVersionsIterator.next(); 1350 if(relatedContentVersion != null && content.getRepository().getId().intValue() == relatedContentVersion.getOwningContent().getRepository().getId().intValue()) 1351 { 1352 contentVersionVOList.add(relatedContentVersion.getValueObject()); 1353 logger.info("Added:" + relatedContentVersion.getId()); 1354 } 1355 1356 } 1357 } 1358 } 1359 catch(Exception e) 1360 { 1361 logger.warn("The related content with id:" + registryVO.getEntityId() + " could not be loaded.", e); 1362 } 1363 1364 checkedContents.add(new Integer (registryVO.getEntityId())); 1365 } 1366 } 1367 1368 } 1369 1370 Collection childContentList = content.getChildren(); 1372 Iterator cit = childContentList.iterator(); 1373 while (cit.hasNext()) 1374 { 1375 Content citContent = (Content) cit.next(); 1376 getContentAndAffectedItemsRecursive(citContent, stateId, checkedSiteNodes, checkedContents, db, siteNodeVersionVOList, contentVersionVOList, mustBeFirst, includeMetaInfo); 1377 } 1378 1379 } 1380 1381 1382} 1383 | Popular Tags |