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