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