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_S(
189                                    groupId, parentFolderId, status, start, end, obc);
190                    }
191                    else {
192                            return dlFolderPersistence.filterFindByG_M_P_S(
193                                    groupId, false, parentFolderId, 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_S(
289                                    groupId, parentFolderId, status);
290                    }
291                    else {
292                            return dlFolderPersistence.filterCountByG_M_P_S(
293                                    groupId, false, parentFolderId, 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 = lockLocalService.hasLock(
411                            getUserId(), DLFolder.class.getName(), folderId);
412    
413                    Lock lock = null;
414    
415                    if (!hasLock) {
416    
417                            // Lock
418    
419                            lock = lockFolder(folderId);
420                    }
421    
422                    try {
423                            return dlFolderLocalService.moveFolder(
424                                    folderId, parentFolderId, serviceContext);
425                    }
426                    finally {
427                            if (!hasLock) {
428    
429                                    // Unlock
430    
431                                    unlockFolder(dlFolder.getGroupId(), folderId, lock.getUuid());
432                            }
433                    }
434            }
435    
436            public Lock refreshFolderLock(
437                            String lockUuid, long companyId, long expirationTime)
438                    throws PortalException, SystemException {
439    
440                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
441            }
442    
443            public void unlockFolder(long groupId, 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                    doUnlockFolder(groupId, folderId, lockUuid);
456            }
457    
458            public void unlockFolder(
459                            long groupId, long parentFolderId, String name, String lockUuid)
460                    throws PortalException, SystemException {
461    
462                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
463    
464                    unlockFolder(groupId, dlFolder.getFolderId(), lockUuid);
465            }
466    
467            public DLFolder updateFolder(
468                            long folderId, String name, String description,
469                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
470                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
471                    throws PortalException, SystemException {
472    
473                    DLFolderPermission.check(
474                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
475                            ActionKeys.UPDATE);
476    
477                    boolean hasLock = lockLocalService.hasLock(
478                            getUserId(), DLFolder.class.getName(), folderId);
479    
480                    Lock lock = null;
481    
482                    if (!hasLock) {
483    
484                            // Lock
485    
486                            lock = doLockFolder(
487                                    folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
488                    }
489    
490                    try {
491                            return dlFolderLocalService.updateFolder(
492                                    folderId, name, description, defaultFileEntryTypeId,
493                                    fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
494                    }
495                    finally {
496                            if (!hasLock) {
497    
498                                    // Unlock
499    
500                                    unlockFolder(
501                                            serviceContext.getScopeGroupId(), folderId, lock.getUuid());
502                            }
503                    }
504            }
505    
506            public boolean verifyInheritableLock(long folderId, String lockUuid)
507                    throws PortalException, SystemException {
508    
509                    boolean verified = false;
510    
511                    try {
512                            Lock lock = lockLocalService.getLock(
513                                    DLFolder.class.getName(), folderId);
514    
515                            if (!lock.isInheritable()) {
516                                    throw new NoSuchLockException();
517                            }
518    
519                            if (lock.getUuid().equals(lockUuid)) {
520                                    verified = true;
521                            }
522                    }
523                    catch (ExpiredLockException ele) {
524                            throw new NoSuchLockException(ele);
525                    }
526    
527                    return verified;
528            }
529    
530            protected Lock doLockFolder(
531                            long folderId, String owner, boolean inheritable,
532                            long expirationTime)
533                    throws PortalException, SystemException {
534    
535                    if ((expirationTime <= 0) ||
536                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
537    
538                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
539                    }
540    
541                    return lockLocalService.lock(
542                            getUserId(), DLFolder.class.getName(), folderId, owner, inheritable,
543                            expirationTime);
544            }
545    
546            protected void doUnlockFolder(long groupId, long folderId, String lockUuid)
547                    throws PortalException, SystemException {
548    
549                    if (Validator.isNotNull(lockUuid)) {
550                            try {
551                                    Lock lock = lockLocalService.getLock(
552                                            DLFolder.class.getName(), folderId);
553    
554                                    if (!lockUuid.equals(lock.getUuid())) {
555                                            throw new InvalidLockException("UUIDs do not match");
556                                    }
557                            }
558                            catch (PortalException pe) {
559                                    if (pe instanceof ExpiredLockException ||
560                                            pe instanceof NoSuchLockException) {
561                                    }
562                                    else {
563                                            throw pe;
564                                    }
565                            }
566                    }
567    
568                    lockLocalService.unlock(DLFolder.class.getName(), folderId);
569            }
570    
571    }