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