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