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