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