001    /**
002     * Copyright (c) 2000-2010 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.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 Lock getFileEntryLock(long groupId, long folderId, String name)
286                    throws PortalException, SystemException {
287    
288                    String lockId = DLUtil.getLockId(groupId, folderId, name);
289    
290                    return lockLocalService.getLock(DLFileEntry.class.getName(), lockId);
291            }
292    
293            public boolean hasFileEntryLock(long groupId, long folderId, String name)
294                    throws PortalException, SystemException {
295    
296                    String lockId = DLUtil.getLockId(groupId, folderId, name);
297    
298                    boolean hasLock = lockLocalService.hasLock(
299                            getUserId(), DLFileEntry.class.getName(), lockId);
300    
301                    if ((!hasLock) &&
302                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
303    
304                            hasLock = dlFolderService.hasInheritableLock(folderId);
305                    }
306    
307                    return hasLock;
308            }
309    
310            public Lock lockFileEntry(long groupId, long folderId, String name)
311                    throws PortalException, SystemException {
312    
313                    return lockFileEntry(
314                            groupId, folderId, name, null,
315                            DLFileEntryImpl.LOCK_EXPIRATION_TIME);
316            }
317    
318            public Lock lockFileEntry(
319                            long groupId, long folderId, String name, String owner,
320                            long expirationTime)
321                    throws PortalException, SystemException {
322    
323                    if ((expirationTime <= 0) ||
324                            (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
325    
326                            expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
327                    }
328    
329                    String lockId = DLUtil.getLockId(groupId, folderId, name);
330    
331                    return lockLocalService.lock(
332                            getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
333                            false, expirationTime);
334            }
335    
336            public DLFileEntry moveFileEntry(
337                            long groupId, long folderId, long newFolderId, String name,
338                            ServiceContext serviceContext)
339                    throws PortalException, SystemException {
340    
341                    DLFileEntryPermission.check(
342                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
343    
344                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
345    
346                    if (!hasLock) {
347    
348                            // Lock
349    
350                            lockFileEntry(groupId, folderId, name);
351                    }
352    
353                    DLFileEntry fileEntry = null;
354    
355                    try {
356                            fileEntry = dlFileEntryLocalService.moveFileEntry(
357                                    getUserId(), groupId, folderId, newFolderId, name,
358                                    serviceContext);
359                    }
360                    finally {
361                            if (!hasLock) {
362    
363                                    // Unlock
364    
365                                    unlockFileEntry(groupId, folderId, name);
366                            }
367                    }
368    
369                    return fileEntry;
370            }
371    
372            public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
373                    throws PortalException, SystemException {
374    
375                    return lockLocalService.refresh(lockUuid, expirationTime);
376            }
377    
378            public void unlockFileEntry(long groupId, long folderId, String name)
379                    throws SystemException {
380    
381                    String lockId = DLUtil.getLockId(groupId, folderId, name);
382    
383                    lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
384            }
385    
386            public void unlockFileEntry(
387                            long groupId, long folderId, String name, String lockUuid)
388                    throws PortalException, SystemException {
389    
390                    String lockId = DLUtil.getLockId(groupId, folderId, name);
391    
392                    if (Validator.isNotNull(lockUuid)) {
393                            try {
394                                    Lock lock = lockLocalService.getLock(
395                                            DLFileEntry.class.getName(), lockId);
396    
397                                    if (!lock.getUuid().equals(lockUuid)) {
398                                            throw new InvalidLockException("UUIDs do not match");
399                                    }
400                            }
401                            catch (PortalException pe) {
402                                    if ((pe instanceof ExpiredLockException) ||
403                                            (pe instanceof NoSuchLockException)) {
404                                    }
405                                    else {
406                                            throw pe;
407                                    }
408                            }
409                    }
410    
411                    lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
412            }
413    
414            public DLFileEntry updateFileEntry(
415                            long groupId, long folderId, String name, String sourceFileName,
416                            String title, String description, String changeLog,
417                            boolean majorVersion, String extraSettings, byte[] bytes,
418                            ServiceContext serviceContext)
419                    throws PortalException, SystemException {
420    
421                    DLFileEntryPermission.check(
422                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
423    
424                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
425    
426                    if (!hasLock) {
427    
428                            // Lock
429    
430                            lockFileEntry(groupId, folderId, name);
431                    }
432    
433                    DLFileEntry fileEntry = null;
434    
435                    try {
436                            fileEntry = dlFileEntryLocalService.updateFileEntry(
437                                    getUserId(), groupId, folderId, name, sourceFileName, title,
438                                    description, changeLog, majorVersion, extraSettings, bytes,
439                                    serviceContext);
440                    }
441                    finally {
442                            if (!hasLock) {
443    
444                                    // Unlock
445    
446                                    unlockFileEntry(groupId, folderId, name);
447                            }
448                    }
449    
450                    return fileEntry;
451            }
452    
453            public DLFileEntry updateFileEntry(
454                            long groupId, long folderId, String name, String sourceFileName,
455                            String title, String description, String changeLog,
456                            boolean majorVersion, String extraSettings, File file,
457                            ServiceContext serviceContext)
458                    throws PortalException, SystemException {
459    
460                    DLFileEntryPermission.check(
461                            getPermissionChecker(), groupId, folderId, name, ActionKeys.UPDATE);
462    
463                    boolean hasLock = hasFileEntryLock(groupId, folderId, name);
464    
465                    if (!hasLock) {
466    
467                            // Lock
468    
469                            lockFileEntry(groupId, folderId, name);
470                    }
471    
472                    DLFileEntry fileEntry = null;
473    
474                    try {
475                            fileEntry = dlFileEntryLocalService.updateFileEntry(
476                                    getUserId(), groupId, folderId, name, sourceFileName, title,
477                                    description, changeLog, majorVersion, extraSettings, file,
478                                    serviceContext);
479                    }
480                    finally {
481                            if (!hasLock) {
482    
483                                    // Unlock
484    
485                                    unlockFileEntry(groupId, folderId, name);
486                            }
487                    }
488    
489                    return fileEntry;
490            }
491    
492            public boolean verifyFileEntryLock(
493                            long groupId, long folderId, String name, String lockUuid)
494                    throws PortalException, SystemException {
495    
496                    boolean verified = false;
497    
498                    try {
499                            String lockId = DLUtil.getLockId(groupId, folderId, name);
500    
501                            Lock lock = lockLocalService.getLock(
502                                    DLFileEntry.class.getName(), lockId);
503    
504                            if (lock.getUuid().equals(lockUuid)) {
505                                    verified = true;
506                            }
507                    }
508                    catch (PortalException pe) {
509                            if ((pe instanceof ExpiredLockException) ||
510                                    (pe instanceof NoSuchLockException)) {
511    
512                                    verified = dlFolderService.verifyInheritableLock(
513                                            folderId, lockUuid);
514                            }
515                            else {
516                                    throw pe;
517                            }
518                    }
519    
520                    return verified;
521            }
522    
523    }