1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.BufferedInputStream ; 27 import java.io.BufferedOutputStream ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.FilenameFilter ; 32 import java.io.InputStream ; 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import org.apache.log4j.Logger; 39 import org.exolab.castor.jdo.Database; 40 import org.infoglue.cms.entities.content.Content; 41 import org.infoglue.cms.entities.content.ContentVersion; 42 import org.infoglue.cms.entities.content.DigitalAsset; 43 import org.infoglue.cms.entities.content.DigitalAssetVO; 44 import org.infoglue.cms.entities.content.impl.simple.DigitalAssetImpl; 45 import org.infoglue.cms.entities.kernel.BaseEntityVO; 46 import org.infoglue.cms.entities.management.GroupProperties; 47 import org.infoglue.cms.entities.management.LanguageVO; 48 import org.infoglue.cms.entities.management.RoleProperties; 49 import org.infoglue.cms.entities.management.UserProperties; 50 import org.infoglue.cms.exception.Bug; 51 import org.infoglue.cms.exception.ConstraintException; 52 import org.infoglue.cms.exception.SystemException; 53 import org.infoglue.cms.util.CmsPropertyHandler; 54 import org.infoglue.cms.util.ConstraintExceptionBuffer; 55 import org.infoglue.cms.util.graphics.ThumbnailGenerator; 56 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController; 57 58 61 62 public class DigitalAssetController extends BaseController 63 { 64 private final static Logger logger = Logger.getLogger(DigitalAssetController.class.getName()); 65 66 public static DigitalAssetController getController() 67 { 68 return new DigitalAssetController(); 69 } 70 71 72 75 76 public static DigitalAssetVO getDigitalAssetVOWithId(Integer digitalAssetId) throws SystemException, Bug 77 { 78 return (DigitalAssetVO) getVOWithId(DigitalAssetImpl.class, digitalAssetId); 79 } 80 81 84 85 public static DigitalAsset getDigitalAssetWithId(Integer digitalAssetId, Database db) throws SystemException, Bug 86 { 87 return (DigitalAsset) getObjectWithId(DigitalAssetImpl.class, digitalAssetId, db); 88 } 89 90 91 95 96 public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream is, Integer contentVersionId) throws SystemException 97 { 98 Database db = CastorDatabaseService.getDatabase(); 99 100 beginTransaction(db); 101 102 try 103 { 104 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionId, db); 105 106 digitalAssetVO = create(digitalAssetVO, is, contentVersion, db); 107 108 commitTransaction(db); 109 } 110 catch(Exception e) 111 { 112 logger.error("An error occurred so we should not complete the transaction:" + e, e); 113 rollbackTransaction(db); 114 throw new SystemException(e.getMessage()); 115 } 116 117 return digitalAssetVO; 118 } 119 120 124 125 public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream is, ContentVersion contentVersion, Database db) throws SystemException, Exception 126 { 127 DigitalAsset digitalAsset = null; 128 129 Collection contentVersions = new ArrayList (); 130 contentVersions.add(contentVersion); 131 logger.info("Added contentVersion:" + contentVersion.getId()); 132 133 digitalAsset = new DigitalAssetImpl(); 134 digitalAsset.setValueObject(digitalAssetVO.createCopy()); 135 digitalAsset.setAssetBlob(is); 136 digitalAsset.setContentVersions(contentVersions); 137 138 db.create(digitalAsset); 139 140 143 contentVersion.getDigitalAssets().add(digitalAsset); 144 145 return digitalAsset.getValueObject(); 146 } 147 148 152 153 public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream is, String entity, Integer entityId) throws ConstraintException, SystemException 154 { 155 Database db = CastorDatabaseService.getDatabase(); 156 157 DigitalAsset digitalAsset = null; 158 159 beginTransaction(db); 160 161 try 162 { 163 if(entity.equalsIgnoreCase("ContentVersion")) 164 { 165 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(entityId, db); 166 Collection contentVersions = new ArrayList (); 167 contentVersions.add(contentVersion); 168 logger.info("Added contentVersion:" + contentVersion.getId()); 169 170 digitalAsset = new DigitalAssetImpl(); 171 digitalAsset.setValueObject(digitalAssetVO); 172 digitalAsset.setAssetBlob(is); 173 digitalAsset.setContentVersions(contentVersions); 174 175 db.create(digitalAsset); 176 177 contentVersion.getDigitalAssets().add(digitalAsset); 178 } 179 else if(entity.equalsIgnoreCase(UserProperties.class.getName())) 180 { 181 UserProperties userProperties = UserPropertiesController.getController().getUserPropertiesWithId(entityId, db); 182 Collection userPropertiesList = new ArrayList (); 183 userPropertiesList.add(userProperties); 184 logger.info("Added userProperties:" + userProperties.getId()); 185 186 digitalAsset = new DigitalAssetImpl(); 187 digitalAsset.setValueObject(digitalAssetVO); 188 digitalAsset.setAssetBlob(is); 189 digitalAsset.setUserProperties(userPropertiesList); 190 191 db.create(digitalAsset); 192 193 userProperties.getDigitalAssets().add(digitalAsset); 194 } 195 else if(entity.equalsIgnoreCase(RoleProperties.class.getName())) 196 { 197 RoleProperties roleProperties = RolePropertiesController.getController().getRolePropertiesWithId(entityId, db); 198 Collection rolePropertiesList = new ArrayList (); 199 rolePropertiesList.add(roleProperties); 200 logger.info("Added roleProperties:" + roleProperties.getId()); 201 202 digitalAsset = new DigitalAssetImpl(); 203 digitalAsset.setValueObject(digitalAssetVO); 204 digitalAsset.setAssetBlob(is); 205 digitalAsset.setRoleProperties(rolePropertiesList); 206 207 db.create(digitalAsset); 208 209 roleProperties.getDigitalAssets().add(digitalAsset); 210 } 211 else if(entity.equalsIgnoreCase(GroupProperties.class.getName())) 212 { 213 GroupProperties groupProperties = GroupPropertiesController.getController().getGroupPropertiesWithId(entityId, db); 214 Collection groupPropertiesList = new ArrayList (); 215 groupPropertiesList.add(groupProperties); 216 logger.info("Added groupProperties:" + groupProperties.getId()); 217 218 digitalAsset = new DigitalAssetImpl(); 219 digitalAsset.setValueObject(digitalAssetVO); 220 digitalAsset.setAssetBlob(is); 221 digitalAsset.setGroupProperties(groupPropertiesList); 222 223 db.create(digitalAsset); 224 225 groupProperties.getDigitalAssets().add(digitalAsset); 226 } 227 228 commitTransaction(db); 229 } 230 catch(Exception e) 231 { 232 logger.error("An error occurred so we should not complete the transaction:" + e, e); 233 rollbackTransaction(db); 234 throw new SystemException(e.getMessage()); 235 } 236 237 return digitalAsset.getValueObject(); 238 } 239 240 244 245 public DigitalAsset create(Database db, DigitalAssetVO digitalAssetVO, InputStream is) throws SystemException, Exception 246 { 247 DigitalAsset digitalAsset = new DigitalAssetImpl(); 248 digitalAsset.setValueObject(digitalAssetVO); 249 digitalAsset.setAssetBlob(is); 250 251 db.create(digitalAsset); 252 253 return digitalAsset; 254 } 255 256 260 261 public DigitalAssetVO createByCopy(Integer originalContentVersionId, String oldAssetKey, Integer newContentVersionId, String newAssetKey, Database db) throws ConstraintException, SystemException 262 { 263 logger.info("Creating by copying...."); 264 logger.info("originalContentVersionId:" + originalContentVersionId); 265 logger.info("oldAssetKey:" + oldAssetKey); 266 logger.info("newContentVersionId:" + newContentVersionId); 267 logger.info("newAssetKey:" + newAssetKey); 268 269 DigitalAsset oldDigitalAsset = getDigitalAsset(originalContentVersionId, oldAssetKey, db); 270 271 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(newContentVersionId, db); 272 Collection contentVersions = new ArrayList (); 273 contentVersions.add(contentVersion); 274 logger.info("Added contentVersion:" + contentVersion.getId()); 275 276 DigitalAssetVO digitalAssetVO = new DigitalAssetVO(); 277 digitalAssetVO.setAssetContentType(oldDigitalAsset.getAssetContentType()); 278 digitalAssetVO.setAssetFileName(oldDigitalAsset.getAssetFileName()); 279 digitalAssetVO.setAssetFilePath(oldDigitalAsset.getAssetFilePath()); 280 digitalAssetVO.setAssetFileSize(oldDigitalAsset.getAssetFileSize()); 281 digitalAssetVO.setAssetKey(newAssetKey); 282 283 DigitalAsset digitalAsset = new DigitalAssetImpl(); 284 digitalAsset.setValueObject(digitalAssetVO); 285 digitalAsset.setAssetBlob(oldDigitalAsset.getAssetBlob()); 286 digitalAsset.setContentVersions(contentVersions); 287 288 try 289 { 290 db.create(digitalAsset); 291 } 292 catch(Exception e) 293 { 294 logger.error("An error occurred so we should not complete the transaction:" + e, e); 295 throw new SystemException(e.getMessage()); 296 } 297 299 return digitalAsset.getValueObject(); 300 } 301 302 305 306 public DigitalAsset getDigitalAsset(Integer contentVersionId, String assetKey, Database db) throws SystemException 307 { 308 DigitalAsset digitalAsset = null; 309 310 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionId, db); 311 Collection digitalAssets = contentVersion.getDigitalAssets(); 312 Iterator assetIterator = digitalAssets.iterator(); 313 while(assetIterator.hasNext()) 314 { 315 DigitalAsset currentDigitalAsset = (DigitalAsset)assetIterator.next(); 316 if(currentDigitalAsset.getAssetKey().equals(assetKey)) 317 { 318 digitalAsset = currentDigitalAsset; 319 break; 320 } 321 } 322 323 return digitalAsset; 324 } 325 326 327 330 331 public static void delete(Integer digitalAssetId) throws ConstraintException, SystemException 332 { 333 deleteEntity(DigitalAssetImpl.class, digitalAssetId); 334 } 335 336 339 340 public void delete(Integer digitalAssetId, Database db) throws ConstraintException, SystemException 341 { 342 deleteEntity(DigitalAssetImpl.class, digitalAssetId, db); 343 } 344 345 348 349 public void delete(Integer digitalAssetId, String entity, Integer entityId) throws ConstraintException, SystemException 350 { 351 Database db = CastorDatabaseService.getDatabase(); 352 353 beginTransaction(db); 354 355 try 356 { 357 DigitalAsset digitalAsset = DigitalAssetController.getDigitalAssetWithId(digitalAssetId, db); 358 359 if(entity.equalsIgnoreCase("ContentVersion")) 360 ContentVersionController.getContentVersionController().deleteDigitalAssetRelation(entityId, digitalAsset, db); 361 else if(entity.equalsIgnoreCase(UserProperties.class.getName())) 362 UserPropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db); 363 else if(entity.equalsIgnoreCase(RoleProperties.class.getName())) 364 RolePropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db); 365 else if(entity.equalsIgnoreCase(GroupProperties.class.getName())) 366 GroupPropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db); 367 368 db.remove(digitalAsset); 369 370 commitTransaction(db); 371 } 372 catch(Exception e) 373 { 374 logger.error("An error occurred so we should not completes the transaction:" + e, e); 375 rollbackTransaction(db); 376 throw new SystemException(e.getMessage()); 377 } 378 } 379 380 381 384 385 public static void deleteCachedDigitalAssets(Integer digitalAssetId) throws SystemException, Exception 386 { 387 try 388 { 389 File assetDirectory = new File (CmsPropertyHandler.getDigitalAssetPath()); 390 File [] files = assetDirectory.listFiles(new FilenameFilterImpl(digitalAssetId.toString())); 391 for(int i=0; i<files.length; i++) 392 { 393 File file = files[i]; 394 file.delete(); 395 } 396 397 } 398 catch(Exception e) 399 { 400 logger.error("Could not delete the assets for the digitalAsset " + digitalAssetId + ":" + e.getMessage(), e); 401 } 402 } 403 404 407 408 public static DigitalAssetVO update(DigitalAssetVO digitalAssetVO, InputStream is) throws ConstraintException, SystemException 409 { 410 Database db = CastorDatabaseService.getDatabase(); 411 412 DigitalAsset digitalAsset = null; 413 414 beginTransaction(db); 415 416 try 417 { 418 digitalAsset = getDigitalAssetWithId(digitalAssetVO.getId(), db); 419 420 digitalAsset.setValueObject(digitalAssetVO); 421 if(is != null) 422 digitalAsset.setAssetBlob(is); 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 digitalAsset.getValueObject(); 434 } 435 436 480 481 484 485 public static List getDigitalAssetVOList(Integer contentVersionId) throws SystemException, Bug 486 { 487 Database db = CastorDatabaseService.getDatabase(); 488 489 List digitalAssetVOList = new ArrayList (); 490 491 beginTransaction(db); 492 493 try 494 { 495 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getReadOnlyContentVersionWithId(contentVersionId, db); 496 if(contentVersion != null) 497 { 498 Collection digitalAssets = contentVersion.getDigitalAssets(); 499 digitalAssetVOList = toVOList(digitalAssets); 500 } 501 502 commitTransaction(db); 503 } 504 catch(Exception e) 505 { 506 logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this contentVersion:" + e); 507 e.printStackTrace(); 508 rollbackTransaction(db); 509 throw new SystemException(e.getMessage()); 510 } 511 512 return digitalAssetVOList; 513 } 514 515 516 519 520 public static List getDigitalAssetVOList(Integer contentId, Integer languageId, boolean useLanguageFallback) throws SystemException, Bug 521 { 522 Database db = CastorDatabaseService.getDatabase(); 523 524 List digitalAssetVOList = null; 525 526 beginTransaction(db); 527 528 try 529 { 530 digitalAssetVOList = getDigitalAssetVOList(contentId, languageId, useLanguageFallback, db); 531 532 commitTransaction(db); 533 } 534 catch(Exception e) 535 { 536 e.printStackTrace(); 537 logger.info("An error occurred when we tried to cache and show the digital asset:" + e); 538 rollbackTransaction(db); 539 throw new SystemException(e.getMessage()); 540 } 541 542 return digitalAssetVOList; 543 } 544 545 548 549 public static List getDigitalAssetVOList(Integer contentId, Integer languageId, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception 550 { 551 List digitalAssetVOList = new ArrayList (); 552 553 Content content = ContentController.getContentController().getContentWithId(contentId, db); 554 logger.info("content:" + content.getName()); 555 logger.info("repositoryId:" + content.getRepository().getId()); 556 logger.info("languageId:" + languageId); 557 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db); 558 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db); 559 560 logger.info("contentVersion:" + contentVersion); 561 if(contentVersion != null) 562 { 563 Collection digitalAssets = contentVersion.getDigitalAssets(); 564 digitalAssetVOList = toModifiableVOList(digitalAssets); 565 566 logger.info("digitalAssetVOList:" + digitalAssetVOList.size()); 567 if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue()) 568 { 569 List masterDigitalAssetVOList = getDigitalAssetVOList(contentId, masterLanguageVO.getId(), useLanguageFallback, db); 570 Iterator digitalAssetVOListIterator = digitalAssetVOList.iterator(); 571 while(digitalAssetVOListIterator.hasNext()) 572 { 573 DigitalAssetVO currentDigitalAssetVO = (DigitalAssetVO)digitalAssetVOListIterator.next(); 574 575 Iterator masterDigitalAssetVOListIterator = masterDigitalAssetVOList.iterator(); 576 while(masterDigitalAssetVOListIterator.hasNext()) 577 { 578 DigitalAssetVO masterCurrentDigitalAssetVO = (DigitalAssetVO)masterDigitalAssetVOListIterator.next(); 579 if(currentDigitalAssetVO.getAssetKey().equalsIgnoreCase(masterCurrentDigitalAssetVO.getAssetKey())) 580 masterDigitalAssetVOListIterator.remove(); 581 } 582 } 583 digitalAssetVOList.addAll(masterDigitalAssetVOList); 584 } 585 } 586 else if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue()) 587 { 588 contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, masterLanguageVO.getId(), db); 589 590 logger.info("contentVersion:" + contentVersion); 591 if(contentVersion != null) 592 { 593 Collection digitalAssets = contentVersion.getDigitalAssets(); 594 digitalAssetVOList = toModifiableVOList(digitalAssets); 595 } 596 } 597 598 return digitalAssetVOList; 599 } 600 601 602 603 606 607 public static String getDigitalAssetUrl(Integer digitalAssetId) throws SystemException, Bug 608 { 609 Database db = CastorDatabaseService.getDatabase(); 610 611 String assetUrl = null; 612 613 beginTransaction(db); 614 615 try 616 { 617 DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetId, db); 618 619 if(digitalAsset != null) 620 { 621 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 622 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 623 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 625 dumpDigitalAsset(digitalAsset, fileName, filePath); 626 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 627 } 628 629 commitTransaction(db); 630 } 631 catch(Exception e) 632 { 633 logger.info("An error occurred when we tried to cache and show the digital asset:" + e); 634 e.printStackTrace(); 635 rollbackTransaction(db); 636 throw new SystemException(e.getMessage()); 637 } 638 639 return assetUrl; 640 } 641 642 646 647 public InputStream getAssetInputStream(DigitalAsset digitalAsset) throws Exception 648 { 649 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 650 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 651 dumpDigitalAsset(digitalAsset, fileName, filePath); 652 return new FileInputStream (filePath + File.separator + fileName); 653 } 654 655 658 659 public String getDigitalAssetUrl(DigitalAssetVO digitalAssetVO, Database db) throws SystemException, Bug, Exception 660 { 661 String assetUrl = null; 662 663 DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetVO.getId(), db); 664 665 if(digitalAsset != null) 666 { 667 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 668 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 669 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 670 dumpDigitalAsset(digitalAsset, fileName, filePath); 671 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 672 } 673 674 return assetUrl; 675 } 676 677 678 683 684 public static String getDigitalAssetThumbnailUrl(Integer digitalAssetId) throws SystemException, Bug 685 { 686 Database db = CastorDatabaseService.getDatabase(); 687 688 String assetUrl = null; 689 690 beginTransaction(db); 691 692 try 693 { 694 DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetId, db); 695 696 if(digitalAsset != null) 697 { 698 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 699 String contentType = digitalAsset.getAssetContentType(); 700 701 if(contentType.equalsIgnoreCase("image/gif") || contentType.equalsIgnoreCase("image/jpg") || contentType.equalsIgnoreCase("image/pjpeg") || contentType.equalsIgnoreCase("image/jpeg")) 702 { 703 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 704 logger.info("fileName:" + fileName); 705 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 706 logger.info("filePath:" + filePath); 707 String thumbnailFileName = digitalAsset.getDigitalAssetId() + "_thumbnail_" + digitalAsset.getAssetFileName(); 708 File thumbnailFile = new File (filePath + File.separator + thumbnailFileName); 710 if(!thumbnailFile.exists()) 711 { 712 logger.info("transforming..."); 713 ThumbnailGenerator tg = new ThumbnailGenerator(); 714 tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFileName, 75, 75, 100); 715 logger.info("transform done..."); 716 } 717 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName; 718 logger.info("assetUrl:" + assetUrl); 719 } 720 else 721 { 722 if(contentType.equalsIgnoreCase("application/pdf")) 723 { 724 assetUrl = "images/pdf.gif"; 725 } 726 else if(contentType.equalsIgnoreCase("application/msword")) 727 { 728 assetUrl = "images/msword.gif"; 729 } 730 else if(contentType.equalsIgnoreCase("application/vnd.ms-excel")) 731 { 732 assetUrl = "images/msexcel.gif"; 733 } 734 else if(contentType.equalsIgnoreCase("application/vnd.ms-powerpoint")) 735 { 736 assetUrl = "images/mspowerpoint.gif"; 737 } 738 else 739 { 740 assetUrl = "images/digitalAsset.gif"; 741 } 742 } 743 } 744 745 commitTransaction(db); 746 } 747 catch(Exception e) 748 { 749 logger.info("An error occurred when we tried to cache and show the digital asset thumbnail:" + e); 750 rollbackTransaction(db); 751 throw new SystemException(e.getMessage()); 752 } 753 754 return assetUrl; 755 } 756 757 758 761 762 public static String getDigitalAssetUrl(Integer contentId, Integer languageId) throws SystemException, Bug 763 { 764 Database db = CastorDatabaseService.getDatabase(); 765 766 String assetUrl = null; 767 768 beginTransaction(db); 769 770 try 771 { 772 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestContentVersion(contentId, languageId, db); 773 if(contentVersion != null) 774 { 775 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion); 776 777 if(digitalAsset != null) 778 { 779 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 780 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 781 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 783 784 dumpDigitalAsset(digitalAsset, fileName, filePath); 785 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 786 } 787 } 788 789 commitTransaction(db); 790 } 791 catch(Exception e) 792 { 793 logger.info("An error occurred when we tried to cache and show the digital asset:" + e); 794 e.printStackTrace(); 795 rollbackTransaction(db); 796 throw new SystemException(e.getMessage()); 797 } 798 799 return assetUrl; 800 } 801 802 805 806 public static String getDigitalAssetUrl(Integer contentId, Integer languageId, String assetKey, boolean useLanguageFallback) throws SystemException, Bug 807 { 808 Database db = CastorDatabaseService.getDatabase(); 809 810 String assetUrl = null; 811 812 beginTransaction(db); 813 814 try 815 { 816 assetUrl = getDigitalAssetUrl(contentId, languageId, assetKey, useLanguageFallback, db); 817 818 commitTransaction(db); 819 } 820 catch(Exception e) 821 { 822 logger.info("An error occurred when we tried to cache and show the digital asset:" + e); 823 e.printStackTrace(); 824 rollbackTransaction(db); 825 throw new SystemException(e.getMessage()); 826 } 827 828 return assetUrl; 829 } 830 831 834 835 public static String getDigitalAssetUrl(Integer contentId, Integer languageId, String assetKey, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception 836 { 837 String assetUrl = null; 838 839 Content content = ContentController.getContentController().getContentWithId(contentId, db); 840 logger.info("content:" + content.getName()); 841 logger.info("repositoryId:" + content.getRepository().getId()); 842 logger.info("languageId:" + languageId); 843 logger.info("assetKey:" + assetKey); 844 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db); 845 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db); 846 logger.info("contentVersion:" + contentVersion); 847 if(contentVersion != null) 848 { 849 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey); 850 logger.info("digitalAsset:" + digitalAsset); 851 if(digitalAsset != null) 852 { 853 logger.info("digitalAsset:" + digitalAsset.getAssetKey()); 854 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 855 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 856 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 857 858 dumpDigitalAsset(digitalAsset, fileName, filePath); 859 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 860 } 861 else 862 { 863 if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue()) 865 return getDigitalAssetUrl(contentId, masterLanguageVO.getId(), assetKey, useLanguageFallback, db); 866 } 867 } 868 else if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue()) 869 { 870 contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, masterLanguageVO.getId(), db); 871 872 logger.info("contentVersion:" + contentVersion); 873 if(contentVersion != null) 874 { 875 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey); 876 logger.info("digitalAsset:" + digitalAsset); 877 if(digitalAsset != null) 878 { 879 logger.info("digitalAsset:" + digitalAsset.getAssetKey()); 880 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 881 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 882 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 883 884 dumpDigitalAsset(digitalAsset, fileName, filePath); 885 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 886 } 887 } 888 } 889 890 return assetUrl; 891 } 892 893 896 897 public static DigitalAssetVO getDigitalAssetVO(Integer contentId, Integer languageId, String assetKey, boolean useLanguageFallback) throws SystemException, Bug 898 { 899 Database db = CastorDatabaseService.getDatabase(); 900 901 DigitalAssetVO digitalAssetVO = null; 902 903 beginTransaction(db); 904 905 try 906 { 907 digitalAssetVO = getDigitalAssetVO(contentId, languageId, assetKey, useLanguageFallback, db); 908 909 commitTransaction(db); 910 } 911 catch(Exception e) 912 { 913 logger.info("An error occurred when we tried to get a digitalAssetVO:" + e); 914 e.printStackTrace(); 915 rollbackTransaction(db); 916 throw new SystemException(e.getMessage()); 917 } 918 919 return digitalAssetVO; 920 } 921 922 925 926 public static DigitalAssetVO getDigitalAssetVO(Integer contentId, Integer languageId, String assetKey, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception 927 { 928 DigitalAssetVO digitalAssetVO = null; 929 930 Content content = ContentController.getContentController().getContentWithId(contentId, db); 931 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db); 932 if(contentVersion != null) 933 { 934 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey); 935 if(digitalAsset != null) 936 { 937 digitalAssetVO = digitalAsset.getValueObject(); 938 } 939 else 940 { 941 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db); 942 if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue()) 943 return getDigitalAssetVO(contentId, masterLanguageVO.getId(), assetKey, useLanguageFallback, db); 944 } 945 } 946 947 return digitalAssetVO; 948 } 949 950 951 952 957 958 public static String getDigitalAssetThumbnailUrl(Integer contentId, Integer languageId) throws SystemException, Bug 959 { 960 Database db = CastorDatabaseService.getDatabase(); 961 962 String assetUrl = null; 963 964 beginTransaction(db); 965 966 try 967 { 968 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestContentVersion(contentId, languageId, db); 969 if(contentVersion != null) 970 { 971 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion); 972 973 if(digitalAsset != null) 974 { 975 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName()); 976 String contentType = digitalAsset.getAssetContentType(); 977 978 if(contentType.equalsIgnoreCase("image/gif") || contentType.equalsIgnoreCase("image/jpg")) 979 { 980 String fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName(); 981 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 983 String thumbnailFileName = digitalAsset.getDigitalAssetId() + "_thumbnail_" + digitalAsset.getAssetFileName(); 984 File thumbnailFile = new File (filePath + File.separator + thumbnailFileName); 986 if(!thumbnailFile.exists()) 987 { 988 ThumbnailGenerator tg = new ThumbnailGenerator(); 989 tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFileName, 150, 150, 100); 990 } 991 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName; 992 } 993 else 994 { 995 if(contentType.equalsIgnoreCase("application/pdf")) 996 { 997 assetUrl = "images/pdf.gif"; 998 } 999 else if(contentType.equalsIgnoreCase("application/msword")) 1000 { 1001 assetUrl = "images/msword.gif"; 1002 } 1003 else if(contentType.equalsIgnoreCase("application/vnd.ms-excel")) 1004 { 1005 assetUrl = "images/msexcel.gif"; 1006 } 1007 else if(contentType.equalsIgnoreCase("application/vnd.ms-powerpoint")) 1008 { 1009 assetUrl = "images/mspowerpoint.gif"; 1010 } 1011 else 1012 { 1013 assetUrl = "images/digitalAsset.gif"; 1014 } 1015 } 1016 } 1017 else 1018 { 1019 assetUrl = "images/notDefined.gif"; 1020 } 1021 } 1022 1023 commitTransaction(db); 1024 } 1025 catch(Exception e) 1026 { 1027 logger.info("An error occurred when we tried to cache and show the digital asset thumbnail:" + e); 1028 rollbackTransaction(db); 1029 throw new SystemException(e.getMessage()); 1030 } 1031 1032 return assetUrl; 1033 } 1034 1035 1038 1039 private static DigitalAsset getLatestDigitalAsset(ContentVersion contentVersion) 1040 { 1041 Collection digitalAssets = contentVersion.getDigitalAssets(); 1042 Iterator iterator = digitalAssets.iterator(); 1043 1044 DigitalAsset digitalAsset = null; 1045 while(iterator.hasNext()) 1046 { 1047 DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next(); 1048 if(digitalAsset == null || currentDigitalAsset.getDigitalAssetId().intValue() > digitalAsset.getDigitalAssetId().intValue()) 1049 digitalAsset = currentDigitalAsset; 1050 } 1051 return digitalAsset; 1052 } 1053 1054 1057 1058 private static DigitalAsset getLatestDigitalAsset(ContentVersion contentVersion, String assetKey) 1059 { 1060 Collection digitalAssets = contentVersion.getDigitalAssets(); 1061 Iterator iterator = digitalAssets.iterator(); 1062 1063 DigitalAsset digitalAsset = null; 1064 while(iterator.hasNext()) 1065 { 1066 DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next(); 1067 if((digitalAsset == null || currentDigitalAsset.getDigitalAssetId().intValue() > digitalAsset.getDigitalAssetId().intValue()) && currentDigitalAsset.getAssetKey().equalsIgnoreCase(assetKey)) 1068 digitalAsset = currentDigitalAsset; 1069 } 1070 return digitalAsset; 1071 } 1072 1073 1074 1079 1080 public static void dumpDigitalAsset(DigitalAsset digitalAsset, String fileName, String filePath) throws Exception 1081 { 1082 long timer = System.currentTimeMillis(); 1083 1084 File outputFile = new File (filePath + File.separator + fileName); 1085 if(outputFile.exists()) 1086 { 1087 logger.info("The file allready exists so we don't need to dump it again.."); 1088 return; 1089 } 1090 1091 FileOutputStream fis = new FileOutputStream (outputFile); 1092 BufferedOutputStream bos = new BufferedOutputStream (fis); 1093 1094 BufferedInputStream bis = new BufferedInputStream (digitalAsset.getAssetBlob()); 1095 1096 int character; 1097 while ((character = bis.read()) != -1) 1098 { 1099 bos.write(character); 1100 } 1101 bos.flush(); 1102 1103 bis.close(); 1104 fis.close(); 1105 bos.close(); 1106 logger.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer)); 1107 } 1108 1109 1113 1114 public BaseEntityVO getNewVO() 1115 { 1116 return new DigitalAssetVO(); 1117 } 1118 1119} 1120 1121class FilenameFilterImpl implements FilenameFilter 1122{ 1123 private String filter = "."; 1124 1125 public FilenameFilterImpl(String aFilter) 1126 { 1127 filter = aFilter; 1128 } 1129 1130 public boolean accept(File dir, String name) 1131 { 1132 return name.startsWith(filter); 1133 } 1134}; 1135 | Popular Tags |