001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.repository.LocalRepository;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.Repository;
029    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
032    import com.liferay.portlet.documentlibrary.model.DLFileRank;
033    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
034    import com.liferay.portlet.documentlibrary.model.DLFolder;
035    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036    import com.liferay.portlet.documentlibrary.service.base.DLAppLocalServiceBaseImpl;
037    import com.liferay.portlet.documentlibrary.util.DLAppUtil;
038    import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
039    
040    import java.io.File;
041    import java.io.IOException;
042    import java.io.InputStream;
043    
044    import java.util.List;
045    
046    /**
047     * The document library local service. All portlets should interact with the
048     * document library through this class or through {@link DLAppServiceImpl},
049     * rather than through the individual document library service classes.
050     *
051     * <p>
052     * This class provides a unified interface to all Liferay and third party
053     * repositories. While the method signatures are universal for all repositories.
054     * Additional implementation-specific parameters may be specified in the
055     * serviceContext.
056     * </p>
057     *
058     * <p>
059     * The <code>repositoryId</code> parameter used by most of the methods is the
060     * primary key of the specific repository. If the repository is a default
061     * Liferay repository, the <code>repositoryId</code> is the <code>groupId</code>
062     * or <code>scopeGroupId</code>. Otherwise, the <code>repositoryId</code> will
063     * correspond to values obtained from {@link
064     * com.liferay.portal.service.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 java.io.File}
134             * object.
135             *
136             * <p>
137             * This method takes two file names, the <code>sourceFileName</code> and the
138             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
139             * name of the actual file being uploaded. The <code>title</code>
140             * corresponds to a name the client wishes to assign this file after it has
141             * been uploaded to the portal. If it is <code>null</code>, the <code>
142             * sourceFileName</code> will be used.
143             * </p>
144             *
145             * @param  userId the primary key of the file entry's creator/owner
146             * @param  repositoryId the primary key of the repository
147             * @param  folderId the primary key of the file entry's parent folder
148             * @param  sourceFileName the original file's name
149             * @param  mimeType the file's MIME type
150             * @param  title the name to be assigned to the file (optionally <code>null
151             *         </code>)
152             * @param  description the file's description
153             * @param  changeLog the file's version change log
154             * @param  file the file's data (optionally <code>null</code>)
155             * @param  serviceContext the service context to be applied. Can set the
156             *         asset category IDs, asset tag names, and expando bridge
157             *         attributes for the file entry. In a Liferay repository, it may
158             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
159             *         type </li> <li> fieldsMap - mapping for fields associated with a
160             *         custom file entry type </li> </ul>
161             * @return the file entry
162             * @throws PortalException if the parent folder could not be found or if the
163             *         file entry's information was invalid
164             * @throws SystemException if a system exception occurred
165             */
166            public FileEntry addFileEntry(
167                            long userId, long repositoryId, long folderId,
168                            String sourceFileName, String mimeType, String title,
169                            String description, String changeLog, File file,
170                            ServiceContext serviceContext)
171                    throws PortalException, SystemException {
172    
173                    if ((file == null) || !file.exists() || (file.length() == 0)) {
174                            return addFileEntry(
175                                    userId, repositoryId, folderId, sourceFileName, mimeType, title,
176                                    description, changeLog, null, 0, serviceContext);
177                    }
178    
179                    mimeType = DLAppUtil.getMimeType(
180                            sourceFileName, mimeType, title, file, null);
181    
182                    LocalRepository localRepository = getLocalRepository(repositoryId);
183    
184                    FileEntry fileEntry = localRepository.addFileEntry(
185                            userId, folderId, sourceFileName, mimeType, title, description,
186                            changeLog, file, serviceContext);
187    
188                    dlAppHelperLocalService.addFileEntry(
189                            userId, fileEntry, fileEntry.getFileVersion(), serviceContext);
190    
191                    return fileEntry;
192            }
193    
194            /**
195             * Adds a file entry and associated metadata based on an {@link
196             * java.io.InputStream} object.
197             *
198             * <p>
199             * This method takes two file names, the <code>sourceFileName</code> and the
200             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
201             * name of the actual file being uploaded. The <code>title</code>
202             * corresponds to a name the client wishes to assign this file after it has
203             * been uploaded to the portal. If it is <code>null</code>, the <code>
204             * sourceFileName</code> will be used.
205             * </p>
206             *
207             * @param  userId the primary key of the file entry's creator/owner
208             * @param  repositoryId the primary key of the repository
209             * @param  folderId the primary key of the file entry's parent folder
210             * @param  sourceFileName the original file's name
211             * @param  mimeType the file's MIME type
212             * @param  title the name to be assigned to the file (optionally <code>null
213             *         </code>)
214             * @param  description the file's description
215             * @param  changeLog the file's version change log
216             * @param  is the file's data (optionally <code>null</code>)
217             * @param  size the file's size (optionally <code>0</code>)
218             * @param  serviceContext the service context to be applied. Can set the
219             *         asset category IDs, asset tag names, and expando bridge
220             *         attributes for the file entry. In a Liferay repository, it may
221             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
222             *         type </li> <li> fieldsMap - mapping for fields associated with a
223             *         custom file entry type </li> </ul>
224             * @return the file entry
225             * @throws PortalException if the parent folder could not be found or if the
226             *         file entry's information was invalid
227             * @throws SystemException if a system exception occurred
228             */
229            public FileEntry addFileEntry(
230                            long userId, long repositoryId, long folderId,
231                            String sourceFileName, String mimeType, String title,
232                            String description, String changeLog, InputStream is, long size,
233                            ServiceContext serviceContext)
234                    throws PortalException, SystemException {
235    
236                    if (is == null) {
237                            is = new UnsyncByteArrayInputStream(new byte[0]);
238                            size = 0;
239                    }
240    
241                    mimeType = DLAppUtil.getMimeType(
242                            sourceFileName, mimeType, title, null, is);
243    
244                    LocalRepository localRepository = getLocalRepository(repositoryId);
245    
246                    FileEntry fileEntry = localRepository.addFileEntry(
247                            userId, folderId, sourceFileName, mimeType, title, description,
248                            changeLog, is, size, serviceContext);
249    
250                    dlAppHelperLocalService.addFileEntry(
251                            userId, fileEntry, fileEntry.getFileVersion(), serviceContext);
252    
253                    return fileEntry;
254            }
255    
256            /**
257             * Adds the file rank to the existing file entry. This method is only
258             * supported by the Liferay repository.
259             *
260             * @param  repositoryId the primary key of the repository
261             * @param  companyId the primary key of the company
262             * @param  userId the primary key of the file rank's creator/owner
263             * @param  fileEntryId the primary key of the file entry
264             * @param  serviceContext the service context to be applied
265             * @return the file rank
266             * @throws SystemException if a system exception occurred
267             */
268            public DLFileRank addFileRank(
269                            long repositoryId, long companyId, long userId, long fileEntryId,
270                            ServiceContext serviceContext)
271                    throws SystemException {
272    
273                    return dlFileRankLocalService.addFileRank(
274                            repositoryId, companyId, userId, fileEntryId, serviceContext);
275            }
276    
277            /**
278             * Adds the file shortcut to the existing file entry. This method is only
279             * supported by the Liferay repository.
280             *
281             * @param  userId the primary key of the file shortcut's creator/owner
282             * @param  repositoryId the primary key of the repository
283             * @param  folderId the primary key of the file shortcut's parent folder
284             * @param  toFileEntryId the primary key of the file entry to point to
285             * @param  serviceContext the service context to be applied. Can set the
286             *         asset category IDs, asset tag names, and expando bridge
287             *         attributes for the file entry.
288             * @return the file shortcut
289             * @throws PortalException if the parent folder or file entry could not be
290             *         found, or if the file shortcut's information was invalid
291             * @throws SystemException if a system exception occurred
292             */
293            public DLFileShortcut addFileShortcut(
294                            long userId, long repositoryId, long folderId, long toFileEntryId,
295                            ServiceContext serviceContext)
296                    throws PortalException, SystemException {
297    
298                    return dlFileShortcutLocalService.addFileShortcut(
299                            userId, repositoryId, folderId, toFileEntryId, serviceContext);
300            }
301    
302            /**
303             * Adds a folder.
304             *
305             * @param  userId the primary key of the folder's creator/owner
306             * @param  repositoryId the primary key of the repository
307             * @param  parentFolderId the primary key of the folder's parent folder
308             * @param  name the folder's name
309             * @param  description the folder's description
310             * @param  serviceContext the service context to be applied. In a Liferay
311             *         repository, it may include mountPoint which is a boolean
312             *         specifying whether the folder is a facade for mounting a
313             *         third-party repository
314             * @return the folder
315             * @throws PortalException if the parent folder could not be found or if the
316             *         new folder's information was invalid
317             * @throws SystemException if a system exception occurred
318             */
319            public Folder addFolder(
320                            long userId, long repositoryId, long parentFolderId, String name,
321                            String description, ServiceContext serviceContext)
322                    throws PortalException, SystemException {
323    
324                    LocalRepository localRepository = getLocalRepository(repositoryId);
325    
326                    return localRepository.addFolder(
327                            userId, parentFolderId, name, description, serviceContext);
328            }
329    
330            /**
331             * Delete all data associated to the given repository. This method is only
332             * supported by the Liferay repository.
333             *
334             * @param  repositoryId the primary key of the data's repository
335             * @throws PortalException if the repository could not be found
336             * @throws SystemException if a system exception occurred
337             */
338            public void deleteAll(long repositoryId)
339                    throws PortalException, SystemException {
340    
341                    LocalRepository localRepository = getLocalRepository(repositoryId);
342    
343                    localRepository.deleteAll();
344            }
345    
346            /**
347             * Deletes the file entry.
348             *
349             * @param  fileEntryId the primary key of the file entry
350             * @throws PortalException if the file entry could not be found
351             * @throws SystemException if a system exception occurred
352             */
353            public void deleteFileEntry(long fileEntryId)
354                    throws PortalException, SystemException {
355    
356                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
357    
358                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
359    
360                    localRepository.deleteFileEntry(fileEntryId);
361    
362                    dlAppHelperLocalService.deleteFileEntry(fileEntry);
363            }
364    
365            /**
366             * Deletes the file ranks associated to a given file entry. This method is
367             * only supported by the Liferay repository.
368             *
369             * @param  fileEntryId the primary key of the file entry
370             * @throws SystemException if a system exception occurred
371             */
372            public void deleteFileRanksByFileEntryId(long fileEntryId)
373                    throws SystemException {
374    
375                    dlFileRankLocalService.deleteFileRanksByFileEntryId(fileEntryId);
376            }
377    
378            /**
379             * Deletes the file ranks associated to a given user. This method is only
380             * supported by the Liferay repository.
381             *
382             * @param  userId the primary key of the user
383             * @throws SystemException if a system exception occurred
384             */
385            public void deleteFileRanksByUserId(long userId) throws SystemException {
386                    dlFileRankLocalService.deleteFileRanksByUserId(userId);
387            }
388    
389            /**
390             * Deletes the file shortcut. This method is only supported by the Liferay
391             * repository.
392             *
393             * @param  dlFileShortcut the file shortcut
394             * @throws PortalException if the file shortcut could not be found
395             * @throws SystemException if a system exception occurred
396             */
397            public void deleteFileShortcut(DLFileShortcut dlFileShortcut)
398                    throws PortalException, SystemException {
399    
400                    dlFileShortcutLocalService.deleteFileShortcut(dlFileShortcut);
401            }
402    
403            /**
404             * Deletes the file shortcut. This method is only supported by the Liferay
405             * repository.
406             *
407             * @param  fileShortcutId the primary key of the file shortcut
408             * @throws PortalException if the file shortcut could not be found
409             * @throws SystemException if a system exception occurred
410             */
411            public void deleteFileShortcut(long fileShortcutId)
412                    throws PortalException, SystemException {
413    
414                    dlFileShortcutLocalService.deleteDLFileShortcut(fileShortcutId);
415            }
416    
417            /**
418             * Deletes all file shortcuts associated to the file entry. This method is
419             * only supported by the Liferay repository.
420             *
421             * @param  toFileEntryId the primary key of the associated file entry
422             * @throws PortalException if the file shortcut for the file entry could not
423             *         be found
424             * @throws SystemException if a system exception occurred
425             */
426            public void deleteFileShortcuts(long toFileEntryId)
427                    throws PortalException, SystemException {
428    
429                    dlFileShortcutLocalService.deleteFileShortcuts(toFileEntryId);
430            }
431    
432            /**
433             * Deletes the folder and all of its subfolders and file entries.
434             *
435             * @param  folderId the primary key of the folder
436             * @throws PortalException if the folder could not be found
437             * @throws SystemException if a system exception occurred
438             */
439            public void deleteFolder(long folderId)
440                    throws PortalException, SystemException {
441    
442                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
443    
444                    localRepository.deleteFolder(folderId);
445            }
446    
447            /**
448             * Returns the file entry with the primary key.
449             *
450             * @param  fileEntryId the primary key of the file entry
451             * @return the file entry with the primary key
452             * @throws PortalException if the file entry could not be found
453             * @throws SystemException if a system exception occurred
454             */
455            public FileEntry getFileEntry(long fileEntryId)
456                    throws PortalException, SystemException {
457    
458                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
459    
460                    return localRepository.getFileEntry(fileEntryId);
461            }
462    
463            /**
464             * Returns the file entry with the title in the folder.
465             *
466             * @param  groupId the primary key of the file entry's group
467             * @param  folderId the primary key of the file entry's folder
468             * @param  title the file entry's title
469             * @return the file entry with the title in the folder
470             * @throws PortalException if the file entry could not be found
471             * @throws SystemException if a system exception occurred
472             */
473            public FileEntry getFileEntry(long groupId, long folderId, String title)
474                    throws PortalException, SystemException {
475    
476                    try {
477                            LocalRepository localRepository = getLocalRepository(groupId);
478    
479                            return localRepository.getFileEntry(folderId, title);
480                    }
481                    catch (NoSuchFileEntryException nsfee) {
482                    }
483    
484                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
485    
486                    return localRepository.getFileEntry(folderId, title);
487            }
488    
489            /**
490             * Returns the file entry with the UUID and group.
491             *
492             * @param  uuid the file entry's universally unique identifier
493             * @param  groupId the primary key of the file entry's group
494             * @return the file entry with the UUID and group
495             * @throws PortalException if the file entry could not be found
496             * @throws SystemException if a system exception occurred
497             */
498            public FileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
499                    throws PortalException, SystemException {
500    
501                    try {
502                            LocalRepository localRepository = getLocalRepository(groupId);
503    
504                            return localRepository.getFileEntryByUuid(uuid);
505                    }
506                    catch (NoSuchFileEntryException nsfee) {
507                            List<com.liferay.portal.model.Repository> repositories =
508                                    repositoryPersistence.findByGroupId(groupId);
509    
510                            for (Repository repository : repositories) {
511                                    try {
512                                            LocalRepository localRepository = getLocalRepository(
513                                                    repository.getRepositoryId());
514    
515                                            return localRepository.getFileEntryByUuid(uuid);
516                                    }
517                                    catch (NoSuchFileEntryException nsfee2) {
518                                    }
519                            }
520                    }
521    
522                    StringBundler msg = new StringBundler(6);
523    
524                    msg.append("No DLFileEntry exists with the key {");
525                    msg.append("uuid=");
526                    msg.append(uuid);
527                    msg.append(", groupId=");
528                    msg.append(groupId);
529                    msg.append(StringPool.CLOSE_CURLY_BRACE);
530    
531                    throw new NoSuchFileEntryException(msg.toString());
532            }
533    
534            /**
535             * Returns the file ranks from the user. This method is only supported by
536             * the Liferay repository.
537             *
538             * @param  repositoryId the primary key of the repository
539             * @param  userId the primary key of the user
540             * @return the file ranks from the user
541             * @throws SystemException if a system exception occurred
542             */
543            public List<DLFileRank> getFileRanks(long repositoryId, long userId)
544                    throws SystemException {
545    
546                    return dlFileRankLocalService.getFileRanks(repositoryId, userId);
547            }
548    
549            /**
550             * Returns the file shortcut with the primary key. This method is only
551             * supported by the Liferay repository.
552             *
553             * @param  fileShortcutId the primary key of the file shortcut
554             * @return the file shortcut with the primary key
555             * @throws PortalException if the file shortcut could not be found
556             * @throws SystemException if a system exception occurred
557             */
558            public DLFileShortcut getFileShortcut(long fileShortcutId)
559                    throws PortalException, SystemException {
560    
561                    return dlFileShortcutLocalService.getFileShortcut(fileShortcutId);
562            }
563    
564            /**
565             * Returns the file version with the primary key.
566             *
567             * @param  fileVersionId the primary key of the file version
568             * @return the file version with the primary key
569             * @throws PortalException if the file version could not be found
570             * @throws SystemException if a system exception occurred
571             */
572            public FileVersion getFileVersion(long fileVersionId)
573                    throws PortalException, SystemException {
574    
575                    LocalRepository localRepository = getLocalRepository(
576                            0, 0, fileVersionId);
577    
578                    return localRepository.getFileVersion(fileVersionId);
579            }
580    
581            /**
582             * Returns the folder with the primary key.
583             *
584             * @param  folderId the primary key of the folder
585             * @return the folder with the primary key
586             * @throws PortalException if the folder could not be found
587             * @throws SystemException if a system exception occurred
588             */
589            public Folder getFolder(long folderId)
590                    throws PortalException, SystemException {
591    
592                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
593    
594                    return localRepository.getFolder(folderId);
595            }
596    
597            /**
598             * Returns the folder with the name in the parent folder.
599             *
600             * @param  repositoryId the primary key of the folder's repository
601             * @param  parentFolderId the primary key of the folder's parent folder
602             * @param  name the folder's name
603             * @return the folder with the name in the parent folder
604             * @throws PortalException if the folder could not be found
605             * @throws SystemException if a system exception occurred
606             */
607            public Folder getFolder(long repositoryId, long parentFolderId, String name)
608                    throws PortalException, SystemException {
609    
610                    LocalRepository localRepository = getLocalRepository(repositoryId);
611    
612                    return localRepository.getFolder(parentFolderId, name);
613            }
614    
615            /**
616             * Returns the mount folder of the repository with the primary key. This
617             * method is only supported by the Liferay repository.
618             *
619             * @param  repositoryId the primary key of the repository
620             * @return the folder used for mounting third-party repositories
621             * @throws PortalException if the repository or mount folder could not be
622             *         found
623             * @throws SystemException if a system exception occurred
624             */
625            public Folder getMountFolder(long repositoryId)
626                    throws PortalException, SystemException {
627    
628                    DLFolder dlFolder = dlFolderLocalService.getMountFolder(repositoryId);
629    
630                    return new LiferayFolder(dlFolder);
631            }
632    
633            /**
634             * Moves the file entry to the new folder.
635             *
636             * @param  userId the primary key of the user
637             * @param  fileEntryId the primary key of the file entry
638             * @param  newFolderId the primary key of the new folder
639             * @param  serviceContext the service context to be applied
640             * @return the file entry
641             * @throws PortalException if the file entry or the new folder could not be
642             *         found
643             * @throws SystemException if a system exception occurred
644             */
645            public FileEntry moveFileEntry(
646                            long userId, long fileEntryId, long newFolderId,
647                            ServiceContext serviceContext)
648                    throws PortalException, SystemException {
649    
650                    LocalRepository fromLocalRepository = getLocalRepository(
651                            0, fileEntryId, 0);
652                    LocalRepository toLocalRepository = getLocalRepository(
653                            newFolderId, serviceContext);
654    
655                    if (fromLocalRepository.getRepositoryId() ==
656                                    toLocalRepository.getRepositoryId()) {
657    
658                            // Move file entries within repository
659    
660                            return fromLocalRepository.moveFileEntry(
661                                    userId, fileEntryId, newFolderId, serviceContext);
662                    }
663    
664                    // Move file entries between repositories
665    
666                    return moveFileEntries(
667                            userId, fileEntryId, newFolderId, fromLocalRepository,
668                            toLocalRepository, serviceContext);
669            }
670    
671            /**
672             * Moves the file entry with the primary key to the trash portlet.
673             *
674             * @param  userId the primary key of the user
675             * @param  fileEntryId the primary key of the file entry
676             * @throws PortalException if the file entry could not be found
677             * @throws SystemException if a system exception occurred
678             */
679            public FileEntry moveFileEntryToTrash(long userId, long fileEntryId)
680                    throws PortalException, SystemException {
681    
682                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
683    
684                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
685    
686                    return dlAppHelperLocalService.moveFileEntryToTrash(userId, fileEntry);
687            }
688    
689            /**
690             * Restores the file entry with the primary key from the trash portlet.
691             *
692             * @param  userId the primary key of the user
693             * @param  fileEntryId the primary key of the file entry
694             * @throws PortalException if the file entry could not be found
695             * @throws SystemException if a system exception occurred
696             */
697            public void restoreFileEntryFromTrash(long userId, long fileEntryId)
698                    throws PortalException, SystemException {
699    
700                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
701    
702                    FileEntry fileEntry = localRepository.getFileEntry(fileEntryId);
703    
704                    dlAppHelperLocalService.restoreFileEntryFromTrash(userId, fileEntry);
705            }
706    
707            /**
708             * Updates the file entry's asset replacing its asset categories, tags, and
709             * links.
710             *
711             * @param  userId the primary key of the user
712             * @param  fileEntry the file entry to update
713             * @param  fileVersion the file version to update
714             * @param  assetCategoryIds the primary keys of the new asset categories
715             * @param  assetTagNames the new asset tag names
716             * @param  assetLinkEntryIds the primary keys of the new asset link entries
717             * @throws PortalException if the file entry or version could not be found
718             * @throws SystemException if a system exception occurred
719             */
720            public void updateAsset(
721                            long userId, FileEntry fileEntry, FileVersion fileVersion,
722                            long[] assetCategoryIds, String[] assetTagNames,
723                            long[] assetLinkEntryIds)
724                    throws PortalException, SystemException {
725    
726                    LocalRepository localRepository = getLocalRepository(
727                            0, fileEntry.getFileEntryId(), 0);
728    
729                    localRepository.updateAsset(
730                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
731                            assetLinkEntryIds);
732            }
733    
734            /**
735             * Updates a file entry and associated metadata based on a byte array
736             * object. If the file data is <code>null</code>, then only the associated
737             * metadata (i.e., <code>title</code>, <code>description</code>, and
738             * parameters in the <code>serviceContext</code>) will be updated.
739             *
740             * <p>
741             * This method takes two file names, the <code>sourceFileName</code> and the
742             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
743             * name of the actual file being uploaded. The <code>title</code>
744             * corresponds to a name the client wishes to assign this file after it has
745             * been uploaded to the portal.
746             * </p>
747             *
748             * @param  userId the primary key of the user
749             * @param  fileEntryId the primary key of the file entry
750             * @param  sourceFileName the original file's name (optionally
751             *         <code>null</code>)
752             * @param  mimeType the file's MIME type (optionally <code>null</code>)
753             * @param  title the new name to be assigned to the file (optionally <code>
754             *         <code>null</code></code>)
755             * @param  description the file's new description
756             * @param  changeLog the file's version change log (optionally
757             *         <code>null</code>)
758             * @param  majorVersion whether the new file version is a major version
759             * @param  bytes the file's data (optionally <code>null</code>)
760             * @param  serviceContext the service context to be applied. Can set the
761             *         asset category IDs, asset tag names, and expando bridge
762             *         attributes for the file entry. In a Liferay repository, it may
763             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
764             *         type </li> <li> fieldsMap - mapping for fields associated with a
765             *         custom file entry type </li> </ul>
766             * @return the file entry
767             * @throws PortalException if the file entry could not be found
768             * @throws SystemException if a system exception occurred
769             */
770            public FileEntry updateFileEntry(
771                            long userId, long fileEntryId, String sourceFileName,
772                            String mimeType, String title, String description, String changeLog,
773                            boolean majorVersion, byte[] bytes, ServiceContext serviceContext)
774                    throws PortalException, SystemException {
775    
776                    File file = null;
777    
778                    try {
779                            if ((bytes != null) && (bytes.length > 0)) {
780                                    file = FileUtil.createTempFile(bytes);
781                            }
782    
783                            return updateFileEntry(
784                                    userId, fileEntryId, sourceFileName, mimeType, title,
785                                    description, changeLog, majorVersion, file, serviceContext);
786                    }
787                    catch (IOException ioe) {
788                            throw new SystemException("Unable to write temporary file", ioe);
789                    }
790                    finally {
791                            FileUtil.delete(file);
792                    }
793            }
794    
795            /**
796             * Updates a file entry and associated metadata based on a {@link
797             * java.io.File} object. If the file data is <code>null</code>, then only
798             * the associated metadata (i.e., <code>title</code>,
799             * <code>description</code>, and parameters in the
800             * <code>serviceContext</code>) will be updated.
801             *
802             * <p>
803             * This method takes two file names, the <code>sourceFileName</code> and the
804             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
805             * name of the actual file being uploaded. The <code>title</code>
806             * corresponds to a name the client wishes to assign this file after it has
807             * been uploaded to the portal.
808             * </p>
809             *
810             * @param  userId the primary key of the user
811             * @param  fileEntryId the primary key of the file entry
812             * @param  sourceFileName the original file's name (optionally
813             *         <code>null</code>)
814             * @param  mimeType the file's MIME type (optionally <code>null</code>)
815             * @param  title the new name to be assigned to the file (optionally <code>
816             *         <code>null</code></code>)
817             * @param  description the file's new description
818             * @param  changeLog the file's version change log (optionally
819             *         <code>null</code>)
820             * @param  majorVersion whether the new file version is a major version
821             * @param  file EntryId the primary key of the file entry
822             * @param  serviceContext the service context to be applied. Can set the
823             *         asset category IDs, asset tag names, and expando bridge
824             *         attributes for the file entry. In a Liferay repository, it may
825             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
826             *         type </li> <li> fieldsMap - mapping for fields associated with a
827             *         custom file entry type </li> </ul>
828             * @return the file entry
829             * @throws PortalException if the file entry could not be found
830             * @throws SystemException if a system exception occurred
831             */
832            public FileEntry updateFileEntry(
833                            long userId, long fileEntryId, String sourceFileName,
834                            String mimeType, String title, String description, String changeLog,
835                            boolean majorVersion, File file, ServiceContext serviceContext)
836                    throws PortalException, SystemException {
837    
838                    if ((file == null) || !file.exists() || (file.length() == 0)) {
839                            return updateFileEntry(
840                                    userId, fileEntryId, sourceFileName, mimeType, title,
841                                    description, changeLog, majorVersion, null, 0, serviceContext);
842                    }
843    
844                    mimeType = DLAppUtil.getMimeType(
845                            sourceFileName, mimeType, title, file, null);
846    
847                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
848    
849                    FileEntry fileEntry = localRepository.updateFileEntry(
850                            userId, fileEntryId, sourceFileName, mimeType, title, description,
851                            changeLog, majorVersion, file, serviceContext);
852    
853                    DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion());
854    
855                    dlAppHelperLocalService.updateFileEntry(
856                            userId, fileEntry, null, fileEntry.getFileVersion(),
857                            serviceContext);
858    
859                    return fileEntry;
860            }
861    
862            /**
863             * Updates a file entry and associated metadata based on an {@link java.io.
864             * InputStream} object. If the file data is <code>null</code>, then only the
865             * associated metadata (i.e., <code>title</code>, <code>description</code>,
866             * and parameters in the <code>serviceContext</code>) will be updated.
867             *
868             * <p>
869             * This method takes two file names, the <code>sourceFileName</code> and the
870             * <code>title</code>. The <code>sourceFileName</code> corresponds to the
871             * name of the actual file being uploaded. The <code>title</code>
872             * corresponds to a name the client wishes to assign this file after it has
873             * been uploaded to the portal.
874             * </p>
875             *
876             * @param  userId the primary key of the user
877             * @param  fileEntryId the primary key of the file entry
878             * @param  sourceFileName the original file's name (optionally
879             *         <code>null</code>)
880             * @param  mimeType the file's MIME type (optionally <code>null</code>)
881             * @param  title the new name to be assigned to the file (optionally <code>
882             *         <code>null</code></code>)
883             * @param  description the file's new description
884             * @param  changeLog the file's version change log (optionally
885             *         <code>null</code>)
886             * @param  majorVersion whether the new file version is a major version
887             * @param  is the file's data (optionally <code>null</code>)
888             * @param  size the file's size (optionally <code>0</code>)
889             * @param  serviceContext the service context to be applied. Can set the
890             *         asset category IDs, asset tag names, and expando bridge
891             *         attributes for the file entry. In a Liferay repository, it may
892             *         include:  <ul> <li> fileEntryTypeId - ID for a custom file entry
893             *         type </li> <li> fieldsMap - mapping for fields associated with a
894             *         custom file entry type </li> </ul>
895             * @return the file entry
896             * @throws PortalException if the file entry could not be found
897             * @throws SystemException if a system exception occurred
898             */
899            public FileEntry updateFileEntry(
900                            long userId, long fileEntryId, String sourceFileName,
901                            String mimeType, String title, String description, String changeLog,
902                            boolean majorVersion, InputStream is, long size,
903                            ServiceContext serviceContext)
904                    throws PortalException, SystemException {
905    
906                    mimeType = DLAppUtil.getMimeType(
907                            sourceFileName, mimeType, title, null, is);
908    
909                    LocalRepository localRepository = getLocalRepository(0, fileEntryId, 0);
910    
911                    FileEntry oldFileEntry = localRepository.getFileEntry(fileEntryId);
912    
913                    FileVersion oldFileVersion = oldFileEntry.getFileVersion();
914    
915                    FileEntry fileEntry = localRepository.updateFileEntry(
916                            userId, fileEntryId, sourceFileName, mimeType, title, description,
917                            changeLog, majorVersion, is, size, serviceContext);
918    
919                    if (is != null) {
920                            DLProcessorRegistryUtil.cleanUp(fileEntry.getLatestFileVersion());
921    
922                            oldFileVersion = null;
923                    }
924    
925                    dlAppHelperLocalService.updateFileEntry(
926                            userId, fileEntry, oldFileVersion, fileEntry.getFileVersion(),
927                            serviceContext);
928    
929                    return fileEntry;
930            }
931    
932            /**
933             * Updates a file rank to the existing file entry. This method is only
934             * supported by the Liferay repository.
935             *
936             * @param  repositoryId the primary key of the file rank's repository
937             * @param  companyId the primary key of the file rank's company
938             * @param  userId the primary key of the file rank's creator/owner
939             * @param  fileEntryId the primary key of the file rank's file entry
940             * @param  serviceContext the service context to be applied
941             * @return the file rank
942             * @throws SystemException if a system exception occurred
943             */
944            public DLFileRank updateFileRank(
945                            long repositoryId, long companyId, long userId, long fileEntryId,
946                            ServiceContext serviceContext)
947                    throws SystemException {
948    
949                    return dlFileRankLocalService.updateFileRank(
950                            repositoryId, companyId, userId, fileEntryId, serviceContext);
951            }
952    
953            /**
954             * Updates a file shortcut to the existing file entry. This method is only
955             * supported by the Liferay repository.
956             *
957             * @param  userId the primary key of the file shortcut's creator/owner
958             * @param  fileShortcutId the primary key of the file shortcut
959             * @param  folderId the primary key of the file shortcut's parent folder
960             * @param  toFileEntryId the primary key of the file shortcut's file entry
961             * @param  serviceContext the service context to be applied. Can set the
962             *         asset category IDs, asset tag names, and expando bridge
963             *         attributes for the file entry.
964             * @return the file shortcut
965             * @throws PortalException if the file shortcut, folder, or file entry could
966             *         not be found
967             * @throws SystemException if a system exception occurred
968             */
969            public DLFileShortcut updateFileShortcut(
970                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
971                            ServiceContext serviceContext)
972                    throws PortalException, SystemException {
973    
974                    return dlFileShortcutLocalService.updateFileShortcut(
975                            userId, fileShortcutId, folderId, toFileEntryId, serviceContext);
976            }
977    
978            /**
979             * Updates all file shortcuts to the existing file entry to the new file
980             * entry. This method is only supported by the Liferay repository.
981             *
982             * @param  toRepositoryId the primary key of the repository
983             * @param  oldToFileEntryId the primary key of the old file entry pointed to
984             * @param  newToFileEntryId the primary key of the new file entry to point
985             *         to
986             * @throws SystemException if a system exception occurred
987             */
988            public void updateFileShortcuts(
989                            long toRepositoryId, long oldToFileEntryId, long newToFileEntryId)
990                    throws SystemException {
991    
992                    dlFileShortcutLocalService.updateFileShortcuts(
993                            oldToFileEntryId, newToFileEntryId);
994            }
995    
996            /**
997             * Updates the folder.
998             *
999             * @param  folderId the primary key of the folder
1000             * @param  parentFolderId the primary key of the folder's new parent folder
1001             * @param  name the folder's new name
1002             * @param  description the folder's new description
1003             * @param  serviceContext the service context to be applied. In a Liferay
1004             *         repository, it may include:  <ul> <li> defaultFileEntryTypeId -
1005             *         the file entry type to default all Liferay file entries to </li>
1006             *         <li> dlFileEntryTypesSearchContainerPrimaryKeys - a
1007             *         comma-delimited list of file entry type primary keys allowed in
1008             *         the given folder and all descendants </li> <li>
1009             *         overrideFileEntryTypes - boolean specifying whether to override
1010             *         ancestral folder's restriction of file entry types allowed </li>
1011             *         <li> workflowDefinitionXYZ - the workflow definition name
1012             *         specified per file entry type. The parameter name must be the
1013             *         string <code>workflowDefinition</code> appended by the <code>
1014             *         fileEntryTypeId</code> (optionally <code>0</code>). </li> </ul>
1015             * @return the folder
1016             * @throws PortalException if the current or new parent folder could not be
1017             *         found, or if the new parent folder's information was invalid
1018             * @throws SystemException if a system exception occurred
1019             */
1020            public Folder updateFolder(
1021                            long folderId, long parentFolderId, String name, String description,
1022                            ServiceContext serviceContext)
1023                    throws PortalException, SystemException {
1024    
1025                    LocalRepository localRepository = getLocalRepository(folderId, 0, 0);
1026    
1027                    return localRepository.updateFolder(
1028                            folderId, parentFolderId, name, description, serviceContext);
1029            }
1030    
1031            protected FileEntry copyFileEntry(
1032                            long userId, LocalRepository toLocalRepository, FileEntry fileEntry,
1033                            long newFolderId, ServiceContext serviceContext)
1034                    throws PortalException, SystemException {
1035    
1036                    List<FileVersion> fileVersions = fileEntry.getFileVersions(
1037                            WorkflowConstants.STATUS_ANY);
1038    
1039                    FileVersion latestFileVersion = fileVersions.get(
1040                            fileVersions.size() - 1);
1041    
1042                    FileEntry destinationFileEntry = toLocalRepository.addFileEntry(
1043                            userId, newFolderId, fileEntry.getTitle(),
1044                            latestFileVersion.getMimeType(), latestFileVersion.getTitle(),
1045                            latestFileVersion.getDescription(), StringPool.BLANK,
1046                            latestFileVersion.getContentStream(false),
1047                            latestFileVersion.getSize(), serviceContext);
1048    
1049                    for (int i = fileVersions.size() - 2; i >= 0; i--) {
1050                            FileVersion fileVersion = fileVersions.get(i);
1051    
1052                            FileVersion previousFileVersion = fileVersions.get(i + 1);
1053    
1054                            try {
1055                                    destinationFileEntry = toLocalRepository.updateFileEntry(
1056                                            userId, destinationFileEntry.getFileEntryId(),
1057                                            fileEntry.getTitle(), destinationFileEntry.getMimeType(),
1058                                            destinationFileEntry.getTitle(),
1059                                            destinationFileEntry.getDescription(), StringPool.BLANK,
1060                                            DLAppUtil.isMajorVersion(fileVersion, previousFileVersion),
1061                                            fileVersion.getContentStream(false), fileVersion.getSize(),
1062                                            serviceContext);
1063                            }
1064                            catch (PortalException pe) {
1065                                    toLocalRepository.deleteFileEntry(
1066                                            destinationFileEntry.getFileEntryId());
1067    
1068                                    throw pe;
1069                            }
1070                    }
1071    
1072                    dlAppHelperLocalService.addFileEntry(
1073                            userId, destinationFileEntry, destinationFileEntry.getFileVersion(),
1074                            serviceContext);
1075    
1076                    return destinationFileEntry;
1077            }
1078    
1079            protected void deleteFileEntry(
1080                            long oldFileEntryId, long newFileEntryId,
1081                            LocalRepository fromLocalRepository,
1082                            LocalRepository toLocalRepository)
1083                    throws PortalException, SystemException {
1084    
1085                    try {
1086                            FileEntry fileEntry = fromLocalRepository.getFileEntry(
1087                                    oldFileEntryId);
1088    
1089                            fromLocalRepository.deleteFileEntry(oldFileEntryId);
1090    
1091                            dlAppHelperLocalService.deleteFileEntry(fileEntry);
1092                    }
1093                    catch (PortalException pe) {
1094                            FileEntry fileEntry = toLocalRepository.getFileEntry(
1095                                    newFileEntryId);
1096    
1097                            toLocalRepository.deleteFileEntry(newFileEntryId);
1098    
1099                            dlAppHelperLocalService.deleteFileEntry(fileEntry);
1100    
1101                            throw pe;
1102                    }
1103            }
1104    
1105            protected LocalRepository getLocalRepository(long repositoryId)
1106                    throws PortalException, SystemException {
1107    
1108                    return repositoryLocalService.getLocalRepositoryImpl(repositoryId);
1109            }
1110    
1111            protected LocalRepository getLocalRepository(
1112                            long folderId, long fileEntryId, long fileVersionId)
1113                    throws PortalException, SystemException {
1114    
1115                    return repositoryLocalService.getLocalRepositoryImpl(
1116                            folderId, fileEntryId, fileVersionId);
1117            }
1118    
1119            protected LocalRepository getLocalRepository(
1120                            long folderId, ServiceContext serviceContext)
1121                    throws PortalException, SystemException {
1122    
1123                    LocalRepository localRepository = null;
1124    
1125                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1126                            localRepository = getLocalRepository(
1127                                    serviceContext.getScopeGroupId());
1128                    }
1129                    else {
1130                            localRepository = getLocalRepository(folderId, 0, 0);
1131                    }
1132    
1133                    return localRepository;
1134            }
1135    
1136            protected FileEntry moveFileEntries(
1137                            long userId, long fileEntryId, long newFolderId,
1138                            LocalRepository fromLocalRepository,
1139                            LocalRepository toLocalRepository, ServiceContext serviceContext)
1140                    throws PortalException, SystemException {
1141    
1142                    FileEntry sourceFileEntry = fromLocalRepository.getFileEntry(
1143                            fileEntryId);
1144    
1145                    FileEntry destinationFileEntry = copyFileEntry(
1146                            userId, toLocalRepository, sourceFileEntry, newFolderId,
1147                            serviceContext);
1148    
1149                    deleteFileEntry(
1150                            fileEntryId, destinationFileEntry.getFileEntryId(),
1151                            fromLocalRepository, toLocalRepository);
1152    
1153                    return destinationFileEntry;
1154            }
1155    
1156    }