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            @Override
358            public void getSubfolderIds(
359                            List<Long> folderIds, long groupId, long folderId)
360                    throws PortalException, SystemException {
361    
362                    if (!DLFolderPermission.contains(
363                                    getPermissionChecker(), groupId, folderId, ActionKeys.VIEW)) {
364    
365                            return;
366                    }
367    
368                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
369                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
370    
371                    for (DLFolder dlFolder : dlFolders) {
372                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrash()) {
373                                    continue;
374                            }
375    
376                            folderIds.add(dlFolder.getFolderId());
377    
378                            getSubfolderIds(
379                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
380                    }
381            }
382    
383            @Override
384            public List<Long> getSubfolderIds(
385                            long groupId, long folderId, boolean recurse)
386                    throws PortalException, SystemException {
387    
388                    List<Long> folderIds = new ArrayList<Long>();
389    
390                    getSubfolderIds(folderIds, groupId, folderId);
391    
392                    return folderIds;
393            }
394    
395            @Override
396            public boolean hasFolderLock(long folderId)
397                    throws PortalException, SystemException {
398    
399                    return lockLocalService.hasLock(
400                            getUserId(), DLFolder.class.getName(), folderId);
401            }
402    
403            @Override
404            public boolean hasInheritableLock(long folderId)
405                    throws PortalException, SystemException {
406    
407                    boolean inheritable = false;
408    
409                    try {
410                            Lock lock = lockLocalService.getLock(
411                                    DLFolder.class.getName(), folderId);
412    
413                            inheritable = lock.isInheritable();
414                    }
415                    catch (ExpiredLockException ele) {
416                    }
417                    catch (NoSuchLockException nsle) {
418                    }
419    
420                    return inheritable;
421            }
422    
423            @Override
424            public boolean isFolderLocked(long folderId) throws SystemException {
425                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
426            }
427    
428            @Override
429            public Lock lockFolder(long folderId)
430                    throws PortalException, SystemException {
431    
432                    return lockFolder(
433                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
434            }
435    
436            @Override
437            public Lock lockFolder(
438                            long folderId, String owner, boolean inheritable,
439                            long expirationTime)
440                    throws PortalException, SystemException {
441    
442                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
443    
444                    DLFolderPermission.check(
445                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
446    
447                    return dlFolderLocalService.lockFolder(
448                            getUserId(), folderId, owner, inheritable, expirationTime);
449            }
450    
451            @Override
452            public DLFolder moveFolder(
453                            long folderId, long parentFolderId, ServiceContext serviceContext)
454                    throws PortalException, SystemException {
455    
456                    PermissionChecker permissionChecker = getPermissionChecker();
457    
458                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
459    
460                    DLFolderPermission.check(
461                            permissionChecker, dlFolder, ActionKeys.UPDATE);
462    
463                    DLFolderPermission.check(
464                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
465                            ActionKeys.ADD_FOLDER);
466    
467                    return dlFolderLocalService.moveFolder(
468                            getUserId(), folderId, parentFolderId, serviceContext);
469            }
470    
471            @Override
472            public Lock refreshFolderLock(
473                            String lockUuid, long companyId, long expirationTime)
474                    throws PortalException, SystemException {
475    
476                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
477            }
478    
479            @Override
480            public void unlockFolder(
481                            long groupId, long parentFolderId, String name, String lockUuid)
482                    throws PortalException, SystemException {
483    
484                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
485    
486                    unlockFolder(dlFolder.getFolderId(), lockUuid);
487            }
488    
489            @Override
490            public void unlockFolder(long folderId, String lockUuid)
491                    throws PortalException, SystemException {
492    
493                    try {
494                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
495    
496                            DLFolderPermission.check(
497                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
498                    }
499                    catch (NoSuchFolderException nsfe) {
500                    }
501    
502                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
503            }
504    
505            @Override
506            public DLFolder updateFolder(
507                            long folderId, String name, String description,
508                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
509                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
510                    throws PortalException, SystemException {
511    
512                    DLFolderPermission.check(
513                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
514                            ActionKeys.UPDATE);
515    
516                    serviceContext.setUserId(getUserId());
517    
518                    return dlFolderLocalService.updateFolder(
519                            folderId, name, description, defaultFileEntryTypeId,
520                            fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
521            }
522    
523            @Override
524            public boolean verifyInheritableLock(long folderId, String lockUuid)
525                    throws PortalException, SystemException {
526    
527                    boolean verified = false;
528    
529                    try {
530                            Lock lock = lockLocalService.getLock(
531                                    DLFolder.class.getName(), folderId);
532    
533                            if (!lock.isInheritable()) {
534                                    throw new NoSuchLockException();
535                            }
536    
537                            if (lock.getUuid().equals(lockUuid)) {
538                                    verified = true;
539                            }
540                    }
541                    catch (ExpiredLockException ele) {
542                            throw new NoSuchLockException(ele);
543                    }
544    
545                    return verified;
546            }
547    
548    }