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 int getFoldersCount(
392                    long parentFolderId, boolean includeMountfolders) {
393    
394                    return getFoldersCount(
395                            parentFolderId, WorkflowConstants.STATUS_APPROVED,
396                            includeMountfolders);
397            }
398    
399            @Override
400            public int getFoldersCount(
401                    long parentFolderId, int status, boolean includeMountfolders) {
402    
403                    return dlFolderLocalService.getFoldersCount(
404                            getGroupId(), toFolderId(parentFolderId), status,
405                            includeMountfolders);
406            }
407    
408            @Override
409            public List<FileEntry> getRepositoryFileEntries(
410                    long userId, long rootFolderId, int start, int end,
411                    OrderByComparator<FileEntry> obc) {
412    
413                    List<DLFileEntry> dlFileEntries =
414                            dlFileEntryLocalService.getGroupFileEntries(
415                                    getGroupId(), 0, getRepositoryId(), toFolderId(rootFolderId),
416                                    start, end,
417                                    DLFileEntryOrderByComparator.getOrderByComparator(obc));
418    
419                    return RepositoryModelUtil.toFileEntries(dlFileEntries);
420            }
421    
422            @Override
423            public FileEntry moveFileEntry(
424                            long userId, long fileEntryId, long newFolderId,
425                            ServiceContext serviceContext)
426                    throws PortalException {
427    
428                    DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
429                            userId, fileEntryId, toFolderId(newFolderId), serviceContext);
430    
431                    return new LiferayFileEntry(dlFileEntry);
432            }
433    
434            @Override
435            public Folder moveFolder(
436                            long userId, long folderId, long parentFolderId,
437                            ServiceContext serviceContext)
438                    throws PortalException {
439    
440                    DLFolder dlFolder = dlFolderLocalService.moveFolder(
441                            userId, toFolderId(folderId), toFolderId(parentFolderId),
442                            serviceContext);
443    
444                    return new LiferayFolder(dlFolder);
445            }
446    
447            @Override
448            public void revertFileEntry(
449                            long userId, long fileEntryId, String version,
450                            ServiceContext serviceContext)
451                    throws PortalException {
452    
453                    dlFileEntryLocalService.revertFileEntry(
454                            userId, fileEntryId, version, serviceContext);
455            }
456    
457            /**
458             * @deprecated As of 7.0.0
459             */
460            @Deprecated
461            @Override
462            public void updateAsset(
463                            long userId, FileEntry fileEntry, FileVersion fileVersion,
464                            long[] assetCategoryIds, String[] assetTagNames,
465                            long[] assetLinkEntryIds)
466                    throws PortalException {
467    
468                    dlAppHelperLocalService.updateAsset(
469                            userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
470                            assetLinkEntryIds);
471            }
472    
473            @Override
474            public FileEntry updateFileEntry(
475                            long userId, long fileEntryId, String sourceFileName,
476                            String mimeType, String title, String description, String changeLog,
477                            boolean majorVersion, File file, ServiceContext serviceContext)
478                    throws PortalException {
479    
480                    long fileEntryTypeId = ParamUtil.getLong(
481                            serviceContext, "fileEntryTypeId", -1L);
482    
483                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
484                            serviceContext, fileEntryTypeId);
485    
486                    long size = 0;
487    
488                    if (file != null) {
489                            size = file.length();
490                    }
491    
492                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
493                            userId, fileEntryId, sourceFileName, mimeType, title, description,
494                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, file,
495                            null, size, serviceContext);
496    
497                    return new LiferayFileEntry(dlFileEntry);
498            }
499    
500            @Override
501            public FileEntry updateFileEntry(
502                            long userId, long fileEntryId, String sourceFileName,
503                            String mimeType, String title, String description, String changeLog,
504                            boolean majorVersion, InputStream is, long size,
505                            ServiceContext serviceContext)
506                    throws PortalException {
507    
508                    long fileEntryTypeId = ParamUtil.getLong(
509                            serviceContext, "fileEntryTypeId", -1L);
510    
511                    Map<String, DDMFormValues> ddmFormValuesMap = getDDMFormValuesMap(
512                            serviceContext, fileEntryTypeId);
513    
514                    DLFileEntry dlFileEntry = dlFileEntryLocalService.updateFileEntry(
515                            userId, fileEntryId, sourceFileName, mimeType, title, description,
516                            changeLog, majorVersion, fileEntryTypeId, ddmFormValuesMap, null,
517                            is, size, serviceContext);
518    
519                    return new LiferayFileEntry(dlFileEntry);
520            }
521    
522            @Override
523            public FileShortcut updateFileShortcut(
524                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
525                            ServiceContext serviceContext)
526                    throws PortalException {
527    
528                    DLFileShortcut dlFileShortcut =
529                            dlFileShortcutLocalService.updateFileShortcut(
530                                    userId, fileShortcutId, getRepositoryId(), toFolderId(folderId),
531                                    toFileEntryId, serviceContext);
532    
533                    return new LiferayFileShortcut(dlFileShortcut);
534            }
535    
536            @Override
537            public void updateFileShortcuts(
538                    long oldToFileEntryId, long newToFileEntryId) {
539    
540                    dlFileShortcutLocalService.updateFileShortcuts(
541                            oldToFileEntryId, newToFileEntryId);
542            }
543    
544            @Override
545            public Folder updateFolder(
546                            long folderId, long parentFolderId, String name, String description,
547                            ServiceContext serviceContext)
548                    throws PortalException {
549    
550                    long defaultFileEntryTypeId = ParamUtil.getLong(
551                            serviceContext, "defaultFileEntryTypeId");
552                    SortedArrayList<Long> fileEntryTypeIds = getLongList(
553                            serviceContext, "dlFileEntryTypesSearchContainerPrimaryKeys");
554                    int restrictionType = ParamUtil.getInteger(
555                            serviceContext, "restrictionType");
556    
557                    DLFolder dlFolder = dlFolderLocalService.updateFolder(
558                            toFolderId(folderId), toFolderId(parentFolderId), name, description,
559                            defaultFileEntryTypeId, fileEntryTypeIds, restrictionType,
560                            serviceContext);
561    
562                    return new LiferayFolder(dlFolder);
563            }
564    
565            public UnicodeProperties updateRepository(
566                    UnicodeProperties typeSettingsProperties) {
567    
568                    return typeSettingsProperties;
569            }
570    
571    }