001    /**
002     * Copyright (c) 2000-2010 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.OrderByComparator;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Lock;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
030    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
031    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
032    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
033    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
034    import com.liferay.portlet.documentlibrary.util.DLUtil;
035    import com.liferay.portlet.documentlibrary.util.comparator.FileEntryModifiedDateComparator;
036    
037    import java.io.File;
038    
039    import java.util.Collections;
040    import java.util.List;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
046    
047            public DLFileEntry addFileEntry(
048                            long groupId, long folderId, String name, String title,
049                            String description, String changeLog, String extraSettings,
050                            byte[] bytes, ServiceContext serviceContext)
051                    throws PortalException, SystemException {
052    
053                    DLFolderPermission.check(
054                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
055    
056                    return dlFileEntryLocalService.addFileEntry(
057                            getUserId(), groupId, folderId, name, title, description, changeLog,
058                            extraSettings, bytes, serviceContext);
059            }
060    
061            public DLFileEntry addFileEntry(
062                            long groupId, long folderId, String name, String title,
063                            String description, String changeLog, String extraSettings,
064                            File file, ServiceContext serviceContext)
065                    throws PortalException, SystemException {
066    
067                    DLFolderPermission.check(
068                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
069    
070                    return dlFileEntryLocalService.addFileEntry(
071                            getUserId(), groupId, folderId, name, title, description, changeLog,
072                            extraSettings, file, serviceContext);
073            }
074    
075            public void deleteFileEntry(long groupId, long folderId, String name)
076                    throws PortalException, SystemException {
077    
078                    DLFileEntryPermission.check(
079                            getPermissionChecker(), groupId, folderId, name, ActionKeys.DELETE);
080    
081                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
082    
083                    if (!hasLock) {
084    
085                            // Lock
086    
087                            lockFileEntry(groupId, folderId, name);
088                    }
089    
090                    try {
091                            dlFileEntryLocalService.deleteFileEntry(groupId, folderId, name);
092                    }
093                    finally {
094    
095                            // Unlock
096    
097                            unlockFileEntry(groupId, folderId, name);
098                    }
099            }
100    
101            public void deleteFileEntry(
102                            long groupId, long folderId, String name, String version)
103                    throws PortalException, SystemException {
104    
105                    DLFileEntryPermission.check(
106                            getPermissionChecker(), groupId, folderId, name, ActionKeys.DELETE);
107    
108                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
109    
110                    if (!hasLock) {
111    
112                            // Lock
113    
114                            lockFileEntry(groupId, folderId, name);
115                    }
116    
117                    try {
118                            dlFileEntryLocalService.deleteFileEntry(
119                                    groupId, folderId, name, version);
120                    }
121                    finally {
122    
123                            // Unlock
124    
125                            unlockFileEntry(groupId, folderId, name);
126                    }
127            }
128    
129            public void deleteFileEntryByTitle(
130                            long groupId, long folderId, String titleWithExtension)
131                    throws PortalException, SystemException {
132    
133                    DLFileEntry fileEntry = getFileEntryByTitle(
134                            groupId, folderId, titleWithExtension);
135    
136                    deleteFileEntry(groupId, folderId, fileEntry.getName());
137            }
138    
139            public List<DLFileEntry> getFileEntries(long groupId, long folderId)
140                    throws PortalException, SystemException {
141    
142                    DLFolderPermission.check(
143                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
144    
145                    return dlFileEntryPersistence.filterFindByG_F(groupId, folderId);
146            }
147    
148            public List<DLFileEntry> getFileEntries(
149                            long groupId, long folderId, int start, int end)
150                    throws PortalException, SystemException {
151    
152                    DLFolderPermission.check(
153                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
154    
155                    return dlFileEntryPersistence.filterFindByG_F(
156                            groupId, folderId, start, end);
157            }
158    
159            public List<DLFileEntry> getFileEntries(
160                            long groupId, long folderId, int start, int end,
161                            OrderByComparator obc)
162                    throws PortalException, SystemException {
163    
164                    DLFolderPermission.check(
165                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
166    
167                    return dlFileEntryPersistence.filterFindByG_F(
168                            groupId, folderId, start, end, obc);
169            }
170    
171            public int getFileEntriesCount(long groupId, long folderId)
172                    throws PortalException, SystemException {
173    
174                    DLFolderPermission.check(
175                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
176    
177                    return dlFileEntryPersistence.filterCountByG_F(groupId, folderId);
178            }
179    
180            public DLFileEntry getFileEntry(long groupId, long folderId, String name)
181                    throws PortalException, SystemException {
182    
183                    DLFileEntryPermission.check(
184                            getPermissionChecker(), groupId, folderId, name, ActionKeys.VIEW);
185    
186                    return dlFileEntryLocalService.getFileEntry(groupId, folderId, name);
187            }
188    
189            public DLFileEntry getFileEntryByTitle(
190                            long groupId, long folderId, String titleWithExtension)
191                    throws PortalException, SystemException {
192    
193                    DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
194                            groupId, folderId, titleWithExtension);
195    
196                    DLFileEntryPermission.check(
197                            getPermissionChecker(), fileEntry, ActionKeys.VIEW);
198    
199                    return fileEntry;
200            }
201    
202            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
203                    throws PortalException, SystemException {
204    
205                    DLFileEntry fileEntry = dlFileEntryPersistence.findByUUID_G(
206                            uuid, groupId);
207    
208                    DLFileEntryPermission.check(
209                            getPermissionChecker(), fileEntry, ActionKeys.VIEW);
210    
211                    return fileEntry;
212            }
213    
214            public int getFoldersFileEntriesCount(
215                            long groupId, List<Long> folderIds, int status)
216                    throws SystemException {
217    
218                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
219                            return dlFileEntryFinder.filterCountByG_F_S(
220                                    groupId, folderIds, status);
221                    }
222                    else {
223                            int start = 0;
224                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
225    
226                            int filesCount = dlFileEntryFinder.filterCountByG_F_S(
227                                    groupId, folderIds.subList(start, end), status);
228    
229                            folderIds.subList(start, end).clear();
230    
231                            filesCount += getFoldersFileEntriesCount(
232                                    groupId, folderIds, status);
233    
234                            return filesCount;
235                    }
236            }
237    
238            public List<DLFileEntry> getGroupFileEntries(
239                            long groupId, long userId, int start, int end)
240                    throws SystemException {
241    
242                    return getGroupFileEntries(
243                            groupId, userId, start, end, new FileEntryModifiedDateComparator());
244            }
245    
246            public List<DLFileEntry> getGroupFileEntries(
247                            long groupId, long userId, int start, int end,
248                            OrderByComparator obc)
249                    throws SystemException {
250    
251                    long[] folderIds = dlFolderService.getFolderIds(
252                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
253    
254                    if (folderIds.length == 0) {
255                            return Collections.EMPTY_LIST;
256                    }
257                    else if (userId <= 0) {
258                            return dlFileEntryPersistence.filterFindByG_F(
259                                    groupId, folderIds, start, end, obc);
260                    }
261                    else {
262                            return dlFileEntryPersistence.filterFindByG_U_F(
263                                    groupId, userId, folderIds, start, end, obc);
264                    }
265            }
266    
267            public int getGroupFileEntriesCount(long groupId, long userId)
268                    throws SystemException {
269    
270                    long[] folderIds = dlFolderService.getFolderIds(
271                            groupId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
272    
273                    if (folderIds.length == 0) {
274                            return 0;
275                    }
276                    else if (userId <= 0) {
277                            return dlFileEntryPersistence.filterCountByG_F(groupId, folderIds);
278                    }
279                    else {
280                            return dlFileEntryPersistence.filterCountByG_U_F(
281                                    groupId, userId, folderIds);
282                    }
283            }
284    
285            public boolean hasFileEntryLock(long groupId, long folderId, String name)
286                    throws PortalException, SystemException {
287    
288                    String lockId = DLUtil.getLockId(groupId, folderId, name);
289    
290                    boolean hasLock = lockLocalService.hasLock(
291                            getUserId(), DLFileEntry.class.getName(), lockId);
292    
293                    if ((!hasLock) &&
294                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
295    
296                            hasLock = dlFolderService.hasInheritableLock(folderId);
297                    }
298    
299                    return hasLock;
300            }
301    
302            public Lock lockFileEntry(long groupId, long folderId, String name)
303                    throws PortalException, SystemException {
304    
305                    return lockFileEntry(
306                            groupId, folderId, name, null,
307                            DLFileEntryImpl.LOCK_EXPIRATION_TIME);
308            }
309    
310            public Lock lockFileEntry(
311                            long groupId, long folderId, String name, String owner,
312                            long expirationTime)
313                    throws PortalException, SystemException {
314    
315                    if ((expirationTime <= 0) ||
316                            (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
317    
318                            expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
319                    }
320    
321                    String lockId = DLUtil.getLockId(groupId, folderId, name);
322    
323                    return lockLocalService.lock(
324                            getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
325                            false, expirationTime);
326            }
327    
328            public DLFileEntry moveFileEntry(
329                            long groupId, long folderId, long newFolderId, String name,
330                            ServiceContext serviceContext)
331                    throws PortalException, SystemException {
332    
333                    DLFileEntryPermission.check(
334                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
335    
336                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
337    
338                    if (!hasLock) {
339    
340                            // Lock
341    
342                            lockFileEntry(groupId, folderId, name);
343                    }
344    
345                    DLFileEntry fileEntry = null;
346    
347                    try {
348                            fileEntry = dlFileEntryLocalService.moveFileEntry(
349                                    getUserId(), groupId, folderId, newFolderId, name,
350                                    serviceContext);
351                    }
352                    finally {
353                            if (!hasLock) {
354    
355                                    // Unlock
356    
357                                    unlockFileEntry(groupId, folderId, name);
358                            }
359                    }
360    
361                    return fileEntry;
362            }
363    
364            public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
365                    throws PortalException, SystemException {
366    
367                    return lockLocalService.refresh(lockUuid, expirationTime);
368            }
369    
370            public void unlockFileEntry(long groupId, long folderId, String name)
371                    throws SystemException {
372    
373                    String lockId = DLUtil.getLockId(groupId, folderId, name);
374    
375                    lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
376            }
377    
378            public void unlockFileEntry(
379                            long groupId, long folderId, String name, String lockUuid)
380                    throws PortalException, SystemException {
381    
382                    String lockId = DLUtil.getLockId(groupId, folderId, name);
383    
384                    if (Validator.isNotNull(lockUuid)) {
385                            try {
386                                    Lock lock = lockLocalService.getLock(
387                                            DLFileEntry.class.getName(), lockId);
388    
389                                    if (!lock.getUuid().equals(lockUuid)) {
390                                            throw new InvalidLockException("UUIDs do not match");
391                                    }
392                            }
393                            catch (PortalException pe) {
394                                    if ((pe instanceof ExpiredLockException) ||
395                                            (pe instanceof NoSuchLockException)) {
396                                    }
397                                    else {
398                                            throw pe;
399                                    }
400                            }
401                    }
402    
403                    lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
404            }
405    
406            public DLFileEntry updateFileEntry(
407                            long groupId, long folderId, String name, String sourceFileName,
408                            String title, String description, String changeLog,
409                            boolean majorVersion, String extraSettings, byte[] bytes,
410                            ServiceContext serviceContext)
411                    throws PortalException, SystemException {
412    
413                    DLFileEntryPermission.check(
414                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
415    
416                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
417    
418                    if (!hasLock) {
419    
420                            // Lock
421    
422                            lockFileEntry(groupId, folderId, name);
423                    }
424    
425                    DLFileEntry fileEntry = null;
426    
427                    try {
428                            fileEntry = dlFileEntryLocalService.updateFileEntry(
429                                    getUserId(), groupId, folderId, name, sourceFileName, title,
430                                    description, changeLog, majorVersion, extraSettings, bytes,
431                                    serviceContext);
432                    }
433                    finally {
434                            if (!hasLock) {
435    
436                                    // Unlock
437    
438                                    unlockFileEntry(groupId, folderId, name);
439                            }
440                    }
441    
442                    return fileEntry;
443            }
444    
445            public DLFileEntry updateFileEntry(
446                            long groupId, long folderId, String name, String sourceFileName,
447                            String title, String description, String changeLog,
448                            boolean majorVersion, String extraSettings, File file,
449                            ServiceContext serviceContext)
450                    throws PortalException, SystemException {
451    
452                    DLFileEntryPermission.check(
453                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
454    
455                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
456    
457                    if (!hasLock) {
458    
459                            // Lock
460    
461                            lockFileEntry(groupId, folderId, name);
462                    }
463    
464                    DLFileEntry fileEntry = null;
465    
466                    try {
467                            fileEntry = dlFileEntryLocalService.updateFileEntry(
468                                    getUserId(), groupId, folderId, name, sourceFileName, title,
469                                    description, changeLog, majorVersion, extraSettings, file,
470                                    serviceContext);
471                    }
472                    finally {
473                            if (!hasLock) {
474    
475                                    // Unlock
476    
477                                    unlockFileEntry(groupId, folderId, name);
478                            }
479                    }
480    
481                    return fileEntry;
482            }
483    
484            public boolean verifyFileEntryLock(
485                            long groupId, long folderId, String name, String lockUuid)
486                    throws PortalException, SystemException {
487    
488                    boolean verified = false;
489    
490                    try {
491                            String lockId = DLUtil.getLockId(groupId, folderId, name);
492    
493                            Lock lock = lockLocalService.getLock(
494                                    DLFileEntry.class.getName(), lockId);
495    
496                            if (lock.getUuid().equals(lockUuid)) {
497                                    verified = true;
498                            }
499                    }
500                    catch (PortalException pe) {
501                            if ((pe instanceof ExpiredLockException) ||
502                                    (pe instanceof NoSuchLockException)) {
503    
504                                    verified = dlFolderService.verifyInheritableLock(
505                                            folderId, lockUuid);
506                            }
507                            else {
508                                    throw pe;
509                            }
510                    }
511    
512                    return verified;
513            }
514    
515    }