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, ServiceContext serviceContext)
565 throws PortalException {
566
567 PermissionChecker permissionChecker = getPermissionChecker();
568
569 DLFileEntryPermission.check(
570 permissionChecker, fileEntryId, ActionKeys.VIEW);
571
572 return dlFileEntryLocalService.isKeepFileVersionLabel(
573 fileEntryId, serviceContext);
574 }
575
576 @Override
577 public DLFileEntry moveFileEntry(
578 long fileEntryId, long newFolderId, ServiceContext serviceContext)
579 throws PortalException {
580
581 PermissionChecker permissionChecker = getPermissionChecker();
582
583 DLFileEntryPermission.check(
584 permissionChecker, fileEntryId, ActionKeys.UPDATE);
585
586 DLFolderPermission.check(
587 permissionChecker, serviceContext.getScopeGroupId(), newFolderId,
588 ActionKeys.ADD_DOCUMENT);
589
590 return dlFileEntryLocalService.moveFileEntry(
591 getUserId(), fileEntryId, newFolderId, serviceContext);
592 }
593
594 @Override
595 public Lock refreshFileEntryLock(
596 String lockUuid, long companyId, long expirationTime)
597 throws PortalException {
598
599 return LockManagerUtil.refresh(lockUuid, companyId, expirationTime);
600 }
601
602 @Override
603 public void revertFileEntry(
604 long fileEntryId, String version, ServiceContext serviceContext)
605 throws PortalException {
606
607 DLFileEntryPermission.check(
608 getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
609
610 dlFileEntryLocalService.revertFileEntry(
611 getUserId(), fileEntryId, version, serviceContext);
612 }
613
614 @Override
615 public Hits search(
616 long groupId, long creatorUserId, int status, int start, int end)
617 throws PortalException {
618
619 return dlFileEntryLocalService.search(
620 groupId, getUserId(), creatorUserId, status, start, end);
621 }
622
623 @Override
624 public Hits search(
625 long groupId, long creatorUserId, long folderId, String[] mimeTypes,
626 int status, int start, int end)
627 throws PortalException {
628
629 return dlFileEntryLocalService.search(
630 groupId, getUserId(), creatorUserId, folderId, mimeTypes, status,
631 start, end);
632 }
633
634 @Override
635 public DLFileEntry updateFileEntry(
636 long fileEntryId, String sourceFileName, String mimeType,
637 String title, String description, String changeLog,
638 boolean majorVersion, long fileEntryTypeId,
639 Map<String, DDMFormValues> ddmFormValuesMap, File file,
640 InputStream is, long size, ServiceContext serviceContext)
641 throws PortalException {
642
643 DLFileEntryPermission.check(
644 getPermissionChecker(), fileEntryId, ActionKeys.UPDATE);
645
646 if (LockManagerUtil.isLocked(
647 DLFileEntryConstants.getClassName(), fileEntryId)) {
648
649 boolean hasLock = LockManagerUtil.hasLock(
650 getUserId(), DLFileEntry.class.getName(), fileEntryId);
651
652 if (!hasLock) {
653 throw new FileEntryLockException.MustOwnLock();
654 }
655 }
656
657 return dlFileEntryLocalService.updateFileEntry(
658 getUserId(), fileEntryId, sourceFileName, mimeType, title,
659 description, changeLog, majorVersion, fileEntryTypeId,
660 ddmFormValuesMap, file, is, size, serviceContext);
661 }
662
663 @Override
664 public DLFileEntry updateStatus(
665 long userId, long fileVersionId, int status,
666 ServiceContext serviceContext,
667 Map<String, Serializable> workflowContext)
668 throws PortalException {
669
670 DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
671 fileVersionId);
672
673 DLFileEntryPermission.check(
674 getPermissionChecker(), dlFileVersion.getFileEntryId(),
675 ActionKeys.UPDATE);
676
677 return dlFileEntryLocalService.updateStatus(
678 userId, fileVersionId, status, serviceContext, workflowContext);
679 }
680
681 @Override
682 public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
683 throws PortalException {
684
685 return dlFileEntryLocalService.verifyFileEntryCheckOut(
686 fileEntryId, lockUuid);
687 }
688
689 @Override
690 public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
691 throws PortalException {
692
693 return dlFileEntryLocalService.verifyFileEntryLock(
694 fileEntryId, lockUuid);
695 }
696
697 private boolean _hasOverrideCheckoutPermission(long fileEntryId)
698 throws PortalException {
699
700 return DLFileEntryPermission.contains(
701 getPermissionChecker(), fileEntryId, ActionKeys.OVERRIDE_CHECKOUT);
702 }
703
704 }