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