001    /**
002     * Copyright (c) 2000-2011 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 DLFolder getFolder(long folderId)
133                    throws PortalException, SystemException {
134    
135                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
136    
137                    DLFolderPermission.check(
138                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
139    
140                    return dlFolder;
141            }
142    
143            public DLFolder getFolder(long groupId, long parentFolderId, String name)
144                    throws PortalException, SystemException {
145    
146                    DLFolder dlFolder = dlFolderLocalService.getFolder(
147                            groupId, parentFolderId, name);
148    
149                    DLFolderPermission.check(
150                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
151    
152                    return dlFolder;
153            }
154    
155            public long[] getFolderIds(long groupId, long folderId)
156                    throws SystemException {
157    
158                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
159    
160                    folderIds.add(0, folderId);
161    
162                    return ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()]));
163            }
164    
165            public List<DLFolder> getFolders(
166                            long groupId, long parentFolderId, boolean includeMountfolders,
167                            int start, int end, OrderByComparator obc)
168                    throws SystemException {
169    
170                    if (includeMountfolders) {
171                            return dlFolderPersistence.filterFindByG_P(
172                                    groupId, parentFolderId, start, end, obc);
173                    }
174                    else {
175                            return dlFolderPersistence.filterFindByG_P_M(
176                                    groupId, parentFolderId, false, start, end, obc);
177                    }
178            }
179    
180            public List<DLFolder> getFolders(
181                            long groupId, long parentFolderId, int start, int end,
182                            OrderByComparator obc)
183                    throws SystemException {
184    
185                    return getFolders(groupId, parentFolderId, true, start, end, obc);
186            }
187    
188            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
189                            long groupId, long folderId, int status,
190                            boolean includeMountFolders, int start, int end,
191                            OrderByComparator obc)
192                    throws SystemException {
193    
194                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_S(
195                            groupId, folderId, status, null, includeMountFolders, start, end,
196                            obc);
197            }
198    
199            public int getFoldersAndFileEntriesAndFileShortcuts(
200                            long groupId, long folderId, int status, String[] mimeTypes,
201                            boolean includeMountFolders)
202                    throws SystemException {
203    
204                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_S(
205                            groupId, folderId, status, mimeTypes, includeMountFolders);
206            }
207    
208            public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
209                            long groupId, long folderId, int status, String[] mimeTypes,
210                            boolean includeMountFolders, int start, int end,
211                            OrderByComparator obc)
212                    throws SystemException {
213    
214                    return dlFolderFinder.filterFindF_FE_FS_ByG_F_S(
215                            groupId, folderId, status, mimeTypes, includeMountFolders, start,
216                            end, obc);
217            }
218    
219            public int getFoldersAndFileEntriesAndFileShortcutsCount(
220                            long groupId, long folderId, int status,
221                            boolean includeMountFolders)
222                    throws SystemException {
223    
224                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_S(
225                            groupId, folderId, status, null, includeMountFolders);
226            }
227    
228            public int getFoldersAndFileEntriesAndFileShortcutsCount(
229                            long groupId, long folderId, int status, String[] mimeTypes,
230                            boolean includeMountFolders)
231                    throws SystemException {
232    
233                    return dlFolderFinder.filterCountF_FE_FS_ByG_F_S(
234                            groupId, folderId, status, mimeTypes, includeMountFolders);
235            }
236    
237            public int getFoldersCount(long groupId, long parentFolderId)
238                    throws SystemException {
239    
240                    return getFoldersCount(groupId, parentFolderId, true);
241            }
242    
243            public int getFoldersCount(
244                            long groupId, long parentFolderId, boolean includeMountfolders)
245                    throws SystemException {
246    
247                    if (includeMountfolders) {
248                            return dlFolderPersistence.filterCountByG_P(
249                                    groupId, parentFolderId);
250                    }
251                    else {
252                            return dlFolderPersistence.filterCountByG_P_M(
253                                    groupId, parentFolderId, false);
254                    }
255    
256            }
257    
258            public List<DLFolder> getMountFolders(
259                            long groupId, long parentFolderId, int start, int end,
260                            OrderByComparator obc)
261                    throws SystemException {
262    
263                    return dlFolderPersistence.filterFindByG_P_M(
264                            groupId, parentFolderId, true, start, end, obc);
265            }
266    
267            public int getMountFoldersCount(long groupId, long parentFolderId)
268                    throws SystemException {
269    
270                    return dlFolderPersistence.filterCountByG_P_M(
271                            groupId, parentFolderId, true);
272            }
273    
274            public void getSubfolderIds(
275                            List<Long> folderIds, long groupId, long folderId)
276                    throws SystemException {
277    
278                    List<DLFolder> dlFolders = dlFolderPersistence.filterFindByG_P(
279                            groupId, folderId);
280    
281                    for (DLFolder dlFolder : dlFolders) {
282                            folderIds.add(dlFolder.getFolderId());
283    
284                            getSubfolderIds(
285                                    folderIds, dlFolder.getGroupId(), dlFolder.getFolderId());
286                    }
287            }
288    
289            public List<Long> getSubfolderIds(
290                            long groupId, long folderId, boolean recurse)
291                    throws SystemException {
292    
293                    List<Long> folderIds = new ArrayList<Long>();
294    
295                    getSubfolderIds(folderIds, groupId, folderId);
296    
297                    return folderIds;
298            }
299    
300            public boolean hasFolderLock(long folderId)
301                    throws PortalException, SystemException {
302    
303                    return lockLocalService.hasLock(
304                            getUserId(), DLFolder.class.getName(), folderId);
305            }
306    
307            public boolean hasInheritableLock(long folderId)
308                    throws PortalException, SystemException {
309    
310                    boolean inheritable = false;
311    
312                    try {
313                            Lock lock = lockLocalService.getLock(
314                                    DLFolder.class.getName(), folderId);
315    
316                            inheritable = lock.isInheritable();
317                    }
318                    catch (ExpiredLockException ele) {
319                    }
320                    catch (NoSuchLockException nsle) {
321                    }
322    
323                    return inheritable;
324            }
325    
326            public boolean isFolderLocked(long folderId) throws SystemException {
327                    return lockLocalService.isLocked(DLFolder.class.getName(), folderId);
328            }
329    
330            public Lock lockFolder(long folderId)
331                    throws PortalException, SystemException {
332    
333                    return lockFolder(
334                            folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
335            }
336    
337            public Lock lockFolder(
338                            long folderId, String owner, boolean inheritable,
339                            long expirationTime)
340                    throws PortalException, SystemException {
341    
342                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
343    
344                    DLFolderPermission.check(
345                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
346    
347                    return doLockFolder(folderId, owner, inheritable, expirationTime);
348            }
349    
350            public DLFolder moveFolder(
351                            long folderId, long parentFolderId, ServiceContext serviceContext)
352                    throws PortalException, SystemException {
353    
354                    DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
355    
356                    DLFolderPermission.check(
357                            getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
358    
359                    boolean hasLock = lockLocalService.hasLock(
360                            getUserId(), DLFolder.class.getName(), folderId);
361    
362                    Lock lock = null;
363    
364                    if (!hasLock) {
365    
366                            // Lock
367    
368                            lock = lockFolder(folderId);
369                    }
370    
371                    try {
372                            return dlFolderLocalService.moveFolder(
373                                    folderId, parentFolderId, serviceContext);
374                    }
375                    finally {
376                            if (!hasLock) {
377    
378                                    // Unlock
379    
380                                    unlockFolder(dlFolder.getGroupId(), folderId, lock.getUuid());
381                            }
382                    }
383            }
384    
385            public Lock refreshFolderLock(String lockUuid, long expirationTime)
386                    throws PortalException, SystemException {
387    
388                    return lockLocalService.refresh(lockUuid, expirationTime);
389            }
390    
391            public void unlockFolder(long groupId, long folderId, String lockUuid)
392                    throws PortalException, SystemException {
393    
394                    try {
395                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
396    
397                            DLFolderPermission.check(
398                                    getPermissionChecker(), dlFolder, ActionKeys.UPDATE);
399                    }
400                    catch (NoSuchFolderException nsfe) {
401                    }
402    
403                    doUnlockFolder(groupId, folderId, lockUuid);
404            }
405    
406            public void unlockFolder(
407                            long groupId, long parentFolderId, String name, String lockUuid)
408                    throws PortalException, SystemException {
409    
410                    DLFolder dlFolder = getFolder(groupId, parentFolderId, name);
411    
412                    unlockFolder(groupId, dlFolder.getFolderId(), lockUuid);
413            }
414    
415            public DLFolder updateFolder(
416                            long folderId, String name, String description,
417                            long defaultFileEntryTypeId, List<Long> fileEntryTypeIds,
418                            boolean overrideFileEntryTypes, ServiceContext serviceContext)
419                    throws PortalException, SystemException {
420    
421                    DLFolderPermission.check(
422                            getPermissionChecker(), serviceContext.getScopeGroupId(), folderId,
423                            ActionKeys.UPDATE);
424    
425                    boolean hasLock = lockLocalService.hasLock(
426                            getUserId(), DLFolder.class.getName(), folderId);
427    
428                    Lock lock = null;
429    
430                    if (!hasLock) {
431    
432                            // Lock
433    
434                            lock = doLockFolder(
435                                    folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
436                    }
437    
438                    try {
439                            return dlFolderLocalService.updateFolder(
440                                    folderId, name, description, defaultFileEntryTypeId,
441                                    fileEntryTypeIds, overrideFileEntryTypes, serviceContext);
442                    }
443                    finally {
444                            if (!hasLock) {
445    
446                                    // Unlock
447    
448                                    unlockFolder(
449                                            serviceContext.getScopeGroupId(), folderId, lock.getUuid());
450                            }
451                    }
452            }
453    
454            public boolean verifyInheritableLock(long folderId, String lockUuid)
455                    throws PortalException, SystemException {
456    
457                    boolean verified = false;
458    
459                    try {
460                            Lock lock = lockLocalService.getLock(
461                                    DLFolder.class.getName(), folderId);
462    
463                            if (!lock.isInheritable()) {
464                                    throw new NoSuchLockException();
465                            }
466    
467                            if (lock.getUuid().equals(lockUuid)) {
468                                    verified = true;
469                            }
470                    }
471                    catch (ExpiredLockException ele) {
472                            throw new NoSuchLockException(ele);
473                    }
474    
475                    return verified;
476            }
477    
478            protected Lock doLockFolder(
479                            long folderId, String owner, boolean inheritable,
480                            long expirationTime)
481                    throws PortalException, SystemException {
482    
483                    if ((expirationTime <= 0) ||
484                            (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
485    
486                            expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
487                    }
488    
489                    return lockLocalService.lock(
490                            getUserId(), DLFolder.class.getName(), folderId, owner,
491                            inheritable, expirationTime);
492            }
493    
494            protected void doUnlockFolder(long groupId, long folderId, String lockUuid)
495                    throws PortalException, SystemException {
496    
497                    if (Validator.isNotNull(lockUuid)) {
498                            try {
499                                    Lock lock = lockLocalService.getLock(
500                                            DLFolder.class.getName(), folderId);
501    
502                                    if (!lockUuid.equals(lock.getUuid())) {
503                                            throw new InvalidLockException("UUIDs do not match");
504                                    }
505                            }
506                            catch (PortalException pe) {
507                                    if (pe instanceof ExpiredLockException ||
508                                            pe instanceof NoSuchLockException) {
509                                    }
510                                    else {
511                                            throw pe;
512                                    }
513                            }
514                    }
515    
516                    lockLocalService.unlock(DLFolder.class.getName(), folderId);
517            }
518    
519    }