001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.model.Lock;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
029    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
030    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
031    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
032    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
033    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
034    
035    import java.io.File;
036    import java.io.InputStream;
037    
038    import java.util.Collections;
039    import java.util.List;
040    import java.util.Map;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Alexander Chow
045     */
046    public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
047    
048            public DLFileEntry addFileEntry(
049                            long groupId, long repositoryId, long folderId,
050                            String sourceFileName, String mimeType, String title,
051                            String description, String changeLog, long fileEntryTypeId,
052                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
053                            ServiceContext serviceContext)
054                    throws PortalException, SystemException {
055    
056                    DLFolderPermission.check(
057                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
058    
059                    return dlFileEntryLocalService.addFileEntry(
060                            getUserId(), groupId, repositoryId, folderId, sourceFileName,
061                            mimeType, title, description, changeLog, fileEntryTypeId,
062                            fieldsMap, file, is, size, serviceContext);
063            }
064    
065            public void cancelCheckOut(long fileEntryId)
066                    throws PortalException, SystemException {
067    
068                    try {
069                            DLFileEntryPermission.check(
070                                    getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
071                    }
072                    catch (NoSuchFileEntryException nsfee) {
073                    }
074    
075                    dlFileEntryLocalService.cancelCheckOut(getUserId(), fileEntryId);
076            }
077    
078            public void checkInFileEntry(
079                            long fileEntryId, boolean major, String changeLog,
080                            ServiceContext serviceContext)
081                    throws PortalException, SystemException {
082    
083                    try {
084                            DLFileEntryPermission.check(
085                                    getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
086                    }
087                    catch (NoSuchFileEntryException nsfee) {
088                    }
089    
090                    dlFileEntryLocalService.checkInFileEntry(
091                            getUserId(), fileEntryId, major, changeLog, serviceContext);
092            }
093    
094            public void checkInFileEntry(long fileEntryId, String lockUuid)
095                    throws PortalException, SystemException {
096    
097                    try {
098                            DLFileEntryPermission.check(
099                                    getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
100                    }
101                    catch (NoSuchFileEntryException nsfee) {
102                    }
103    
104                    dlFileEntryLocalService.checkInFileEntry(
105                            getUserId(), fileEntryId, lockUuid);
106            }
107    
108            public DLFileEntry checkOutFileEntry(long fileEntryId)
109                    throws PortalException, SystemException {
110    
111                    return checkOutFileEntry(
112                            fileEntryId, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
113            }
114    
115            public DLFileEntry checkOutFileEntry(
116                            long fileEntryId, String owner, long expirationTime)
117                    throws PortalException, SystemException {
118    
119                    DLFileEntryPermission.check(
120                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
121    
122                    if ((expirationTime <= 0) ||
123                            (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
124    
125                            expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
126                    }
127    
128                    return dlFileEntryLocalService.checkOutFileEntry(
129                            getUserId(), fileEntryId, owner, expirationTime);
130            }
131    
132            public DLFileEntry copyFileEntry(
133                            long groupId, long repositoryId, long fileEntryId,
134                            long destFolderId, ServiceContext serviceContext)
135                    throws PortalException, SystemException {
136    
137                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
138    
139                    String sourceFileName = "A." + dlFileEntry.getExtension();
140                    InputStream inputStream = DLStoreUtil.getFileAsStream(
141                            dlFileEntry.getCompanyId(), dlFileEntry.getFolderId(),
142                            dlFileEntry.getName());
143    
144                    DLFileEntry newDlFileEntry = addFileEntry(
145                            groupId, repositoryId, destFolderId, sourceFileName,
146                            dlFileEntry.getMimeType(), dlFileEntry.getTitle(),
147                            dlFileEntry.getDescription(), null,
148                            dlFileEntry.getFileEntryTypeId(), null, null, inputStream,
149                            dlFileEntry.getSize(), serviceContext);
150    
151                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
152    
153                    DLFileVersion newDlFileVersion = newDlFileEntry.getFileVersion();
154    
155                    dlFileEntryLocalService.copyFileEntryMetadata(
156                            dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
157                            fileEntryId, newDlFileVersion.getFileVersionId(),
158                            dlFileVersion.getFileVersionId(), serviceContext);
159    
160                    return newDlFileEntry;
161            }
162    
163            public void deleteFileEntry(long fileEntryId)
164                    throws PortalException, SystemException {
165    
166                    DLFileEntryPermission.check(
167                            getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
168    
169                    dlFileEntryLocalService.deleteFileEntry(getUserId(), fileEntryId);
170            }
171    
172            public void deleteFileEntry(long groupId, long folderId, String title)
173                    throws PortalException, SystemException {
174    
175                    DLFileEntry dlFileEntry = getFileEntry(groupId, folderId, title);
176    
177                    deleteFileEntry(dlFileEntry.getFileEntryId());
178            }
179    
180            public DLFileEntry fetchFileEntryByImageId(long imageId)
181                    throws PortalException, SystemException {
182    
183                    DLFileEntry dlFileEntry = dlFileEntryFinder.fetchByAnyImageId(imageId);
184    
185                    if (dlFileEntry != null) {
186                            DLFileEntryPermission.check(
187                                    getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
188                    }
189    
190                    return dlFileEntry;
191            }
192    
193            public InputStream getFileAsStream(long fileEntryId, String version)
194                    throws PortalException, SystemException {
195    
196                    DLFileEntryPermission.check(
197                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
198    
199                    return dlFileEntryLocalService.getFileAsStream(
200                            getGuestOrUserId(), fileEntryId, version);
201            }
202    
203            public InputStream getFileAsStream(
204                            long fileEntryId, String version, boolean incrementCounter)
205                    throws PortalException, SystemException {
206    
207                    DLFileEntryPermission.check(
208                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
209    
210                    return dlFileEntryLocalService.getFileAsStream(
211                            getGuestOrUserId(), fileEntryId, version, incrementCounter);
212            }
213    
214            public List<DLFileEntry> getFileEntries(
215                            long groupId, long folderId, int start, int end,
216                            OrderByComparator obc)
217                    throws SystemException {
218    
219                    return dlFileEntryPersistence.filterFindByG_F(
220                            groupId, folderId, start, end, obc);
221            }
222    
223            public List<DLFileEntry> getFileEntries(
224                            long groupId, long folderId, long fileEntryTypeId, int start,
225                            int end, OrderByComparator obc)
226                    throws SystemException {
227    
228                    return dlFileEntryPersistence.filterFindByG_F_F(
229                            groupId, folderId, fileEntryTypeId, start, end, obc);
230            }
231    
232            public int getFileEntriesCount(long groupId, long folderId)
233                    throws SystemException {
234    
235                    return dlFileEntryPersistence.filterCountByG_F(groupId, folderId);
236            }
237    
238            public int getFileEntriesCount(
239                            long groupId, long folderId, long fileEntryTypeId)
240                    throws SystemException {
241    
242                    return dlFileEntryPersistence.filterCountByG_F_F(
243                            groupId, folderId, fileEntryTypeId);
244            }
245    
246            public DLFileEntry getFileEntry(long fileEntryId)
247                    throws PortalException, SystemException {
248    
249                    DLFileEntryPermission.check(
250                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
251    
252                    return dlFileEntryLocalService.getFileEntry(fileEntryId);
253            }
254    
255            public DLFileEntry getFileEntry(long groupId, long folderId, String title)
256                    throws PortalException, SystemException {
257    
258                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
259                            groupId, folderId, title);
260    
261                    DLFileEntryPermission.check(
262                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
263    
264                    return dlFileEntry;
265            }
266    
267            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
268                    throws PortalException, SystemException {
269    
270                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByUUID_G(
271                            uuid, groupId);
272    
273                    DLFileEntryPermission.check(
274                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
275    
276                    return dlFileEntry;
277            }
278    
279            public Lock getFileEntryLock(long fileEntryId) {
280                    try {
281                            return lockLocalService.getLock(
282                                    DLFileEntry.class.getName(), fileEntryId);
283                    }
284                    catch (Exception e) {
285                            return null;
286                    }
287            }
288    
289            public int getFoldersFileEntriesCount(
290                            long groupId, List<Long> folderIds, int status)
291                    throws SystemException {
292    
293                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
294                            return dlFileEntryFinder.filterCountByG_F_S(
295                                    groupId, folderIds, status);
296                    }
297                    else {
298                            int start = 0;
299                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
300    
301                            int filesCount = dlFileEntryFinder.filterCountByG_F_S(
302                                    groupId, folderIds.subList(start, end), status);
303    
304                            folderIds.subList(start, end).clear();
305    
306                            filesCount += getFoldersFileEntriesCount(
307                                    groupId, folderIds, status);
308    
309                            return filesCount;
310                    }
311            }
312    
313            public List<DLFileEntry> getGroupFileEntries(
314                            long groupId, long userId, long rootFolderId, int start, int end,
315                            OrderByComparator obc)
316                    throws SystemException {
317    
318                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
319    
320                    if (folderIds.length == 0) {
321                            return Collections.emptyList();
322                    }
323                    else if (userId <= 0) {
324                            return dlFileEntryPersistence.filterFindByG_F(
325                                    groupId, folderIds, start, end, obc);
326                    }
327                    else {
328                            return dlFileEntryPersistence.filterFindByG_U_F(
329                                    groupId, userId, folderIds, start, end, obc);
330                    }
331            }
332    
333            public int getGroupFileEntriesCount(
334                            long groupId, long userId, long rootFolderId)
335                    throws SystemException {
336    
337                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
338    
339                    if (folderIds.length == 0) {
340                            return 0;
341                    }
342                    else if (userId <= 0) {
343                            return dlFileEntryPersistence.filterCountByG_F(groupId, folderIds);
344                    }
345                    else {
346                            return dlFileEntryPersistence.filterCountByG_U_F(
347                                    groupId, userId, folderIds);
348                    }
349            }
350    
351            public boolean hasFileEntryLock(long fileEntryId)
352                    throws PortalException, SystemException {
353    
354                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
355                            fileEntryId);
356    
357                    long folderId = dlFileEntry.getFolderId();
358    
359                    boolean hasLock = lockLocalService.hasLock(
360                            getUserId(), DLFileEntry.class.getName(), fileEntryId);
361    
362                    if ((!hasLock) &&
363                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
364    
365                            hasLock = dlFolderService.hasInheritableLock(folderId);
366                    }
367    
368                    return hasLock;
369            }
370    
371            public boolean isFileEntryCheckedOut(long fileEntryId)
372                    throws PortalException, SystemException {
373    
374                    return dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId);
375            }
376    
377            public DLFileEntry moveFileEntry(
378                            long fileEntryId, long newFolderId, ServiceContext serviceContext)
379                    throws PortalException, SystemException {
380    
381                    DLFileEntryPermission.check(
382                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
383    
384                    return dlFileEntryLocalService.moveFileEntry(
385                            getUserId(), fileEntryId, newFolderId, serviceContext);
386    
387            }
388    
389            public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
390                    throws PortalException, SystemException {
391    
392                    return lockLocalService.refresh(lockUuid, expirationTime);
393            }
394    
395            public void revertFileEntry(
396                            long fileEntryId, String version, ServiceContext serviceContext)
397                    throws PortalException, SystemException {
398    
399                    DLFileEntryPermission.check(
400                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
401    
402                    dlFileEntryLocalService.revertFileEntry(
403                            getUserId(), fileEntryId, version, serviceContext);
404            }
405    
406            public DLFileEntry updateFileEntry(
407                            long fileEntryId, String sourceFileName, String mimeType,
408                            String title, String description, String changeLog,
409                            boolean majorVersion, long fileEntryTypeId,
410                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
411                            ServiceContext serviceContext)
412                    throws PortalException, SystemException {
413    
414                    DLFileEntryPermission.check(
415                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
416    
417                    return dlFileEntryLocalService.updateFileEntry(
418                            getUserId(), fileEntryId, sourceFileName, mimeType, title,
419                            description, changeLog, majorVersion, fileEntryTypeId, fieldsMap,
420                            file, is, size, serviceContext);
421            }
422    
423            public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
424                    throws PortalException, SystemException {
425    
426                    return dlFileEntryLocalService.verifyFileEntryCheckOut(
427                            fileEntryId, lockUuid);
428            }
429    
430    }