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