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                    if ((expirationTime <= 0) ||
163                            (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
164    
165                            expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
166                    }
167    
168                    return dlFileEntryLocalService.checkOutFileEntry(
169                            getUserId(), fileEntryId, owner, expirationTime, serviceContext);
170            }
171    
172            public DLFileEntry copyFileEntry(
173                            long groupId, long repositoryId, long fileEntryId,
174                            long destFolderId, ServiceContext serviceContext)
175                    throws PortalException, SystemException {
176    
177                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
178    
179                    String sourceFileName = "A." + dlFileEntry.getExtension();
180                    InputStream inputStream = DLStoreUtil.getFileAsStream(
181                            dlFileEntry.getCompanyId(), dlFileEntry.getFolderId(),
182                            dlFileEntry.getName());
183    
184                    DLFileEntry newDlFileEntry = addFileEntry(
185                            groupId, repositoryId, destFolderId, sourceFileName,
186                            dlFileEntry.getMimeType(), dlFileEntry.getTitle(),
187                            dlFileEntry.getDescription(), null,
188                            dlFileEntry.getFileEntryTypeId(), null, null, inputStream,
189                            dlFileEntry.getSize(), serviceContext);
190    
191                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
192    
193                    DLFileVersion newDlFileVersion = newDlFileEntry.getFileVersion();
194    
195                    dlFileEntryLocalService.copyFileEntryMetadata(
196                            dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
197                            fileEntryId, newDlFileVersion.getFileVersionId(),
198                            dlFileVersion.getFileVersionId(), serviceContext);
199    
200                    return newDlFileEntry;
201            }
202    
203            public void deleteFileEntry(long fileEntryId)
204                    throws PortalException, SystemException {
205    
206                    DLFileEntryPermission.check(
207                            getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
208    
209                    dlFileEntryLocalService.deleteFileEntry(getUserId(), fileEntryId);
210            }
211    
212            public void deleteFileEntry(long groupId, long folderId, String title)
213                    throws PortalException, SystemException {
214    
215                    DLFileEntry dlFileEntry = getFileEntry(groupId, folderId, title);
216    
217                    deleteFileEntry(dlFileEntry.getFileEntryId());
218            }
219    
220            public void deleteFileVersion(long fileEntryId, String version)
221                    throws PortalException, SystemException {
222    
223                    DLFileEntryPermission.check(
224                            getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
225    
226                    dlFileEntryLocalService.deleteFileVersion(
227                            getUserId(), fileEntryId, version);
228            }
229    
230            public DLFileEntry fetchFileEntryByImageId(long imageId)
231                    throws PortalException, SystemException {
232    
233                    DLFileEntry dlFileEntry = dlFileEntryFinder.fetchByAnyImageId(imageId);
234    
235                    if (dlFileEntry != null) {
236                            DLFileEntryPermission.check(
237                                    getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
238                    }
239    
240                    return dlFileEntry;
241            }
242    
243            public InputStream getFileAsStream(long fileEntryId, String version)
244                    throws PortalException, SystemException {
245    
246                    DLFileEntryPermission.check(
247                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
248    
249                    return dlFileEntryLocalService.getFileAsStream(
250                            getGuestOrUserId(), fileEntryId, version);
251            }
252    
253            public InputStream getFileAsStream(
254                            long fileEntryId, String version, boolean incrementCounter)
255                    throws PortalException, SystemException {
256    
257                    DLFileEntryPermission.check(
258                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
259    
260                    return dlFileEntryLocalService.getFileAsStream(
261                            getGuestOrUserId(), fileEntryId, version, incrementCounter);
262            }
263    
264            public List<DLFileEntry> getFileEntries(
265                            long groupId, long folderId, int status, int start, int end,
266                            OrderByComparator obc)
267                    throws PortalException, SystemException {
268    
269                    DLFolderPermission.check(
270                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
271    
272                    List<Long> folderIds = new ArrayList<Long>();
273    
274                    folderIds.add(folderId);
275    
276                    QueryDefinition queryDefinition = new QueryDefinition(
277                            status, false, start, end, obc);
278    
279                    return dlFileEntryFinder.filterFindByG_F(
280                            groupId, folderIds, queryDefinition);
281            }
282    
283            public List<DLFileEntry> getFileEntries(
284                            long groupId, long folderId, int start, int end,
285                            OrderByComparator obc)
286                    throws PortalException, SystemException {
287    
288                    return getFileEntries(
289                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
290                            obc);
291            }
292    
293            public List<DLFileEntry> getFileEntries(
294                            long groupId, long folderId, long fileEntryTypeId, int start,
295                            int end, OrderByComparator obc)
296                    throws PortalException, SystemException {
297    
298                    DLFolderPermission.check(
299                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
300    
301                    return dlFileEntryPersistence.filterFindByG_F_F(
302                            groupId, folderId, fileEntryTypeId, start, end, obc);
303            }
304    
305            public List<DLFileEntry> getFileEntries(
306                            long groupId, long folderId, String[] mimeTypes, int start, int end,
307                            OrderByComparator obc)
308                    throws PortalException, SystemException {
309    
310                    DLFolderPermission.check(
311                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
312    
313                    List<Long> folderIds = new ArrayList<Long>();
314    
315                    folderIds.add(folderId);
316    
317                    QueryDefinition queryDefinition = new QueryDefinition(
318                            WorkflowConstants.STATUS_IN_TRASH, true, start, end, obc);
319    
320                    return dlFileEntryFinder.findByG_U_F_M(
321                            groupId, 0, folderIds, mimeTypes, queryDefinition);
322            }
323    
324            public int getFileEntriesCount(long groupId, long folderId)
325                    throws SystemException {
326    
327                    return getFileEntriesCount(
328                            groupId, folderId, WorkflowConstants.STATUS_APPROVED);
329            }
330    
331            public int getFileEntriesCount(long groupId, long folderId, int status)
332                    throws SystemException {
333    
334                    List<Long> folderIds = new ArrayList<Long>();
335    
336                    folderIds.add(folderId);
337    
338                    return dlFileEntryFinder.filterCountByG_F(
339                            groupId, folderIds, new QueryDefinition(status));
340            }
341    
342            public int getFileEntriesCount(
343                            long groupId, long folderId, long fileEntryTypeId)
344                    throws SystemException {
345    
346                    return dlFileEntryPersistence.filterCountByG_F_F(
347                            groupId, folderId, fileEntryTypeId);
348            }
349    
350            public int getFileEntriesCount(
351                            long groupId, long folderId, String[] mimeTypes)
352                    throws SystemException {
353    
354                    List<Long> folderIds = new ArrayList<Long>();
355    
356                    folderIds.add(folderId);
357    
358                    return dlFileEntryFinder.countByG_U_F_M(
359                            groupId, 0, folderIds, mimeTypes,
360                            new QueryDefinition(WorkflowConstants.STATUS_ANY));
361            }
362    
363            public DLFileEntry getFileEntry(long fileEntryId)
364                    throws PortalException, SystemException {
365    
366                    DLFileEntryPermission.check(
367                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
368    
369                    return dlFileEntryLocalService.getFileEntry(fileEntryId);
370            }
371    
372            public DLFileEntry getFileEntry(long groupId, long folderId, String title)
373                    throws PortalException, SystemException {
374    
375                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
376                            groupId, folderId, title);
377    
378                    DLFileEntryPermission.check(
379                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
380    
381                    return dlFileEntry;
382            }
383    
384            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
385                    throws PortalException, SystemException {
386    
387                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByUUID_G(
388                            uuid, groupId);
389    
390                    DLFileEntryPermission.check(
391                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
392    
393                    return dlFileEntry;
394            }
395    
396            public Lock getFileEntryLock(long fileEntryId) {
397                    try {
398                            return lockLocalService.getLock(
399                                    DLFileEntry.class.getName(), fileEntryId);
400                    }
401                    catch (Exception e) {
402                            return null;
403                    }
404            }
405    
406            public int getFoldersFileEntriesCount(
407                            long groupId, List<Long> folderIds, int status)
408                    throws SystemException {
409    
410                    QueryDefinition queryDefinition = new QueryDefinition(status);
411    
412                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
413                            return dlFileEntryFinder.filterCountByG_F(
414                                    groupId, folderIds, queryDefinition);
415                    }
416                    else {
417                            int start = 0;
418                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
419    
420                            int filesCount = dlFileEntryFinder.filterCountByG_F(
421                                    groupId, folderIds.subList(start, end), queryDefinition);
422    
423                            folderIds.subList(start, end).clear();
424    
425                            filesCount += getFoldersFileEntriesCount(
426                                    groupId, folderIds, status);
427    
428                            return filesCount;
429                    }
430            }
431    
432            public List<DLFileEntry> getGroupFileEntries(
433                            long groupId, long userId, long rootFolderId, int start, int end,
434                            OrderByComparator obc)
435                    throws PortalException, SystemException {
436    
437                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
438    
439                    folderIds = DLAppUtil.filterFolderIds(
440                            getPermissionChecker(), groupId, folderIds);
441    
442                    if (folderIds.length == 0) {
443                            return Collections.emptyList();
444                    }
445                    else if (userId <= 0) {
446                            return dlFileEntryPersistence.filterFindByG_F(
447                                    groupId, folderIds, start, end, obc);
448                    }
449                    else {
450                            return dlFileEntryPersistence.filterFindByG_U_F(
451                                    groupId, userId, folderIds, start, end, obc);
452                    }
453            }
454    
455            public List<DLFileEntry> getGroupFileEntries(
456                            long groupId, long userId, long rootFolderId, String[] mimeTypes,
457                            int status, int start, int end, OrderByComparator obc)
458                    throws PortalException, SystemException {
459    
460                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
461    
462                    folderIds = DLAppUtil.filterFolderIds(
463                            getPermissionChecker(), groupId, folderIds);
464    
465                    if (folderIds.length == 0) {
466                            return Collections.emptyList();
467                    }
468    
469                    List<Long> folderIdsList = ListUtil.toList(folderIds);
470    
471                    QueryDefinition queryDefinition = new QueryDefinition(
472                            status, start, end, obc);
473    
474                    return dlFileEntryFinder.findByG_U_F_M(
475                            groupId, userId, folderIdsList, mimeTypes, queryDefinition);
476            }
477    
478            public int getGroupFileEntriesCount(
479                            long groupId, long userId, long rootFolderId)
480                    throws PortalException, SystemException {
481    
482                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
483    
484                    if (folderIds.length == 0) {
485                            return 0;
486                    }
487                    else if (userId <= 0) {
488                            return dlFileEntryPersistence.filterCountByG_F(groupId, folderIds);
489                    }
490                    else {
491                            return dlFileEntryPersistence.filterCountByG_U_F(
492                                    groupId, userId, folderIds);
493                    }
494            }
495    
496            public int getGroupFileEntriesCount(
497                            long groupId, long userId, long rootFolderId, String[] mimeTypes,
498                            int status)
499                    throws PortalException, SystemException {
500    
501                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
502    
503                    if (folderIds.length == 0) {
504                            return 0;
505                    }
506    
507                    List<Long> folderIdsList = ListUtil.toList(folderIds);
508    
509                    return dlFileEntryFinder.countByG_U_F_M(
510                            groupId, userId, folderIdsList, mimeTypes,
511                            new QueryDefinition(status));
512            }
513    
514            public boolean hasFileEntryLock(long fileEntryId)
515                    throws PortalException, SystemException {
516    
517                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
518                            fileEntryId);
519    
520                    long folderId = dlFileEntry.getFolderId();
521    
522                    boolean hasLock = lockLocalService.hasLock(
523                            getUserId(), DLFileEntry.class.getName(), fileEntryId);
524    
525                    if (!hasLock &&
526                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
527    
528                            hasLock = dlFolderService.hasInheritableLock(folderId);
529                    }
530    
531                    return hasLock;
532            }
533    
534            public boolean isFileEntryCheckedOut(long fileEntryId)
535                    throws PortalException, SystemException {
536    
537                    return dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId);
538            }
539    
540            public DLFileEntry moveFileEntry(
541                            long fileEntryId, long newFolderId, ServiceContext serviceContext)
542                    throws PortalException, SystemException {
543    
544                    PermissionChecker permissionChecker = getPermissionChecker();
545    
546                    DLFileEntryPermission.check(
547                            permissionChecker, fileEntryId, ActionKeys.UPDATE);
548    
549                    DLFolderPermission.check(
550                            permissionChecker, serviceContext.getScopeGroupId(), newFolderId,
551                            ActionKeys.ADD_DOCUMENT);
552    
553                    return dlFileEntryLocalService.moveFileEntry(
554                            getUserId(), fileEntryId, newFolderId, serviceContext);
555            }
556    
557            public Lock refreshFileEntryLock(
558                            String lockUuid, long companyId, long expirationTime)
559                    throws PortalException, SystemException {
560    
561                    return lockLocalService.refresh(lockUuid, companyId, expirationTime);
562            }
563    
564            public void revertFileEntry(
565                            long fileEntryId, String version, ServiceContext serviceContext)
566                    throws PortalException, SystemException {
567    
568                    DLFileEntryPermission.check(
569                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
570    
571                    dlFileEntryLocalService.revertFileEntry(
572                            getUserId(), fileEntryId, version, serviceContext);
573            }
574    
575            public DLFileEntry updateFileEntry(
576                            long fileEntryId, String sourceFileName, String mimeType,
577                            String title, String description, String changeLog,
578                            boolean majorVersion, long fileEntryTypeId,
579                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
580                            ServiceContext serviceContext)
581                    throws PortalException, SystemException {
582    
583                    DLFileEntryPermission.check(
584                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
585    
586                    return dlFileEntryLocalService.updateFileEntry(
587                            getUserId(), fileEntryId, sourceFileName, mimeType, title,
588                            description, changeLog, majorVersion, fileEntryTypeId, fieldsMap,
589                            file, is, size, serviceContext);
590            }
591    
592            public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
593                    throws PortalException, SystemException {
594    
595                    return dlFileEntryLocalService.verifyFileEntryCheckOut(
596                            fileEntryId, lockUuid);
597            }
598    
599            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
600                    throws PortalException, SystemException {
601    
602                    return dlFileEntryLocalService.verifyFileEntryLock(
603                            fileEntryId, lockUuid);
604            }
605    
606    }