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.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.lock.Lock;
020    import com.liferay.portal.kernel.lock.LockManagerUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.documentlibrary.DLGroupServiceSettings;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
030    import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
031    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
032    
033    import java.util.ArrayList;
034    import java.util.Collections;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Alexander Chow
040     */
041    public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
042    
043            @Override
044            public DLFolder addFolder(
045                            long groupId, long repositoryId, boolean mountPoint,
046                            long parentFolderId, String name, String description,
047                            ServiceContext serviceContext)
048                    throws PortalException {
049    
050                    DLFolderPermission.check(
051                            getPermissionChecker(), groupId, parentFolderId,
052                            ActionKeys.ADD_FOLDER);
053    
054                    return dlFolderLocalService.addFolder(
055                            getUserId(), groupId, repositoryId, mountPoint, parentFolderId,
056                            name, description, false, serviceContext);
057            }
058    
059            @Override
060            public void deleteFolder(long folderId) throws PortalException {
061                    deleteFolder(folderId, true);
062            }
063    
064            @Override
065            public void deleteFolder(long folderId, boolean includeTrashedEntries)
066                    throws PortalException {
067    
068                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
069    
070                    DLFolderPermission.check(
071                            getPermissionChecker(), dlFolder, ActionKeys.DELETE);
072    
073                    dlFolderLocalService.deleteFolder(
074                            getUserId(), folderId, includeTrashedEntries);
075            }
076    
077            @Override
078            public void deleteFolder(long groupId, long parentFolderId, String name)
079                    throws PortalException {
080    
081                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
082    
083                    deleteFolder(dlFolder.getFolderId());
084            }
085    
086            @Override
087            public List<Object> getFileEntriesAndFileShortcuts(
088                            long groupId, long folderId, int status, int start, int end)
089                    throws PortalException {
090    
091                    if (!DLFolderPermission.contains(
092                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
093    
094                            return Collections.emptyList();
095                    }
096    
097                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
098                            status, start, end, null);
099    
100                    return dlFolderFinder.filterFindFE_FS_ByG_F(
101                            groupId, folderId, queryDefinition);
102            }
103    
104            @Override
105            public int getFileEntriesAndFileShortcutsCount(
106                            long groupId, long folderId, int status)
107                    throws PortalException {
108    
109                    if (!DLFolderPermission.contains(
110                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
111    
112                            return 0;
113                    }
114    
115                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
116    
117                    return dlFolderFinder.filterCountFE_FS_ByG_F(
118                            groupId, folderId, queryDefinition);
119            }
120    
121            @Override
122            public int getFileEntriesAndFileShortcutsCount(
123                            long groupId, long folderId, int status, String[] mimeTypes)
124                    throws PortalException {
125    
126                    if (!DLFolderPermission.contains(
127                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
128    
129                            return 0;
130                    }
131    
132                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
133    
134                    return dlFolderFinder.filterCountFE_FS_ByG_F_M(
135                            groupId, folderId, mimeTypes, queryDefinition);
136            }
137    
138            @Override
139            public DLFolder getFolder(long folderId) throws PortalException {
140                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
141    
142                    DLFolderPermission.check(
143                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
144    
145                    return dlFolder;
146            }
147    
148            @Override
149            public DLFolder getFolder(long groupId, long parentFolderId, String name)
150                    throws PortalException {
151    
152                    DLFolder dlFolder = dlFolderLocalService.getFolder(
153                            groupId, parentFolderId, name);
154    
155                    DLFolderPermission.check(
156                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
157    
158                    return dlFolder;
159            }
160    
161            @Override
162            public List<Long> getFolderIds(long groupId, long folderId)
163                    throws PortalException {
164    
165                    if (!DLFolderPermission.contains(
166                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
167    
168                            return Collections.emptyList();
169                    }
170    
171                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
172    
173                    folderIds.add(0, folderId);
174    
175                    return folderIds;
176            }
177    
178            @Override
179            public List<DLFolder> getFolders(
180                            long groupId, long parentFolderId, int status,
181                            boolean includeMountfolders, int start, int end,
182                            OrderByComparator<DLFolder> obc)
183                    throws PortalException {
184    
185                    if (!DLFolderPermission.contains(
186                                    getPermissionChecker(), groupId, parentFolderId,
187                                    ActionKeys.VIEW)) {
188    
189                            return Collections.emptyList();
190                    }
191    
192                    if (includeMountfolders) {
193                            return dlFolderPersistence.filterFindByG_P_H_S(
194                                    groupId, parentFolderId, false, status, start, end, obc);
195                    }
196                    else {
197                            return dlFolderPersistence.filterFindByG_M_P_H_S(
198                                    groupId, false, parentFolderId, false, status, start, end, obc);
199                    }
200            }
201    
202            @Override
203            public List<DLFolder> getFolders(
204                            long groupId, long parentFolderId, int start, int end,
205                            OrderByComparator<DLFolder> obc)
206                    throws PortalException {
207    
208                    return getFolders(
209                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true,
210                            start, end, obc);
211            }
212    
213            @Override
214            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
215                            long groupId, long folderId, int status,
216                            boolean includeMountFolders, int start, int end,
217                            OrderByComparator<?> obc)
218                    throws PortalException {
219    
220                    if (!DLFolderPermission.contains(
221                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
222    
223                            return Collections.emptyList();
224                    }
225    
226                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
227                            status, start, end, (OrderByComparator<Object>)obc);
228    
229                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
230                            groupId, folderId, null, includeMountFolders, queryDefinition);
231            }
232    
233            @Override
234            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
235                            long groupId, long folderId, int status, String[] mimeTypes,
236                            boolean includeMountFolders, int start, int end,
237                            OrderByComparator<?> obc)
238                    throws PortalException {
239    
240                    if (!DLFolderPermission.contains(
241                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
242    
243                            return Collections.emptyList();
244                    }
245    
246                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(
247                            status, start, end, (OrderByComparator<Object>)obc);
248    
249                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
250                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
251            }
252    
253            @Override
254            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
255                            long groupId, long folderId, String[] mimeTypes,
256                            boolean includeMountFolders, QueryDefinition<?> queryDefinition)
257                    throws PortalException {
258    
259                    if (!DLFolderPermission.contains(
260                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
261    
262                            return Collections.emptyList();
263                    }
264    
265                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
266                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
267            }
268    
269            @Override
270            public int getFoldersAndFileEntriesAndFileShortcutsCount(
271                            long groupId, long folderId, int status,
272                            boolean includeMountFolders)
273                    throws PortalException {
274    
275                    if (!DLFolderPermission.contains(
276                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
277    
278                            return 0;
279                    }
280    
281                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
282    
283                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
284                            groupId, folderId, null, includeMountFolders, queryDefinition);
285            }
286    
287            @Override
288            public int getFoldersAndFileEntriesAndFileShortcutsCount(
289                            long groupId, long folderId, int status, String[] mimeTypes,
290                            boolean includeMountFolders)
291                    throws PortalException {
292    
293                    if (!DLFolderPermission.contains(
294                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
295    
296                            return 0;
297                    }
298    
299                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
300    
301                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
302                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
303            }
304    
305            @Override
306            public int getFoldersCount(long groupId, long parentFolderId)
307                    throws PortalException {
308    
309                    return getFoldersCount(
310                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true);
311            }
312    
313            @Override
314            public int getFoldersCount(
315                            long groupId, long parentFolderId, int status,
316                            boolean includeMountfolders)
317                    throws PortalException {
318    
319                    if (!DLFolderPermission.contains(
320                                    getPermissionChecker(), groupId, parentFolderId,
321                                    ActionKeys.VIEW)) {
322    
323                            return 0;
324                    }
325    
326                    if (includeMountfolders) {
327                            return dlFolderPersistence.filterCountByG_P_H_S(
328                                    groupId, parentFolderId, false, status);
329                    }
330                    else {
331                            return dlFolderPersistence.filterCountByG_M_P_H_S(
332                                    groupId, false, parentFolderId, false, status);
333                    }
334            }
335    
336            @Override
337            public List<DLFolder> getMountFolders(
338                            long groupId, long parentFolderId, int start, int end,
339                            OrderByComparator<DLFolder> obc)
340                    throws PortalException {
341    
342                    if (!DLFolderPermission.contains(
343                                    getPermissionChecker(), groupId, parentFolderId,
344                                    ActionKeys.VIEW)) {
345    
346                            return Collections.emptyList();
347                    }
348    
349                    DLGroupServiceSettings dlGroupServiceSettings =
350                            DLGroupServiceSettings.getInstance(groupId);
351    
352                    if (dlGroupServiceSettings.isShowHiddenMountFolders()) {
353                            return dlFolderPersistence.filterFindByG_M_P(
354                                    groupId, true, parentFolderId, start, end, obc);
355                    }
356                    else {
357                            return dlFolderPersistence.filterFindByG_M_P_H(
358                                    groupId, true, parentFolderId, false, start, end, obc);
359                    }
360            }
361    
362            @Override
363            public int getMountFoldersCount(long groupId, long parentFolderId)
364                    throws PortalException {
365    
366                    if (!DLFolderPermission.contains(
367                                    getPermissionChecker(), groupId, parentFolderId,
368                                    ActionKeys.VIEW)) {
369    
370                            return 0;
371                    }
372    
373                    return dlFolderPersistence.filterCountByG_M_P_H(
374                            groupId, true, parentFolderId, false);
375            }
376    
377            /**
378             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
379             *             long, boolean)}
380             */
381            @Deprecated
382            @Override
383            public void getSubfolderIds(
384                            List<Long> folderIds, long groupId, long folderId)
385                    throws PortalException {
386    
387                    getSubfolderIds(folderIds, groupId, folderId, true);
388            }
389    
390            @Override
391            public void getSubfolderIds(
392                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
393                    throws PortalException {
394    
395                    if (!DLFolderPermission.contains(
396                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
397    
398                            return;
399                    }
400    
401                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
402                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
403    
404                    for (DLFolder dlFolder : dlFolders) {
405                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrash()) {
406                                    continue;
407                            }
408    
409                            folderIds.add(dlFolder.getFolderId());
410    
411                            if (recurse) {
412                                    getSubfolderIds(
413                                            folderIds, dlFolder.getGroupId(), dlFolder.getFolderId(),
414                                            recurse);
415                            }
416                    }
417            }
418    
419            @Override
420            public List<Long> getSubfolderIds(
421                            long groupId, long folderId, boolean recurse)
422                    throws PortalException {
423    
424                    List<Long> folderIds = new ArrayList<>();
425    
426                    getSubfolderIds(folderIds, groupId, folderId, recurse);
427    
428                    return folderIds;
429            }
430    
431            @Override
432            public boolean hasFolderLock(long folderId) throws PortalException {
433                    return LockManagerUtil.hasLock(
434                            getUserId(), DLFolder.class.getName(), folderId);
435            }
436    
437            @Override
438            public boolean hasInheritableLock(long folderId) throws PortalException {
439                    return dlFolderLocalService.hasInheritableLock(folderId);
440            }
441    
442            @Override
443            public boolean isFolderLocked(long folderId) {
444                    return LockManagerUtil.isLocked(DLFolder.class.getName(), folderId);
445            }
446    
447            @Override
448            public Lock lockFolder(long folderId) throws PortalException {
449                    return lockFolder(
450                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
451            }
452    
453            @Override
454            public Lock lockFolder(
455                            long folderId, String owner, boolean inheritable,
456                            long expirationTime)
457                    throws PortalException {
458    
459                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
460    
461                    DLFolderPermission.check(
462                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
463    
464                    return dlFolderLocalService.lockFolder(
465                            getUserId(), folderId, owner, inheritable, expirationTime);
466            }
467    
468            @Override
469            public DLFolder moveFolder(
470                            long folderId, long parentFolderId, ServiceContext serviceContext)
471                    throws PortalException {
472    
473                    PermissionChecker permissionChecker = getPermissionChecker();
474    
475                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
476    
477                    DLFolderPermission.check(
478                            permissionChecker, dlFolder, ActionKeys.UPDATE);
479    
480                    DLFolderPermission.check(
481                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
482                            ActionKeys.ADD_FOLDER);
483    
484                    return dlFolderLocalService.moveFolder(
485                            getUserId(), folderId, parentFolderId, serviceContext);
486            }
487    
488            @Override
489            public Lock refreshFolderLock(
490                            String lockUuid, long companyId, long expirationTime)
491                    throws PortalException {
492    
493                    return LockManagerUtil.refresh(lockUuid, companyId, expirationTime);
494            }
495    
496            @Override
497            public void unlockFolder(
498                            long groupId, long parentFolderId, String name, String lockUuid)
499                    throws PortalException {
500    
501                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
502    
503                    unlockFolder(dlFolder.getFolderId(), lockUuid);
504            }
505    
506            @Override
507            public void unlockFolder(long folderId, String lockUuid)
508                    throws PortalException {
509    
510                    DLFolder dlFolder = dlFolderLocalService.fetchFolder(folderId);
511    
512                    if (dlFolder != null) {
513                            DLFolderPermission.check(
514                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
515                    }
516    
517                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
518            }
519    
520            @Override
521            public DLFolder updateFolder(
522                            long folderId, long parentFolderId, String name, String description,
523                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
524                            int restrictionType, ServiceContext serviceContext)
525                    throws PortalException {
526    
527                    DLFolderPermission.check(
528                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
529                            ActionKeys.UPDATE);
530    
531                    serviceContext.setUserId(getUserId());
532    
533                    return dlFolderLocalService.updateFolder(
534                            folderId, parentFolderId, name, description, defaultFileEntryTypeId,
535                            fileEntryTypeIds, restrictionType, serviceContext);
536            }
537    
538            /**
539             * @deprecated As of 7.0.0, replaced by more general {@link
540             *             #updateFolder(long, String, String, long, List, int,
541             *             ServiceContext)}
542             */
543            @Deprecated
544            @Override
545            public DLFolder updateFolder(
546                            long folderId, String name, String description,
547                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
548                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
549                    throws PortalException {
550    
551                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
552    
553                    if (overrideFileEntryTypes) {
554                            restrictionType =
555                                    DLFolderConstants.
556                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
557                    }
558    
559                    return dlFolderLocalService.updateFolder(
560                            folderId, name, description, defaultFileEntryTypeId,
561                            fileEntryTypeIds, restrictionType, serviceContext);
562            }
563    
564            @Override
565            public DLFolder updateFolder(
566                            long folderId, String name, String description,
567                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
568                            int restrictionType, ServiceContext serviceContext)
569                    throws PortalException {
570    
571                    DLFolderPermission.check(
572                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
573                            ActionKeys.UPDATE);
574    
575                    serviceContext.setUserId(getUserId());
576    
577                    return dlFolderLocalService.updateFolder(
578                            folderId, name, description, defaultFileEntryTypeId,
579                            fileEntryTypeIds, restrictionType, serviceContext);
580            }
581    
582            @Override
583            public boolean verifyInheritableLock(long folderId, String lockUuid)
584                    throws PortalException {
585    
586                    return dlFolderLocalService.verifyInheritableLock(folderId, lockUuid);
587            }
588    
589    }