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.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.LocalRepository;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.FileShortcut;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.repository.model.RepositoryEntry;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.SortedArrayList;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
031    import com.liferay.portal.repository.liferayrepository.model.LiferayFileShortcut;
032    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
033    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
034    import com.liferay.portal.service.RepositoryLocalService;
035    import com.liferay.portal.service.RepositoryService;
036    import com.liferay.portal.service.ResourceLocalService;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
040    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
041    import com.liferay.portlet.documentlibrary.model.DLFolder;
042    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
043    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
044    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
045    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService;
046    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalService;
047    import com.liferay.portlet.documentlibrary.service.DLFileShortcutService;
048    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
049    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
050    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
051    import com.liferay.portlet.documentlibrary.service.DLFolderService;
052    import com.liferay.portlet.documentlibrary.util.RepositoryModelUtil;
053    import com.liferay.portlet.documentlibrary.util.comparator.DLFileEntryOrderByComparator;
054    import com.liferay.portlet.documentlibrary.util.comparator.DLFolderOrderByComparator;
055    import com.liferay.portlet.dynamicdatamapping.DDMFormValues;
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 getFoldersCount(
409                    long parentFolderId, boolean includeMountfolders) {
410    
411                    return getFoldersCount(
412                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
413                            includeMountfolders);
414            }
415    
416            @Override
417            public int getFoldersCount(
418                    long parentFolderId, int status, boolean includeMountfolders) {
419    
420                    return dlFolderLocalService.getFoldersCount(
421                            getGroupId(), toFolderId(parentFolderId), status,
422                            includeMountfolders);
423            }
424    
425            @Override
426            public List<FileEntry> getRepositoryFileEntries(
427                    long userId, long rootFolderId, int start, int end,
428                    OrderByComparator<FileEntry> obc) {
429    
430                    List<DLFileEntry> dlFileEntries =
431                            dlFileEntryLocalService.getGroupFileEntries(
432                                    getGroupId(), 0, getRepositoryId(), toFolderId(rootFolderId),
433                                    start, end,
434                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
435    
436                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
437            }
438    
439            @Override
440            public FileEntry moveFileEntry(
441                            long userId, long fileEntryId, long newFolderId,
442                            ServiceContext serviceContext)
443                    throws PortalException {
444    
445                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
446                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
447    
448                    return new LiferayFileEntry(dlFileEntry);
449            }
450    
451            @Override
452            public Folder moveFolder(
453                            long userId, long folderId, long parentFolderId,
454                            ServiceContext serviceContext)
455                    throws PortalException {
456    
457                    DLFolder dlFolder = dlFolderLocalService.moveFolder(
458                            userId, toFolderId(folderId), toFolderId(parentFolderId),
459                            serviceContext);
460    
461                    return new LiferayFolder(dlFolder);
462            }
463    
464            @Override
465            public void revertFileEntry(
466                            long userId, long fileEntryId, String version,
467                            ServiceContext serviceContext)
468                    throws PortalException {
469    
470                    dlFileEntryLocalService.revertFileEntry(
471                            userId, fileEntryId, version, serviceContext);
472            }
473    
474            /**
475             * @deprecated As of 7.0.0
476             */
477            @Deprecated
478            @Override
479            public void updateAsset(
480                            long userId, FileEntry fileEntry, FileVersion fileVersion,
481                            long[] assetCategoryIds, String[] assetTagNames,
482                            long[] assetLinkEntryIds)
483                    throws PortalException {
484    
485                    dlAppHelperLocalService.updateAsset(
486                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
487                            assetLinkEntryIds);
488            }
489    
490            @Override
491            public FileEntry updateFileEntry(
492                            long userId, long fileEntryId, String sourceFileName,
493                            String mimeType, String title, String description, String changeLog,
494                            boolean majorVersion, File file, ServiceContext serviceContext)
495                    throws PortalException {
496    
497                    long fileEntryTypeId = ParamUtil.getLong(
498                            serviceContext, "fileEntryTypeId", -1L);
499    
500                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
501                            serviceContext, fileEntryTypeId);
502    
503                    long size = 0;
504    
505                    if (file != null) {
506                            size = file.length();
507                    }
508    
509                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
510                            userId, fileEntryId, sourceFileName, mimeType, title, description,
511                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, file,
512                            null, size, serviceContext);
513    
514                    return new LiferayFileEntry(dlFileEntry);
515            }
516    
517            @Override
518            public FileEntry updateFileEntry(
519                            long userId, long fileEntryId, String sourceFileName,
520                            String mimeType, String title, String description, String changeLog,
521                            boolean majorVersion, InputStream is, long size,
522                            ServiceContext serviceContext)
523                    throws PortalException {
524    
525                    long fileEntryTypeId = ParamUtil.getLong(
526                            serviceContext, "fileEntryTypeId", -1L);
527    
528                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
529                            serviceContext, fileEntryTypeId);
530    
531                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
532                            userId, fileEntryId, sourceFileName, mimeType, title, description,
533                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, null,
534                            is, size, serviceContext);
535    
536                    return new LiferayFileEntry(dlFileEntry);
537            }
538    
539            @Override
540            public FileShortcut updateFileShortcut(
541                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
542                            ServiceContext serviceContext)
543                    throws PortalException {
544    
545                    DLFileShortcut dlFileShortcut =
546                            dlFileShortcutLocalService.updateFileShortcut(
547                                    userId, fileShortcutId, getRepositoryId(), toFolderId(folderId),
548                                    toFileEntryId, serviceContext);
549    
550                    return new LiferayFileShortcut(dlFileShortcut);
551            }
552    
553            @Override
554            public void updateFileShortcuts(
555                    long oldToFileEntryId, long newToFileEntryId) {
556    
557                    dlFileShortcutLocalService.updateFileShortcuts(
558                            oldToFileEntryId, newToFileEntryId);
559            }
560    
561            @Override
562            public Folder updateFolder(
563                            long folderId, long parentFolderId, String name, String description,
564                            ServiceContext serviceContext)
565                    throws PortalException {
566    
567                    long defaultFileEntryTypeId = ParamUtil.getLong(
568                            serviceContext, "defaultFileEntryTypeId");
569                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
570                            serviceContext, "dlFileEntryTypesSearchContainerPrimaryKeys");
571                    int restrictionType = ParamUtil.getInteger(
572                            serviceContext, "restrictionType");
573    
574                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
575                            toFolderId(folderId), toFolderId(parentFolderId), name, description,
576                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
577                            serviceContext);
578    
579                    return new LiferayFolder(dlFolder);
580            }
581    
582            public UnicodeProperties updateRepository(
583                    UnicodeProperties typeSettingsProperties) {
584    
585                    return typeSettingsProperties;
586            }
587    
588    }