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.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.documentlibrary.util.DLAppUtil;
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     * @author Brian Wing Shun Chan
051     * @author Alexander Chow
052     */
053    public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
054    
055            public DLFileEntry addFileEntry(
056                            long groupId, long repositoryId, long folderId,
057                            String sourceFileName, String mimeType, String title,
058                            String description, String changeLog, long fileEntryTypeId,
059                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
060                            ServiceContext serviceContext)
061                    throws PortalException, SystemException {
062    
063                    DLFolderPermission.check(
064                            getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
065    
066                    return dlFileEntryLocalService.addFileEntry(
067                            getUserId(), groupId, repositoryId, folderId, sourceFileName,
068                            mimeType, title, description, changeLog, fileEntryTypeId, fieldsMap,
069                            file, is, size, serviceContext);
070            }
071    
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            public void checkInFileEntry(
087                            long fileEntryId, boolean major, String changeLog,
088                            ServiceContext serviceContext)
089                    throws PortalException, SystemException {
090    
091                    try {
092                            if (!hasFileEntryLock(fileEntryId)) {
093                                    throw new PrincipalException();
094                            }
095                    }
096                    catch (NoSuchFileEntryException nsfee) {
097                    }
098    
099                    dlFileEntryLocalService.checkInFileEntry(
100                            getUserId(), fileEntryId, major, changeLog, serviceContext);
101            }
102    
103            /**
104             * @deprecated {@link #checkInFileEntry(long, String, ServiceContext)}
105             */
106            public void checkInFileEntry(long fileEntryId, String lockUuid)
107                    throws PortalException, SystemException {
108    
109                    checkInFileEntry(fileEntryId, lockUuid, new ServiceContext());
110            }
111    
112            public void checkInFileEntry(
113                            long fileEntryId, String lockUuid, ServiceContext serviceContext)
114                    throws PortalException, SystemException {
115    
116                    try {
117                            if (!hasFileEntryLock(fileEntryId)) {
118                                    throw new PrincipalException();
119                            }
120                    }
121                    catch (NoSuchFileEntryException nsfee) {
122                    }
123    
124                    dlFileEntryLocalService.checkInFileEntry(
125                            getUserId(), fileEntryId, lockUuid, serviceContext);
126            }
127    
128            /**
129             * @deprecated {@link #checkOutFileEntry(long, ServiceContext)}
130             */
131            public DLFileEntry checkOutFileEntry(long fileEntryId)
132                    throws PortalException, SystemException {
133    
134                    return checkOutFileEntry(fileEntryId, new ServiceContext());
135            }
136    
137            public DLFileEntry checkOutFileEntry(
138                            long fileEntryId, ServiceContext serviceContext)
139                    throws PortalException, SystemException {
140    
141                    return checkOutFileEntry(
142                            fileEntryId, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME,
143                            serviceContext);
144            }
145    
146            /**
147             * @deprecated {@link #checkOutFileEntry(long, String, long,
148             *             ServiceContext)}
149             */
150            public DLFileEntry checkOutFileEntry(
151                            long fileEntryId, String owner, long expirationTime)
152                    throws PortalException, SystemException {
153    
154                    return checkOutFileEntry(
155                            fileEntryId, owner, expirationTime, new ServiceContext());
156            }
157    
158            public DLFileEntry checkOutFileEntry(
159                            long fileEntryId, String owner, long expirationTime,
160                            ServiceContext serviceContext)
161                    throws PortalException, SystemException {
162    
163                    DLFileEntryPermission.check(
164                            getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
165    
166                    return dlFileEntryLocalService.checkOutFileEntry(
167                            getUserId(), fileEntryId, owner, expirationTime, serviceContext);
168            }
169    
170            public DLFileEntry copyFileEntry(
171                            long groupId, long repositoryId, long fileEntryId,
172                            long destFolderId, ServiceContext serviceContext)
173                    throws PortalException, SystemException {
174    
175                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
176    
177                    String sourceFileName = "A." + dlFileEntry.getExtension();
178                    InputStream inputStream = DLStoreUtil.getFileAsStream(
179                            dlFileEntry.getCompanyId(), dlFileEntry.getFolderId(),
180                            dlFileEntry.getName());
181    
182                    DLFileEntry newDlFileEntry = addFileEntry(
183                            groupId, repositoryId, destFolderId, sourceFileName,
184                            dlFileEntry.getMimeType(), dlFileEntry.getTitle(),
185                            dlFileEntry.getDescription(), null,
186                            dlFileEntry.getFileEntryTypeId(), null, null, inputStream,
187                            dlFileEntry.getSize(), serviceContext);
188    
189                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
190    
191                    DLFileVersion newDlFileVersion = newDlFileEntry.getFileVersion();
192    
193                    dlFileEntryLocalService.copyFileEntryMetadata(
194                            dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
195                            fileEntryId, newDlFileVersion.getFileVersionId(),
196                            dlFileVersion.getFileVersionId(), serviceContext);
197    
198                    return newDlFileEntry;
199            }
200    
201            public void deleteFileEntry(long fileEntryId)
202                    throws PortalException, SystemException {
203    
204                    DLFileEntryPermission.check(
205                            getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
206    
207                    dlFileEntryLocalService.deleteFileEntry(getUserId(), fileEntryId);
208            }
209    
210            public void deleteFileEntry(long groupId, long folderId, String title)
211                    throws PortalException, SystemException {
212    
213                    DLFileEntry dlFileEntry = getFileEntry(groupId, folderId, title);
214    
215                    deleteFileEntry(dlFileEntry.getFileEntryId());
216            }
217    
218            public void deleteFileVersion(long fileEntryId, String version)
219                    throws PortalException, SystemException {
220    
221                    DLFileEntryPermission.check(
222                            getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
223    
224                    dlFileEntryLocalService.deleteFileVersion(
225                            getUserId(), fileEntryId, version);
226            }
227    
228            public DLFileEntry fetchFileEntryByImageId(long imageId)
229                    throws PortalException, SystemException {
230    
231                    DLFileEntry dlFileEntry = dlFileEntryFinder.fetchByAnyImageId(imageId);
232    
233                    if (dlFileEntry != null) {
234                            DLFileEntryPermission.check(
235                                    getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
236                    }
237    
238                    return dlFileEntry;
239            }
240    
241            public InputStream getFileAsStream(long fileEntryId, String version)
242                    throws PortalException, SystemException {
243    
244                    DLFileEntryPermission.check(
245                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
246    
247                    return dlFileEntryLocalService.getFileAsStream(
248                            getGuestOrUserId(), fileEntryId, version);
249            }
250    
251            public InputStream getFileAsStream(
252                            long fileEntryId, String version, boolean incrementCounter)
253                    throws PortalException, SystemException {
254    
255                    DLFileEntryPermission.check(
256                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
257    
258                    return dlFileEntryLocalService.getFileAsStream(
259                            getGuestOrUserId(), fileEntryId, version, incrementCounter);
260            }
261    
262            public List<DLFileEntry> getFileEntries(
263                            long groupId, long folderId, int status, int start, int end,
264                            OrderByComparator obc)
265                    throws PortalException, SystemException {
266    
267                    DLFolderPermission.check(
268                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
269    
270                    List<Long> folderIds = new ArrayList<Long>();
271    
272                    folderIds.add(folderId);
273    
274                    QueryDefinition queryDefinition = new QueryDefinition(
275                            status, false, start, end, obc);
276    
277                    return dlFileEntryFinder.filterFindByG_F(
278                            groupId, folderIds, queryDefinition);
279            }
280    
281            public List<DLFileEntry> getFileEntries(
282                            long groupId, long folderId, int start, int end,
283                            OrderByComparator obc)
284                    throws PortalException, SystemException {
285    
286                    return getFileEntries(
287                            groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
288                            obc);
289            }
290    
291            public List<DLFileEntry> getFileEntries(
292                            long groupId, long folderId, long fileEntryTypeId, int start,
293                            int end, OrderByComparator obc)
294                    throws PortalException, SystemException {
295    
296                    DLFolderPermission.check(
297                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
298    
299                    return dlFileEntryPersistence.filterFindByG_F_F(
300                            groupId, folderId, fileEntryTypeId, start, end, obc);
301            }
302    
303            public List<DLFileEntry> getFileEntries(
304                            long groupId, long folderId, String[] mimeTypes, int start, int end,
305                            OrderByComparator obc)
306                    throws PortalException, SystemException {
307    
308                    DLFolderPermission.check(
309                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
310    
311                    List<Long> folderIds = new ArrayList<Long>();
312    
313                    folderIds.add(folderId);
314    
315                    QueryDefinition queryDefinition = new QueryDefinition(
316                            WorkflowConstants.STATUS_IN_TRASH, true, start, end, obc);
317    
318                    return dlFileEntryFinder.findByG_U_F_M(
319                            groupId, 0, folderIds, mimeTypes, queryDefinition);
320            }
321    
322            public int getFileEntriesCount(long groupId, long folderId)
323                    throws SystemException {
324    
325                    return getFileEntriesCount(
326                            groupId, folderId, WorkflowConstants.STATUS_APPROVED);
327            }
328    
329            public int getFileEntriesCount(long groupId, long folderId, int status)
330                    throws SystemException {
331    
332                    List<Long> folderIds = new ArrayList<Long>();
333    
334                    folderIds.add(folderId);
335    
336                    return dlFileEntryFinder.filterCountByG_F(
337                            groupId, folderIds, new QueryDefinition(status));
338            }
339    
340            public int getFileEntriesCount(
341                            long groupId, long folderId, long fileEntryTypeId)
342                    throws SystemException {
343    
344                    return dlFileEntryPersistence.filterCountByG_F_F(
345                            groupId, folderId, fileEntryTypeId);
346            }
347    
348            public int getFileEntriesCount(
349                            long groupId, long folderId, String[] mimeTypes)
350                    throws SystemException {
351    
352                    List<Long> folderIds = new ArrayList<Long>();
353    
354                    folderIds.add(folderId);
355    
356                    return dlFileEntryFinder.countByG_U_F_M(
357                            groupId, 0, folderIds, mimeTypes,
358                            new QueryDefinition(WorkflowConstants.STATUS_ANY));
359            }
360    
361            public DLFileEntry getFileEntry(long fileEntryId)
362                    throws PortalException, SystemException {
363    
364                    DLFileEntryPermission.check(
365                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
366    
367                    return dlFileEntryLocalService.getFileEntry(fileEntryId);
368            }
369    
370            public DLFileEntry getFileEntry(long groupId, long folderId, String title)
371                    throws PortalException, SystemException {
372    
373                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
374                            groupId, folderId, title);
375    
376                    DLFileEntryPermission.check(
377                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
378    
379                    return dlFileEntry;
380            }
381    
382            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
383                    throws PortalException, SystemException {
384    
385                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByUUID_G(
386                            uuid, groupId);
387    
388                    DLFileEntryPermission.check(
389                            getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
390    
391                    return dlFileEntry;
392            }
393    
394            public Lock getFileEntryLock(long fileEntryId) {
395                    try {
396                            return lockLocalService.getLock(
397                                    DLFileEntry.class.getName(), fileEntryId);
398                    }
399                    catch (Exception e) {
400                            return null;
401                    }
402            }
403    
404            public int getFoldersFileEntriesCount(
405                            long groupId, List<Long> folderIds, int status)
406                    throws SystemException {
407    
408                    QueryDefinition queryDefinition = new QueryDefinition(status);
409    
410                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
411                            return dlFileEntryFinder.filterCountByG_F(
412                                    groupId, folderIds, queryDefinition);
413                    }
414                    else {
415                            int start = 0;
416                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
417    
418                            int filesCount = dlFileEntryFinder.filterCountByG_F(
419                                    groupId, folderIds.subList(start, end), queryDefinition);
420    
421                            folderIds.subList(start, end).clear();
422    
423                            filesCount += getFoldersFileEntriesCount(
424                                    groupId, folderIds, status);
425    
426                            return filesCount;
427                    }
428            }
429    
430            public List<DLFileEntry> getGroupFileEntries(
431                            long groupId, long userId, long rootFolderId, int start, int end,
432                            OrderByComparator obc)
433                    throws PortalException, SystemException {
434    
435                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
436    
437                    folderIds = DLAppUtil.filterFolderIds(
438                            getPermissionChecker(), groupId, folderIds);
439    
440                    if (folderIds.length == 0) {
441                            return Collections.emptyList();
442                    }
443                    else if (userId <= 0) {
444                            return dlFileEntryPersistence.filterFindByG_F(
445                                    groupId, folderIds, start, end, obc);
446                    }
447                    else {
448                            return dlFileEntryPersistence.filterFindByG_U_F(
449                                    groupId, userId, folderIds, start, end, obc);
450                    }
451            }
452    
453            public List<DLFileEntry> getGroupFileEntries(
454                            long groupId, long userId, long rootFolderId, String[] mimeTypes,
455                            int status, int start, int end, OrderByComparator obc)
456                    throws PortalException, SystemException {
457    
458                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
459    
460                    folderIds = DLAppUtil.filterFolderIds(
461                            getPermissionChecker(), groupId, folderIds);
462    
463                    if (folderIds.length == 0) {
464                            return Collections.emptyList();
465                    }
466    
467                    List<Long> folderIdsList = ListUtil.toList(folderIds);
468    
469                    QueryDefinition queryDefinition = new QueryDefinition(
470                            status, start, end, obc);
471    
472                    return dlFileEntryFinder.findByG_U_F_M(
473                            groupId, userId, folderIdsList, mimeTypes, queryDefinition);
474            }
475    
476            public int getGroupFileEntriesCount(
477                            long groupId, long userId, long rootFolderId)
478                    throws PortalException, SystemException {
479    
480                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
481    
482                    if (folderIds.length == 0) {
483                            return 0;
484                    }
485                    else if (userId <= 0) {
486                            return dlFileEntryPersistence.filterCountByG_F(groupId, folderIds);
487                    }
488                    else {
489                            return dlFileEntryPersistence.filterCountByG_U_F(
490                                    groupId, userId, folderIds);
491                    }
492            }
493    
494            public int getGroupFileEntriesCount(
495                            long groupId, long userId, long rootFolderId, String[] mimeTypes,
496                            int status)
497                    throws PortalException, SystemException {
498    
499                    long[] folderIds = dlFolderService.getFolderIds(groupId, rootFolderId);
500    
501                    if (folderIds.length == 0) {
502                            return 0;
503                    }
504    
505                    List<Long> folderIdsList = ListUtil.toList(folderIds);
506    
507                    return dlFileEntryFinder.countByG_U_F_M(
508                            groupId, userId, folderIdsList, mimeTypes,
509                            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    }