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