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