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