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