001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.Lock;
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.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, SystemException {
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)
062                    throws PortalException, SystemException {
063    
064                    deleteFolder(folderId, true);
065            }
066    
067            @Override
068            public void deleteFolder(long folderId, boolean includeTrashedEntries)
069                    throws PortalException, SystemException {
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, SystemException {
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, SystemException {
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, SystemException {
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, SystemException {
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)
143                    throws PortalException, SystemException {
144    
145                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
146    
147                    DLFolderPermission.check(
148                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
149    
150                    return dlFolder;
151            }
152    
153            @Override
154            public DLFolder getFolder(long groupId, long parentFolderId, String name)
155                    throws PortalException, SystemException {
156    
157                    DLFolder dlFolder = dlFolderLocalService.getFolder(
158                            groupId, parentFolderId, name);
159    
160                    DLFolderPermission.check(
161                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
162    
163                    return dlFolder;
164            }
165    
166            @Override
167            public List<Long> getFolderIds(long groupId, long folderId)
168                    throws PortalException, SystemException {
169    
170                    if (!DLFolderPermission.contains(
171                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
172    
173                            return Collections.emptyList();
174                    }
175    
176                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
177    
178                    folderIds.add(0, folderId);
179    
180                    return folderIds;
181            }
182    
183            @Override
184            public List<DLFolder> getFolders(
185                            long groupId, long parentFolderId, int status,
186                            boolean includeMountfolders, int start, int end,
187                            OrderByComparator obc)
188                    throws PortalException, SystemException {
189    
190                    if (!DLFolderPermission.contains(
191                                    getPermissionChecker(), groupId, parentFolderId,
192                                    ActionKeys.VIEW)) {
193    
194                            return Collections.emptyList();
195                    }
196    
197                    if (includeMountfolders) {
198                            return dlFolderPersistence.filterFindByG_P_H_S(
199                                    groupId, parentFolderId, false, status, start, end, obc);
200                    }
201                    else {
202                            return dlFolderPersistence.filterFindByG_M_P_H_S(
203                                    groupId, false, parentFolderId, false, status, start, end, obc);
204                    }
205            }
206    
207            @Override
208            public List<DLFolder> getFolders(
209                            long groupId, long parentFolderId, int start, int end,
210                            OrderByComparator obc)
211                    throws PortalException, SystemException {
212    
213                    return getFolders(
214                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true,
215                            start, end, obc);
216            }
217    
218            @Override
219            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
220                            long groupId, long folderId, int status,
221                            boolean includeMountFolders, int start, int end,
222                            OrderByComparator obc)
223                    throws PortalException, SystemException {
224    
225                    if (!DLFolderPermission.contains(
226                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
227    
228                            return Collections.emptyList();
229                    }
230    
231                    QueryDefinition queryDefinition = new QueryDefinition(
232                            status, start, end, obc);
233    
234                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
235                            groupId, folderId, null, includeMountFolders, queryDefinition);
236            }
237    
238            @Override
239            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
240                            long groupId, long folderId, int status, String[] mimeTypes,
241                            boolean includeMountFolders, int start, int end,
242                            OrderByComparator obc)
243                    throws PortalException, SystemException {
244    
245                    if (!DLFolderPermission.contains(
246                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
247    
248                            return Collections.emptyList();
249                    }
250    
251                    QueryDefinition queryDefinition = new QueryDefinition(
252                            status, start, end, obc);
253    
254                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
255                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
256            }
257    
258            @Override
259            public int getFoldersAndFileEntriesAndFileShortcutsCount(
260                            long groupId, long folderId, int status,
261                            boolean includeMountFolders)
262                    throws PortalException, SystemException {
263    
264                    if (!DLFolderPermission.contains(
265                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
266    
267                            return 0;
268                    }
269    
270                    QueryDefinition queryDefinition = new QueryDefinition(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, SystemException {
281    
282                    if (!DLFolderPermission.contains(
283                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
284    
285                            return 0;
286                    }
287    
288                    QueryDefinition queryDefinition = new QueryDefinition(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, SystemException {
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, SystemException {
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 obc)
329                    throws PortalException, SystemException {
330    
331                    if (!DLFolderPermission.contains(
332                                    getPermissionChecker(), groupId, parentFolderId,
333                                    ActionKeys.VIEW)) {
334    
335                            return Collections.emptyList();
336                    }
337    
338                    return dlFolderPersistence.filterFindByG_M_P_H(
339                            groupId, true, parentFolderId, false, start, end, obc);
340            }
341    
342            @Override
343            public int getMountFoldersCount(long groupId, long parentFolderId)
344                    throws PortalException, SystemException {
345    
346                    if (!DLFolderPermission.contains(
347                                    getPermissionChecker(), groupId, parentFolderId,
348                                    ActionKeys.VIEW)) {
349    
350                            return 0;
351                    }
352    
353                    return dlFolderPersistence.filterCountByG_M_P_H(
354                            groupId, true, parentFolderId, false);
355            }
356    
357            /**
358             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
359             *             long, boolean)}
360             */
361            @Deprecated
362            @Override
363            public void getSubfolderIds(
364                            List<Long> folderIds, long groupId, long folderId)
365                    throws PortalException, SystemException {
366    
367                    getSubfolderIds(folderIds, groupId, folderId, true);
368            }
369    
370            @Override
371            public void getSubfolderIds(
372                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
373                    throws PortalException, SystemException {
374    
375                    if (!DLFolderPermission.contains(
376                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
377    
378                            return;
379                    }
380    
381                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
382                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
383    
384                    for (DLFolder dlFolder : dlFolders) {
385                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrash()) {
386                                    continue;
387                            }
388    
389                            folderIds.add(dlFolder.getFolderId());
390    
391                            if (recurse) {
392                                    getSubfolderIds(
393                                            folderIds, dlFolder.getGroupId(), dlFolder.getFolderId(),
394                                            recurse);
395                            }
396                    }
397            }
398    
399            @Override
400            public List<Long> getSubfolderIds(
401                            long groupId, long folderId, boolean recurse)
402                    throws PortalException, SystemException {
403    
404                    List<Long> folderIds = new ArrayList<Long>();
405    
406                    getSubfolderIds(folderIds, groupId, folderId, recurse);
407    
408                    return folderIds;
409            }
410    
411            @Override
412            public boolean hasFolderLock(long folderId)
413                    throws PortalException, SystemException {
414    
415                    return lockLocalService.hasLock(
416                            getUserId(), DLFolder.class.getName(), folderId);
417            }
418    
419            @Override
420            public boolean hasInheritableLock(long folderId)
421                    throws PortalException, SystemException {
422    
423                    boolean inheritable = false;
424    
425                    try {
426                            Lock lock = lockLocalService.getLock(
427                                    DLFolder.class.getName(), folderId);
428    
429                            inheritable = lock.isInheritable();
430                    }
431                    catch (ExpiredLockException ele) {
432                    }
433                    catch (NoSuchLockException nsle) {
434                    }
435    
436                    return inheritable;
437            }
438    
439            @Override
440            public boolean isFolderLocked(long folderId) throws SystemException {
441                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
442            }
443    
444            @Override
445            public Lock lockFolder(long folderId)
446                    throws PortalException, SystemException {
447    
448                    return lockFolder(
449                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
450            }
451    
452            @Override
453            public Lock lockFolder(
454                            long folderId, String owner, boolean inheritable,
455                            long expirationTime)
456                    throws PortalException, SystemException {
457    
458                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
459    
460                    DLFolderPermission.check(
461                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
462    
463                    return dlFolderLocalService.lockFolder(
464                            getUserId(), folderId, owner, inheritable, expirationTime);
465            }
466    
467            @Override
468            public DLFolder moveFolder(
469                            long folderId, long parentFolderId, ServiceContext serviceContext)
470                    throws PortalException, SystemException {
471    
472                    PermissionChecker permissionChecker = getPermissionChecker();
473    
474                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
475    
476                    DLFolderPermission.check(
477                            permissionChecker, dlFolder, ActionKeys.UPDATE);
478    
479                    DLFolderPermission.check(
480                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
481                            ActionKeys.ADD_FOLDER);
482    
483                    return dlFolderLocalService.moveFolder(
484                            getUserId(), folderId, parentFolderId, serviceContext);
485            }
486    
487            @Override
488            public Lock refreshFolderLock(
489                            String lockUuid, long companyId, long expirationTime)
490                    throws PortalException, SystemException {
491    
492                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
493            }
494    
495            @Override
496            public void unlockFolder(
497                            long groupId, long parentFolderId, String name, String lockUuid)
498                    throws PortalException, SystemException {
499    
500                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
501    
502                    unlockFolder(dlFolder.getFolderId(), lockUuid);
503            }
504    
505            @Override
506            public void unlockFolder(long folderId, String lockUuid)
507                    throws PortalException, SystemException {
508    
509                    try {
510                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
511    
512                            DLFolderPermission.check(
513                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
514                    }
515                    catch (NoSuchFolderException nsfe) {
516                    }
517    
518                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
519            }
520    
521            @Override
522            public DLFolder updateFolder(
523                            long folderId, String name, String description,
524                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
525                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
526                    throws PortalException, SystemException {
527    
528                    DLFolderPermission.check(
529                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
530                            ActionKeys.UPDATE);
531    
532                    serviceContext.setUserId(getUserId());
533    
534                    return dlFolderLocalService.updateFolder(
535                            folderId, name, description, defaultFileEntryTypeId,
536                            fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
537            }
538    
539            @Override
540            public boolean verifyInheritableLock(long folderId, String lockUuid)
541                    throws PortalException, SystemException {
542    
543                    boolean verified = false;
544    
545                    try {
546                            Lock lock = lockLocalService.getLock(
547                                    DLFolder.class.getName(), folderId);
548    
549                            if (!lock.isInheritable()) {
550                                    throw new NoSuchLockException("{folderId=" + folderId + "}");
551                            }
552    
553                            if (lock.getUuid().equals(lockUuid)) {
554                                    verified = true;
555                            }
556                    }
557                    catch (ExpiredLockException ele) {
558                            throw new NoSuchLockException(ele);
559                    }
560    
561                    return verified;
562            }
563    
564    }