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