001 /** 002 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portlet.documentlibrary.service.impl; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream; 020 import com.liferay.portal.kernel.repository.LocalRepository; 021 import com.liferay.portal.kernel.repository.model.FileEntry; 022 import com.liferay.portal.kernel.repository.model.FileVersion; 023 import com.liferay.portal.kernel.repository.model.Folder; 024 import com.liferay.portal.kernel.util.ContentTypes; 025 import com.liferay.portal.kernel.util.FileUtil; 026 import com.liferay.portal.kernel.util.MimeTypesUtil; 027 import com.liferay.portal.kernel.util.StringBundler; 028 import com.liferay.portal.kernel.util.StringPool; 029 import com.liferay.portal.kernel.util.Validator; 030 import com.liferay.portal.kernel.workflow.WorkflowConstants; 031 import com.liferay.portal.model.Repository; 032 import com.liferay.portal.repository.liferayrepository.model.LiferayFolder; 033 import com.liferay.portal.service.ServiceContext; 034 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException; 035 import com.liferay.portlet.documentlibrary.model.DLFileEntryType; 036 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants; 037 import com.liferay.portlet.documentlibrary.model.DLFileRank; 038 import com.liferay.portlet.documentlibrary.model.DLFileShortcut; 039 import com.liferay.portlet.documentlibrary.model.DLFolder; 040 import com.liferay.portlet.documentlibrary.model.DLFolderConstants; 041 import com.liferay.portlet.documentlibrary.service.base.DLAppLocalServiceBaseImpl; 042 import com.liferay.portlet.documentlibrary.util.DLAppUtil; 043 import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil; 044 045 import java.io.File; 046 import java.io.IOException; 047 import java.io.InputStream; 048 049 import java.util.List; 050 051 /** 052 * The document library local service. All portlets should interact with the 053 * document library through this class or through {@link DLAppServiceImpl}, 054 * rather than through the individual document library service classes. 055 * 056 * <p> 057 * This class provides a unified interface to all Liferay and third party 058 * repositories. While the method signatures are universal for all repositories. 059 * Additional implementation-specific parameters may be specified in the 060 * serviceContext. 061 * </p> 062 * 063 * <p> 064 * The <code>repositoryId</code> parameter used by most of the methods is the 065 * primary key of the specific repository. If the repository is a default 066 * Liferay repository, the <code>repositoryId</code> is the <code>groupId</code> 067 * or <code>scopeGroupId</code>. Otherwise, the <code>repositoryId</code> will 068 * correspond to values obtained from {@link 069 * com.liferay.portal.service.RepositoryLocalServiceUtil}. 070 * </p> 071 * 072 * @author Alexander Chow 073 * @author Mika Koivisto 074 * @see DLAppServiceImpl 075 */ 076 public class DLAppLocalServiceImpl extends DLAppLocalServiceBaseImpl { 077 078 /** 079 * Adds a file entry and associated metadata based on a byte array. 080 * 081 * <p> 082 * This method takes two file names, the <code>sourceFileName</code> and the 083 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 084 * name of the actual file being uploaded. The <code>title</code> 085 * corresponds to a name the client wishes to assign this file after it has 086 * been uploaded to the portal. If it is <code>null</code>, the <code> 087 * sourceFileName</code> will be used. 088 * </p> 089 * 090 * @param userId the primary key of the file entry's creator/owner 091 * @param repositoryId the primary key of the file entry's repository 092 * @param folderId the primary key of the file entry's parent folder 093 * @param sourceFileName the original file's name 094 * @param mimeType the file's MIME type 095 * @param title the name to be assigned to the file (optionally <code>null 096 * </code>) 097 * @param description the file's description 098 * @param changeLog the file's version change log 099 * @param bytes the file's data (optionally <code>null</code>) 100 * @param serviceContext the service context to be applied. Can set the 101 * asset category IDs, asset tag names, and expando bridge 102 * attributes for the file entry. In a Liferay repository, it may 103 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 104 * type </li> <li> fieldsMap - mapping for fields associated with a 105 * custom file entry type </li> </ul> 106 * @return the file entry 107 * @throws PortalException if the parent folder could not be found or if the 108 * file entry's information was invalid 109 * @throws SystemException if a system exception occurred 110 */ 111 public FileEntry addFileEntry( 112 long userId, long repositoryId, long folderId, 113 String sourceFileName, String mimeType, String title, 114 String description, String changeLog, byte[] bytes, 115 ServiceContext serviceContext) 116 throws PortalException, SystemException { 117 118 File file = null; 119 120 try { 121 if ((bytes != null) && (bytes.length > 0)) { 122 file = FileUtil.createTempFile(bytes); 123 } 124 125 return addFileEntry( 126 userId, repositoryId, folderId, sourceFileName, mimeType, title, 127 description, changeLog, file, serviceContext); 128 } 129 catch (IOException ioe) { 130 throw new SystemException("Unable to write temporary file", ioe); 131 } 132 finally { 133 FileUtil.delete(file); 134 } 135 } 136 137 /** 138 * Adds a file entry and associated metadata based on a {@link java.io.File} 139 * object. 140 * 141 * <p> 142 * This method takes two file names, the <code>sourceFileName</code> and the 143 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 144 * name of the actual file being uploaded. The <code>title</code> 145 * corresponds to a name the client wishes to assign this file after it has 146 * been uploaded to the portal. If it is <code>null</code>, the <code> 147 * sourceFileName</code> will be used. 148 * </p> 149 * 150 * @param userId the primary key of the file entry's creator/owner 151 * @param repositoryId the primary key of the repository 152 * @param folderId the primary key of the file entry's parent folder 153 * @param sourceFileName the original file's name 154 * @param mimeType the file's MIME type 155 * @param title the name to be assigned to the file (optionally <code>null 156 * </code>) 157 * @param description the file's description 158 * @param changeLog the file's version change log 159 * @param file the file's data (optionally <code>null</code>) 160 * @param serviceContext the service context to be applied. Can set the 161 * asset category IDs, asset tag names, and expando bridge 162 * attributes for the file entry. In a Liferay repository, it may 163 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 164 * type </li> <li> fieldsMap - mapping for fields associated with a 165 * custom file entry type </li> </ul> 166 * @return the file entry 167 * @throws PortalException if the parent folder could not be found or if the 168 * file entry's information was invalid 169 * @throws SystemException if a system exception occurred 170 */ 171 public FileEntry addFileEntry( 172 long userId, long repositoryId, long folderId, 173 String sourceFileName, String mimeType, String title, 174 String description, String changeLog, File file, 175 ServiceContext serviceContext) 176 throws PortalException, SystemException { 177 178 if ((file == null) || !file.exists() || (file.length() == 0)) { 179 return addFileEntry( 180 userId, repositoryId, folderId, sourceFileName, mimeType, title, 181 description, changeLog, null, 0, serviceContext); 182 } 183 184 mimeType = DLAppUtil.getMimeType(sourceFileName, mimeType, title, file); 185 186 LocalRepository localRepository = getLocalRepository(repositoryId); 187 188 FileEntry fileEntry = localRepository.addFileEntry( 189 userId, folderId, sourceFileName, mimeType, title, description, 190 changeLog, file, serviceContext); 191 192 dlAppHelperLocalService.addFileEntry( 193 userId, fileEntry, fileEntry.getFileVersion(), serviceContext); 194 195 return fileEntry; 196 } 197 198 /** 199 * Adds a file entry and associated metadata based on an {@link 200 * java.io.InputStream} object. 201 * 202 * <p> 203 * This method takes two file names, the <code>sourceFileName</code> and the 204 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 205 * name of the actual file being uploaded. The <code>title</code> 206 * corresponds to a name the client wishes to assign this file after it has 207 * been uploaded to the portal. If it is <code>null</code>, the <code> 208 * sourceFileName</code> will be used. 209 * </p> 210 * 211 * @param userId the primary key of the file entry's creator/owner 212 * @param repositoryId the primary key of the repository 213 * @param folderId the primary key of the file entry's parent folder 214 * @param sourceFileName the original file's name 215 * @param mimeType the file's MIME type 216 * @param title the name to be assigned to the file (optionally <code>null 217 * </code>) 218 * @param description the file's description 219 * @param changeLog the file's version change log 220 * @param is the file's data (optionally <code>null</code>) 221 * @param size the file's size (optionally <code>0</code>) 222 * @param serviceContext the service context to be applied. Can set the 223 * asset category IDs, asset tag names, and expando bridge 224 * attributes for the file entry. In a Liferay repository, it may 225 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 226 * type </li> <li> fieldsMap - mapping for fields associated with a 227 * custom file entry type </li> </ul> 228 * @return the file entry 229 * @throws PortalException if the parent folder could not be found or if the 230 * file entry's information was invalid 231 * @throws SystemException if a system exception occurred 232 */ 233 public FileEntry addFileEntry( 234 long userId, long repositoryId, long folderId, 235 String sourceFileName, String mimeType, String title, 236 String description, String changeLog, InputStream is, long size, 237 ServiceContext serviceContext) 238 throws PortalException, SystemException { 239 240 if (is == null) { 241 is = new UnsyncByteArrayInputStream(new byte[0]); 242 size = 0; 243 } 244 245 if (Validator.isNull(mimeType) || 246 mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) { 247 248 String extension = DLAppUtil.getExtension(title, sourceFileName); 249 250 if (size == 0) { 251 mimeType = MimeTypesUtil.getExtensionContentType(extension); 252 } 253 else { 254 File file = null; 255 256 try { 257 file = FileUtil.createTempFile(is); 258 259 return addFileEntry( 260 userId, repositoryId, folderId, sourceFileName, 261 mimeType, title, description, changeLog, file, 262 serviceContext); 263 } 264 catch (IOException ioe) { 265 throw new SystemException( 266 "Unable to write temporary file", ioe); 267 } 268 finally { 269 FileUtil.delete(file); 270 } 271 } 272 } 273 274 LocalRepository localRepository = getLocalRepository(repositoryId); 275 276 FileEntry fileEntry = localRepository.addFileEntry( 277 userId, folderId, sourceFileName, mimeType, title, description, 278 changeLog, is, size, serviceContext); 279 280 dlAppHelperLocalService.addFileEntry( 281 userId, fileEntry, fileEntry.getFileVersion(), serviceContext); 282 283 return fileEntry; 284 } 285 286 /** 287 * Adds the file rank to the existing file entry. This method is only 288 * supported by the Liferay repository. 289 * 290 * @param repositoryId the primary key of the repository 291 * @param companyId the primary key of the company 292 * @param userId the primary key of the file rank's creator/owner 293 * @param fileEntryId the primary key of the file entry 294 * @param serviceContext the service context to be applied 295 * @return the file rank 296 * @throws PortalException if a portal exception occurred 297 * @throws SystemException if a system exception occurred 298 */ 299 public DLFileRank addFileRank( 300 long repositoryId, long companyId, long userId, long fileEntryId, 301 ServiceContext serviceContext) 302 throws PortalException, SystemException { 303 304 return dlFileRankLocalService.addFileRank( 305 repositoryId, companyId, userId, fileEntryId, serviceContext); 306 } 307 308 /** 309 * Adds the file shortcut to the existing file entry. This method is only 310 * supported by the Liferay repository. 311 * 312 * @param userId the primary key of the file shortcut's creator/owner 313 * @param repositoryId the primary key of the repository 314 * @param folderId the primary key of the file shortcut's parent folder 315 * @param toFileEntryId the primary key of the file entry to point to 316 * @param serviceContext the service context to be applied. Can set the 317 * asset category IDs, asset tag names, and expando bridge 318 * attributes for the file entry. 319 * @return the file shortcut 320 * @throws PortalException if the parent folder or file entry could not be 321 * found, or if the file shortcut's information was invalid 322 * @throws SystemException if a system exception occurred 323 */ 324 public DLFileShortcut addFileShortcut( 325 long userId, long repositoryId, long folderId, long toFileEntryId, 326 ServiceContext serviceContext) 327 throws PortalException, SystemException { 328 329 return dlFileShortcutLocalService.addFileShortcut( 330 userId, repositoryId, folderId, toFileEntryId, serviceContext); 331 } 332 333 /** 334 * Adds a folder. 335 * 336 * @param userId the primary key of the folder's creator/owner 337 * @param repositoryId the primary key of the repository 338 * @param parentFolderId the primary key of the folder's parent folder 339 * @param name the folder's name 340 * @param description the folder's description 341 * @param serviceContext the service context to be applied. In a Liferay 342 * repository, it may include mountPoint which is a boolean 343 * specifying whether the folder is a facade for mounting a 344 * third-party repository 345 * @return the folder 346 * @throws PortalException if the parent folder could not be found or if the 347 * new folder's information was invalid 348 * @throws SystemException if a system exception occurred 349 */ 350 public Folder addFolder( 351 long userId, long repositoryId, long parentFolderId, String name, 352 String description, ServiceContext serviceContext) 353 throws PortalException, SystemException { 354 355 LocalRepository localRepository = getLocalRepository(repositoryId); 356 357 return localRepository.addFolder( 358 userId, parentFolderId, name, description, serviceContext); 359 } 360 361 /** 362 * Delete all data associated to the given repository. This method is only 363 * supported by the Liferay repository. 364 * 365 * @param repositoryId the primary key of the data's repository 366 * @throws PortalException if the repository could not be found 367 * @throws SystemException if a system exception occurred 368 */ 369 public void deleteAll(long repositoryId) 370 throws PortalException, SystemException { 371 372 LocalRepository localRepository = getLocalRepository(repositoryId); 373 374 localRepository.deleteAll(); 375 } 376 377 /** 378 * Deletes the file entry. 379 * 380 * @param fileEntryId the primary key of the file entry 381 * @throws PortalException if the file entry could not be found 382 * @throws SystemException if a system exception occurred 383 */ 384 public void deleteFileEntry(long fileEntryId) 385 throws PortalException, SystemException { 386 387 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 388 389 FileEntry fileEntry = localRepository.getFileEntry(fileEntryId); 390 391 localRepository.deleteFileEntry(fileEntryId); 392 393 dlAppHelperLocalService.deleteFileEntry(fileEntry); 394 } 395 396 /** 397 * Deletes the file ranks associated to a given file entry. This method is 398 * only supported by the Liferay repository. 399 * 400 * @param fileEntryId the primary key of the file entry 401 * @throws SystemException if a system exception occurred 402 */ 403 public void deleteFileRanksByFileEntryId(long fileEntryId) 404 throws SystemException { 405 406 dlFileRankLocalService.deleteFileRanksByFileEntryId(fileEntryId); 407 } 408 409 /** 410 * Deletes the file ranks associated to a given user. This method is only 411 * supported by the Liferay repository. 412 * 413 * @param userId the primary key of the user 414 * @throws SystemException if a system exception occurred 415 */ 416 public void deleteFileRanksByUserId(long userId) throws SystemException { 417 dlFileRankLocalService.deleteFileRanksByUserId(userId); 418 } 419 420 /** 421 * Deletes the file shortcut. This method is only supported by the Liferay 422 * repository. 423 * 424 * @param dlFileShortcut the file shortcut 425 * @throws PortalException if the file shortcut could not be found 426 * @throws SystemException if a system exception occurred 427 */ 428 public void deleteFileShortcut(DLFileShortcut dlFileShortcut) 429 throws PortalException, SystemException { 430 431 dlFileShortcutLocalService.deleteFileShortcut(dlFileShortcut); 432 } 433 434 /** 435 * Deletes the file shortcut. This method is only supported by the Liferay 436 * repository. 437 * 438 * @param fileShortcutId the primary key of the file shortcut 439 * @throws PortalException if the file shortcut could not be found 440 * @throws SystemException if a system exception occurred 441 */ 442 public void deleteFileShortcut(long fileShortcutId) 443 throws PortalException, SystemException { 444 445 dlFileShortcutLocalService.deleteDLFileShortcut(fileShortcutId); 446 } 447 448 /** 449 * Deletes all file shortcuts associated to the file entry. This method is 450 * only supported by the Liferay repository. 451 * 452 * @param toFileEntryId the primary key of the associated file entry 453 * @throws PortalException if the file shortcut for the file entry could not 454 * be found 455 * @throws SystemException if a system exception occurred 456 */ 457 public void deleteFileShortcuts(long toFileEntryId) 458 throws PortalException, SystemException { 459 460 dlFileShortcutLocalService.deleteFileShortcuts(toFileEntryId); 461 } 462 463 /** 464 * Deletes the folder and all of its subfolders and file entries. 465 * 466 * @param folderId the primary key of the folder 467 * @throws PortalException if the folder could not be found 468 * @throws SystemException if a system exception occurred 469 */ 470 public void deleteFolder(long folderId) 471 throws PortalException, SystemException { 472 473 LocalRepository localRepository = getLocalRepository(folderId, 0, 0); 474 475 localRepository.deleteFolder(folderId); 476 } 477 478 /** 479 * Returns the file entry with the primary key. 480 * 481 * @param fileEntryId the primary key of the file entry 482 * @return the file entry with the primary key 483 * @throws PortalException if the file entry could not be found 484 * @throws SystemException if a system exception occurred 485 */ 486 public FileEntry getFileEntry(long fileEntryId) 487 throws PortalException, SystemException { 488 489 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 490 491 return localRepository.getFileEntry(fileEntryId); 492 } 493 494 /** 495 * Returns the file entry with the title in the folder. 496 * 497 * @param groupId the primary key of the file entry's group 498 * @param folderId the primary key of the file entry's folder 499 * @param title the file entry's title 500 * @return the file entry with the title in the folder 501 * @throws PortalException if the file entry could not be found 502 * @throws SystemException if a system exception occurred 503 */ 504 public FileEntry getFileEntry(long groupId, long folderId, String title) 505 throws PortalException, SystemException { 506 507 try { 508 LocalRepository localRepository = getLocalRepository(groupId); 509 510 return localRepository.getFileEntry(folderId, title); 511 } 512 catch (NoSuchFileEntryException nsfee) { 513 } 514 515 LocalRepository localRepository = getLocalRepository(folderId, 0, 0); 516 517 return localRepository.getFileEntry(folderId, title); 518 } 519 520 /** 521 * Returns the file entry with the UUID and group. 522 * 523 * @param uuid the file entry's UUID 524 * @param groupId the primary key of the file entry's group 525 * @return the file entry with the UUID and group 526 * @throws PortalException if the file entry could not be found 527 * @throws SystemException if a system exception occurred 528 */ 529 public FileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId) 530 throws PortalException, SystemException { 531 532 try { 533 LocalRepository localRepository = getLocalRepository(groupId); 534 535 return localRepository.getFileEntryByUuid(uuid); 536 } 537 catch (NoSuchFileEntryException nsfee) { 538 } 539 540 List<com.liferay.portal.model.Repository> repositories = 541 repositoryPersistence.findByGroupId(groupId); 542 543 for (Repository repository : repositories) { 544 try { 545 LocalRepository localRepository = getLocalRepository( 546 repository.getRepositoryId()); 547 548 return localRepository.getFileEntryByUuid(uuid); 549 } 550 catch (NoSuchFileEntryException nsfee) { 551 } 552 } 553 554 StringBundler msg = new StringBundler(6); 555 556 msg.append("No DLFileEntry exists with the key {"); 557 msg.append("uuid="); 558 msg.append(uuid); 559 msg.append(", groupId="); 560 msg.append(groupId); 561 msg.append(StringPool.CLOSE_CURLY_BRACE); 562 563 throw new NoSuchFileEntryException(msg.toString()); 564 } 565 566 /** 567 * Returns the file ranks from the user. This method is only supported by 568 * the Liferay repository. 569 * 570 * @param repositoryId the primary key of the repository 571 * @param userId the primary key of the user 572 * @return the file ranks from the user 573 * @throws SystemException if a system exception occurred 574 */ 575 public List<DLFileRank> getFileRanks(long repositoryId, long userId) 576 throws SystemException { 577 578 return dlFileRankLocalService.getFileRanks(repositoryId, userId); 579 } 580 581 /** 582 * Returns the file shortcut with the primary key. This method is only 583 * supported by the Liferay repository. 584 * 585 * @param fileShortcutId the primary key of the file shortcut 586 * @return the file shortcut with the primary key 587 * @throws PortalException if the file shortcut could not be found 588 * @throws SystemException if a system exception occurred 589 */ 590 public DLFileShortcut getFileShortcut(long fileShortcutId) 591 throws PortalException, SystemException { 592 593 return dlFileShortcutLocalService.getFileShortcut(fileShortcutId); 594 } 595 596 /** 597 * Returns the file version with the primary key. 598 * 599 * @param fileVersionId the primary key of the file version 600 * @return the file version with the primary key 601 * @throws PortalException if the file version could not be found 602 * @throws SystemException if a system exception occurred 603 */ 604 public FileVersion getFileVersion(long fileVersionId) 605 throws PortalException, SystemException { 606 607 LocalRepository localRepository = getLocalRepository( 608 0, 0, fileVersionId); 609 610 return localRepository.getFileVersion(fileVersionId); 611 } 612 613 /** 614 * Returns the folder with the primary key. 615 * 616 * @param folderId the primary key of the folder 617 * @return the folder with the primary key 618 * @throws PortalException if the folder could not be found 619 * @throws SystemException if a system exception occurred 620 */ 621 public Folder getFolder(long folderId) 622 throws PortalException, SystemException { 623 624 LocalRepository localRepository = getLocalRepository(folderId, 0, 0); 625 626 return localRepository.getFolder(folderId); 627 } 628 629 /** 630 * Returns the folder with the name in the parent folder. 631 * 632 * @param repositoryId the primary key of the folder's repository 633 * @param parentFolderId the primary key of the folder's parent folder 634 * @param name the folder's name 635 * @return the folder with the name in the parent folder 636 * @throws PortalException if the folder could not be found 637 * @throws SystemException if a system exception occurred 638 */ 639 public Folder getFolder(long repositoryId, long parentFolderId, String name) 640 throws PortalException, SystemException { 641 642 LocalRepository localRepository = getLocalRepository(repositoryId); 643 644 return localRepository.getFolder(parentFolderId, name); 645 } 646 647 /** 648 * Returns the mount folder of the repository with the primary key. This 649 * method is only supported by the Liferay repository. 650 * 651 * @param repositoryId the primary key of the repository 652 * @return the folder used for mounting third-party repositories 653 * @throws PortalException if the repository or mount folder could not be 654 * found 655 * @throws SystemException if a system exception occurred 656 */ 657 public Folder getMountFolder(long repositoryId) 658 throws PortalException, SystemException { 659 660 DLFolder dlFolder = dlFolderLocalService.getMountFolder(repositoryId); 661 662 return new LiferayFolder(dlFolder); 663 } 664 665 /** 666 * Moves the file entry to the new folder. 667 * 668 * @param userId the primary key of the user 669 * @param fileEntryId the primary key of the file entry 670 * @param newFolderId the primary key of the new folder 671 * @param serviceContext the service context to be applied 672 * @return the file entry 673 * @throws PortalException if the file entry or the new folder could not be 674 * found 675 * @throws SystemException if a system exception occurred 676 */ 677 public FileEntry moveFileEntry( 678 long userId, long fileEntryId, long newFolderId, 679 ServiceContext serviceContext) 680 throws PortalException, SystemException { 681 682 LocalRepository fromLocalRepository = getLocalRepository( 683 0, fileEntryId, 0); 684 LocalRepository toLocalRepository = getLocalRepository( 685 newFolderId, serviceContext); 686 687 if (fromLocalRepository.getRepositoryId() == 688 toLocalRepository.getRepositoryId()) { 689 690 // Move file entries within repository 691 692 return fromLocalRepository.moveFileEntry( 693 userId, fileEntryId, newFolderId, serviceContext); 694 } 695 696 // Move file entries between repositories 697 698 return moveFileEntries( 699 userId, fileEntryId, newFolderId, fromLocalRepository, 700 toLocalRepository, serviceContext); 701 } 702 703 /** 704 * Moves the file entry with the primary key to the trash portlet. 705 * 706 * @param userId the primary key of the user 707 * @param fileEntryId the primary key of the file entry 708 * @throws PortalException if the file entry could not be found 709 * @throws SystemException if a system exception occurred 710 */ 711 public FileEntry moveFileEntryToTrash(long userId, long fileEntryId) 712 throws PortalException, SystemException { 713 714 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 715 716 FileEntry fileEntry = localRepository.getFileEntry(fileEntryId); 717 718 return dlAppHelperLocalService.moveFileEntryToTrash(userId, fileEntry); 719 } 720 721 /** 722 * Restores the file entry with the primary key from the trash portlet. 723 * 724 * @param userId the primary key of the user 725 * @param fileEntryId the primary key of the file entry 726 * @throws PortalException if the file entry could not be found 727 * @throws SystemException if a system exception occurred 728 */ 729 public void restoreFileEntryFromTrash(long userId, long fileEntryId) 730 throws PortalException, SystemException { 731 732 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 733 734 FileEntry fileEntry = localRepository.getFileEntry(fileEntryId); 735 736 dlAppHelperLocalService.restoreFileEntryFromTrash(userId, fileEntry); 737 } 738 739 /** 740 * Subscribe the user to changes in documents of the file entry type. This 741 * method is only supported by the Liferay repository. 742 * 743 * @param userId the primary key of the user 744 * @param groupId the primary key of the file entry type's group 745 * @param fileEntryTypeId the primary key of the file entry type 746 * @throws PortalException if the user or group could not be found 747 * @throws SystemException if a system exception occurred 748 */ 749 public void subscribeFileEntryType( 750 long userId, long groupId, long fileEntryTypeId) 751 throws PortalException, SystemException { 752 753 if (fileEntryTypeId == 754 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) { 755 756 fileEntryTypeId = groupId; 757 } 758 759 subscriptionLocalService.addSubscription( 760 userId, groupId, DLFileEntryType.class.getName(), fileEntryTypeId); 761 } 762 763 /** 764 * Subscribe the user to document changes in the folder. This method is only 765 * supported by the Liferay repository. 766 * 767 * @param userId the primary key of the user 768 * @param groupId the primary key of the folder's group 769 * @param folderId the primary key of the folder 770 * @throws PortalException if the user or group could not be found 771 * @throws SystemException if a system exception occurred 772 */ 773 public void subscribeFolder(long userId, long groupId, long folderId) 774 throws PortalException, SystemException { 775 776 if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) { 777 folderId = groupId; 778 } 779 780 subscriptionLocalService.addSubscription( 781 userId, groupId, Folder.class.getName(), folderId); 782 } 783 784 /** 785 * Unsubscribe the user from changes in documents of the file entry type. 786 * This method is only supported by the Liferay repository. 787 * 788 * @param userId the primary key of the user 789 * @param groupId the primary key of the file entry type's group 790 * @param fileEntryTypeId the primary key of the file entry type 791 * @throws PortalException if the user or group could not be found 792 * @throws SystemException if a system exception occurred 793 */ 794 public void unsubscribeFileEntryType( 795 long userId, long groupId, long fileEntryTypeId) 796 throws PortalException, SystemException { 797 798 if (fileEntryTypeId == 799 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) { 800 801 fileEntryTypeId = groupId; 802 } 803 804 subscriptionLocalService.deleteSubscription( 805 userId, DLFileEntryType.class.getName(), fileEntryTypeId); 806 } 807 808 /** 809 * Unsubscribe the user from document changes in the folder. This method is 810 * only supported by the Liferay repository. 811 * 812 * @param userId the primary key of the user 813 * @param groupId the primary key of the folder's group 814 * @param folderId the primary key of the folder 815 * @throws PortalException if the user or group could not be found 816 * @throws SystemException if a system exception occurred 817 */ 818 public void unsubscribeFolder(long userId, long groupId, long folderId) 819 throws PortalException, SystemException { 820 821 if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) { 822 folderId = groupId; 823 } 824 825 subscriptionLocalService.deleteSubscription( 826 userId, Folder.class.getName(), folderId); 827 } 828 829 /** 830 * Updates the file entry's asset replacing its asset categories, tags, and 831 * links. 832 * 833 * @param userId the primary key of the user 834 * @param fileEntry the file entry to update 835 * @param fileVersion the file version to update 836 * @param assetCategoryIds the primary keys of the new asset categories 837 * @param assetTagNames the new asset tag names 838 * @param assetLinkEntryIds the primary keys of the new asset link entries 839 * @throws PortalException if the file entry or version could not be found 840 * @throws SystemException if a system exception occurred 841 */ 842 public void updateAsset( 843 long userId, FileEntry fileEntry, FileVersion fileVersion, 844 long[] assetCategoryIds, String[] assetTagNames, 845 long[] assetLinkEntryIds) 846 throws PortalException, SystemException { 847 848 LocalRepository localRepository = getLocalRepository( 849 0, fileEntry.getFileEntryId(), 0); 850 851 localRepository.updateAsset( 852 userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames, 853 assetLinkEntryIds); 854 } 855 856 /** 857 * Updates a file entry and associated metadata based on a byte array 858 * object. If the file data is <code>null</code>, then only the associated 859 * metadata (i.e., <code>title</code>, <code>description</code>, and 860 * parameters in the <code>serviceContext</code>) will be updated. 861 * 862 * <p> 863 * This method takes two file names, the <code>sourceFileName</code> and the 864 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 865 * name of the actual file being uploaded. The <code>title</code> 866 * corresponds to a name the client wishes to assign this file after it has 867 * been uploaded to the portal. 868 * </p> 869 * 870 * @param userId the primary key of the user 871 * @param fileEntryId the primary key of the file entry 872 * @param sourceFileName the original file's name (optionally 873 * <code>null</code>) 874 * @param mimeType the file's MIME type (optionally <code>null</code>) 875 * @param title the new name to be assigned to the file (optionally <code> 876 * <code>null</code></code>) 877 * @param description the file's new description 878 * @param changeLog the file's version change log (optionally 879 * <code>null</code>) 880 * @param majorVersion whether the new file version is a major version 881 * @param bytes the file's data (optionally <code>null</code>) 882 * @param serviceContext the service context to be applied. Can set the 883 * asset category IDs, asset tag names, and expando bridge 884 * attributes for the file entry. In a Liferay repository, it may 885 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 886 * type </li> <li> fieldsMap - mapping for fields associated with a 887 * custom file entry type </li> </ul> 888 * @return the file entry 889 * @throws PortalException if the file entry could not be found 890 * @throws SystemException if a system exception occurred 891 */ 892 public FileEntry updateFileEntry( 893 long userId, long fileEntryId, String sourceFileName, 894 String mimeType, String title, String description, String changeLog, 895 boolean majorVersion, byte[] bytes, ServiceContext serviceContext) 896 throws PortalException, SystemException { 897 898 File file = null; 899 900 try { 901 if ((bytes != null) && (bytes.length > 0)) { 902 file = FileUtil.createTempFile(bytes); 903 } 904 905 return updateFileEntry( 906 userId, fileEntryId, sourceFileName, mimeType, title, 907 description, changeLog, majorVersion, file, serviceContext); 908 } 909 catch (IOException ioe) { 910 throw new SystemException("Unable to write temporary file", ioe); 911 } 912 finally { 913 FileUtil.delete(file); 914 } 915 } 916 917 /** 918 * Updates a file entry and associated metadata based on a {@link 919 * java.io.File} object. If the file data is <code>null</code>, then only 920 * the associated metadata (i.e., <code>title</code>, 921 * <code>description</code>, and parameters in the 922 * <code>serviceContext</code>) will be updated. 923 * 924 * <p> 925 * This method takes two file names, the <code>sourceFileName</code> and the 926 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 927 * name of the actual file being uploaded. The <code>title</code> 928 * corresponds to a name the client wishes to assign this file after it has 929 * been uploaded to the portal. 930 * </p> 931 * 932 * @param userId the primary key of the user 933 * @param fileEntryId the primary key of the file entry 934 * @param sourceFileName the original file's name (optionally 935 * <code>null</code>) 936 * @param mimeType the file's MIME type (optionally <code>null</code>) 937 * @param title the new name to be assigned to the file (optionally <code> 938 * <code>null</code></code>) 939 * @param description the file's new description 940 * @param changeLog the file's version change log (optionally 941 * <code>null</code>) 942 * @param majorVersion whether the new file version is a major version 943 * @param file EntryId the primary key of the file entry 944 * @param serviceContext the service context to be applied. Can set the 945 * asset category IDs, asset tag names, and expando bridge 946 * attributes for the file entry. In a Liferay repository, it may 947 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 948 * type </li> <li> fieldsMap - mapping for fields associated with a 949 * custom file entry type </li> </ul> 950 * @return the file entry 951 * @throws PortalException if the file entry could not be found 952 * @throws SystemException if a system exception occurred 953 */ 954 public FileEntry updateFileEntry( 955 long userId, long fileEntryId, String sourceFileName, 956 String mimeType, String title, String description, String changeLog, 957 boolean majorVersion, File file, ServiceContext serviceContext) 958 throws PortalException, SystemException { 959 960 if ((file == null) || !file.exists() || (file.length() == 0)) { 961 return updateFileEntry( 962 userId, fileEntryId, sourceFileName, mimeType, title, 963 description, changeLog, majorVersion, null, 0, serviceContext); 964 } 965 966 mimeType = DLAppUtil.getMimeType(sourceFileName, mimeType, title, file); 967 968 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 969 970 FileEntry fileEntry = localRepository.updateFileEntry( 971 userId, fileEntryId, sourceFileName, mimeType, title, description, 972 changeLog, majorVersion, file, serviceContext); 973 974 DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion()); 975 976 dlAppHelperLocalService.updateFileEntry( 977 userId, fileEntry, null, fileEntry.getFileVersion(), 978 serviceContext); 979 980 return fileEntry; 981 } 982 983 /** 984 * Updates a file entry and associated metadata based on an {@link java.io. 985 * InputStream} object. If the file data is <code>null</code>, then only the 986 * associated metadata (i.e., <code>title</code>, <code>description</code>, 987 * and parameters in the <code>serviceContext</code>) will be updated. 988 * 989 * <p> 990 * This method takes two file names, the <code>sourceFileName</code> and the 991 * <code>title</code>. The <code>sourceFileName</code> corresponds to the 992 * name of the actual file being uploaded. The <code>title</code> 993 * corresponds to a name the client wishes to assign this file after it has 994 * been uploaded to the portal. 995 * </p> 996 * 997 * @param userId the primary key of the user 998 * @param fileEntryId the primary key of the file entry 999 * @param sourceFileName the original file's name (optionally 1000 * <code>null</code>) 1001 * @param mimeType the file's MIME type (optionally <code>null</code>) 1002 * @param title the new name to be assigned to the file (optionally <code> 1003 * <code>null</code></code>) 1004 * @param description the file's new description 1005 * @param changeLog the file's version change log (optionally 1006 * <code>null</code>) 1007 * @param majorVersion whether the new file version is a major version 1008 * @param is the file's data (optionally <code>null</code>) 1009 * @param size the file's size (optionally <code>0</code>) 1010 * @param serviceContext the service context to be applied. Can set the 1011 * asset category IDs, asset tag names, and expando bridge 1012 * attributes for the file entry. In a Liferay repository, it may 1013 * include: <ul> <li> fileEntryTypeId - ID for a custom file entry 1014 * type </li> <li> fieldsMap - mapping for fields associated with a 1015 * custom file entry type </li> </ul> 1016 * @return the file entry 1017 * @throws PortalException if the file entry could not be found 1018 * @throws SystemException if a system exception occurred 1019 */ 1020 public FileEntry updateFileEntry( 1021 long userId, long fileEntryId, String sourceFileName, 1022 String mimeType, String title, String description, String changeLog, 1023 boolean majorVersion, InputStream is, long size, 1024 ServiceContext serviceContext) 1025 throws PortalException, SystemException { 1026 1027 if (Validator.isNull(mimeType) || 1028 mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) { 1029 1030 String extension = DLAppUtil.getExtension(title, sourceFileName); 1031 1032 if (size == 0) { 1033 mimeType = MimeTypesUtil.getExtensionContentType(extension); 1034 } 1035 else { 1036 File file = null; 1037 1038 try { 1039 file = FileUtil.createTempFile(is); 1040 1041 return updateFileEntry( 1042 userId, fileEntryId, sourceFileName, mimeType, title, 1043 description, changeLog, majorVersion, file, 1044 serviceContext); 1045 } 1046 catch (IOException ioe) { 1047 throw new SystemException( 1048 "Unable to write temporary file", ioe); 1049 } 1050 finally { 1051 FileUtil.delete(file); 1052 } 1053 } 1054 } 1055 1056 LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0); 1057 1058 FileEntry oldFileEntry = localRepository.getFileEntry(fileEntryId); 1059 1060 FileVersion oldFileVersion = oldFileEntry.getFileVersion(); 1061 1062 FileEntry fileEntry = localRepository.updateFileEntry( 1063 userId, fileEntryId, sourceFileName, mimeType, title, description, 1064 changeLog, majorVersion, is, size, serviceContext); 1065 1066 if (is != null) { 1067 DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion()); 1068 1069 oldFileVersion = null; 1070 } 1071 1072 dlAppHelperLocalService.updateFileEntry( 1073 userId, fileEntry, oldFileVersion, fileEntry.getFileVersion(), 1074 serviceContext); 1075 1076 return fileEntry; 1077 } 1078 1079 /** 1080 * Updates a file rank to the existing file entry. This method is only 1081 * supported by the Liferay repository. 1082 * 1083 * @param repositoryId the primary key of the file rank's repository 1084 * @param companyId the primary key of the file rank's company 1085 * @param userId the primary key of the file rank's creator/owner 1086 * @param fileEntryId the primary key of the file rank's file entry 1087 * @param serviceContext the service context to be applied 1088 * @return the file rank 1089 * @throws PortalException if a portal exception occurred 1090 * @throws SystemException if a system exception occurred 1091 */ 1092 public DLFileRank updateFileRank( 1093 long repositoryId, long companyId, long userId, long fileEntryId, 1094 ServiceContext serviceContext) 1095 throws PortalException, SystemException { 1096 1097 return dlFileRankLocalService.updateFileRank( 1098 repositoryId, companyId, userId, fileEntryId, serviceContext); 1099 } 1100 1101 /** 1102 * Updates a file shortcut to the existing file entry. This method is only 1103 * supported by the Liferay repository. 1104 * 1105 * @param userId the primary key of the file shortcut's creator/owner 1106 * @param fileShortcutId the primary key of the file shortcut 1107 * @param folderId the primary key of the file shortcut's parent folder 1108 * @param toFileEntryId the primary key of the file shortcut's file entry 1109 * @param serviceContext the service context to be applied. Can set the 1110 * asset category IDs, asset tag names, and expando bridge 1111 * attributes for the file entry. 1112 * @return the file shortcut 1113 * @throws PortalException if the file shortcut, folder, or file entry could 1114 * not be found 1115 * @throws SystemException if a system exception occurred 1116 */ 1117 public DLFileShortcut updateFileShortcut( 1118 long userId, long fileShortcutId, long folderId, long toFileEntryId, 1119 ServiceContext serviceContext) 1120 throws PortalException, SystemException { 1121 1122 return dlFileShortcutLocalService.updateFileShortcut( 1123 userId, fileShortcutId, folderId, toFileEntryId, serviceContext); 1124 } 1125 1126 /** 1127 * Updates all file shortcuts to the existing file entry to the new file 1128 * entry. This method is only supported by the Liferay repository. 1129 * 1130 * @param toRepositoryId the primary key of the repository 1131 * @param oldToFileEntryId the primary key of the old file entry pointed to 1132 * @param newToFileEntryId the primary key of the new file entry to point 1133 * to 1134 * @throws SystemException if a system exception occurred 1135 */ 1136 public void updateFileShortcuts( 1137 long toRepositoryId, long oldToFileEntryId, long newToFileEntryId) 1138 throws SystemException { 1139 1140 dlFileShortcutLocalService.updateFileShortcuts( 1141 oldToFileEntryId, newToFileEntryId); 1142 } 1143 1144 /** 1145 * Updates the folder. 1146 * 1147 * @param folderId the primary key of the folder 1148 * @param parentFolderId the primary key of the folder's new parent folder 1149 * @param name the folder's new name 1150 * @param description the folder's new description 1151 * @param serviceContext the service context to be applied. In a Liferay 1152 * repository, it may include: <ul> <li> defaultFileEntryTypeId - 1153 * the file entry type to default all Liferay file entries to </li> 1154 * <li> dlFileEntryTypesSearchContainerPrimaryKeys - a 1155 * comma-delimited list of file entry type primary keys allowed in 1156 * the given folder and all descendants </li> <li> 1157 * overrideFileEntryTypes - boolean specifying whether to override 1158 * ancestral folder's restriction of file entry types allowed </li> 1159 * <li> workflowDefinitionXYZ - the workflow definition name 1160 * specified per file entry type. The parameter name must be the 1161 * string <code>workflowDefinition</code> appended by the <code> 1162 * fileEntryTypeId</code> (optionally <code>0</code>). </li> </ul> 1163 * @return the folder 1164 * @throws PortalException if the current or new parent folder could not be 1165 * found, or if the new parent folder's information was invalid 1166 * @throws SystemException if a system exception occurred 1167 */ 1168 public Folder updateFolder( 1169 long folderId, long parentFolderId, String name, String description, 1170 ServiceContext serviceContext) 1171 throws PortalException, SystemException { 1172 1173 LocalRepository localRepository = getLocalRepository(folderId, 0, 0); 1174 1175 return localRepository.updateFolder( 1176 folderId, parentFolderId, name, description, serviceContext); 1177 } 1178 1179 protected FileEntry copyFileEntry( 1180 long userId, LocalRepository toLocalRepository, FileEntry fileEntry, 1181 long newFolderId, ServiceContext serviceContext) 1182 throws PortalException, SystemException { 1183 1184 List<FileVersion> fileVersions = fileEntry.getFileVersions( 1185 WorkflowConstants.STATUS_ANY); 1186 1187 FileVersion latestFileVersion = fileVersions.get( 1188 fileVersions.size() - 1); 1189 1190 FileEntry destinationFileEntry = toLocalRepository.addFileEntry( 1191 userId, newFolderId, fileEntry.getTitle(), 1192 latestFileVersion.getMimeType(), latestFileVersion.getTitle(), 1193 latestFileVersion.getDescription(), StringPool.BLANK, 1194 latestFileVersion.getContentStream(false), 1195 latestFileVersion.getSize(), serviceContext); 1196 1197 for (int i = fileVersions.size() - 2; i >= 0; i--) { 1198 FileVersion fileVersion = fileVersions.get(i); 1199 1200 FileVersion previousFileVersion = fileVersions.get(i + 1); 1201 1202 try { 1203 destinationFileEntry = toLocalRepository.updateFileEntry( 1204 userId, destinationFileEntry.getFileEntryId(), 1205 fileEntry.getTitle(), destinationFileEntry.getMimeType(), 1206 destinationFileEntry.getTitle(), 1207 destinationFileEntry.getDescription(), StringPool.BLANK, 1208 DLAppUtil.isMajorVersion(fileVersion, previousFileVersion), 1209 fileVersion.getContentStream(false), fileVersion.getSize(), 1210 serviceContext); 1211 } 1212 catch (PortalException pe) { 1213 toLocalRepository.deleteFileEntry( 1214 destinationFileEntry.getFileEntryId()); 1215 1216 throw pe; 1217 } 1218 } 1219 1220 dlAppHelperLocalService.addFileEntry( 1221 userId, destinationFileEntry, destinationFileEntry.getFileVersion(), 1222 serviceContext); 1223 1224 return destinationFileEntry; 1225 } 1226 1227 protected void deleteFileEntry( 1228 long oldFileEntryId, long newFileEntryId, 1229 LocalRepository fromLocalRepository, 1230 LocalRepository toLocalRepository) 1231 throws PortalException, SystemException { 1232 1233 try { 1234 FileEntry fileEntry = fromLocalRepository.getFileEntry( 1235 oldFileEntryId); 1236 1237 fromLocalRepository.deleteFileEntry(oldFileEntryId); 1238 1239 dlAppHelperLocalService.deleteFileEntry(fileEntry); 1240 } 1241 catch (PortalException pe) { 1242 FileEntry fileEntry = toLocalRepository.getFileEntry( 1243 newFileEntryId); 1244 1245 toLocalRepository.deleteFileEntry(newFileEntryId); 1246 1247 dlAppHelperLocalService.deleteFileEntry(fileEntry); 1248 1249 throw pe; 1250 } 1251 } 1252 1253 protected LocalRepository getLocalRepository(long repositoryId) 1254 throws PortalException, SystemException { 1255 1256 return repositoryLocalService.getLocalRepositoryImpl(repositoryId); 1257 } 1258 1259 protected LocalRepository getLocalRepository( 1260 long folderId, long fileEntryId, long fileVersionId) 1261 throws PortalException, SystemException { 1262 1263 return repositoryLocalService.getLocalRepositoryImpl( 1264 folderId, fileEntryId, fileVersionId); 1265 } 1266 1267 protected LocalRepository getLocalRepository( 1268 long folderId, ServiceContext serviceContext) 1269 throws PortalException, SystemException { 1270 1271 LocalRepository localRepository = null; 1272 1273 if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) { 1274 localRepository = getLocalRepository( 1275 serviceContext.getScopeGroupId()); 1276 } 1277 else { 1278 localRepository = getLocalRepository(folderId, 0, 0); 1279 } 1280 1281 return localRepository; 1282 } 1283 1284 protected FileEntry moveFileEntries( 1285 long userId, long fileEntryId, long newFolderId, 1286 LocalRepository fromLocalRepository, 1287 LocalRepository toLocalRepository, ServiceContext serviceContext) 1288 throws PortalException, SystemException { 1289 1290 FileEntry sourceFileEntry = fromLocalRepository.getFileEntry( 1291 fileEntryId); 1292 1293 FileEntry destinationFileEntry = copyFileEntry( 1294 userId, toLocalRepository, sourceFileEntry, newFolderId, 1295 serviceContext); 1296 1297 deleteFileEntry( 1298 fileEntryId, destinationFileEntry.getFileEntryId(), 1299 fromLocalRepository, toLocalRepository); 1300 1301 return destinationFileEntry; 1302 } 1303 1304 }