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