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, int end)
542                    throws PortalException, SystemException {
543    
544                    LocalRepository localRepository = getLocalRepository(repositoryId);
545    
546                    return localRepository.getFileEntriesAndFileShortcuts(
547                            folderId, status, start, end);
548            }
549    
550            /**
551             * Returns the number of file entries and shortcuts in the folder.
552             *
553             * @param  repositoryId the primary key of the repository
554             * @param  folderId the primary key of the folder
555             * @param  status the workflow status
556             * @return the number of file entries and shortcuts in the folder
557             * @throws PortalException if the folder could not be found
558             * @throws SystemException if a system exception occurred
559             */
560            public int getFileEntriesAndFileShortcutsCount(
561                            long repositoryId, long folderId, int status)
562                    throws PortalException, SystemException {
563    
564                    LocalRepository localRepository = getLocalRepository(repositoryId);
565    
566                    return localRepository.getFileEntriesAndFileShortcutsCount(
567                            folderId, status);
568            }
569    
570            /**
571             * Returns the number of file entries in the folder.
572             *
573             * @param  repositoryId the primary key of the file entry's repository
574             * @param  folderId the primary key of the file entry's folder
575             * @return the number of file entries in the folder
576             * @throws PortalException if the folder could not be found
577             * @throws SystemException if a system exception occurred
578             */
579            public int getFileEntriesCount(long repositoryId, long folderId)
580                    throws PortalException, SystemException {
581    
582                    LocalRepository localRepository = getLocalRepository(repositoryId);
583    
584                    return localRepository.getFileEntriesCount(folderId);
585            }
586    
587            /**
588             * Returns the file entry with the primary key.
589             *
590             * @param  fileEntryId the primary key of the file entry
591             * @return the file entry with the primary key
592             * @throws PortalException if the file entry could not be found
593             * @throws SystemException if a system exception occurred
594             */
595            public FileEntry getFileEntry(long fileEntryId)
596                    throws PortalException, SystemException {
597    
598                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
599    
600                    return localRepository.getFileEntry(fileEntryId);
601            }
602    
603            /**
604             * Returns the file entry with the title in the folder.
605             *
606             * @param  groupId the primary key of the file entry's group
607             * @param  folderId the primary key of the file entry's folder
608             * @param  title the file entry's title
609             * @return the file entry with the title in the folder
610             * @throws PortalException if the file entry could not be found
611             * @throws SystemException if a system exception occurred
612             */
613            public FileEntry getFileEntry(long groupId, long folderId, String title)
614                    throws PortalException, SystemException {
615    
616                    try {
617                            LocalRepository localRepository = getLocalRepository(groupId);
618    
619                            return localRepository.getFileEntry(folderId, title);
620                    }
621                    catch (NoSuchFileEntryException nsfee) {
622                    }
623    
624                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
625    
626                    return localRepository.getFileEntry(folderId, title);
627            }
628    
629            /**
630             * Returns the file entry with the UUID and group.
631             *
632             * @param  uuid the file entry's universally unique identifier
633             * @param  groupId the primary key of the file entry's group
634             * @return the file entry with the UUID and group
635             * @throws PortalException if the file entry could not be found
636             * @throws SystemException if a system exception occurred
637             */
638            public FileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
639                    throws PortalException, SystemException {
640    
641                    try {
642                            LocalRepository localRepository = getLocalRepository(groupId);
643    
644                            return localRepository.getFileEntryByUuid(uuid);
645                    }
646                    catch (NoSuchFileEntryException nsfee) {
647                            List<com.liferay.portal.model.Repository> repositories =
648                                    repositoryPersistence.findByGroupId(groupId);
649    
650                            for (int i = 0; i < repositories.size(); i++) {
651                                    try {
652                                            long repositoryId = repositories.get(i).getRepositoryId();
653    
654                                            LocalRepository localRepository = getLocalRepository(
655                                                    repositoryId);
656    
657                                            return localRepository.getFileEntryByUuid(uuid);
658                                    }
659                                    catch (NoSuchFileEntryException nsfee2) {
660                                    }
661                            }
662                    }
663    
664                    StringBundler msg = new StringBundler(6);
665    
666                    msg.append("No DLFileEntry exists with the key {");
667                    msg.append("uuid=");
668                    msg.append(uuid);
669                    msg.append(", groupId=");
670                    msg.append(groupId);
671                    msg.append(StringPool.CLOSE_CURLY_BRACE);
672    
673                    throw new NoSuchFileEntryException(msg.toString());
674            }
675    
676            /**
677             * Returns the file ranks from the user. This method is only supported by
678             * the Liferay repository.
679             *
680             * @param  repositoryId the primary key of the repository
681             * @param  userId the primary key of the user
682             * @return the file ranks from the user
683             * @throws SystemException if a system exception occurred
684             */
685            public List<DLFileRank> getFileRanks(long repositoryId, long userId)
686                    throws SystemException {
687    
688                    return dlFileRankLocalService.getFileRanks(repositoryId, userId);
689            }
690    
691            /**
692             * Returns the file shortcut with the primary key. This method is only
693             * supported by the Liferay repository.
694             *
695             * @param  fileShortcutId the primary key of the file shortcut
696             * @return the file shortcut with the primary key
697             * @throws PortalException if the file shortcut could not be found
698             * @throws SystemException if a system exception occurred
699             */
700            public DLFileShortcut getFileShortcut(long fileShortcutId)
701                    throws PortalException, SystemException {
702    
703                    return dlFileShortcutLocalService.getFileShortcut(fileShortcutId);
704            }
705    
706            /**
707             * Returns the file version with the primary key.
708             *
709             * @param  fileVersionId the primary key of the file version
710             * @return the file version with the primary key
711             * @throws PortalException if the file version could not be found
712             * @throws SystemException if a system exception occurred
713             */
714            public FileVersion getFileVersion(long fileVersionId)
715                    throws PortalException, SystemException {
716    
717                    LocalRepository localRepository = getLocalRepository(
718                            0, 0, fileVersionId);
719    
720                    return localRepository.getFileVersion(fileVersionId);
721            }
722    
723            /**
724             * Returns the folder with the primary key.
725             *
726             * @param  folderId the primary key of the folder
727             * @return the folder with the primary key
728             * @throws PortalException if the folder could not be found
729             * @throws SystemException if a system exception occurred
730             */
731            public Folder getFolder(long folderId)
732                    throws PortalException, SystemException {
733    
734                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
735    
736                    return localRepository.getFolder(folderId);
737            }
738    
739            /**
740             * Returns the folder with the name in the parent folder.
741             *
742             * @param  repositoryId the primary key of the folder's repository
743             * @param  parentFolderId the primary key of the folder's parent folder
744             * @param  name the folder's name
745             * @return the folder with the name in the parent folder
746             * @throws PortalException if the folder could not be found
747             * @throws SystemException if a system exception occurred
748             */
749            public Folder getFolder(long repositoryId, long parentFolderId, String name)
750                    throws PortalException, SystemException {
751    
752                    LocalRepository localRepository = getLocalRepository(repositoryId);
753    
754                    return localRepository.getFolder(parentFolderId, name);
755            }
756    
757            /**
758             * Returns all immediate subfolders of the parent folder.
759             *
760             * @param  repositoryId the primary key of the folder's repository
761             * @param  parentFolderId the primary key of the folder's parent folder
762             * @return the immediate subfolders of the parent folder
763             * @throws PortalException if the parent folder could not be found
764             * @throws SystemException if a system exception occurred
765             */
766            public List<Folder> getFolders(long repositoryId, long parentFolderId)
767                    throws PortalException, SystemException {
768    
769                    return getFolders(repositoryId, parentFolderId, true);
770            }
771    
772            /**
773             * Returns all immediate subfolders of the parent folder, optionally
774             * including mount folders for third-party repositories.
775             *
776             * @param  repositoryId the primary key of the folder's repository
777             * @param  parentFolderId the primary key of the folder's parent folder
778             * @param  includeMountFolders whether to include mount folders for
779             *         third-party repositories
780             * @return the immediate subfolders of the parent folder
781             * @throws PortalException if the parent folder could not be found
782             * @throws SystemException if a system exception occurred
783             */
784            public List<Folder> getFolders(
785                            long repositoryId, long parentFolderId, boolean includeMountFolders)
786                    throws PortalException, SystemException {
787    
788                    return getFolders(
789                            repositoryId, parentFolderId, includeMountFolders,
790                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
791            }
792    
793            /**
794             * Returns a range of all the immediate subfolders of the parent folder,
795             * optionally including mount folders for third-party repositories.
796             *
797             * <p>
798             * Useful when paginating results. Returns a maximum of <code>end -
799             * start</code> instances. <code>start</code> and <code>end</code> are not
800             * primary keys, they are indexes in the result set. Thus, <code>0</code>
801             * refers to the first result in the set. Setting both <code>start</code>
802             * and <code>end</code> to {@link
803             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
804             * result set.
805             * </p>
806             *
807             * @param  repositoryId the primary key of the folder's repository
808             * @param  parentFolderId the primary key of the folder's parent folder
809             * @param  includeMountFolders whether to include mount folders for
810             *         third-party repositories
811             * @param  start the lower bound of the range of results
812             * @param  end the upper bound of the range of results (not inclusive)
813             * @return the range of immediate subfolders of the parent folder
814             * @throws PortalException if the parent folder could not be found
815             * @throws SystemException if a system exception occurred
816             */
817            public List<Folder> getFolders(
818                            long repositoryId, long parentFolderId, boolean includeMountFolders,
819                            int start, int end)
820                    throws PortalException, SystemException {
821    
822                    return getFolders(
823                            repositoryId, parentFolderId, includeMountFolders,
824                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
825            }
826    
827            /**
828             * Returns an ordered range of all the immediate subfolders of the parent
829             * folder.
830             *
831             * <p>
832             * Useful when paginating results. Returns a maximum of <code>end -
833             * start</code> instances. <code>start</code> and <code>end</code> are not
834             * primary keys, they are indexes in the result set. Thus, <code>0</code>
835             * refers to the first result in the set. Setting both <code>start</code>
836             * and <code>end</code> to {@link
837             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
838             * result set.
839             * </p>
840             *
841             * @param  repositoryId the primary key of the folder's repository
842             * @param  parentFolderId the primary key of the folder's parent folder
843             * @param  includeMountFolders whether to include mount folders for
844             *         third-party repositories
845             * @param  start the lower bound of the range of results
846             * @param  end the upper bound of the range of results (not inclusive)
847             * @param  obc the comparator to order the folders (optionally
848             *         <code>null</code>)
849             * @return the range of immediate subfolders of the parent folder ordered by
850             *         comparator <code>obc</code>
851             * @throws PortalException if the parent folder could not be found
852             * @throws SystemException if a system exception occurred
853             */
854            public List<Folder> getFolders(
855                            long repositoryId, long parentFolderId, boolean includeMountFolders,
856                            int start, int end, OrderByComparator obc)
857                    throws PortalException, SystemException {
858    
859                    LocalRepository localRepository = getLocalRepository(repositoryId);
860    
861                    return localRepository.getFolders(
862                            parentFolderId, includeMountFolders, start, end, obc);
863            }
864    
865            /**
866             * Returns a range of all the immediate subfolders of the parent folder.
867             *
868             * <p>
869             * Useful when paginating results. Returns a maximum of <code>end -
870             * start</code> instances. <code>start</code> and <code>end</code> are not
871             * primary keys, they are indexes in the result set. Thus, <code>0</code>
872             * refers to the first result in the set. Setting both <code>start</code>
873             * and <code>end</code> to {@link
874             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
875             * result set.
876             * </p>
877             *
878             * @param  repositoryId the primary key of the folder's repository
879             * @param  parentFolderId the primary key of the folder's parent folder
880             * @param  start the lower bound of the range of results
881             * @param  end the upper bound of the range of results (not inclusive)
882             * @return the range of immediate subfolders of the parent folder
883             * @throws PortalException if the parent folder could not be found
884             * @throws SystemException if a system exception occurred
885             */
886            public List<Folder> getFolders(
887                            long repositoryId, long parentFolderId, int start, int end)
888                    throws PortalException, SystemException {
889    
890                    return getFolders(repositoryId, parentFolderId, true, start, end);
891            }
892    
893            /**
894             * Returns an ordered range of all the immediate subfolders of the parent
895             * folder.
896             *
897             * <p>
898             * Useful when paginating results. Returns a maximum of <code>end -
899             * start</code> instances. <code>start</code> and <code>end</code> are not
900             * primary keys, they are indexes in the result set. Thus, <code>0</code>
901             * refers to the first result in the set. Setting both <code>start</code>
902             * and <code>end</code> to {@link
903             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
904             * result set.
905             * </p>
906             *
907             * @param  repositoryId the primary key of the folder's repository
908             * @param  parentFolderId the primary key of the folder's parent folder
909             * @param  start the lower bound of the range of results
910             * @param  end the upper bound of the range of results (not inclusive)
911             * @param  obc the comparator to order the folders (optionally
912             *         <code>null</code>)
913             * @return the range of immediate subfolders of the parent folder ordered by
914             *         comparator <code>obc</code>
915             * @throws PortalException if the parent folder could not be found
916             * @throws SystemException if a system exception occurred
917             */
918            public List<Folder> getFolders(
919                            long repositoryId, long parentFolderId, int start, int end,
920                            OrderByComparator obc)
921                    throws PortalException, SystemException {
922    
923                    return getFolders(repositoryId, parentFolderId, true, start, end, obc);
924            }
925    
926            /**
927             * Returns an ordered range of all the immediate subfolders, file entries,
928             * and file shortcuts in the parent folder.
929             *
930             * <p>
931             * Useful when paginating results. Returns a maximum of <code>end -
932             * start</code> instances. <code>start</code> and <code>end</code> are not
933             * primary keys, they are indexes in the result set. Thus, <code>0</code>
934             * refers to the first result in the set. Setting both <code>start</code>
935             * and <code>end</code> to {@link
936             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
937             * result set.
938             * </p>
939             *
940             * @param  repositoryId the primary key of the repository
941             * @param  folderId the primary key of the parent folder
942             * @param  status the workflow status
943             * @param  includeMountFolders whether to include mount folders for
944             *         third-party repositories
945             * @param  start the lower bound of the range of results
946             * @param  end the upper bound of the range of results (not inclusive)
947             * @param  obc the comparator to order the results (optionally
948             *         <code>null</code>)
949             * @return the range of immediate subfolders, file entries, and file
950             *         shortcuts in the parent folder ordered by comparator
951             *         <code>obc</code>
952             * @throws PortalException if the folder could not be found
953             * @throws SystemException if a system exception occurred
954             */
955            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
956                            long repositoryId, long folderId, int status,
957                            boolean includeMountFolders, int start, int end,
958                            OrderByComparator obc)
959                    throws PortalException, SystemException {
960    
961                    return getFoldersAndFileEntriesAndFileShortcuts(
962                            repositoryId, folderId, status, null, includeMountFolders, start,
963                            end, obc);
964            }
965    
966            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
967                            long repositoryId, long folderId, int status, String[] mimeTypes,
968                            boolean includeMountFolders, int start, int end,
969                            OrderByComparator obc)
970                    throws PortalException, SystemException {
971    
972                    LocalRepository localRepository = getLocalRepository(repositoryId);
973    
974                    return localRepository.getFoldersAndFileEntriesAndFileShortcuts(
975                            folderId, status, mimeTypes, includeMountFolders, start, end, obc);
976            }
977    
978            /**
979             * Returns the number of immediate subfolders, file entries, and file
980             * shortcuts in the parent folder.
981             *
982             * @param  repositoryId the primary key of the repository
983             * @param  folderId the primary key of the parent folder
984             * @param  status the workflow status
985             * @param  includeMountFolders whether to include mount folders for
986             *         third-party repositories
987             * @return the number of immediate subfolders, file entries, and file
988             *         shortcuts in the parent folder
989             * @throws PortalException if the folder could not be found
990             * @throws SystemException if a system exception occurred
991             */
992            public int getFoldersAndFileEntriesAndFileShortcutsCount(
993                            long repositoryId, long folderId, int status,
994                            boolean includeMountFolders)
995                    throws PortalException, SystemException {
996    
997                    return getFoldersAndFileEntriesAndFileShortcutsCount(
998                            repositoryId, folderId, status, null, includeMountFolders);
999            }
1000    
1001            public int getFoldersAndFileEntriesAndFileShortcutsCount(
1002                            long repositoryId, long folderId, int status, String[] mimeTypes,
1003                            boolean includeMountFolders)
1004                    throws PortalException, SystemException {
1005    
1006                    LocalRepository localRepository = getLocalRepository(repositoryId);
1007    
1008                    return localRepository.getFoldersAndFileEntriesAndFileShortcutsCount(
1009                            folderId, status, mimeTypes, includeMountFolders);
1010            }
1011    
1012            /**
1013             * Returns the number of immediate subfolders of the parent folder.
1014             *
1015             * @param  repositoryId the primary key of the folder's repository
1016             * @param  parentFolderId the primary key of the folder's parent folder
1017             * @return the number of immediate subfolders of the parent folder
1018             * @throws PortalException if the parent folder could not be found
1019             * @throws SystemException if a system exception occurred
1020             */
1021            public int getFoldersCount(long repositoryId, long parentFolderId)
1022                    throws PortalException, SystemException {
1023    
1024                    return getFoldersCount(repositoryId, parentFolderId, true);
1025            }
1026    
1027            /**
1028             * Returns the number of immediate subfolders of the parent folder,
1029             * optionally including mount folders for third-party repositories.
1030             *
1031             * @param  repositoryId the primary key of the folder's repository
1032             * @param  parentFolderId the primary key of the folder's parent folder
1033             * @param  includeMountFolders whether to include mount folders for
1034             *         third-party repositories
1035             * @return the number of immediate subfolders of the parent folder
1036             * @throws PortalException if the parent folder could not be found
1037             * @throws SystemException if a system exception occurred
1038             */
1039            public int getFoldersCount(
1040                            long repositoryId, long parentFolderId, boolean includeMountFolders)
1041                    throws PortalException, SystemException {
1042    
1043                    LocalRepository localRepository = getLocalRepository(repositoryId);
1044    
1045                    return localRepository.getFoldersCount(
1046                            parentFolderId, includeMountFolders);
1047            }
1048    
1049            /**
1050             * Returns the number of immediate subfolders and file entries across the
1051             * folders.
1052             *
1053             * @param  repositoryId the primary key of the repository
1054             * @param  folderIds the primary keys of folders from which to count
1055             *         immediate subfolders and file entries
1056             * @param  status the workflow status
1057             * @return the number of immediate subfolders and file entries across the
1058             *         folders
1059             * @throws PortalException if the repository could not be found
1060             * @throws SystemException if a system exception occurred
1061             */
1062            public int getFoldersFileEntriesCount(
1063                            long repositoryId, List<Long> folderIds, int status)
1064                    throws PortalException, SystemException {
1065    
1066                    LocalRepository localRepository = getLocalRepository(repositoryId);
1067    
1068                    return localRepository.getFoldersFileEntriesCount(folderIds, status);
1069            }
1070    
1071            /**
1072             * Returns the mount folder of the repository with the primary key. This
1073             * method is only supported by the Liferay repository.
1074             *
1075             * @param  repositoryId the primary key of the repository
1076             * @return the folder used for mounting third-party repositories
1077             * @throws PortalException if the repository or mount folder could not be
1078             *         found
1079             * @throws SystemException if a system exception occurred
1080             */
1081            public Folder getMountFolder(long repositoryId)
1082                    throws PortalException, SystemException {
1083    
1084                    DLFolder dlFolder = dlFolderLocalService.getMountFolder(repositoryId);
1085    
1086                    return new LiferayFolder(dlFolder);
1087            }
1088    
1089            /**
1090             * Returns all immediate subfolders of the parent folder that are used for
1091             * mounting third-party repositories. This method is only supported by the
1092             * Liferay repository.
1093             *
1094             * @param  repositoryId the primary key of the folder's repository
1095             * @param  parentFolderId the primary key of the folder's parent folder
1096             * @return the immediate subfolders of the parent folder that are used for
1097             *         mounting third-party repositories
1098             * @throws PortalException if the repository or parent folder could not be
1099             *         found
1100             * @throws SystemException if a system exception occurred
1101             */
1102            public List<Folder> getMountFolders(long repositoryId, long parentFolderId)
1103                    throws PortalException, SystemException {
1104    
1105                    return getMountFolders(
1106                            repositoryId, parentFolderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1107            }
1108    
1109            /**
1110             * Returns a range of all the immediate subfolders of the parent folder that
1111             * are used for mounting third-party repositories. This method is only
1112             * supported by the Liferay repository.
1113             *
1114             * <p>
1115             * Useful when paginating results. Returns a maximum of <code>end -
1116             * start</code> instances. <code>start</code> and <code>end</code> are not
1117             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1118             * refers to the first result in the set. Setting both <code>start</code>
1119             * and <code>end</code> to {@link
1120             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1121             * result set.
1122             * </p>
1123             *
1124             * @param  repositoryId the primary key of the repository
1125             * @param  parentFolderId the primary key of the parent folder
1126             * @param  start the lower bound of the range of results
1127             * @param  end the upper bound of the range of results (not inclusive)
1128             * @return the range of immediate subfolders of the parent folder that are
1129             *         used for mounting third-party repositories
1130             * @throws PortalException if the repository or parent folder could not be
1131             *         found
1132             * @throws SystemException if a system exception occurred
1133             */
1134            public List<Folder> getMountFolders(
1135                            long repositoryId, long parentFolderId, int start, int end)
1136                    throws PortalException, SystemException {
1137    
1138                    return getMountFolders(repositoryId, parentFolderId, start, end, null);
1139            }
1140    
1141            /**
1142             * Returns an ordered range of all the immediate subfolders of the parent
1143             * folder that are used for mounting third-party repositories. This method
1144             * is only supported by the Liferay repository.
1145             *
1146             * <p>
1147             * Useful when paginating results. Returns a maximum of <code>end -
1148             * start</code> instances. <code>start</code> and <code>end</code> are not
1149             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1150             * refers to the first result in the set. Setting both <code>start</code>
1151             * and <code>end</code> to {@link
1152             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1153             * result set.
1154             * </p>
1155             *
1156             * @param  repositoryId the primary key of the folder's repository
1157             * @param  parentFolderId the primary key of the folder's parent folder
1158             * @param  start the lower bound of the range of results
1159             * @param  end the upper bound of the range of results (not inclusive)
1160             * @param  obc the comparator to order the folders (optionally
1161             *         <code>null</code>)
1162             * @return the range of immediate subfolders of the parent folder that are
1163             *         used for mounting third-party repositories ordered by comparator
1164             *         <code>obc</code>
1165             * @throws PortalException if the repository or parent folder could not be
1166             *         found
1167             * @throws SystemException if a system exception occurred
1168             */
1169            public List<Folder> getMountFolders(
1170                            long repositoryId, long parentFolderId, int start, int end,
1171                            OrderByComparator obc)
1172                    throws PortalException, SystemException {
1173    
1174                    LocalRepository localRepository = getLocalRepository(repositoryId);
1175    
1176                    return localRepository.getMountFolders(parentFolderId, start, end, obc);
1177            }
1178    
1179            /**
1180             * Returns the number of immediate subfolders of the parent folder that are
1181             * used for mounting third-party repositories. This method is only supported
1182             * by the Liferay repository.
1183             *
1184             * @param  repositoryId the primary key of the repository
1185             * @param  parentFolderId the primary key of the parent folder
1186             * @return the number of folders of the parent folder that are used for
1187             *         mounting third-party repositories
1188             * @throws PortalException if the repository or parent folder could not be
1189             *         found
1190             * @throws SystemException if a system exception occurred
1191             */
1192            public int getMountFoldersCount(long repositoryId, long parentFolderId)
1193                    throws PortalException, SystemException {
1194    
1195                    LocalRepository localRepository = getLocalRepository(repositoryId);
1196    
1197                    return localRepository.getMountFoldersCount(parentFolderId);
1198            }
1199    
1200            /**
1201             * Moves the file entry to the new folder.
1202             *
1203             * @param  userId the primary key of the user
1204             * @param  fileEntryId the primary key of the file entry
1205             * @param  newFolderId the primary key of the new folder
1206             * @param  serviceContext the service context to be applied
1207             * @return the file entry
1208             * @throws PortalException if the file entry or the new folder could not be
1209             *         found
1210             * @throws SystemException if a system exception occurred
1211             */
1212            public FileEntry moveFileEntry(
1213                            long userId, long fileEntryId, long newFolderId,
1214                            ServiceContext serviceContext)
1215                    throws PortalException, SystemException {
1216    
1217                    LocalRepository fromLocalRepository = getLocalRepository(
1218                            0, fileEntryId, 0);
1219                    LocalRepository toLocalRepository = getLocalRepository(
1220                            newFolderId, serviceContext);
1221    
1222                    if (fromLocalRepository.getRepositoryId() ==
1223                                    toLocalRepository.getRepositoryId()) {
1224    
1225                            // Move file entries within repository
1226    
1227                            FileEntry fileEntry = fromLocalRepository.moveFileEntry(
1228                                    userId, fileEntryId, newFolderId, serviceContext);
1229    
1230                            return fileEntry;
1231                    }
1232    
1233                    // Move file entries between repositories
1234    
1235                    return moveFileEntries(
1236                            userId, fileEntryId, newFolderId, fromLocalRepository,
1237                            toLocalRepository, serviceContext);
1238            }
1239    
1240            /**
1241             * Updates the file entry's asset replacing its asset categories, tags, and
1242             * links.
1243             *
1244             * @param  userId the primary key of the user
1245             * @param  fileEntry the file entry to update
1246             * @param  fileVersion the file version to update
1247             * @param  assetCategoryIds the primary keys of the new asset categories
1248             * @param  assetTagNames the new asset tag names
1249             * @param  assetLinkEntryIds the primary keys of the new asset link entries
1250             * @throws PortalException if the file entry or version could not be found
1251             * @throws SystemException if a system exception occurred
1252             */
1253            public void updateAsset(
1254                            long userId, FileEntry fileEntry, FileVersion fileVersion,
1255                            long[] assetCategoryIds, String[] assetTagNames,
1256                            long[] assetLinkEntryIds)
1257                    throws PortalException, SystemException {
1258    
1259                    LocalRepository localRepository = getLocalRepository(
1260                            0, fileEntry.getFileEntryId(), 0);
1261    
1262                    localRepository.updateAsset(
1263                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
1264                            assetLinkEntryIds);
1265            }
1266    
1267            /**
1268             * Updates a file entry and associated metadata based on a byte array
1269             * object. If the file data is <code>null</code>, then only the associated
1270             * metadata (i.e., <code>title</code>, <code>description</code>, and
1271             * parameters in the <code>serviceContext</code>) will be updated.
1272             *
1273             * <p>
1274             * This method takes two file names, the <code>sourceFileName</code> and the
1275             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
1276             * name of the actual file being uploaded. The <code>title</code>
1277             * corresponds to a name the client wishes to assign this file after it has
1278             * been uploaded to the portal.
1279             * </p>
1280             *
1281             * @param  userId the primary key of the user
1282             * @param  fileEntryId the primary key of the file entry
1283             * @param  sourceFileName the original file's name (optionally
1284             *         <code>null</code>)
1285             * @param  mimeType the file's MIME type (optionally <code>null</code>)
1286             * @param  title the new name to be assigned to the file (optionally <code>
1287             *         <code>null</code></code>)
1288             * @param  description the file's new description
1289             * @param  changeLog the file's version change log (optionally
1290             *         <code>null</code>)
1291             * @param  majorVersion whether the new file version is a major version
1292             * @param  bytes the file's data (optionally <code>null</code>)
1293             * @param  serviceContext the service context to be applied. Can set the
1294             *         asset category IDs, asset tag names, and expando bridge
1295             *         attributes for the file entry. In a Liferay repository, it may
1296             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
1297             *         type </li> <li> fieldsMap - mapping for fields associated with a
1298             *         custom file entry type </li> </ul>
1299             * @return the file entry
1300             * @throws PortalException if the file entry could not be found
1301             * @throws SystemException if a system exception occurred
1302             */
1303            public FileEntry updateFileEntry(
1304                            long userId, long fileEntryId, String sourceFileName,
1305                            String mimeType, String title, String description, String changeLog,
1306                            boolean majorVersion, byte[] bytes, ServiceContext serviceContext)
1307                    throws PortalException, SystemException {
1308    
1309                    File file = null;
1310    
1311                    try {
1312                            if ((bytes != null) && (bytes.length > 0)) {
1313                                    file = FileUtil.createTempFile(bytes);
1314                            }
1315    
1316                            return updateFileEntry(
1317                                    userId, fileEntryId, sourceFileName, mimeType, title,
1318                                    description, changeLog, majorVersion, file, serviceContext);
1319                    }
1320                    catch (IOException ioe) {
1321                            throw new SystemException("Unable to write temporary file", ioe);
1322                    }
1323                    finally {
1324                            FileUtil.delete(file);
1325                    }
1326            }
1327    
1328            /**
1329             * Updates a file entry and associated metadata based on a {@link File}
1330             * object. If the file data is <code>null</code>, then only the associated
1331             * metadata (i.e., <code>title</code>, <code>description</code>, and
1332             * parameters in the <code>serviceContext</code>) will be updated.
1333             *
1334             * <p>
1335             * This method takes two file names, the <code>sourceFileName</code> and the
1336             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
1337             * name of the actual file being uploaded. The <code>title</code>
1338             * corresponds to a name the client wishes to assign this file after it has
1339             * been uploaded to the portal.
1340             * </p>
1341             *
1342             * @param  userId the primary key of the user
1343             * @param  fileEntryId the primary key of the file entry
1344             * @param  sourceFileName the original file's name (optionally
1345             *         <code>null</code>)
1346             * @param  mimeType the file's MIME type (optionally <code>null</code>)
1347             * @param  title the new name to be assigned to the file (optionally <code>
1348             *         <code>null</code></code>)
1349             * @param  description the file's new description
1350             * @param  changeLog the file's version change log (optionally
1351             *         <code>null</code>)
1352             * @param  majorVersion whether the new file version is a major version
1353             * @param  file EntryId the primary key of the file entry
1354             * @param  serviceContext the service context to be applied. Can set the
1355             *         asset category IDs, asset tag names, and expando bridge
1356             *         attributes for the file entry. In a Liferay repository, it may
1357             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
1358             *         type </li> <li> fieldsMap - mapping for fields associated with a
1359             *         custom file entry type </li> </ul>
1360             * @return the file entry
1361             * @throws PortalException if the file entry could not be found
1362             * @throws SystemException if a system exception occurred
1363             */
1364            public FileEntry updateFileEntry(
1365                            long userId, long fileEntryId, String sourceFileName,
1366                            String mimeType, String title, String description, String changeLog,
1367                            boolean majorVersion, File file, ServiceContext serviceContext)
1368                    throws PortalException, SystemException {
1369    
1370                    if (file == null || !file.exists() || file.length() == 0) {
1371                            return updateFileEntry(
1372                                    userId, fileEntryId, sourceFileName, mimeType, title,
1373                                    description, changeLog, majorVersion, null, 0, serviceContext);
1374                    }
1375    
1376                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
1377    
1378                    FileEntry fileEntry = localRepository.updateFileEntry(
1379                            userId, fileEntryId, sourceFileName, mimeType, title, description,
1380                            changeLog, majorVersion, file, serviceContext);
1381    
1382                    DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion());
1383    
1384                    dlAppHelperLocalService.updateFileEntry(
1385                            userId, fileEntry, fileEntry.getFileVersion(), serviceContext);
1386    
1387                    return fileEntry;
1388            }
1389    
1390            /**
1391             * Updates a file entry and associated metadata based on an {@link
1392             * InputStream} object. If the file data is <code>null</code>, then only the
1393             * associated metadata (i.e., <code>title</code>, <code>description</code>,
1394             * and parameters in the <code>serviceContext</code>) will be updated.
1395             *
1396             * <p>
1397             * This method takes two file names, the <code>sourceFileName</code> and the
1398             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
1399             * name of the actual file being uploaded. The <code>title</code>
1400             * corresponds to a name the client wishes to assign this file after it has
1401             * been uploaded to the portal.
1402             * </p>
1403             *
1404             * @param  userId the primary key of the user
1405             * @param  fileEntryId the primary key of the file entry
1406             * @param  sourceFileName the original file's name (optionally
1407             *         <code>null</code>)
1408             * @param  mimeType the file's MIME type (optionally <code>null</code>)
1409             * @param  title the new name to be assigned to the file (optionally <code>
1410             *         <code>null</code></code>)
1411             * @param  description the file's new description
1412             * @param  changeLog the file's version change log (optionally
1413             *         <code>null</code>)
1414             * @param  majorVersion whether the new file version is a major version
1415             * @param  is the file's data (optionally <code>null</code>)
1416             * @param  size the file's size (optionally <code>0</code>)
1417             * @param  serviceContext the service context to be applied. Can set the
1418             *         asset category IDs, asset tag names, and expando bridge
1419             *         attributes for the file entry. In a Liferay repository, it may
1420             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
1421             *         type </li> <li> fieldsMap - mapping for fields associated with a
1422             *         custom file entry type </li> </ul>
1423             * @return the file entry
1424             * @throws PortalException if the file entry could not be found
1425             * @throws SystemException if a system exception occurred
1426             */
1427            public FileEntry updateFileEntry(
1428                            long userId, long fileEntryId, String sourceFileName,
1429                            String mimeType, String title, String description, String changeLog,
1430                            boolean majorVersion, InputStream is, long size,
1431                            ServiceContext serviceContext)
1432                    throws PortalException, SystemException {
1433    
1434                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
1435    
1436                    FileEntry fileEntry = localRepository.updateFileEntry(
1437                            userId, fileEntryId, sourceFileName, mimeType, title, description,
1438                            changeLog, majorVersion, is, size, serviceContext);
1439    
1440                    if (is != null) {
1441                            DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion());
1442                    }
1443    
1444                    dlAppHelperLocalService.updateFileEntry(
1445                            userId, fileEntry, fileEntry.getFileVersion(), serviceContext);
1446    
1447                    return fileEntry;
1448            }
1449    
1450            /**
1451             * Updates a file rank to the existing file entry. This method is only
1452             * supported by the Liferay repository.
1453             *
1454             * @param  repositoryId the primary key of the file rank's repository
1455             * @param  companyId the primary key of the file rank's company
1456             * @param  userId the primary key of the file rank's creator/owner
1457             * @param  fileEntryId the primary key of the file rank's file entry
1458             * @param  serviceContext the service context to be applied
1459             * @return the file rank
1460             * @throws SystemException if a system exception occurred
1461             */
1462            public DLFileRank updateFileRank(
1463                            long repositoryId, long companyId, long userId, long fileEntryId,
1464                            ServiceContext serviceContext)
1465                    throws SystemException {
1466    
1467                    return dlFileRankLocalService.updateFileRank(
1468                            repositoryId, companyId, userId, fileEntryId, serviceContext);
1469            }
1470    
1471            /**
1472             * Updates a file shortcut to the existing file entry. This method is only
1473             * supported by the Liferay repository.
1474             *
1475             * @param  userId the primary key of the file shortcut's creator/owner
1476             * @param  fileShortcutId the primary key of the file shortcut
1477             * @param  folderId the primary key of the file shortcut's parent folder
1478             * @param  toFileEntryId the primary key of the file shortcut's file entry
1479             * @param  serviceContext the service context to be applied. Can set the
1480             *         asset category IDs, asset tag names, and expando bridge
1481             *         attributes for the file entry.
1482             * @return the file shortcut
1483             * @throws PortalException if the file shortcut, folder, or file entry could
1484             *         not be found
1485             * @throws SystemException if a system exception occurred
1486             */
1487            public DLFileShortcut updateFileShortcut(
1488                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
1489                            ServiceContext serviceContext)
1490                    throws PortalException, SystemException {
1491    
1492                    return dlFileShortcutLocalService.updateFileShortcut(
1493                            userId, fileShortcutId, folderId, toFileEntryId, serviceContext);
1494            }
1495    
1496            /**
1497             * Updates all file shortcuts to the existing file entry to the new file
1498             * entry. This method is only supported by the Liferay repository.
1499             *
1500             * @param  toRepositoryId the primary key of the repository
1501             * @param  oldToFileEntryId the primary key of the old file entry pointed to
1502             * @param  newToFileEntryId the primary key of the new file entry to point
1503             *         to
1504             * @throws SystemException if a system exception occurred
1505             */
1506            public void updateFileShortcuts(
1507                            long toRepositoryId, long oldToFileEntryId, long newToFileEntryId)
1508                    throws SystemException {
1509    
1510                    dlFileShortcutLocalService.updateFileShortcuts(
1511                            oldToFileEntryId, newToFileEntryId);
1512            }
1513    
1514            /**
1515             * Updates the folder.
1516             *
1517             * @param  folderId the primary key of the folder
1518             * @param  parentFolderId the primary key of the folder's new parent folder
1519             * @param  name the folder's new name
1520             * @param  description the folder's new description
1521             * @param  serviceContext the service context to be applied. In a Liferay
1522             *         repository, it may include:  <ul> <li> defaultFileEntryTypeId -
1523             *         the file entry type to default all Liferay file entries to </li>
1524             *         <li> fileEntryTypeSearchContainerPrimaryKeys - a comma-delimited
1525             *         list of file entry type primary keys allowed in the given folder
1526             *         and all descendants </li> <li> mountPoint - boolean specifying
1527             *         whether folder is a facade for mounting a third-party repository
1528             *         </li> <li> overrideFileEntryTypes - boolean specifying whether to
1529             *         override ancestral folder's restriction of file entry types
1530             *         allowed </li> <li> workflowDefinitionXYZ - the workflow
1531             *         definition name specified per file entry type. The parameter name
1532             *         must be the string <code>workflowDefinition</code> appended by
1533             *         the <code>fileEntryTypeId</code> (optionally <code>0</code>).
1534             *         </li> </ul>
1535             * @return the folder
1536             * @throws PortalException if the current or new parent folder could not be
1537             *         found, or if the new parent folder's information was invalid
1538             * @throws SystemException if a system exception occurred
1539             */
1540            public Folder updateFolder(
1541                            long folderId, long parentFolderId, String name, String description,
1542                            ServiceContext serviceContext)
1543                    throws PortalException, SystemException {
1544    
1545                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
1546    
1547                    return localRepository.updateFolder(
1548                            folderId, parentFolderId, name, description, serviceContext);
1549            }
1550    
1551            protected FileEntry copyFileEntry(
1552                            long userId, LocalRepository toLocalRepository, FileEntry fileEntry,
1553                            long newFolderId, ServiceContext serviceContext)
1554                    throws PortalException, SystemException {
1555    
1556                    List<FileVersion> fileVersions = fileEntry.getFileVersions(
1557                            WorkflowConstants.STATUS_ANY);
1558    
1559                    FileVersion latestFileVersion = fileVersions.get(
1560                            fileVersions.size() - 1);
1561    
1562                    FileEntry destinationFileEntry = toLocalRepository.addFileEntry(
1563                            userId, newFolderId, fileEntry.getTitle(),
1564                            latestFileVersion.getMimeType(), latestFileVersion.getTitle(),
1565                            latestFileVersion.getDescription(), StringPool.BLANK,
1566                            latestFileVersion.getContentStream(false),
1567                            latestFileVersion.getSize(), serviceContext);
1568    
1569                    for (int i = fileVersions.size() - 2; i >= 0 ; i--) {
1570                            FileVersion fileVersion = fileVersions.get(i);
1571    
1572                            FileVersion previousFileVersion = fileVersions.get(i + 1);
1573    
1574                            try {
1575                                    destinationFileEntry = toLocalRepository.updateFileEntry(
1576                                            userId, destinationFileEntry.getFileEntryId(),
1577                                            fileEntry.getTitle(), destinationFileEntry.getMimeType(),
1578                                            destinationFileEntry.getTitle(),
1579                                            destinationFileEntry.getDescription(), StringPool.BLANK,
1580                                            isMajorVersion(fileVersion, previousFileVersion),
1581                                            fileVersion.getContentStream(false), fileVersion.getSize(),
1582                                            serviceContext);
1583                            }
1584                            catch (PortalException pe) {
1585                                    toLocalRepository.deleteFileEntry(
1586                                            destinationFileEntry.getFileEntryId());
1587    
1588                                    throw pe;
1589                            }
1590                    }
1591    
1592                    dlAppHelperLocalService.addFileEntry(
1593                            userId, destinationFileEntry, destinationFileEntry.getFileVersion(),
1594                            serviceContext);
1595    
1596                    return destinationFileEntry;
1597            }
1598    
1599            protected void deleteFileEntry(
1600                            long oldFileEntryId, long newFileEntryId,
1601                            LocalRepository fromLocalRepository,
1602                            LocalRepository toLocalRepository)
1603                    throws PortalException, SystemException {
1604    
1605                    try {
1606                            FileEntry fileEntry = fromLocalRepository.getFileEntry(
1607                                    oldFileEntryId);
1608    
1609                            fromLocalRepository.deleteFileEntry(oldFileEntryId);
1610    
1611                            dlAppHelperLocalService.deleteFileEntry(fileEntry);
1612                    }
1613                    catch (PortalException pe) {
1614                            FileEntry fileEntry = toLocalRepository.getFileEntry(
1615                                    newFileEntryId);
1616    
1617                            toLocalRepository.deleteFileEntry(newFileEntryId);
1618    
1619                            dlAppHelperLocalService.deleteFileEntry(fileEntry);
1620    
1621                            throw pe;
1622                    }
1623            }
1624    
1625            protected LocalRepository getLocalRepository(long repositoryId)
1626                    throws PortalException, SystemException {
1627    
1628                    return repositoryLocalService.getLocalRepositoryImpl(repositoryId);
1629            }
1630    
1631            protected LocalRepository getLocalRepository(
1632                            long folderId, long fileEntryId, long fileVersionId)
1633                    throws PortalException, SystemException {
1634    
1635                    return repositoryLocalService.getLocalRepositoryImpl(
1636                            folderId, fileEntryId, fileVersionId);
1637            }
1638    
1639            protected LocalRepository getLocalRepository(
1640                            long folderId, ServiceContext serviceContext)
1641                    throws PortalException, SystemException {
1642    
1643                    LocalRepository localRepository = null;
1644    
1645                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1646                            localRepository = getLocalRepository(
1647                                    serviceContext.getScopeGroupId());
1648                    }
1649                    else {
1650                            localRepository = getLocalRepository(folderId, 0, 0);
1651                    }
1652    
1653                    return localRepository;
1654            }
1655    
1656            protected boolean isMajorVersion(
1657                    FileVersion previousFileVersion, FileVersion currentFileVersion) {
1658    
1659                    long currentVersion = GetterUtil.getLong(
1660                            currentFileVersion.getVersion());
1661                    long previousVersion = GetterUtil.getLong(
1662                            previousFileVersion.getVersion());
1663    
1664                    return (currentVersion - previousVersion) >= 1;
1665            }
1666    
1667            protected FileEntry moveFileEntries(
1668                            long userId, long fileEntryId, long newFolderId,
1669                            LocalRepository fromLocalRepository,
1670                            LocalRepository toLocalRepository, ServiceContext serviceContext)
1671                    throws SystemException, PortalException {
1672    
1673                    FileEntry sourceFileEntry = fromLocalRepository.getFileEntry(
1674                            fileEntryId);
1675    
1676                    FileEntry destinationFileEntry = copyFileEntry(
1677                            userId, toLocalRepository, sourceFileEntry, newFolderId,
1678                            serviceContext);
1679    
1680                    deleteFileEntry(
1681                            fileEntryId, destinationFileEntry.getFileEntryId(),
1682                            fromLocalRepository, toLocalRepository);
1683    
1684                    return destinationFileEntry;
1685            }
1686    
1687    }