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