001    /**
002     * Copyright (c) 2000-present 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.portal.repository.liferayrepository;
016    
017    import com.liferay.document.library.kernel.model.DLFileEntry;
018    import com.liferay.document.library.kernel.model.DLFileShortcut;
019    import com.liferay.document.library.kernel.model.DLFileVersion;
020    import com.liferay.document.library.kernel.model.DLFolder;
021    import com.liferay.document.library.kernel.service.DLAppHelperLocalService;
022    import com.liferay.document.library.kernel.service.DLFileEntryLocalService;
023    import com.liferay.document.library.kernel.service.DLFileEntryService;
024    import com.liferay.document.library.kernel.service.DLFileEntryTypeLocalService;
025    import com.liferay.document.library.kernel.service.DLFileShortcutLocalService;
026    import com.liferay.document.library.kernel.service.DLFileShortcutService;
027    import com.liferay.document.library.kernel.service.DLFileVersionLocalService;
028    import com.liferay.document.library.kernel.service.DLFileVersionService;
029    import com.liferay.document.library.kernel.service.DLFolderLocalService;
030    import com.liferay.document.library.kernel.service.DLFolderService;
031    import com.liferay.dynamic.data.mapping.kernel.DDMFormValues;
032    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
033    import com.liferay.portal.kernel.exception.PortalException;
034    import com.liferay.portal.kernel.repository.LocalRepository;
035    import com.liferay.portal.kernel.repository.model.FileEntry;
036    import com.liferay.portal.kernel.repository.model.FileShortcut;
037    import com.liferay.portal.kernel.repository.model.FileVersion;
038    import com.liferay.portal.kernel.repository.model.Folder;
039    import com.liferay.portal.kernel.repository.model.RepositoryEntry;
040    import com.liferay.portal.kernel.service.RepositoryLocalService;
041    import com.liferay.portal.kernel.service.RepositoryService;
042    import com.liferay.portal.kernel.service.ResourceLocalService;
043    import com.liferay.portal.kernel.service.ServiceContext;
044    import com.liferay.portal.kernel.util.OrderByComparator;
045    import com.liferay.portal.kernel.util.ParamUtil;
046    import com.liferay.portal.kernel.util.SortedArrayList;
047    import com.liferay.portal.kernel.util.UnicodeProperties;
048    import com.liferay.portal.kernel.workflow.WorkflowConstants;
049    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
050    import com.liferay.portal.repository.liferayrepository.model.LiferayFileShortcut;
051    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
052    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
053    import com.liferay.portlet.documentlibrary.util.RepositoryModelUtil;
054    import com.liferay.portlet.documentlibrary.util.comparator.DLFileEntryOrderByComparator;
055    import com.liferay.portlet.documentlibrary.util.comparator.DLFolderOrderByComparator;
056    
057    import java.io.File;
058    import java.io.InputStream;
059    
060    import java.util.List;
061    import java.util.Map;
062    
063    /**
064     * @author Alexander Chow
065     */
066    public class LiferayLocalRepository
067            extends LiferayRepositoryBase implements LocalRepository {
068    
069            public LiferayLocalRepository(
070                    RepositoryLocalService repositoryLocalService,
071                    RepositoryService repositoryService,
072                    DLAppHelperLocalService dlAppHelperLocalService,
073                    DLFileEntryLocalService dlFileEntryLocalService,
074                    DLFileEntryService dlFileEntryService,
075                    DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
076                    DLFileShortcutLocalService dlFileShortcutLocalService,
077                    DLFileShortcutService dlFileShortcutService,
078                    DLFileVersionLocalService dlFileVersionLocalService,
079                    DLFileVersionService dlFileVersionService,
080                    DLFolderLocalService dlFolderLocalService,
081                    DLFolderService dlFolderService,
082                    ResourceLocalService resourceLocalService, long groupId,
083                    long repositoryId, long dlFolderId) {
084    
085                    super(
086                            repositoryLocalService, repositoryService, dlAppHelperLocalService,
087                            dlFileEntryLocalService, dlFileEntryService,
088                            dlFileEntryTypeLocalService, dlFileShortcutLocalService,
089                            dlFileShortcutService, dlFileVersionLocalService,
090                            dlFileVersionService, dlFolderLocalService, dlFolderService,
091                            resourceLocalService, groupId, repositoryId, dlFolderId);
092            }
093    
094            @Override
095            public FileEntry addFileEntry(
096                            long userId, long folderId, String sourceFileName, String mimeType,
097                            String title, String description, String changeLog, File file,
098                            ServiceContext serviceContext)
099                    throws PortalException {
100    
101                    long fileEntryTypeId = ParamUtil.getLong(
102                            serviceContext, "fileEntryTypeId",
103                            getDefaultFileEntryTypeId(serviceContext, folderId));
104    
105                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
106                            serviceContext, fileEntryTypeId);
107    
108                    long size = 0;
109    
110                    if (file != null) {
111                            size = file.length();
112                    }
113    
114                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
115                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
116                            sourceFileName, mimeType, title, description, changeLog,
117                            fileEntryTypeId, ddmFormValuesMap, file, null, size,
118                            serviceContext);
119    
120                    addFileEntryResources(dlFileEntry, serviceContext);
121    
122                    return new LiferayFileEntry(dlFileEntry);
123            }
124    
125            @Override
126            public FileEntry addFileEntry(
127                            long userId, long folderId, String sourceFileName, String mimeType,
128                            String title, String description, String changeLog, InputStream is,
129                            long size, ServiceContext serviceContext)
130                    throws PortalException {
131    
132                    long fileEntryTypeId = ParamUtil.getLong(
133                            serviceContext, "fileEntryTypeId",
134                            getDefaultFileEntryTypeId(serviceContext, folderId));
135    
136                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
137                            serviceContext, fileEntryTypeId);
138    
139                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
140                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
141                            sourceFileName, mimeType, title, description, changeLog,
142                            fileEntryTypeId, ddmFormValuesMap, null, is, size, serviceContext);
143    
144                    addFileEntryResources(dlFileEntry, serviceContext);
145    
146                    return new LiferayFileEntry(dlFileEntry);
147            }
148    
149            @Override
150            public FileShortcut addFileShortcut(
151                            long userId, long folderId, long toFileEntryId,
152                            ServiceContext serviceContext)
153                    throws PortalException {
154    
155                    DLFileShortcut dlFileShortcut =
156                            dlFileShortcutLocalService.addFileShortcut(
157                                    userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
158                                    toFileEntryId, serviceContext);
159    
160                    return new LiferayFileShortcut(dlFileShortcut);
161            }
162    
163            @Override
164            public Folder addFolder(
165                            long userId, long parentFolderId, String name, String description,
166                            ServiceContext serviceContext)
167                    throws PortalException {
168    
169                    boolean mountPoint = ParamUtil.getBoolean(serviceContext, "mountPoint");
170    
171                    DLFolder dlFolder = dlFolderLocalService.addFolder(
172                            userId, getGroupId(), getRepositoryId(), mountPoint,
173                            toFolderId(parentFolderId), name, description, false,
174                            serviceContext);
175    
176                    return new LiferayFolder(dlFolder);
177            }
178    
179            @Override
180            public void checkInFileEntry(
181                            long userId, long fileEntryId, boolean major, String changeLog,
182                            ServiceContext serviceContext)
183                    throws PortalException {
184    
185                    dlFileEntryLocalService.checkInFileEntry(
186                            userId, fileEntryId, major, changeLog, serviceContext);
187            }
188    
189            @Override
190            public void checkInFileEntry(
191                            long userId, long fileEntryId, String lockUuid,
192                            ServiceContext serviceContext)
193                    throws PortalException {
194    
195                    dlFileEntryLocalService.checkInFileEntry(
196                            userId, fileEntryId, lockUuid, serviceContext);
197            }
198    
199            @Override
200            public FileEntry copyFileEntry(
201                            long userId, long groupId, long fileEntryId, long destFolderId,
202                            ServiceContext serviceContext)
203                    throws PortalException {
204    
205                    DLFileEntry dlFileEntry = dlFileEntryLocalService.copyFileEntry(
206                            userId, groupId, getRepositoryId(), fileEntryId,
207                            toFolderId(destFolderId), serviceContext);
208    
209                    return new LiferayFileEntry(dlFileEntry);
210            }
211    
212            @Override
213            public void deleteAll() throws PortalException {
214                    dlFolderLocalService.deleteAllByRepository(getRepositoryId());
215            }
216    
217            @Override
218            public void deleteFileEntry(long fileEntryId) throws PortalException {
219                    dlFileEntryLocalService.deleteFileEntry(fileEntryId);
220            }
221    
222            @Override
223            public void deleteFileShortcut(long fileShortcutId) throws PortalException {
224                    dlFileShortcutLocalService.deleteFileShortcut(fileShortcutId);
225            }
226    
227            @Override
228            public void deleteFileShortcuts(long toFileEntryId) throws PortalException {
229                    dlFileShortcutLocalService.deleteFileShortcuts(toFileEntryId);
230            }
231    
232            @Override
233            public void deleteFolder(long folderId) throws PortalException {
234                    DLFolder dlFolder = dlFolderLocalService.fetchFolder(folderId);
235    
236                    if (dlFolder != null) {
237                            dlFolderLocalService.deleteFolder(folderId);
238                    }
239            }
240    
241            @Override
242            public List<FileEntry> getFileEntries(
243                    long folderId, int status, int start, int end,
244                    OrderByComparator<FileEntry> obc) {
245    
246                    List<DLFileEntry> dlFileEntries =
247                            dlFileEntryLocalService.getFileEntries(
248                                    getGroupId(), toFolderId(folderId), status, start, end,
249                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
250    
251                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
252            }
253    
254            @Override
255            public List<FileEntry> getFileEntries(
256                    long folderId, int start, int end, OrderByComparator<FileEntry> obc) {
257    
258                    List<DLFileEntry> dlFileEntries =
259                            dlFileEntryLocalService.getFileEntries(
260                                    getGroupId(), toFolderId(folderId), start, end,
261                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
262    
263                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
264            }
265    
266            @Override
267            public List<RepositoryEntry> getFileEntriesAndFileShortcuts(
268                    long folderId, int status, int start, int end) {
269    
270                    QueryDefinition<RepositoryEntry> queryDefinition =
271                            new QueryDefinition<>(status, start, end, null);
272    
273                    List<Object> dlFileEntriesAndFileShortcuts =
274                            dlFolderLocalService.getFileEntriesAndFileShortcuts(
275                                    getGroupId(), toFolderId(folderId), queryDefinition);
276    
277                    return RepositoryModelUtil.toRepositoryEntries(
278                            dlFileEntriesAndFileShortcuts);
279            }
280    
281            @Override
282            public int getFileEntriesAndFileShortcutsCount(long folderId, int status) {
283                    QueryDefinition<RepositoryEntry> queryDefinition =
284                            new QueryDefinition<>(status);
285    
286                    return dlFolderLocalService.getFileEntriesAndFileShortcutsCount(
287                            getGroupId(), toFolderId(folderId), queryDefinition);
288            }
289    
290            @Override
291            public int getFileEntriesCount(long folderId) {
292                    return dlFileEntryLocalService.getFileEntriesCount(
293                            getGroupId(), toFolderId(folderId));
294            }
295    
296            @Override
297            public int getFileEntriesCount(long folderId, int status) {
298                    return dlFileEntryLocalService.getFileEntriesCount(
299                            getGroupId(), toFolderId(folderId), status);
300            }
301    
302            @Override
303            public FileEntry getFileEntry(long fileEntryId) throws PortalException {
304                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
305                            fileEntryId);
306    
307                    return new LiferayFileEntry(dlFileEntry);
308            }
309    
310            @Override
311            public FileEntry getFileEntry(long folderId, String title)
312                    throws PortalException {
313    
314                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
315                            getGroupId(), toFolderId(folderId), title);
316    
317                    return new LiferayFileEntry(dlFileEntry);
318            }
319    
320            @Override
321            public FileEntry getFileEntryByUuid(String uuid) throws PortalException {
322                    DLFileEntry dlFileEntry =
323                            dlFileEntryLocalService.getFileEntryByUuidAndGroupId(
324                                    uuid, getGroupId());
325    
326                    return new LiferayFileEntry(dlFileEntry);
327            }
328    
329            @Override
330            public FileShortcut getFileShortcut(long fileShortcutId)
331                    throws PortalException {
332    
333                    DLFileShortcut dlFileShortcut =
334                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
335    
336                    return new LiferayFileShortcut(dlFileShortcut);
337            }
338    
339            @Override
340            public FileVersion getFileVersion(long fileVersionId)
341                    throws PortalException {
342    
343                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
344                            fileVersionId);
345    
346                    return new LiferayFileVersion(dlFileVersion);
347            }
348    
349            @Override
350            public Folder getFolder(long folderId) throws PortalException {
351                    DLFolder dlFolder = dlFolderLocalService.getFolder(
352                            toFolderId(folderId));
353    
354                    return new LiferayFolder(dlFolder);
355            }
356    
357            @Override
358            public Folder getFolder(long parentFolderId, String name)
359                    throws PortalException {
360    
361                    DLFolder dlFolder = dlFolderLocalService.getFolder(
362                            getGroupId(), toFolderId(parentFolderId), name);
363    
364                    return new LiferayFolder(dlFolder);
365            }
366    
367            @Override
368            public List<Folder> getFolders(
369                    long parentFolderId, boolean includeMountfolders, int start, int end,
370                    OrderByComparator<Folder> obc) {
371    
372                    return getFolders(
373                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
374                            includeMountfolders, start, end, obc);
375            }
376    
377            @Override
378            public List<Folder> getFolders(
379                    long parentFolderId, int status, boolean includeMountfolders, int start,
380                    int end, OrderByComparator<Folder> obc) {
381    
382                    List<DLFolder> dlFolders = dlFolderLocalService.getFolders(
383                            getGroupId(), toFolderId(parentFolderId), status,
384                            includeMountfolders, start, end,
385                            DLFolderOrderByComparator.getOrderByComparator(obc));
386    
387                    return RepositoryModelUtil.toFolders(dlFolders);
388            }
389    
390            @Override
391            public List<RepositoryEntry> getFoldersAndFileEntriesAndFileShortcuts(
392                    long folderId, int status, boolean includeMountFolders, int start,
393                    int end, OrderByComparator<?> obc) {
394    
395                    QueryDefinition<Object> queryDefinition = new QueryDefinition<>(
396                            status, start, end, (OrderByComparator<Object>)obc);
397    
398                    List<Object> dlFoldersAndDLFileEntriesAndDLFileShortcuts =
399                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
400                                    getGroupId(), toFolderId(folderId), null, includeMountFolders,
401                                    queryDefinition);
402    
403                    return RepositoryModelUtil.toRepositoryEntries(
404                            dlFoldersAndDLFileEntriesAndDLFileShortcuts);
405            }
406    
407            @Override
408            public int getFoldersAndFileEntriesAndFileShortcutsCount(
409                    long folderId, int status, boolean includeMountFolders) {
410    
411                    QueryDefinition<Object> queryDefinition = new QueryDefinition<>(status);
412    
413                    return dlFolderLocalService.
414                            getFoldersAndFileEntriesAndFileShortcutsCount(
415                                    getGroupId(), toFolderId(folderId), null, includeMountFolders,
416                                    queryDefinition);
417            }
418    
419            @Override
420            public int getFoldersCount(
421                    long parentFolderId, boolean includeMountfolders) {
422    
423                    return getFoldersCount(
424                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
425                            includeMountfolders);
426            }
427    
428            @Override
429            public int getFoldersCount(
430                    long parentFolderId, int status, boolean includeMountfolders) {
431    
432                    return dlFolderLocalService.getFoldersCount(
433                            getGroupId(), toFolderId(parentFolderId), status,
434                            includeMountfolders);
435            }
436    
437            @Override
438            public List<FileEntry> getRepositoryFileEntries(
439                    long userId, long rootFolderId, int start, int end,
440                    OrderByComparator<FileEntry> obc) {
441    
442                    List<DLFileEntry> dlFileEntries =
443                            dlFileEntryLocalService.getGroupFileEntries(
444                                    getGroupId(), 0, getRepositoryId(), toFolderId(rootFolderId),
445                                    start, end,
446                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
447    
448                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
449            }
450    
451            @Override
452            public FileEntry moveFileEntry(
453                            long userId, long fileEntryId, long newFolderId,
454                            ServiceContext serviceContext)
455                    throws PortalException {
456    
457                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
458                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
459    
460                    return new LiferayFileEntry(dlFileEntry);
461            }
462    
463            @Override
464            public Folder moveFolder(
465                            long userId, long folderId, long parentFolderId,
466                            ServiceContext serviceContext)
467                    throws PortalException {
468    
469                    DLFolder dlFolder = dlFolderLocalService.moveFolder(
470                            userId, toFolderId(folderId), toFolderId(parentFolderId),
471                            serviceContext);
472    
473                    return new LiferayFolder(dlFolder);
474            }
475    
476            @Override
477            public void revertFileEntry(
478                            long userId, long fileEntryId, String version,
479                            ServiceContext serviceContext)
480                    throws PortalException {
481    
482                    dlFileEntryLocalService.revertFileEntry(
483                            userId, fileEntryId, version, serviceContext);
484            }
485    
486            /**
487             * @deprecated As of 7.0.0
488             */
489            @Deprecated
490            @Override
491            public void updateAsset(
492                            long userId, FileEntry fileEntry, FileVersion fileVersion,
493                            long[] assetCategoryIds, String[] assetTagNames,
494                            long[] assetLinkEntryIds)
495                    throws PortalException {
496    
497                    dlAppHelperLocalService.updateAsset(
498                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
499                            assetLinkEntryIds);
500            }
501    
502            @Override
503            public FileEntry updateFileEntry(
504                            long userId, long fileEntryId, String sourceFileName,
505                            String mimeType, String title, String description, String changeLog,
506                            boolean majorVersion, File file, ServiceContext serviceContext)
507                    throws PortalException {
508    
509                    long fileEntryTypeId = ParamUtil.getLong(
510                            serviceContext, "fileEntryTypeId", -1L);
511    
512                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
513                            serviceContext, fileEntryTypeId);
514    
515                    long size = 0;
516    
517                    if (file != null) {
518                            size = file.length();
519                    }
520    
521                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
522                            userId, fileEntryId, sourceFileName, mimeType, title, description,
523                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, file,
524                            null, size, serviceContext);
525    
526                    return new LiferayFileEntry(dlFileEntry);
527            }
528    
529            @Override
530            public FileEntry updateFileEntry(
531                            long userId, long fileEntryId, String sourceFileName,
532                            String mimeType, String title, String description, String changeLog,
533                            boolean majorVersion, InputStream is, long size,
534                            ServiceContext serviceContext)
535                    throws PortalException {
536    
537                    long fileEntryTypeId = ParamUtil.getLong(
538                            serviceContext, "fileEntryTypeId", -1L);
539    
540                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
541                            serviceContext, fileEntryTypeId);
542    
543                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
544                            userId, fileEntryId, sourceFileName, mimeType, title, description,
545                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, null,
546                            is, size, serviceContext);
547    
548                    return new LiferayFileEntry(dlFileEntry);
549            }
550    
551            @Override
552            public FileShortcut updateFileShortcut(
553                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
554                            ServiceContext serviceContext)
555                    throws PortalException {
556    
557                    DLFileShortcut dlFileShortcut =
558                            dlFileShortcutLocalService.updateFileShortcut(
559                                    userId, fileShortcutId, getRepositoryId(), toFolderId(folderId),
560                                    toFileEntryId, serviceContext);
561    
562                    return new LiferayFileShortcut(dlFileShortcut);
563            }
564    
565            @Override
566            public void updateFileShortcuts(
567                    long oldToFileEntryId, long newToFileEntryId) {
568    
569                    dlFileShortcutLocalService.updateFileShortcuts(
570                            oldToFileEntryId, newToFileEntryId);
571            }
572    
573            @Override
574            public Folder updateFolder(
575                            long folderId, long parentFolderId, String name, String description,
576                            ServiceContext serviceContext)
577                    throws PortalException {
578    
579                    long defaultFileEntryTypeId = ParamUtil.getLong(
580                            serviceContext, "defaultFileEntryTypeId");
581                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
582                            serviceContext, "dlFileEntryTypesSearchContainerPrimaryKeys");
583                    int restrictionType = ParamUtil.getInteger(
584                            serviceContext, "restrictionType");
585    
586                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
587                            toFolderId(folderId), toFolderId(parentFolderId), name, description,
588                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
589                            serviceContext);
590    
591                    return new LiferayFolder(dlFolder);
592            }
593    
594            public UnicodeProperties updateRepository(
595                    UnicodeProperties typeSettingsProperties) {
596    
597                    return typeSettingsProperties;
598            }
599    
600    }