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