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