001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.document.library.kernel.exception.FileEntryLockException;
018 import com.liferay.document.library.kernel.model.DLFileEntry;
019 import com.liferay.document.library.kernel.model.DLFileEntryConstants;
020 import com.liferay.document.library.kernel.model.DLFileVersion;
021 import com.liferay.document.library.kernel.model.DLFolderConstants;
022 import com.liferay.dynamic.data.mapping.kernel.DDMFormValues;
023 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
024 import com.liferay.portal.kernel.exception.PortalException;
025 import com.liferay.portal.kernel.lock.Lock;
026 import com.liferay.portal.kernel.lock.LockManagerUtil;
027 import com.liferay.portal.kernel.search.Hits;
028 import com.liferay.portal.kernel.security.auth.PrincipalException;
029 import com.liferay.portal.kernel.security.permission.ActionKeys;
030 import com.liferay.portal.kernel.security.permission.PermissionChecker;
031 import com.liferay.portal.kernel.service.ServiceContext;
032 import com.liferay.portal.kernel.util.ArrayUtil;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.workflow.WorkflowConstants;
035 import com.liferay.portal.util.PropsValues;
036 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
037 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
038 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
039 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
040
041 import java.io.File;
042 import java.io.InputStream;
043 import java.io.Serializable;
044
045 import java.util.ArrayList;
046 import java.util.Collections;
047 import java.util.List;
048 import java.util.Map;
049
050
058 public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
059
060 @Override
061 public DLFileEntry addFileEntry(
062 long groupId, long repositoryId, long folderId,
063 String sourceFileName, String mimeType, String title,
064 String description, String changeLog, long fileEntryTypeId,
065 Map<String, DDMFormValues> ddmFormValuesMap, File file,
066 InputStream is, long size, ServiceContext serviceContext)
067 throws PortalException {
068
069 DLFolderPermission.check(
070 getPermissionChecker(), groupId, folderId, ActionKeys.ADD_DOCUMENT);
071
072 return dlFileEntryLocalService.addFileEntry(
073 getUserId(), groupId, repositoryId, folderId, sourceFileName,
074 mimeType, title, description, changeLog, fileEntryTypeId,
075 ddmFormValuesMap, file, is, size, serviceContext);
076 }
077
078 @Override
079 public DLFileVersion cancelCheckOut(long fileEntryId)
080 throws PortalException {
081
082 boolean locked = LockManagerUtil.isLocked(
083 DLFileEntry.class.getName(), fileEntryId);
084
085 if (locked && !hasFileEntryLock(fileEntryId) &&
086 !_hasOverrideCheckoutPermission(fileEntryId)) {
087
088 throw new PrincipalException.MustHavePermission(
089 getUserId(), DLFileEntry.class.getName(), fileEntryId,
090 ActionKeys.OVERRIDE_CHECKOUT);
091 }
092
093 return dlFileEntryLocalService.cancelCheckOut(getUserId(), fileEntryId);
094 }
095
096 @Override
097 public void checkInFileEntry(
098 long fileEntryId, boolean major, String changeLog,
099 ServiceContext serviceContext)
100 throws PortalException {
101
102 boolean isLocked = LockManagerUtil.isLocked(
103 DLFileEntry.class.getName(), fileEntryId);
104
105 if (isLocked && !hasFileEntryLock(fileEntryId)) {
106 throw new FileEntryLockException.MustOwnLock();
107 }
108
109 dlFileEntryLocalService.checkInFileEntry(
110 getUserId(), fileEntryId, major, changeLog, serviceContext);
111 }
112
113 @Override
114 public void checkInFileEntry(
115 long fileEntryId, String lockUuid, ServiceContext serviceContext)
116 throws PortalException {
117
118 boolean locked = LockManagerUtil.isLocked(
119 DLFileEntryConstants.getClassName(), fileEntryId);
120
121 if (locked && !hasFileEntryLock(fileEntryId)) {
122 throw new FileEntryLockException.MustOwnLock();
123 }
124
125 dlFileEntryLocalService.checkInFileEntry(
126 getUserId(), fileEntryId, lockUuid, serviceContext);
127 }
128
129 @Override
130 public DLFileEntry checkOutFileEntry(
131 long fileEntryId, ServiceContext serviceContext)
132 throws PortalException {
133
134 return checkOutFileEntry(
135 fileEntryId, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME,
136 serviceContext);
137 }
138
139 @Override
140 public DLFileEntry checkOutFileEntry(
141 long fileEntryId, String owner, long expirationTime,
142 ServiceContext serviceContext)
143 throws PortalException {
144
145 DLFileEntryPermission.check(
146 getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
147
148 return dlFileEntryLocalService.checkOutFileEntry(
149 getUserId(), fileEntryId, owner, expirationTime, serviceContext);
150 }
151
152 @Override
153 public DLFileEntry copyFileEntry(
154 long groupId, long repositoryId, long fileEntryId,
155 long destFolderId, ServiceContext serviceContext)
156 throws PortalException {
157
158 DLFileEntryPermission.check(
159 getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
160
161 DLFolderPermission.check(
162 getPermissionChecker(), groupId, destFolderId,
163 ActionKeys.ADD_DOCUMENT);
164
165 return dlFileEntryLocalService.copyFileEntry(
166 getUserId(), groupId, repositoryId, fileEntryId, destFolderId,
167 serviceContext);
168 }
169
170 @Override
171 public void deleteFileEntry(long fileEntryId) throws PortalException {
172 DLFileEntryPermission.check(
173 getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
174
175 dlFileEntryLocalService.deleteFileEntry(getUserId(), fileEntryId);
176 }
177
178 @Override
179 public void deleteFileEntry(long groupId, long folderId, String title)
180 throws PortalException {
181
182 DLFileEntry dlFileEntry = getFileEntry(groupId, folderId, title);
183
184 deleteFileEntry(dlFileEntry.getFileEntryId());
185 }
186
187 @Override
188 public void deleteFileVersion(long fileEntryId, String version)
189 throws PortalException {
190
191 DLFileEntryPermission.check(
192 getPermissionChecker(), fileEntryId, ActionKeys.DELETE);
193
194 dlFileEntryLocalService.deleteFileVersion(
195 getUserId(), fileEntryId, version);
196 }
197
198 @Override
199 public DLFileEntry fetchFileEntryByImageId(long imageId)
200 throws PortalException {
201
202 DLFileEntry dlFileEntry = dlFileEntryFinder.fetchByAnyImageId(imageId);
203
204 if (dlFileEntry != null) {
205 DLFileEntryPermission.check(
206 getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
207 }
208
209 return dlFileEntry;
210 }
211
212 @Override
213 public InputStream getFileAsStream(long fileEntryId, String version)
214 throws PortalException {
215
216 DLFileEntryPermission.check(
217 getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
218
219 return dlFileEntryLocalService.getFileAsStream(fileEntryId, version);
220 }
221
222 @Override
223 public InputStream getFileAsStream(
224 long fileEntryId, String version, boolean incrementCounter)
225 throws PortalException {
226
227 DLFileEntryPermission.check(
228 getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
229
230 return dlFileEntryLocalService.getFileAsStream(
231 fileEntryId, version, incrementCounter);
232 }
233
234 @Override
235 public List<DLFileEntry> getFileEntries(
236 long groupId, long folderId, int status, int start, int end,
237 OrderByComparator<DLFileEntry> obc)
238 throws PortalException {
239
240 DLFolderPermission.check(
241 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
242
243 List<Long> folderIds = new ArrayList<>();
244
245 folderIds.add(folderId);
246
247 QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>(
248 status, false, start, end, obc);
249
250 return dlFileEntryFinder.filterFindByG_F(
251 groupId, folderIds, queryDefinition);
252 }
253
254 @Override
255 public List<DLFileEntry> getFileEntries(
256 long groupId, long folderId, int start, int end,
257 OrderByComparator<DLFileEntry> obc)
258 throws PortalException {
259
260 return getFileEntries(
261 groupId, folderId, WorkflowConstants.STATUS_APPROVED, start, end,
262 obc);
263 }
264
265 @Override
266 public List<DLFileEntry> getFileEntries(
267 long groupId, long folderId, long fileEntryTypeId, int start,
268 int end, OrderByComparator<DLFileEntry> obc)
269 throws PortalException {
270
271 DLFolderPermission.check(
272 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
273
274 return dlFileEntryPersistence.filterFindByG_F_F(
275 groupId, folderId, fileEntryTypeId, start, end, obc);
276 }
277
278 @Override
279 public List<DLFileEntry> getFileEntries(
280 long groupId, long folderId, String[] mimeTypes, int start, int end,
281 OrderByComparator<DLFileEntry> obc)
282 throws PortalException {
283
284 DLFolderPermission.check(
285 getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
286
287 List<Long> folderIds = new ArrayList<>();
288
289 folderIds.add(folderId);
290
291 QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>(
292 WorkflowConstants.STATUS_IN_TRASH, true, start, end, obc);
293
294 return dlFileEntryFinder.filterFindByG_U_F_M(
295 groupId, 0, folderIds, mimeTypes, queryDefinition);
296 }
297
298 @Override
299 public int getFileEntriesCount(long groupId, long folderId) {
300 return getFileEntriesCount(
301 groupId, folderId, WorkflowConstants.STATUS_APPROVED);
302 }
303
304 @Override
305 public int getFileEntriesCount(long groupId, long folderId, int status) {
306 List<Long> folderIds = new ArrayList<>();
307
308 folderIds.add(folderId);
309
310 return dlFileEntryFinder.filterCountByG_F(
311 groupId, folderIds, new QueryDefinition<DLFileEntry>(status));
312 }
313
314 @Override
315 public int getFileEntriesCount(
316 long groupId, long folderId, long fileEntryTypeId) {
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
326 List<Long> folderIds = new ArrayList<>();
327
328 folderIds.add(folderId);
329
330 return dlFileEntryFinder.filterCountByG_U_F_M(
331 groupId, 0, folderIds, mimeTypes,
332 new QueryDefinition<DLFileEntry>(WorkflowConstants.STATUS_ANY));
333 }
334
335 @Override
336 public DLFileEntry getFileEntry(long fileEntryId) throws PortalException {
337 DLFileEntryPermission.check(
338 getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
339
340 return dlFileEntryLocalService.getFileEntry(fileEntryId);
341 }
342
343 @Override
344 public DLFileEntry getFileEntry(long groupId, long folderId, String title)
345 throws PortalException {
346
347 DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
348 groupId, folderId, title);
349
350 DLFileEntryPermission.check(
351 getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
352
353 return dlFileEntry;
354 }
355
356 @Override
357 public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
358 throws PortalException {
359
360 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByUUID_G(
361 uuid, groupId);
362
363 DLFileEntryPermission.check(
364 getPermissionChecker(), dlFileEntry, ActionKeys.VIEW);
365
366 return dlFileEntry;
367 }
368
369 @Override
370 public Lock getFileEntryLock(long fileEntryId) {
371 try {
372 return LockManagerUtil.getLock(
373 DLFileEntry.class.getName(), fileEntryId);
374 }
375 catch (Exception e) {
376 return null;
377 }
378 }
379
380 @Override
381 public int getFoldersFileEntriesCount(
382 long groupId, List<Long> folderIds, int status) {
383
384 QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>(
385 status);
386
387 if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
388 return dlFileEntryFinder.filterCountByG_F(
389 groupId, folderIds, queryDefinition);
390 }
391 else {
392 int start = 0;
393 int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
394
395 int filesCount = dlFileEntryFinder.filterCountByG_F(
396 groupId, folderIds.subList(start, end), queryDefinition);
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<DLFileEntry> obc)
411 throws PortalException {
412
413 List<Long> folderIds = dlFolderService.getFolderIds(
414 groupId, rootFolderId);
415
416 if (folderIds.isEmpty()) {
417 return Collections.emptyList();
418 }
419 else if (userId <= 0) {
420 return dlFileEntryPersistence.filterFindByG_F(
421 groupId, ArrayUtil.toLongArray(folderIds), start, end, obc);
422 }
423 else {
424 return dlFileEntryPersistence.filterFindByG_U_F(
425 groupId, userId, ArrayUtil.toLongArray(folderIds), start, end,
426 obc);
427 }
428 }
429
430 @Override
431 public List<DLFileEntry> getGroupFileEntries(
432 long groupId, long userId, long repositoryId, long rootFolderId,
433 String[] mimeTypes, int status, int start, int end,
434 OrderByComparator<DLFileEntry> obc)
435 throws PortalException {
436
437 List<Long> repositoryIds = new ArrayList<>();
438
439 if (repositoryId != 0) {
440 repositoryIds.add(repositoryId);
441 }
442
443 QueryDefinition<DLFileEntry> queryDefinition = new QueryDefinition<>(
444 status, start, end, obc);
445
446 if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
447 return dlFileEntryFinder.filterFindByG_U_R_F_M(
448 groupId, userId, repositoryIds, new ArrayList<Long>(),
449 mimeTypes, queryDefinition);
450 }
451
452 List<Long> folderIds = dlFolderService.getFolderIds(
453 groupId, rootFolderId);
454
455 if (folderIds.isEmpty()) {
456 return Collections.emptyList();
457 }
458
459 return dlFileEntryFinder.filterFindByG_U_R_F_M(
460 groupId, userId, repositoryIds, folderIds, mimeTypes,
461 queryDefinition);
462 }
463
464 @Override
465 public List<DLFileEntry> getGroupFileEntries(
466 long groupId, long userId, long rootFolderId, String[] mimeTypes,
467 int status, int start, int end, OrderByComparator<DLFileEntry> obc)
468 throws PortalException {
469
470 return getGroupFileEntries(
471 groupId, userId, 0, rootFolderId, mimeTypes, status, start, end,
472 obc);
473 }
474
475 @Override
476 public int getGroupFileEntriesCount(
477 long groupId, long userId, long rootFolderId)
478 throws PortalException {
479
480 List<Long> folderIds = dlFolderService.getFolderIds(
481 groupId, rootFolderId);
482
483 if (folderIds.isEmpty()) {
484 return 0;
485 }
486 else if (userId <= 0) {
487 return dlFileEntryPersistence.filterCountByG_F(
488 groupId, ArrayUtil.toLongArray(folderIds));
489 }
490 else {
491 return dlFileEntryPersistence.filterCountByG_U_F(
492 groupId, userId, ArrayUtil.toLongArray(folderIds));
493 }
494 }
495
496 @Override
497 public int getGroupFileEntriesCount(
498 long groupId, long userId, long repositoryId, long rootFolderId,
499 String[] mimeTypes, int status)
500 throws PortalException {
501
502 List<Long> repositoryIds = new ArrayList<>();
503
504 if (repositoryId != 0) {
505 repositoryIds.add(repositoryId);
506 }
507
508 if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
509 return dlFileEntryFinder.filterCountByG_U_R_F_M(
510 groupId, userId, repositoryIds, new ArrayList<Long>(),
511 mimeTypes, new QueryDefinition<DLFileEntry>(status));
512 }
513
514 List<Long> folderIds = dlFolderService.getFolderIds(
515 groupId, rootFolderId);
516
517 if (folderIds.isEmpty()) {
518 return 0;
519 }
520
521 return dlFileEntryFinder.filterCountByG_U_R_F_M(
522 groupId, userId, repositoryIds, folderIds, mimeTypes,
523 new QueryDefinition<DLFileEntry>(status));
524 }
525
526 @Override
527 public int getGroupFileEntriesCount(
528 long groupId, long userId, long rootFolderId, String[] mimeTypes,
529 int status)
530 throws PortalException {
531
532 return getGroupFileEntriesCount(
533 groupId, userId, 0, rootFolderId, mimeTypes, status);
534 }
535
536 @Override
537 public boolean hasFileEntryLock(long fileEntryId) throws PortalException {
538 DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
539 fileEntryId);
540
541 long folderId = dlFileEntry.getFolderId();
542
543 boolean hasLock = LockManagerUtil.hasLock(
544 getUserId(), DLFileEntry.class.getName(), fileEntryId);
545
546 if (!hasLock &&
547 (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
548
549 hasLock = dlFolderLocalService.hasInheritableLock(folderId);
550 }
551
552 return hasLock;
553 }
554
555 @Override
556 public boolean isFileEntryCheckedOut(long fileEntryId)
557 throws PortalException {
558
559 return dlFileEntryLocalService.isFileEntryCheckedOut(fileEntryId);
560 }
561
562 @Override
563 public boolean isKeepFileVersionLabel(
564 long fileEntryId, boolean majorVersion,
565 ServiceContext serviceContext)
566 throws PortalException {
567
568 PermissionChecker permissionChecker = getPermissionChecker();
569
570 DLFileEntryPermission.check(
571 permissionChecker, fileEntryId, ActionKeys.VIEW);
572
573 return dlFileEntryLocalService.isKeepFileVersionLabel(
574 fileEntryId, majorVersion, serviceContext);
575 }
576
577
581 @Deprecated
582 @Override
583 public boolean isKeepFileVersionLabel(
584 long fileEntryId, ServiceContext serviceContext)
585 throws PortalException {
586
587 PermissionChecker permissionChecker = getPermissionChecker();
588
589 DLFileEntryPermission.check(
590 permissionChecker, fileEntryId, ActionKeys.VIEW);
591
592 return dlFileEntryLocalService.isKeepFileVersionLabel(
593 fileEntryId, serviceContext);
594 }
595
596 @Override
597 public DLFileEntry moveFileEntry(
598 long fileEntryId, long newFolderId, ServiceContext serviceContext)
599 throws PortalException {
600
601 PermissionChecker permissionChecker = getPermissionChecker();
602
603 DLFileEntryPermission.check(
604 permissionChecker, fileEntryId, ActionKeys.UPDATE);
605
606 DLFolderPermission.check(
607 permissionChecker, serviceContext.getScopeGroupId(), newFolderId,
608 ActionKeys.ADD_DOCUMENT);
609
610 return dlFileEntryLocalService.moveFileEntry(
611 getUserId(), fileEntryId, newFolderId, serviceContext);
612 }
613
614 @Override
615 public Lock refreshFileEntryLock(
616 String lockUuid, long companyId, long expirationTime)
617 throws PortalException {
618
619 return LockManagerUtil.refresh(lockUuid, companyId, expirationTime);
620 }
621
622 @Override
623 public void revertFileEntry(
624 long fileEntryId, String version, ServiceContext serviceContext)
625 throws PortalException {
626
627 DLFileEntryPermission.check(
628 getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
629
630 dlFileEntryLocalService.revertFileEntry(
631 getUserId(), fileEntryId, version, serviceContext);
632 }
633
634 @Override
635 public Hits search(
636 long groupId, long creatorUserId, int status, int start, int end)
637 throws PortalException {
638
639 return dlFileEntryLocalService.search(
640 groupId, getUserId(), creatorUserId, status, start, end);
641 }
642
643 @Override
644 public Hits search(
645 long groupId, long creatorUserId, long folderId, String[] mimeTypes,
646 int status, int start, int end)
647 throws PortalException {
648
649 return dlFileEntryLocalService.search(
650 groupId, getUserId(), creatorUserId, folderId, mimeTypes, status,
651 start, end);
652 }
653
654 @Override
655 public DLFileEntry updateFileEntry(
656 long fileEntryId, String sourceFileName, String mimeType,
657 String title, String description, String changeLog,
658 boolean majorVersion, long fileEntryTypeId,
659 Map<String, DDMFormValues> ddmFormValuesMap, File file,
660 InputStream is, long size, ServiceContext serviceContext)
661 throws PortalException {
662
663 DLFileEntryPermission.check(
664 getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
665
666 if (LockManagerUtil.isLocked(
667 DLFileEntryConstants.getClassName(), fileEntryId)) {
668
669 boolean hasLock = LockManagerUtil.hasLock(
670 getUserId(), DLFileEntry.class.getName(), fileEntryId);
671
672 if (!hasLock) {
673 throw new FileEntryLockException.MustOwnLock();
674 }
675 }
676
677 return dlFileEntryLocalService.updateFileEntry(
678 getUserId(), fileEntryId, sourceFileName, mimeType, title,
679 description, changeLog, majorVersion, fileEntryTypeId,
680 ddmFormValuesMap, file, is, size, serviceContext);
681 }
682
683 @Override
684 public DLFileEntry updateStatus(
685 long userId, long fileVersionId, int status,
686 ServiceContext serviceContext,
687 Map<String, Serializable> workflowContext)
688 throws PortalException {
689
690 DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
691 fileVersionId);
692
693 DLFileEntryPermission.check(
694 getPermissionChecker(), dlFileVersion.getFileEntryId(),
695 ActionKeys.UPDATE);
696
697 return dlFileEntryLocalService.updateStatus(
698 userId, fileVersionId, status, serviceContext, workflowContext);
699 }
700
701 @Override
702 public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
703 throws PortalException {
704
705 return dlFileEntryLocalService.verifyFileEntryCheckOut(
706 fileEntryId, lockUuid);
707 }
708
709 @Override
710 public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
711 throws PortalException {
712
713 return dlFileEntryLocalService.verifyFileEntryLock(
714 fileEntryId, lockUuid);
715 }
716
717 private boolean _hasOverrideCheckoutPermission(long fileEntryId)
718 throws PortalException {
719
720 return DLFileEntryPermission.contains(
721 getPermissionChecker(), fileEntryId, ActionKeys.OVERRIDE_CHECKOUT);
722 }
723
724 }