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