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                    return new LiferayFileEntry(dlFileEntry);
121            }
122    
123            @Override
124            public FileEntry addFileEntry(
125                            long userId, long folderId, String sourceFileName, String mimeType,
126                            String title, String description, String changeLog, InputStream is,
127                            long size, ServiceContext serviceContext)
128                    throws PortalException {
129    
130                    long fileEntryTypeId = ParamUtil.getLong(
131                            serviceContext, "fileEntryTypeId",
132                            getDefaultFileEntryTypeId(serviceContext, folderId));
133    
134                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
135                            serviceContext, fileEntryTypeId);
136    
137                    DLFileEntry dlFileEntry = dlFileEntryLocalService.addFileEntry(
138                            userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
139                            sourceFileName, mimeType, title, description, changeLog,
140                            fileEntryTypeId, ddmFormValuesMap, null, is, size, serviceContext);
141    
142                    return new LiferayFileEntry(dlFileEntry);
143            }
144    
145            @Override
146            public FileShortcut addFileShortcut(
147                            long userId, long folderId, long toFileEntryId,
148                            ServiceContext serviceContext)
149                    throws PortalException {
150    
151                    DLFileShortcut dlFileShortcut =
152                            dlFileShortcutLocalService.addFileShortcut(
153                                    userId, getGroupId(), getRepositoryId(), toFolderId(folderId),
154                                    toFileEntryId, serviceContext);
155    
156                    return new LiferayFileShortcut(dlFileShortcut);
157            }
158    
159            @Override
160            public Folder addFolder(
161                            long userId, long parentFolderId, String name, String description,
162                            ServiceContext serviceContext)
163                    throws PortalException {
164    
165                    boolean mountPoint = ParamUtil.getBoolean(serviceContext, "mountPoint");
166    
167                    DLFolder dlFolder = dlFolderLocalService.addFolder(
168                            userId, getGroupId(), getRepositoryId(), mountPoint,
169                            toFolderId(parentFolderId), name, description, false,
170                            serviceContext);
171    
172                    return new LiferayFolder(dlFolder);
173            }
174    
175            @Override
176            public void checkInFileEntry(
177                            long userId, long fileEntryId, boolean majorVersion,
178                            String changeLog, ServiceContext serviceContext)
179                    throws PortalException {
180    
181                    dlFileEntryLocalService.checkInFileEntry(
182                            userId, fileEntryId, majorVersion, changeLog, serviceContext);
183            }
184    
185            @Override
186            public void checkInFileEntry(
187                            long userId, long fileEntryId, String lockUuid,
188                            ServiceContext serviceContext)
189                    throws PortalException {
190    
191                    dlFileEntryLocalService.checkInFileEntry(
192                            userId, fileEntryId, lockUuid, serviceContext);
193            }
194    
195            @Override
196            public FileEntry copyFileEntry(
197                            long userId, long groupId, long fileEntryId, long destFolderId,
198                            ServiceContext serviceContext)
199                    throws PortalException {
200    
201                    DLFileEntry dlFileEntry = dlFileEntryLocalService.copyFileEntry(
202                            userId, groupId, getRepositoryId(), fileEntryId,
203                            toFolderId(destFolderId), serviceContext);
204    
205                    return new LiferayFileEntry(dlFileEntry);
206            }
207    
208            @Override
209            public void deleteAll() throws PortalException {
210                    dlFolderLocalService.deleteAllByRepository(getRepositoryId());
211            }
212    
213            @Override
214            public void deleteFileEntry(long fileEntryId) throws PortalException {
215                    dlFileEntryLocalService.deleteFileEntry(fileEntryId);
216            }
217    
218            @Override
219            public void deleteFileShortcut(long fileShortcutId) throws PortalException {
220                    dlFileShortcutLocalService.deleteFileShortcut(fileShortcutId);
221            }
222    
223            @Override
224            public void deleteFileShortcuts(long toFileEntryId) throws PortalException {
225                    dlFileShortcutLocalService.deleteFileShortcuts(toFileEntryId);
226            }
227    
228            @Override
229            public void deleteFolder(long folderId) throws PortalException {
230                    DLFolder dlFolder = dlFolderLocalService.fetchFolder(folderId);
231    
232                    if (dlFolder != null) {
233                            dlFolderLocalService.deleteFolder(folderId);
234                    }
235            }
236    
237            @Override
238            public List<FileEntry> getFileEntries(
239                    long folderId, int status, int start, int end,
240                    OrderByComparator<FileEntry> obc) {
241    
242                    List<DLFileEntry> dlFileEntries =
243                            dlFileEntryLocalService.getFileEntries(
244                                    getGroupId(), toFolderId(folderId), status, start, end,
245                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
246    
247                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
248            }
249    
250            @Override
251            public List<FileEntry> getFileEntries(
252                    long folderId, int start, int end, OrderByComparator<FileEntry> obc) {
253    
254                    List<DLFileEntry> dlFileEntries =
255                            dlFileEntryLocalService.getFileEntries(
256                                    getGroupId(), toFolderId(folderId), start, end,
257                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
258    
259                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
260            }
261    
262            @Override
263            public List<RepositoryEntry> getFileEntriesAndFileShortcuts(
264                    long folderId, int status, int start, int end) {
265    
266                    QueryDefinition<RepositoryEntry> queryDefinition =
267                            new QueryDefinition<>(status, start, end, null);
268    
269                    List<Object> dlFileEntriesAndFileShortcuts =
270                            dlFolderLocalService.getFileEntriesAndFileShortcuts(
271                                    getGroupId(), toFolderId(folderId), queryDefinition);
272    
273                    return RepositoryModelUtil.toRepositoryEntries(
274                            dlFileEntriesAndFileShortcuts);
275            }
276    
277            @Override
278            public int getFileEntriesAndFileShortcutsCount(long folderId, int status) {
279                    QueryDefinition<RepositoryEntry> queryDefinition =
280                            new QueryDefinition<>(status);
281    
282                    return dlFolderLocalService.getFileEntriesAndFileShortcutsCount(
283                            getGroupId(), toFolderId(folderId), queryDefinition);
284            }
285    
286            @Override
287            public int getFileEntriesCount(long folderId) {
288                    return dlFileEntryLocalService.getFileEntriesCount(
289                            getGroupId(), toFolderId(folderId));
290            }
291    
292            @Override
293            public int getFileEntriesCount(long folderId, int status) {
294                    return dlFileEntryLocalService.getFileEntriesCount(
295                            getGroupId(), toFolderId(folderId), status);
296            }
297    
298            @Override
299            public FileEntry getFileEntry(long fileEntryId) throws PortalException {
300                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
301                            fileEntryId);
302    
303                    return new LiferayFileEntry(dlFileEntry);
304            }
305    
306            @Override
307            public FileEntry getFileEntry(long folderId, String title)
308                    throws PortalException {
309    
310                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
311                            getGroupId(), toFolderId(folderId), title);
312    
313                    return new LiferayFileEntry(dlFileEntry);
314            }
315    
316            @Override
317            public FileEntry getFileEntryByUuid(String uuid) throws PortalException {
318                    DLFileEntry dlFileEntry =
319                            dlFileEntryLocalService.getFileEntryByUuidAndGroupId(
320                                    uuid, getGroupId());
321    
322                    return new LiferayFileEntry(dlFileEntry);
323            }
324    
325            @Override
326            public FileShortcut getFileShortcut(long fileShortcutId)
327                    throws PortalException {
328    
329                    DLFileShortcut dlFileShortcut =
330                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
331    
332                    return new LiferayFileShortcut(dlFileShortcut);
333            }
334    
335            @Override
336            public FileVersion getFileVersion(long fileVersionId)
337                    throws PortalException {
338    
339                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
340                            fileVersionId);
341    
342                    return new LiferayFileVersion(dlFileVersion);
343            }
344    
345            @Override
346            public Folder getFolder(long folderId) throws PortalException {
347                    DLFolder dlFolder = dlFolderLocalService.getFolder(
348                            toFolderId(folderId));
349    
350                    return new LiferayFolder(dlFolder);
351            }
352    
353            @Override
354            public Folder getFolder(long parentFolderId, String name)
355                    throws PortalException {
356    
357                    DLFolder dlFolder = dlFolderLocalService.getFolder(
358                            getGroupId(), toFolderId(parentFolderId), name);
359    
360                    return new LiferayFolder(dlFolder);
361            }
362    
363            @Override
364            public List<Folder> getFolders(
365                    long parentFolderId, boolean includeMountfolders, int start, int end,
366                    OrderByComparator<Folder> obc) {
367    
368                    return getFolders(
369                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
370                            includeMountfolders, start, end, obc);
371            }
372    
373            @Override
374            public List<Folder> getFolders(
375                    long parentFolderId, int status, boolean includeMountfolders, int start,
376                    int end, OrderByComparator<Folder> obc) {
377    
378                    List<DLFolder> dlFolders = dlFolderLocalService.getFolders(
379                            getGroupId(), toFolderId(parentFolderId), status,
380                            includeMountfolders, start, end,
381                            DLFolderOrderByComparator.getOrderByComparator(obc));
382    
383                    return RepositoryModelUtil.toFolders(dlFolders);
384            }
385    
386            @Override
387            public List<RepositoryEntry> getFoldersAndFileEntriesAndFileShortcuts(
388                    long folderId, int status, boolean includeMountFolders, int start,
389                    int end, OrderByComparator<?> obc) {
390    
391                    QueryDefinition<Object> queryDefinition = new QueryDefinition<>(
392                            status, start, end, (OrderByComparator<Object>)obc);
393    
394                    List<Object> dlFoldersAndDLFileEntriesAndDLFileShortcuts =
395                            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
396                                    getGroupId(), toFolderId(folderId), null, includeMountFolders,
397                                    queryDefinition);
398    
399                    return RepositoryModelUtil.toRepositoryEntries(
400                            dlFoldersAndDLFileEntriesAndDLFileShortcuts);
401            }
402    
403            @Override
404            public int getFoldersAndFileEntriesAndFileShortcutsCount(
405                    long folderId, int status, boolean includeMountFolders) {
406    
407                    QueryDefinition<Object> queryDefinition = new QueryDefinition<>(status);
408    
409                    return dlFolderLocalService.
410                            getFoldersAndFileEntriesAndFileShortcutsCount(
411                                    getGroupId(), toFolderId(folderId), null, includeMountFolders,
412                                    queryDefinition);
413            }
414    
415            @Override
416            public int getFoldersCount(
417                    long parentFolderId, boolean includeMountfolders) {
418    
419                    return getFoldersCount(
420                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
421                            includeMountfolders);
422            }
423    
424            @Override
425            public int getFoldersCount(
426                    long parentFolderId, int status, boolean includeMountfolders) {
427    
428                    return dlFolderLocalService.getFoldersCount(
429                            getGroupId(), toFolderId(parentFolderId), status,
430                            includeMountfolders);
431            }
432    
433            @Override
434            public List<FileEntry> getRepositoryFileEntries(
435                    long userId, long rootFolderId, int start, int end,
436                    OrderByComparator<FileEntry> obc) {
437    
438                    List<DLFileEntry> dlFileEntries =
439                            dlFileEntryLocalService.getGroupFileEntries(
440                                    getGroupId(), 0, getRepositoryId(), toFolderId(rootFolderId),
441                                    start, end,
442                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
443    
444                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
445            }
446    
447            @Override
448            public FileEntry moveFileEntry(
449                            long userId, long fileEntryId, long newFolderId,
450                            ServiceContext serviceContext)
451                    throws PortalException {
452    
453                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
454                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
455    
456                    return new LiferayFileEntry(dlFileEntry);
457            }
458    
459            @Override
460            public Folder moveFolder(
461                            long userId, long folderId, long parentFolderId,
462                            ServiceContext serviceContext)
463                    throws PortalException {
464    
465                    DLFolder dlFolder = dlFolderLocalService.moveFolder(
466                            userId, toFolderId(folderId), toFolderId(parentFolderId),
467                            serviceContext);
468    
469                    return new LiferayFolder(dlFolder);
470            }
471    
472            @Override
473            public void revertFileEntry(
474                            long userId, long fileEntryId, String version,
475                            ServiceContext serviceContext)
476                    throws PortalException {
477    
478                    dlFileEntryLocalService.revertFileEntry(
479                            userId, fileEntryId, version, serviceContext);
480            }
481    
482            /**
483             * @deprecated As of 7.0.0
484             */
485            @Deprecated
486            @Override
487            public void updateAsset(
488                            long userId, FileEntry fileEntry, FileVersion fileVersion,
489                            long[] assetCategoryIds, String[] assetTagNames,
490                            long[] assetLinkEntryIds)
491                    throws PortalException {
492    
493                    dlAppHelperLocalService.updateAsset(
494                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
495                            assetLinkEntryIds);
496            }
497    
498            @Override
499            public FileEntry updateFileEntry(
500                            long userId, long fileEntryId, String sourceFileName,
501                            String mimeType, String title, String description, String changeLog,
502                            boolean majorVersion, File file, ServiceContext serviceContext)
503                    throws PortalException {
504    
505                    long fileEntryTypeId = ParamUtil.getLong(
506                            serviceContext, "fileEntryTypeId", -1L);
507    
508                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
509                            serviceContext, fileEntryTypeId);
510    
511                    long size = 0;
512    
513                    if (file != null) {
514                            size = file.length();
515                    }
516    
517                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
518                            userId, fileEntryId, sourceFileName, mimeType, title, description,
519                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, file,
520                            null, size, serviceContext);
521    
522                    return new LiferayFileEntry(dlFileEntry);
523            }
524    
525            @Override
526            public FileEntry updateFileEntry(
527                            long userId, long fileEntryId, String sourceFileName,
528                            String mimeType, String title, String description, String changeLog,
529                            boolean majorVersion, InputStream is, long size,
530                            ServiceContext serviceContext)
531                    throws PortalException {
532    
533                    long fileEntryTypeId = ParamUtil.getLong(
534                            serviceContext, "fileEntryTypeId", -1L);
535    
536                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
537                            serviceContext, fileEntryTypeId);
538    
539                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
540                            userId, fileEntryId, sourceFileName, mimeType, title, description,
541                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, null,
542                            is, size, serviceContext);
543    
544                    return new LiferayFileEntry(dlFileEntry);
545            }
546    
547            @Override
548            public FileShortcut updateFileShortcut(
549                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
550                            ServiceContext serviceContext)
551                    throws PortalException {
552    
553                    DLFileShortcut dlFileShortcut =
554                            dlFileShortcutLocalService.updateFileShortcut(
555                                    userId, fileShortcutId, getRepositoryId(), toFolderId(folderId),
556                                    toFileEntryId, serviceContext);
557    
558                    return new LiferayFileShortcut(dlFileShortcut);
559            }
560    
561            @Override
562            public void updateFileShortcuts(
563                    long oldToFileEntryId, long newToFileEntryId) {
564    
565                    dlFileShortcutLocalService.updateFileShortcuts(
566                            oldToFileEntryId, newToFileEntryId);
567            }
568    
569            @Override
570            public Folder updateFolder(
571                            long folderId, long parentFolderId, String name, String description,
572                            ServiceContext serviceContext)
573                    throws PortalException {
574    
575                    long defaultFileEntryTypeId = ParamUtil.getLong(
576                            serviceContext, "defaultFileEntryTypeId");
577                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
578                            serviceContext, "dlFileEntryTypesSearchContainerPrimaryKeys");
579                    int restrictionType = ParamUtil.getInteger(
580                            serviceContext, "restrictionType");
581    
582                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
583                            toFolderId(folderId), toFolderId(parentFolderId), name, description,
584                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
585                            serviceContext);
586    
587                    return new LiferayFolder(dlFolder);
588            }
589    
590            public UnicodeProperties updateRepository(
591                    UnicodeProperties typeSettingsProperties) {
592    
593                    return typeSettingsProperties;
594            }
595    
596    }