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