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.document.library.kernel.model.DLFolder;
018    import com.liferay.document.library.kernel.model.DLFolderConstants;
019    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.lock.Lock;
022    import com.liferay.portal.kernel.lock.LockManagerUtil;
023    import com.liferay.portal.kernel.security.permission.ActionKeys;
024    import com.liferay.portal.kernel.security.permission.PermissionChecker;
025    import com.liferay.portal.kernel.service.ServiceContext;
026    import com.liferay.portal.kernel.util.OrderByComparator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portlet.documentlibrary.DLGroupServiceSettings;
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 (queryDefinition.isIncludeOwner() &&
260                            (queryDefinition.getOwnerUserId() != 0)) {
261    
262                            queryDefinition.setOwnerUserId(getUserId());
263                    }
264    
265                    if (!DLFolderPermission.contains(
266                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
267    
268                            return Collections.emptyList();
269                    }
270    
271                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
272                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
273            }
274    
275            @Override
276            public int getFoldersAndFileEntriesAndFileShortcutsCount(
277                            long groupId, long folderId, int status,
278                            boolean includeMountFolders)
279                    throws PortalException {
280    
281                    if (!DLFolderPermission.contains(
282                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
283    
284                            return 0;
285                    }
286    
287                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
288    
289                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
290                            groupId, folderId, null, includeMountFolders, queryDefinition);
291            }
292    
293            @Override
294            public int getFoldersAndFileEntriesAndFileShortcutsCount(
295                            long groupId, long folderId, int status, String[] mimeTypes,
296                            boolean includeMountFolders)
297                    throws PortalException {
298    
299                    if (!DLFolderPermission.contains(
300                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
301    
302                            return 0;
303                    }
304    
305                    QueryDefinition<?> queryDefinition = new QueryDefinition<>(status);
306    
307                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
308                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
309            }
310    
311            @Override
312            public int getFoldersAndFileEntriesAndFileShortcutsCount(
313                            long groupId, long folderId, String[] mimeTypes,
314                            boolean includeMountFolders, QueryDefinition<?> queryDefinition)
315                    throws PortalException {
316    
317                    if (!DLFolderPermission.contains(
318                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
319    
320                            return 0;
321                    }
322    
323                    if (queryDefinition.isIncludeOwner() &&
324                            (queryDefinition.getOwnerUserId() != 0)) {
325    
326                            queryDefinition.setOwnerUserId(getUserId());
327                    }
328    
329                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
330                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
331            }
332    
333            @Override
334            public int getFoldersCount(long groupId, long parentFolderId)
335                    throws PortalException {
336    
337                    return getFoldersCount(
338                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true);
339            }
340    
341            @Override
342            public int getFoldersCount(
343                            long groupId, long parentFolderId, int status,
344                            boolean includeMountfolders)
345                    throws PortalException {
346    
347                    if (!DLFolderPermission.contains(
348                                    getPermissionChecker(), groupId, parentFolderId,
349                                    ActionKeys.VIEW)) {
350    
351                            return 0;
352                    }
353    
354                    if (includeMountfolders) {
355                            return dlFolderPersistence.filterCountByG_P_H_S(
356                                    groupId, parentFolderId, false, status);
357                    }
358                    else {
359                            return dlFolderPersistence.filterCountByG_M_P_H_S(
360                                    groupId, false, parentFolderId, false, status);
361                    }
362            }
363    
364            @Override
365            public List<DLFolder> getMountFolders(
366                            long groupId, long parentFolderId, int start, int end,
367                            OrderByComparator<DLFolder> obc)
368                    throws PortalException {
369    
370                    if (!DLFolderPermission.contains(
371                                    getPermissionChecker(), groupId, parentFolderId,
372                                    ActionKeys.VIEW)) {
373    
374                            return Collections.emptyList();
375                    }
376    
377                    DLGroupServiceSettings dlGroupServiceSettings =
378                            DLGroupServiceSettings.getInstance(groupId);
379    
380                    if (dlGroupServiceSettings.isShowHiddenMountFolders()) {
381                            return dlFolderPersistence.filterFindByG_M_P(
382                                    groupId, true, parentFolderId, start, end, obc);
383                    }
384                    else {
385                            return dlFolderPersistence.filterFindByG_M_P_H(
386                                    groupId, true, parentFolderId, false, start, end, obc);
387                    }
388            }
389    
390            @Override
391            public int getMountFoldersCount(long groupId, long parentFolderId)
392                    throws PortalException {
393    
394                    if (!DLFolderPermission.contains(
395                                    getPermissionChecker(), groupId, parentFolderId,
396                                    ActionKeys.VIEW)) {
397    
398                            return 0;
399                    }
400    
401                    return dlFolderPersistence.filterCountByG_M_P_H(
402                            groupId, true, parentFolderId, false);
403            }
404    
405            /**
406             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
407             *             long, boolean)}
408             */
409            @Deprecated
410            @Override
411            public void getSubfolderIds(
412                            List<Long> folderIds, long groupId, long folderId)
413                    throws PortalException {
414    
415                    getSubfolderIds(folderIds, groupId, folderId, true);
416            }
417    
418            @Override
419            public void getSubfolderIds(
420                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
421                    throws PortalException {
422    
423                    if (!DLFolderPermission.contains(
424                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
425    
426                            return;
427                    }
428    
429                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
430                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
431    
432                    for (DLFolder dlFolder : dlFolders) {
433                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrash()) {
434                                    continue;
435                            }
436    
437                            folderIds.add(dlFolder.getFolderId());
438    
439                            if (recurse) {
440                                    getSubfolderIds(
441                                            folderIds, dlFolder.getGroupId(), dlFolder.getFolderId(),
442                                            recurse);
443                            }
444                    }
445            }
446    
447            @Override
448            public List<Long> getSubfolderIds(
449                            long groupId, long folderId, boolean recurse)
450                    throws PortalException {
451    
452                    List<Long> folderIds = new ArrayList<>();
453    
454                    getSubfolderIds(folderIds, groupId, folderId, recurse);
455    
456                    return folderIds;
457            }
458    
459            @Override
460            public boolean hasFolderLock(long folderId) throws PortalException {
461                    return LockManagerUtil.hasLock(
462                            getUserId(), DLFolder.class.getName(), folderId);
463            }
464    
465            @Override
466            public boolean hasInheritableLock(long folderId) throws PortalException {
467                    return dlFolderLocalService.hasInheritableLock(folderId);
468            }
469    
470            @Override
471            public boolean isFolderLocked(long folderId) {
472                    return LockManagerUtil.isLocked(DLFolder.class.getName(), folderId);
473            }
474    
475            @Override
476            public Lock lockFolder(long folderId) throws PortalException {
477                    return lockFolder(
478                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
479            }
480    
481            @Override
482            public Lock lockFolder(
483                            long folderId, String owner, boolean inheritable,
484                            long expirationTime)
485                    throws PortalException {
486    
487                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
488    
489                    DLFolderPermission.check(
490                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
491    
492                    return dlFolderLocalService.lockFolder(
493                            getUserId(), folderId, owner, inheritable, expirationTime);
494            }
495    
496            @Override
497            public DLFolder moveFolder(
498                            long folderId, long parentFolderId, ServiceContext serviceContext)
499                    throws PortalException {
500    
501                    PermissionChecker permissionChecker = getPermissionChecker();
502    
503                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
504    
505                    DLFolderPermission.check(
506                            permissionChecker, dlFolder, ActionKeys.UPDATE);
507    
508                    DLFolderPermission.check(
509                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
510                            ActionKeys.ADD_FOLDER);
511    
512                    return dlFolderLocalService.moveFolder(
513                            getUserId(), folderId, parentFolderId, serviceContext);
514            }
515    
516            @Override
517            public Lock refreshFolderLock(
518                            String lockUuid, long companyId, long expirationTime)
519                    throws PortalException {
520    
521                    return LockManagerUtil.refresh(lockUuid, companyId, expirationTime);
522            }
523    
524            @Override
525            public void unlockFolder(
526                            long groupId, long parentFolderId, String name, String lockUuid)
527                    throws PortalException {
528    
529                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
530    
531                    unlockFolder(dlFolder.getFolderId(), lockUuid);
532            }
533    
534            @Override
535            public void unlockFolder(long folderId, String lockUuid)
536                    throws PortalException {
537    
538                    DLFolder dlFolder = dlFolderLocalService.fetchFolder(folderId);
539    
540                    if (dlFolder != null) {
541                            DLFolderPermission.check(
542                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
543                    }
544    
545                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
546            }
547    
548            @Override
549            public DLFolder updateFolder(
550                            long folderId, long parentFolderId, String name, String description,
551                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
552                            int restrictionType, ServiceContext serviceContext)
553                    throws PortalException {
554    
555                    DLFolderPermission.check(
556                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
557                            ActionKeys.UPDATE);
558    
559                    serviceContext.setUserId(getUserId());
560    
561                    return dlFolderLocalService.updateFolder(
562                            folderId, parentFolderId, name, description, defaultFileEntryTypeId,
563                            fileEntryTypeIds, restrictionType, serviceContext);
564            }
565    
566            /**
567             * @deprecated As of 7.0.0, replaced by more general {@link
568             *             #updateFolder(long, String, String, long, List, int,
569             *             ServiceContext)}
570             */
571            @Deprecated
572            @Override
573            public DLFolder updateFolder(
574                            long folderId, String name, String description,
575                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
576                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
577                    throws PortalException {
578    
579                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
580    
581                    if (overrideFileEntryTypes) {
582                            restrictionType =
583                                    DLFolderConstants.
584                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
585                    }
586    
587                    return dlFolderLocalService.updateFolder(
588                            folderId, name, description, defaultFileEntryTypeId,
589                            fileEntryTypeIds, restrictionType, serviceContext);
590            }
591    
592            @Override
593            public DLFolder updateFolder(
594                            long folderId, String name, String description,
595                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
596                            int restrictionType, ServiceContext serviceContext)
597                    throws PortalException {
598    
599                    DLFolderPermission.check(
600                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
601                            ActionKeys.UPDATE);
602    
603                    serviceContext.setUserId(getUserId());
604    
605                    return dlFolderLocalService.updateFolder(
606                            folderId, name, description, defaultFileEntryTypeId,
607                            fileEntryTypeIds, restrictionType, serviceContext);
608            }
609    
610            @Override
611            public boolean verifyInheritableLock(long folderId, String lockUuid)
612                    throws PortalException {
613    
614                    return dlFolderLocalService.verifyInheritableLock(folderId, lockUuid);
615            }
616    
617    }