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