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.ExpiredLockException;
018    import com.liferay.portal.NoSuchLockException;
019    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.Lock;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.documentlibrary.DLSettings;
028    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
029    import com.liferay.portlet.documentlibrary.model.DLFolder;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
032    import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
033    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
034    
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Alexander Chow
042     */
043    public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
044    
045            @Override
046            public DLFolder addFolder(
047                            long groupId, long repositoryId, boolean mountPoint,
048                            long parentFolderId, String name, String description,
049                            ServiceContext serviceContext)
050                    throws PortalException {
051    
052                    DLFolderPermission.check(
053                            getPermissionChecker(), groupId, parentFolderId,
054                            ActionKeys.ADD_FOLDER);
055    
056                    return dlFolderLocalService.addFolder(
057                            getUserId(), groupId, repositoryId, mountPoint, parentFolderId,
058                            name, description, false, serviceContext);
059            }
060    
061            @Override
062            public void deleteFolder(long folderId) throws PortalException {
063                    deleteFolder(folderId, true);
064            }
065    
066            @Override
067            public void deleteFolder(long folderId, boolean includeTrashedEntries)
068                    throws PortalException {
069    
070                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
071    
072                    DLFolderPermission.check(
073                            getPermissionChecker(), dlFolder, ActionKeys.DELETE);
074    
075                    dlFolderLocalService.deleteFolder(
076                            getUserId(), folderId, includeTrashedEntries);
077            }
078    
079            @Override
080            public void deleteFolder(long groupId, long parentFolderId, String name)
081                    throws PortalException {
082    
083                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
084    
085                    deleteFolder(dlFolder.getFolderId());
086            }
087    
088            @Override
089            public List<Object> getFileEntriesAndFileShortcuts(
090                            long groupId, long folderId, int status, int start, int end)
091                    throws PortalException {
092    
093                    if (!DLFolderPermission.contains(
094                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
095    
096                            return Collections.emptyList();
097                    }
098    
099                    QueryDefinition<?> queryDefinition = new QueryDefinition<Object>(
100                            status, start, end, null);
101    
102                    return dlFolderFinder.filterFindFE_FS_ByG_F(
103                            groupId, folderId, queryDefinition);
104            }
105    
106            @Override
107            public int getFileEntriesAndFileShortcutsCount(
108                            long groupId, long folderId, int status)
109                    throws PortalException {
110    
111                    if (!DLFolderPermission.contains(
112                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
113    
114                            return 0;
115                    }
116    
117                    QueryDefinition<?> queryDefinition = new QueryDefinition<Object>(
118                            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<Object>(
136                            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<Object>(
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<Object>(
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 int getFoldersAndFileEntriesAndFileShortcutsCount(
259                            long groupId, long folderId, int status,
260                            boolean includeMountFolders)
261                    throws PortalException {
262    
263                    if (!DLFolderPermission.contains(
264                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
265    
266                            return 0;
267                    }
268    
269                    QueryDefinition<?> queryDefinition = new QueryDefinition<Object>(
270                            status);
271    
272                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
273                            groupId, folderId, null, includeMountFolders, queryDefinition);
274            }
275    
276            @Override
277            public int getFoldersAndFileEntriesAndFileShortcutsCount(
278                            long groupId, long folderId, int status, String[] mimeTypes,
279                            boolean includeMountFolders)
280                    throws PortalException {
281    
282                    if (!DLFolderPermission.contains(
283                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
284    
285                            return 0;
286                    }
287    
288                    QueryDefinition<?> queryDefinition = new QueryDefinition<Object>(
289                            status);
290    
291                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
292                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
293            }
294    
295            @Override
296            public int getFoldersCount(long groupId, long parentFolderId)
297                    throws PortalException {
298    
299                    return getFoldersCount(
300                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true);
301            }
302    
303            @Override
304            public int getFoldersCount(
305                            long groupId, long parentFolderId, int status,
306                            boolean includeMountfolders)
307                    throws PortalException {
308    
309                    if (!DLFolderPermission.contains(
310                                    getPermissionChecker(), groupId, parentFolderId,
311                                    ActionKeys.VIEW)) {
312    
313                            return 0;
314                    }
315    
316                    if (includeMountfolders) {
317                            return dlFolderPersistence.filterCountByG_P_H_S(
318                                    groupId, parentFolderId, false, status);
319                    }
320                    else {
321                            return dlFolderPersistence.filterCountByG_M_P_H_S(
322                                    groupId, false, parentFolderId, false, status);
323                    }
324            }
325    
326            @Override
327            public List<DLFolder> getMountFolders(
328                            long groupId, long parentFolderId, int start, int end,
329                            OrderByComparator<DLFolder> obc)
330                    throws PortalException {
331    
332                    if (!DLFolderPermission.contains(
333                                    getPermissionChecker(), groupId, parentFolderId,
334                                    ActionKeys.VIEW)) {
335    
336                            return Collections.emptyList();
337                    }
338    
339                    DLSettings dlSettings = DLSettings.getInstance(groupId);
340    
341                    if (dlSettings.isShowHiddenMountFolders()) {
342                            return dlFolderPersistence.filterFindByG_M_P(
343                                    groupId, true, parentFolderId, start, end, obc);
344                    }
345                    else {
346                            return dlFolderPersistence.filterFindByG_M_P_H(
347                                    groupId, true, parentFolderId, false, start, end, obc);
348                    }
349            }
350    
351            @Override
352            public int getMountFoldersCount(long groupId, long parentFolderId)
353                    throws PortalException {
354    
355                    if (!DLFolderPermission.contains(
356                                    getPermissionChecker(), groupId, parentFolderId,
357                                    ActionKeys.VIEW)) {
358    
359                            return 0;
360                    }
361    
362                    return dlFolderPersistence.filterCountByG_M_P_H(
363                            groupId, true, parentFolderId, false);
364            }
365    
366            /**
367             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
368             *             long, boolean)}
369             */
370            @Deprecated
371            @Override
372            public void getSubfolderIds(
373                            List<Long> folderIds, long groupId, long folderId)
374                    throws PortalException {
375    
376                    getSubfolderIds(folderIds, groupId, folderId, true);
377            }
378    
379            @Override
380            public void getSubfolderIds(
381                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
382                    throws PortalException {
383    
384                    if (!DLFolderPermission.contains(
385                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
386    
387                            return;
388                    }
389    
390                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
391                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
392    
393                    for (DLFolder dlFolder : dlFolders) {
394                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrash()) {
395                                    continue;
396                            }
397    
398                            folderIds.add(dlFolder.getFolderId());
399    
400                            if (recurse) {
401                                    getSubfolderIds(
402                                            folderIds, dlFolder.getGroupId(), dlFolder.getFolderId(),
403                                            recurse);
404                            }
405                    }
406            }
407    
408            @Override
409            public List<Long> getSubfolderIds(
410                            long groupId, long folderId, boolean recurse)
411                    throws PortalException {
412    
413                    List<Long> folderIds = new ArrayList<Long>();
414    
415                    getSubfolderIds(folderIds, groupId, folderId, recurse);
416    
417                    return folderIds;
418            }
419    
420            @Override
421            public boolean hasFolderLock(long folderId) throws PortalException {
422                    return lockLocalService.hasLock(
423                            getUserId(), DLFolder.class.getName(), folderId);
424            }
425    
426            @Override
427            public boolean hasInheritableLock(long folderId) throws PortalException {
428                    boolean inheritable = false;
429    
430                    try {
431                            Lock lock = lockLocalService.getLock(
432                                    DLFolder.class.getName(), folderId);
433    
434                            inheritable = lock.isInheritable();
435                    }
436                    catch (ExpiredLockException ele) {
437                    }
438                    catch (NoSuchLockException nsle) {
439                    }
440    
441                    return inheritable;
442            }
443    
444            @Override
445            public boolean isFolderLocked(long folderId) {
446                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
447            }
448    
449            @Override
450            public Lock lockFolder(long folderId) throws PortalException {
451                    return lockFolder(
452                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
453            }
454    
455            @Override
456            public Lock lockFolder(
457                            long folderId, String owner, boolean inheritable,
458                            long expirationTime)
459                    throws PortalException {
460    
461                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
462    
463                    DLFolderPermission.check(
464                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
465    
466                    return dlFolderLocalService.lockFolder(
467                            getUserId(), folderId, owner, inheritable, expirationTime);
468            }
469    
470            @Override
471            public DLFolder moveFolder(
472                            long folderId, long parentFolderId, ServiceContext serviceContext)
473                    throws PortalException {
474    
475                    PermissionChecker permissionChecker = getPermissionChecker();
476    
477                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
478    
479                    DLFolderPermission.check(
480                            permissionChecker, dlFolder, ActionKeys.UPDATE);
481    
482                    DLFolderPermission.check(
483                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
484                            ActionKeys.ADD_FOLDER);
485    
486                    return dlFolderLocalService.moveFolder(
487                            getUserId(), folderId, parentFolderId, serviceContext);
488            }
489    
490            @Override
491            public Lock refreshFolderLock(
492                            String lockUuid, long companyId, long expirationTime)
493                    throws PortalException {
494    
495                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
496            }
497    
498            @Override
499            public void unlockFolder(
500                            long groupId, long parentFolderId, String name, String lockUuid)
501                    throws PortalException {
502    
503                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
504    
505                    unlockFolder(dlFolder.getFolderId(), lockUuid);
506            }
507    
508            @Override
509            public void unlockFolder(long folderId, String lockUuid)
510                    throws PortalException {
511    
512                    try {
513                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
514    
515                            DLFolderPermission.check(
516                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
517                    }
518                    catch (NoSuchFolderException nsfe) {
519                    }
520    
521                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
522            }
523    
524            /**
525             * @deprecated As of 7.0.0, replaced by more general {@link
526             *             #updateFolder(long, String, String, long, List, int,
527             *             ServiceContext)}
528             */
529            @Deprecated
530            @Override
531            public DLFolder updateFolder(
532                            long folderId, String name, String description,
533                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
534                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
535                    throws PortalException {
536    
537                    int restrictionType = DLFolderConstants.RESTRICTION_TYPE_INHERIT;
538    
539                    if (overrideFileEntryTypes) {
540                            restrictionType =
541                                    DLFolderConstants.
542                                            RESTRICTION_TYPE_FILE_ENTRY_TYPES_AND_WORKFLOW;
543                    }
544    
545                    return dlFolderLocalService.updateFolder(
546                            folderId, name, description, defaultFileEntryTypeId,
547                            fileEntryTypeIds, restrictionType, serviceContext);
548            }
549    
550            @Override
551            public DLFolder updateFolder(
552                            long folderId, String name, String description,
553                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
554                            int restrictionType, ServiceContext serviceContext)
555                    throws PortalException {
556    
557                    DLFolderPermission.check(
558                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
559                            ActionKeys.UPDATE);
560    
561                    serviceContext.setUserId(getUserId());
562    
563                    return dlFolderLocalService.updateFolder(
564                            folderId, name, description, defaultFileEntryTypeId,
565                            fileEntryTypeIds, restrictionType, serviceContext);
566            }
567    
568            @Override
569            public boolean verifyInheritableLock(long folderId, String lockUuid)
570                    throws PortalException {
571    
572                    boolean verified = false;
573    
574                    try {
575                            Lock lock = lockLocalService.getLock(
576                                    DLFolder.class.getName(), folderId);
577    
578                            if (!lock.isInheritable()) {
579                                    throw new NoSuchLockException("{folderId=" + folderId + "}");
580                            }
581    
582                            if (lock.getUuid().equals(lockUuid)) {
583                                    verified = true;
584                            }
585                    }
586                    catch (ExpiredLockException ele) {
587                            throw new NoSuchLockException(ele);
588                    }
589    
590                    return verified;
591            }
592    
593    }