001    /**
002     * Copyright (c) 2000-2012 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.InvalidLockException;
019    import com.liferay.portal.NoSuchLockException;
020    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.model.Lock;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
034    import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Alexander Chow
043     */
044    public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
045    
046            public DLFolder addFolder(
047                            long groupId, long repositoryId, boolean mountPoint,
048                            long parentFolderId, String name, String description,
049                            ServiceContext serviceContext)
050                    throws PortalException, SystemException {
051    
052                    DLFolderPermission.check(
053                            getPermissionChecker(), groupId, parentFolderId,
054                            ActionKeys.ADD_FOLDER);
055    
056                    return dlFolderLocalService.addFolder(
057                            getUserId(), groupId, repositoryId, mountPoint, parentFolderId,
058                            name, description, false, serviceContext);
059            }
060    
061            public void deleteFolder(long folderId)
062                    throws PortalException, SystemException {
063    
064                    deleteFolder(folderId, true);
065            }
066    
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                    boolean hasLock = hasFolderLock(folderId);
076    
077                    Lock lock = null;
078    
079                    if (!hasLock) {
080    
081                            // Lock
082    
083                            lock = doLockFolder(
084                                    folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
085                    }
086    
087                    try {
088                            dlFolderLocalService.deleteFolder(folderId, includeTrashedEntries);
089                    }
090                    finally {
091                            if (!hasLock) {
092    
093                                    // Unlock
094    
095                                    doUnlockFolder(dlFolder.getGroupId(), folderId, lock.getUuid());
096                            }
097                    }
098            }
099    
100            public void deleteFolder(long groupId, long parentFolderId, String name)
101                    throws PortalException, SystemException {
102    
103                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
104    
105                    deleteFolder(dlFolder.getFolderId());
106            }
107    
108            public List<Object> getFileEntriesAndFileShortcuts(
109                            long groupId, long folderId, int status, int start, int end)
110                    throws PortalException, SystemException {
111    
112                    DLFolderPermission.check(
113                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
114    
115                    QueryDefinition queryDefinition = new QueryDefinition(
116                            status, start, end, null);
117    
118                    return dlFolderFinder.filterFindFE_FS_ByG_F(
119                            groupId, folderId, queryDefinition);
120            }
121    
122            public int getFileEntriesAndFileShortcutsCount(
123                            long groupId, long folderId, int status)
124                    throws SystemException {
125    
126                    QueryDefinition queryDefinition = new QueryDefinition(status);
127    
128                    return dlFolderFinder.filterCountFE_FS_ByG_F(
129                            groupId, folderId, queryDefinition);
130            }
131    
132            public int getFileEntriesAndFileShortcutsCount(
133                            long groupId, long folderId, int status, String[] mimeTypes)
134                    throws SystemException {
135    
136                    QueryDefinition queryDefinition = new QueryDefinition(status);
137    
138                    return dlFolderFinder.filterCountFE_FS_ByG_F_M(
139                            groupId, folderId, mimeTypes, queryDefinition);
140            }
141    
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            public DLFolder getFolder(long groupId, long parentFolderId, String name)
154                    throws PortalException, SystemException {
155    
156                    DLFolder dlFolder = dlFolderLocalService.getFolder(
157                            groupId, parentFolderId, name);
158    
159                    DLFolderPermission.check(
160                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
161    
162                    return dlFolder;
163            }
164    
165            public long[] getFolderIds(long groupId, long folderId)
166                    throws PortalException, SystemException {
167    
168                    DLFolderPermission.check(
169                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
170    
171                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
172    
173                    folderIds.add(0, folderId);
174    
175                    return ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()]));
176            }
177    
178            public List<DLFolder> getFolders(
179                            long groupId, long parentFolderId, int status,
180                            boolean includeMountfolders, int start, int end,
181                            OrderByComparator obc)
182                    throws PortalException, SystemException {
183    
184                    DLFolderPermission.check(
185                            getPermissionChecker(), groupId, parentFolderId, ActionKeys.VIEW);
186    
187                    if (includeMountfolders) {
188                            return dlFolderPersistence.filterFindByG_P_H_S(
189                                    groupId, parentFolderId, false, status, start, end, obc);
190                    }
191                    else {
192                            return dlFolderPersistence.filterFindByG_M_P_H_S(
193                                    groupId, false, parentFolderId, false, status, start, end, obc);
194                    }
195            }
196    
197            public List<DLFolder> getFolders(
198                            long groupId, long parentFolderId, int start, int end,
199                            OrderByComparator obc)
200                    throws PortalException, SystemException {
201    
202                    return getFolders(
203                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true,
204                            start, end, obc);
205            }
206    
207            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
208                            long groupId, long folderId, int status,
209                            boolean includeMountFolders, int start, int end,
210                            OrderByComparator obc)
211                    throws PortalException, SystemException {
212    
213                    DLFolderPermission.check(
214                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
215    
216                    QueryDefinition queryDefinition = new QueryDefinition(
217                            status, start, end, obc);
218    
219                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
220                            groupId, folderId, null, includeMountFolders, queryDefinition);
221            }
222    
223            public int getFoldersAndFileEntriesAndFileShortcuts(
224                            long groupId, long folderId, int status, String[] mimeTypes,
225                            boolean includeMountFolders)
226                    throws PortalException, SystemException {
227    
228                    DLFolderPermission.check(
229                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
230    
231                    QueryDefinition queryDefinition = new QueryDefinition(status);
232    
233                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
234                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
235            }
236    
237            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
238                            long groupId, long folderId, int status, String[] mimeTypes,
239                            boolean includeMountFolders, int start, int end,
240                            OrderByComparator obc)
241                    throws PortalException, SystemException {
242    
243                    DLFolderPermission.check(
244                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
245    
246                    QueryDefinition queryDefinition = new QueryDefinition(
247                            status, start, end, obc);
248    
249                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_M_M(
250                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
251            }
252    
253            public int getFoldersAndFileEntriesAndFileShortcutsCount(
254                            long groupId, long folderId, int status,
255                            boolean includeMountFolders)
256                    throws SystemException {
257    
258                    QueryDefinition queryDefinition = new QueryDefinition(status);
259    
260                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
261                            groupId, folderId, null, includeMountFolders, queryDefinition);
262            }
263    
264            public int getFoldersAndFileEntriesAndFileShortcutsCount(
265                            long groupId, long folderId, int status, String[] mimeTypes,
266                            boolean includeMountFolders)
267                    throws SystemException {
268    
269                    QueryDefinition queryDefinition = new QueryDefinition(status);
270    
271                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_M_M(
272                            groupId, folderId, mimeTypes, includeMountFolders, queryDefinition);
273            }
274    
275            public int getFoldersCount(long groupId, long parentFolderId)
276                    throws SystemException {
277    
278                    return getFoldersCount(
279                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, true);
280            }
281    
282            public int getFoldersCount(
283                            long groupId, long parentFolderId, int status,
284                            boolean includeMountfolders)
285                    throws SystemException {
286    
287                    if (includeMountfolders) {
288                            return dlFolderPersistence.filterCountByG_P_H_S(
289                                    groupId, parentFolderId, false, status);
290                    }
291                    else {
292                            return dlFolderPersistence.filterCountByG_M_P_H_S(
293                                    groupId, false, parentFolderId, false, status);
294                    }
295            }
296    
297            public List<DLFolder> getMountFolders(
298                            long groupId, long parentFolderId, int start, int end,
299                            OrderByComparator obc)
300                    throws PortalException, SystemException {
301    
302                    DLFolderPermission.check(
303                            getPermissionChecker(), groupId, parentFolderId, ActionKeys.VIEW);
304    
305                    return dlFolderPersistence.filterFindByG_M_P_H(
306                            groupId, true, parentFolderId, false, start, end, obc);
307            }
308    
309            public int getMountFoldersCount(long groupId, long parentFolderId)
310                    throws SystemException {
311    
312                    return dlFolderPersistence.filterCountByG_M_P_H(
313                            groupId, true, parentFolderId, false);
314            }
315    
316            public void getSubfolderIds(
317                            List<Long> folderIds, long groupId, long folderId)
318                    throws PortalException, SystemException {
319    
320                    DLFolderPermission.check(
321                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
322    
323                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P(
324                            groupId, folderId);
325    
326                    for (DLFolder dlFolder : dlFolders) {
327                            folderIds.add(dlFolder.getFolderId());
328    
329                            getSubfolderIds(
330                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
331                    }
332            }
333    
334            public List<Long> getSubfolderIds(
335                            long groupId, long folderId, boolean recurse)
336                    throws PortalException, SystemException {
337    
338                    List<Long> folderIds = new ArrayList<Long>();
339    
340                    getSubfolderIds(folderIds, groupId, folderId);
341    
342                    return folderIds;
343            }
344    
345            public boolean hasFolderLock(long folderId)
346                    throws PortalException, SystemException {
347    
348                    return lockLocalService.hasLock(
349                            getUserId(), DLFolder.class.getName(), folderId);
350            }
351    
352            public boolean hasInheritableLock(long folderId)
353                    throws PortalException, SystemException {
354    
355                    boolean inheritable = false;
356    
357                    try {
358                            Lock lock = lockLocalService.getLock(
359                                    DLFolder.class.getName(), folderId);
360    
361                            inheritable = lock.isInheritable();
362                    }
363                    catch (ExpiredLockException ele) {
364                    }
365                    catch (NoSuchLockException nsle) {
366                    }
367    
368                    return inheritable;
369            }
370    
371            public boolean isFolderLocked(long folderId) throws SystemException {
372                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
373            }
374    
375            public Lock lockFolder(long folderId)
376                    throws PortalException, SystemException {
377    
378                    return lockFolder(
379                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
380            }
381    
382            public Lock lockFolder(
383                            long folderId, String owner, boolean inheritable,
384                            long expirationTime)
385                    throws PortalException, SystemException {
386    
387                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
388    
389                    DLFolderPermission.check(
390                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
391    
392                    return doLockFolder(folderId, owner, inheritable, expirationTime);
393            }
394    
395            public DLFolder moveFolder(
396                            long folderId, long parentFolderId, ServiceContext serviceContext)
397                    throws PortalException, SystemException {
398    
399                    PermissionChecker permissionChecker = getPermissionChecker();
400    
401                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
402    
403                    DLFolderPermission.check(
404                            permissionChecker, dlFolder, ActionKeys.UPDATE);
405    
406                    DLFolderPermission.check(
407                            permissionChecker, serviceContext.getScopeGroupId(), parentFolderId,
408                            ActionKeys.ADD_FOLDER);
409    
410                    boolean hasLock = hasFolderLock(folderId);
411    
412                    Lock lock = null;
413    
414                    if (!hasLock) {
415    
416                            // Lock
417    
418                            lock = lockFolder(folderId);
419                    }
420    
421                    try {
422                            return dlFolderLocalService.moveFolder(
423                                    folderId, parentFolderId, serviceContext);
424                    }
425                    finally {
426                            if (!hasLock) {
427    
428                                    // Unlock
429    
430                                    unlockFolder(dlFolder.getGroupId(), folderId, lock.getUuid());
431                            }
432                    }
433            }
434    
435            public Lock refreshFolderLock(
436                            String lockUuid, long companyId, long expirationTime)
437                    throws PortalException, SystemException {
438    
439                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
440            }
441    
442            public void unlockFolder(long groupId, long folderId, String lockUuid)
443                    throws PortalException, SystemException {
444    
445                    try {
446                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
447    
448                            DLFolderPermission.check(
449                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
450                    }
451                    catch (NoSuchFolderException nsfe) {
452                    }
453    
454                    doUnlockFolder(groupId, folderId, lockUuid);
455            }
456    
457            public void unlockFolder(
458                            long groupId, long parentFolderId, String name, String lockUuid)
459                    throws PortalException, SystemException {
460    
461                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
462    
463                    unlockFolder(groupId, dlFolder.getFolderId(), lockUuid);
464            }
465    
466            public DLFolder updateFolder(
467                            long folderId, String name, String description,
468                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
469                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
470                    throws PortalException, SystemException {
471    
472                    DLFolderPermission.check(
473                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
474                            ActionKeys.UPDATE);
475    
476                    boolean hasLock = hasFolderLock(folderId);
477    
478                    Lock lock = null;
479    
480                    if (!hasLock) {
481    
482                            // Lock
483    
484                            lock = doLockFolder(
485                                    folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
486                    }
487    
488                    try {
489                            return dlFolderLocalService.updateFolder(
490                                    folderId, name, description, defaultFileEntryTypeId,
491                                    fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
492                    }
493                    finally {
494                            if (!hasLock) {
495    
496                                    // Unlock
497    
498                                    unlockFolder(
499                                            serviceContext.getScopeGroupId(), folderId, lock.getUuid());
500                            }
501                    }
502            }
503    
504            public boolean verifyInheritableLock(long folderId, String lockUuid)
505                    throws PortalException, SystemException {
506    
507                    boolean verified = false;
508    
509                    try {
510                            Lock lock = lockLocalService.getLock(
511                                    DLFolder.class.getName(), folderId);
512    
513                            if (!lock.isInheritable()) {
514                                    throw new NoSuchLockException();
515                            }
516    
517                            if (lock.getUuid().equals(lockUuid)) {
518                                    verified = true;
519                            }
520                    }
521                    catch (ExpiredLockException ele) {
522                            throw new NoSuchLockException(ele);
523                    }
524    
525                    return verified;
526            }
527    
528            protected Lock doLockFolder(
529                            long folderId, String owner, boolean inheritable,
530                            long expirationTime)
531                    throws PortalException, SystemException {
532    
533                    if ((expirationTime <= 0) ||
534                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
535    
536                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
537                    }
538    
539                    return lockLocalService.lock(
540                            getUserId(), DLFolder.class.getName(), folderId, owner, inheritable,
541                            expirationTime);
542            }
543    
544            protected void doUnlockFolder(long groupId, long folderId, String lockUuid)
545                    throws PortalException, SystemException {
546    
547                    if (Validator.isNotNull(lockUuid)) {
548                            try {
549                                    Lock lock = lockLocalService.getLock(
550                                            DLFolder.class.getName(), folderId);
551    
552                                    if (!lockUuid.equals(lock.getUuid())) {
553                                            throw new InvalidLockException("UUIDs do not match");
554                                    }
555                            }
556                            catch (PortalException pe) {
557                                    if (pe instanceof ExpiredLockException ||
558                                            pe instanceof NoSuchLockException) {
559                                    }
560                                    else {
561                                            throw pe;
562                                    }
563                            }
564                    }
565    
566                    lockLocalService.unlock(DLFolder.class.getName(), folderId);
567            }
568    
569    }