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