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