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