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