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.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Alexander Chow
040     */
041    public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
042    
043            @Override
044            public DLFolder addFolder(
045                            long groupId, long repositoryId, boolean mountPoint,
046                            long parentFolderId, String name, String description,
047                            ServiceContext serviceContext)
048                    throws PortalException, SystemException {
049    
050                    DLFolderPermission.check(
051                            getPermissionChecker(), groupId, parentFolderId,
052                            ActionKeys.ADD_FOLDER);
053    
054                    return dlFolderLocalService.addFolder(
055                            getUserId(), groupId, repositoryId, mountPoint, parentFolderId,
056                            name, description, false, serviceContext);
057            }
058    
059            @Override
060            public void deleteFolder(long folderId)
061                    throws PortalException, SystemException {
062    
063                    deleteFolder(folderId, true);
064            }
065    
066            @Override
067            public void deleteFolder(long folderId, boolean includeTrashedEntries)
068                    throws PortalException, SystemException {
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, SystemException {
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, SystemException {
092    
093                    DLFolderPermission.check(
094                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
095    
096                    QueryDefinition queryDefinition = new QueryDefinition(
097                            status, start, end, null);
098    
099                    return dlFolderFinder.filterFindFE_FS_ByG_F(
100                            groupId, folderId, queryDefinition);
101            }
102    
103            @Override
104            public int getFileEntriesAndFileShortcutsCount(
105                            long groupId, long folderId, int status)
106                    throws SystemException {
107    
108                    QueryDefinition queryDefinition = new QueryDefinition(status);
109    
110                    return dlFolderFinder.filterCountFE_FS_ByG_F(
111                            groupId, folderId, queryDefinition);
112            }
113    
114            @Override
115            public int getFileEntriesAndFileShortcutsCount(
116                            long groupId, long folderId, int status, String[] mimeTypes)
117                    throws SystemException {
118    
119                    QueryDefinition queryDefinition = new QueryDefinition(status);
120    
121                    return dlFolderFinder.filterCountFE_FS_ByG_F_M(
122                            groupId, folderId, mimeTypes, queryDefinition);
123            }
124    
125            @Override
126            public DLFolder getFolder(long folderId)
127                    throws PortalException, SystemException {
128    
129                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
130    
131                    DLFolderPermission.check(
132                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
133    
134                    return dlFolder;
135            }
136    
137            @Override
138            public DLFolder getFolder(long groupId, long parentFolderId, String name)
139                    throws PortalException, SystemException {
140    
141                    DLFolder dlFolder = dlFolderLocalService.getFolder(
142                            groupId, parentFolderId, name);
143    
144                    DLFolderPermission.check(
145                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
146    
147                    return dlFolder;
148            }
149    
150            @Override
151            public List<Long> getFolderIds(long groupId, long folderId)
152                    throws PortalException, SystemException {
153    
154                    DLFolderPermission.check(
155                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
156    
157                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
158    
159                    folderIds.add(0, folderId);
160    
161                    return folderIds;
162            }
163    
164            @Override
165            public List<DLFolder> getFolders(
166                            long groupId, long parentFolderId, int status,
167                            boolean includeMountfolders, int start, int end,
168                            OrderByComparator obc)
169                    throws PortalException, SystemException {
170    
171                    DLFolderPermission.check(
172                            getPermissionChecker(), groupId, parentFolderId, ActionKeys.VIEW);
173    
174                    if (includeMountfolders) {
175                            return dlFolderPersistence.filterFindByG_P_H_S(
176                                    groupId, parentFolderId, false, status, start, end, obc);
177                    }
178                    else {
179                            return dlFolderPersistence.filterFindByG_M_P_H_S(
180                                    groupId, false, parentFolderId, false, status, start, end, obc);
181                    }
182            }
183    
184            @Override
185            public List<DLFolder> getFolders(
186                            long groupId, long parentFolderId, int start, int end,
187                            OrderByComparator obc)
188                    throws PortalException, SystemException {
189    
190                    return getFolders(
191                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true,
192                            start, end, obc);
193            }
194    
195            @Override
196            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
197                            long groupId, long folderId, int status,
198                            boolean includeMountFolders, int start, int end,
199                            OrderByComparator obc)
200                    throws PortalException, SystemException {
201    
202                    DLFolderPermission.check(
203                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
204    
205                    QueryDefinition queryDefinition = new QueryDefinition(
206                            status, start, end, obc);
207    
208                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
209                            groupId, folderId, null, includeMountFolders, queryDefinition);
210            }
211    
212            @Override
213            public int getFoldersAndFileEntriesAndFileShortcuts(
214                            long groupId, long folderId, int status, String[] mimeTypes,
215                            boolean includeMountFolders)
216                    throws PortalException, SystemException {
217    
218                    DLFolderPermission.check(
219                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
220    
221                    QueryDefinition queryDefinition = new QueryDefinition(status);
222    
223                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
224                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
225            }
226    
227            @Override
228            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
229                            long groupId, long folderId, int status, String[] mimeTypes,
230                            boolean includeMountFolders, int start, int end,
231                            OrderByComparator obc)
232                    throws PortalException, SystemException {
233    
234                    DLFolderPermission.check(
235                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
236    
237                    QueryDefinition queryDefinition = new QueryDefinition(
238                            status, start, end, obc);
239    
240                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
241                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
242            }
243    
244            @Override
245            public int getFoldersAndFileEntriesAndFileShortcutsCount(
246                            long groupId, long folderId, int status,
247                            boolean includeMountFolders)
248                    throws SystemException {
249    
250                    QueryDefinition queryDefinition = new QueryDefinition(status);
251    
252                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
253                            groupId, folderId, null, includeMountFolders, queryDefinition);
254            }
255    
256            @Override
257            public int getFoldersAndFileEntriesAndFileShortcutsCount(
258                            long groupId, long folderId, int status, String[] mimeTypes,
259                            boolean includeMountFolders)
260                    throws SystemException {
261    
262                    QueryDefinition queryDefinition = new QueryDefinition(status);
263    
264                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
265                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
266            }
267    
268            @Override
269            public int getFoldersCount(long groupId, long parentFolderId)
270                    throws SystemException {
271    
272                    return getFoldersCount(
273                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true);
274            }
275    
276            @Override
277            public int getFoldersCount(
278                            long groupId, long parentFolderId, int status,
279                            boolean includeMountfolders)
280                    throws SystemException {
281    
282                    if (includeMountfolders) {
283                            return dlFolderPersistence.filterCountByG_P_H_S(
284                                    groupId, parentFolderId, false, status);
285                    }
286                    else {
287                            return dlFolderPersistence.filterCountByG_M_P_H_S(
288                                    groupId, false, parentFolderId, false, status);
289                    }
290            }
291    
292            @Override
293            public List<DLFolder> getMountFolders(
294                            long groupId, long parentFolderId, int start, int end,
295                            OrderByComparator obc)
296                    throws PortalException, SystemException {
297    
298                    DLFolderPermission.check(
299                            getPermissionChecker(), groupId, parentFolderId, ActionKeys.VIEW);
300    
301                    return dlFolderPersistence.filterFindByG_M_P_H(
302                            groupId, true, parentFolderId, false, start, end, obc);
303            }
304    
305            @Override
306            public int getMountFoldersCount(long groupId, long parentFolderId)
307                    throws SystemException {
308    
309                    return dlFolderPersistence.filterCountByG_M_P_H(
310                            groupId, true, parentFolderId, false);
311            }
312    
313            @Override
314            public void getSubfolderIds(
315                            List<Long> folderIds, long groupId, long folderId)
316                    throws PortalException, SystemException {
317    
318                    DLFolderPermission.check(
319                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
320    
321                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P_H_S(
322                            groupId, folderId, false, WorkflowConstants.STATUS_APPROVED);
323    
324                    for (DLFolder dlFolder : dlFolders) {
325                            if (dlFolder.isInHiddenFolder() || dlFolder.isInTrashContainer()) {
326                                    continue;
327                            }
328    
329                            folderIds.add(dlFolder.getFolderId());
330    
331                            getSubfolderIds(
332                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
333                    }
334            }
335    
336            @Override
337            public List<Long> getSubfolderIds(
338                            long groupId, long folderId, boolean recurse)
339                    throws PortalException, SystemException {
340    
341                    List<Long> folderIds = new ArrayList<Long>();
342    
343                    getSubfolderIds(folderIds, groupId, folderId);
344    
345                    return folderIds;
346            }
347    
348            @Override
349            public boolean hasFolderLock(long folderId)
350                    throws PortalException, SystemException {
351    
352                    return lockLocalService.hasLock(
353                            getUserId(), DLFolder.class.getName(), folderId);
354            }
355    
356            @Override
357            public boolean hasInheritableLock(long folderId)
358                    throws PortalException, SystemException {
359    
360                    boolean inheritable = false;
361    
362                    try {
363                            Lock lock = lockLocalService.getLock(
364                                    DLFolder.class.getName(), folderId);
365    
366                            inheritable = lock.isInheritable();
367                    }
368                    catch (ExpiredLockException ele) {
369                    }
370                    catch (NoSuchLockException nsle) {
371                    }
372    
373                    return inheritable;
374            }
375    
376            @Override
377            public boolean isFolderLocked(long folderId) throws SystemException {
378                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
379            }
380    
381            @Override
382            public Lock lockFolder(long folderId)
383                    throws PortalException, SystemException {
384    
385                    return lockFolder(
386                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
387            }
388    
389            @Override
390            public Lock lockFolder(
391                            long folderId, String owner, boolean inheritable,
392                            long expirationTime)
393                    throws PortalException, SystemException {
394    
395                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
396    
397                    DLFolderPermission.check(
398                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
399    
400                    return dlFolderLocalService.lockFolder(
401                            getUserId(), folderId, owner, inheritable, expirationTime);
402            }
403    
404            @Override
405            public DLFolder moveFolder(
406                            long folderId, long parentFolderId, ServiceContext serviceContext)
407                    throws PortalException, SystemException {
408    
409                    PermissionChecker permissionChecker = getPermissionChecker();
410    
411                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
412    
413                    DLFolderPermission.check(
414                            permissionChecker, dlFolder, ActionKeys.UPDATE);
415    
416                    DLFolderPermission.check(
417                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
418                            ActionKeys.ADD_FOLDER);
419    
420                    return dlFolderLocalService.moveFolder(
421                            getUserId(), folderId, parentFolderId, serviceContext);
422            }
423    
424            @Override
425            public Lock refreshFolderLock(
426                            String lockUuid, long companyId, long expirationTime)
427                    throws PortalException, SystemException {
428    
429                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
430            }
431    
432            @Override
433            public void unlockFolder(
434                            long groupId, long parentFolderId, String name, String lockUuid)
435                    throws PortalException, SystemException {
436    
437                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
438    
439                    unlockFolder(dlFolder.getFolderId(), lockUuid);
440            }
441    
442            @Override
443            public void unlockFolder(long folderId, String lockUuid)
444                    throws PortalException, SystemException {
445    
446                    try {
447                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
448    
449                            DLFolderPermission.check(
450                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
451                    }
452                    catch (NoSuchFolderException nsfe) {
453                    }
454    
455                    dlFolderLocalService.unlockFolder(folderId, lockUuid);
456            }
457    
458            @Override
459            public DLFolder updateFolder(
460                            long folderId, String name, String description,
461                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
462                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
463                    throws PortalException, SystemException {
464    
465                    DLFolderPermission.check(
466                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
467                            ActionKeys.UPDATE);
468    
469                    return dlFolderLocalService.updateFolder(
470                            folderId, name, description, defaultFileEntryTypeId,
471                            fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
472            }
473    
474            @Override
475            public boolean verifyInheritableLock(long folderId, String lockUuid)
476                    throws PortalException, SystemException {
477    
478                    boolean verified = false;
479    
480                    try {
481                            Lock lock = lockLocalService.getLock(
482                                    DLFolder.class.getName(), folderId);
483    
484                            if (!lock.isInheritable()) {
485                                    throw new NoSuchLockException();
486                            }
487    
488                            if (lock.getUuid().equals(lockUuid)) {
489                                    verified = true;
490                            }
491                    }
492                    catch (ExpiredLockException ele) {
493                            throw new NoSuchLockException(ele);
494                    }
495    
496                    return verified;
497            }
498    
499    }