1
14
15 package com.liferay.portlet.documentlibrary.service.impl;
16
17 import com.liferay.documentlibrary.DuplicateFileException;
18 import com.liferay.documentlibrary.FileSizeException;
19 import com.liferay.documentlibrary.NoSuchFileException;
20 import com.liferay.documentlibrary.util.JCRHook;
21 import com.liferay.portal.kernel.exception.PortalException;
22 import com.liferay.portal.kernel.exception.SystemException;
23 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
24 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.search.Indexer;
28 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.MimeTypesUtil;
31 import com.liferay.portal.kernel.util.OrderByComparator;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.kernel.workflow.WorkflowConstants;
36 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
37 import com.liferay.portal.model.Resource;
38 import com.liferay.portal.model.ResourceConstants;
39 import com.liferay.portal.model.User;
40 import com.liferay.portal.service.ServiceContext;
41 import com.liferay.portal.util.PortalUtil;
42 import com.liferay.portal.util.PortletKeys;
43 import com.liferay.portal.util.PropsValues;
44 import com.liferay.portlet.asset.NoSuchEntryException;
45 import com.liferay.portlet.asset.model.AssetEntry;
46 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
47 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
48 import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
49 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
50 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
51 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
52 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
53 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
54 import com.liferay.portlet.documentlibrary.model.DLFolder;
55 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
56 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
57 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
58 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
59 import com.liferay.portlet.documentlibrary.util.DLUtil;
60 import com.liferay.portlet.documentlibrary.util.comparator.FileEntryModifiedDateComparator;
61 import com.liferay.portlet.messageboards.model.MBDiscussion;
62 import com.liferay.portlet.ratings.model.RatingsEntry;
63 import com.liferay.portlet.ratings.model.RatingsStats;
64
65 import java.io.File;
66 import java.io.FileInputStream;
67 import java.io.FileNotFoundException;
68 import java.io.InputStream;
69
70 import java.util.Date;
71 import java.util.List;
72
73
87 public class DLFileEntryLocalServiceImpl
88 extends DLFileEntryLocalServiceBaseImpl {
89
90 public DLFileEntry addFileEntry(
91 String uuid, long userId, long groupId, long folderId, String name,
92 String title, String description, String versionDescription,
93 String extraSettings, byte[] bytes, ServiceContext serviceContext)
94 throws PortalException, SystemException {
95
96 if (bytes == null) {
97 throw new FileSizeException();
98 }
99
100 InputStream is = new UnsyncByteArrayInputStream(bytes);
101
102 return addFileEntry(
103 uuid, userId, groupId, folderId, name, title, description,
104 versionDescription, extraSettings, is, bytes.length,
105 serviceContext);
106 }
107
108 public DLFileEntry addFileEntry(
109 String uuid, long userId, long groupId, long folderId, String name,
110 String title, String description, String versionDescription,
111 String extraSettings, File file, ServiceContext serviceContext)
112 throws PortalException, SystemException {
113
114 if (file == null) {
115 throw new FileSizeException();
116 }
117
118 try {
119 InputStream is = new UnsyncBufferedInputStream(
120 new FileInputStream(file));
121
122 return addFileEntry(
123 uuid, userId, groupId, folderId, name, title, description,
124 versionDescription, extraSettings, is, file.length(),
125 serviceContext);
126 }
127 catch (FileNotFoundException fnfe) {
128 throw new FileSizeException();
129 }
130 }
131
132 public DLFileEntry addFileEntry(
133 String uuid, long userId, long groupId, long folderId, String name,
134 String title, String description, String versionDescription,
135 String extraSettings, InputStream is, long size,
136 ServiceContext serviceContext)
137 throws PortalException, SystemException {
138
139
141 User user = userPersistence.findByPrimaryKey(userId);
142 folderId = getFolderId(user.getCompanyId(), folderId);
143
144 if (Validator.isNull(title)) {
145 title = name;
146 }
147
148 name = String.valueOf(
149 counterLocalService.increment(DLFileEntry.class.getName()));
150
151 Date now = new Date();
152
153 validate(groupId, folderId, title, is);
154
155 long fileEntryId = counterLocalService.increment();
156
157 DLFileEntry fileEntry = dlFileEntryPersistence.create(fileEntryId);
158
159 fileEntry.setUuid(uuid);
160 fileEntry.setGroupId(groupId);
161 fileEntry.setCompanyId(user.getCompanyId());
162 fileEntry.setUserId(user.getUserId());
163 fileEntry.setUserName(user.getFullName());
164 fileEntry.setVersionUserId(user.getUserId());
165 fileEntry.setVersionUserName(user.getFullName());
166 fileEntry.setCreateDate(serviceContext.getCreateDate(now));
167 fileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
168 fileEntry.setFolderId(folderId);
169 fileEntry.setName(name);
170 fileEntry.setTitle(title);
171 fileEntry.setDescription(description);
172 fileEntry.setVersion(DLFileEntryConstants.DEFAULT_VERSION);
173 fileEntry.setSize(size);
174 fileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
175 fileEntry.setExtraSettings(extraSettings);
176 fileEntry.setExpandoBridgeAttributes(serviceContext);
177
178 dlFileEntryPersistence.update(fileEntry, false);
179
180
182 if (serviceContext.getAddCommunityPermissions() ||
183 serviceContext.getAddGuestPermissions()) {
184
185 addFileEntryResources(
186 fileEntry, serviceContext.getAddCommunityPermissions(),
187 serviceContext.getAddGuestPermissions());
188 }
189 else {
190 addFileEntryResources(
191 fileEntry, serviceContext.getCommunityPermissions(),
192 serviceContext.getGuestPermissions());
193 }
194
195
197 DLFileVersion fileVersion = addFileVersion(
198 user, fileEntry, serviceContext.getModifiedDate(now),
199 DLFileEntryConstants.DEFAULT_VERSION, null, size,
200 WorkflowConstants.STATUS_DRAFT);
201
202
204 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
205 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
206
207 folder.setLastPostDate(fileEntry.getModifiedDate());
208
209 dlFolderPersistence.update(folder, false);
210 }
211
212
214 updateAsset(
215 userId, fileEntry, fileVersion,
216 serviceContext.getAssetCategoryIds(),
217 serviceContext.getAssetTagNames());
218
219
221 if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
222 mbMessageLocalService.addDiscussionMessage(
223 userId, fileEntry.getUserName(), groupId,
224 DLFileEntry.class.getName(), fileEntryId,
225 WorkflowConstants.ACTION_PUBLISH);
226 }
227
228
230 dlLocalService.addFile(
231 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
232 fileEntry.getGroupId(), fileEntry.getRepositoryId(), name, false,
233 fileEntryId, fileEntry.getLuceneProperties(),
234 fileEntry.getModifiedDate(), serviceContext, is);
235
236
238 WorkflowHandlerRegistryUtil.startWorkflowInstance(
239 user.getCompanyId(), groupId, userId, DLFileEntry.class.getName(),
240 fileEntryId, fileEntry, serviceContext);
241
242 return fileEntry;
243 }
244
245 public void addFileEntryResources(
246 DLFileEntry fileEntry, boolean addCommunityPermissions,
247 boolean addGuestPermissions)
248 throws PortalException, SystemException {
249
250 resourceLocalService.addResources(
251 fileEntry.getCompanyId(), fileEntry.getGroupId(),
252 fileEntry.getUserId(), DLFileEntry.class.getName(),
253 fileEntry.getFileEntryId(), false, addCommunityPermissions,
254 addGuestPermissions);
255 }
256
257 public void addFileEntryResources(
258 DLFileEntry fileEntry, String[] communityPermissions,
259 String[] guestPermissions)
260 throws PortalException, SystemException {
261
262 resourceLocalService.addModelResources(
263 fileEntry.getCompanyId(), fileEntry.getGroupId(),
264 fileEntry.getUserId(), DLFileEntry.class.getName(),
265 fileEntry.getFileEntryId(), communityPermissions, guestPermissions);
266 }
267
268 public void addFileEntryResources(
269 long fileEntryId, boolean addCommunityPermissions,
270 boolean addGuestPermissions)
271 throws PortalException, SystemException {
272
273 DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
274 fileEntryId);
275
276 addFileEntryResources(
277 fileEntry, addCommunityPermissions, addGuestPermissions);
278 }
279
280 public void addFileEntryResources(
281 long fileEntryId, String[] communityPermissions,
282 String[] guestPermissions)
283 throws PortalException, SystemException {
284
285 DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
286 fileEntryId);
287
288 addFileEntryResources(
289 fileEntry, communityPermissions, guestPermissions);
290 }
291
292 public DLFileEntry addOrOverwriteFileEntry(
293 long userId, long groupId, long folderId, String name,
294 String sourceName, String title, String description,
295 String versionDescription, String extraSettings, File file,
296 ServiceContext serviceContext)
297 throws PortalException, SystemException {
298
299 try {
300 dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
301
302 return updateFileEntry(
303 userId, groupId, folderId, folderId, name, sourceName, title,
304 description, versionDescription, false, extraSettings, file,
305 serviceContext);
306 }
307 catch (NoSuchFileEntryException nsfee) {
308 return addFileEntry(
309 null, userId, groupId, folderId, name, title, description,
310 versionDescription, extraSettings, file, serviceContext);
311 }
312 }
313
314 public void deleteFileEntries(long groupId, long folderId)
315 throws PortalException, SystemException {
316
317 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByG_F(
318 groupId, folderId);
319
320 for (DLFileEntry fileEntry : fileEntries) {
321 deleteFileEntry(fileEntry);
322 }
323 }
324
325 public void deleteFileEntry(DLFileEntry fileEntry)
326 throws PortalException, SystemException {
327
328
330 dlFileEntryPersistence.remove(fileEntry);
331
332
334 resourceLocalService.deleteResource(
335 fileEntry.getCompanyId(), DLFileEntry.class.getName(),
336 ResourceConstants.SCOPE_INDIVIDUAL, fileEntry.getFileEntryId());
337
338
340 webDAVPropsLocalService.deleteWebDAVProps(
341 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
342
343
345 workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
346 fileEntry.getCompanyId(), fileEntry.getGroupId(),
347 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
348
349
351 dlFileRankLocalService.deleteFileRanks(
352 fileEntry.getFolderId(), fileEntry.getName());
353
354
356 dlFileShortcutLocalService.deleteFileShortcuts(
357 fileEntry.getGroupId(), fileEntry.getFolderId(),
358 fileEntry.getName());
359
360
362 List<DLFileVersion> fileVersions = dlFileVersionPersistence.findByG_F_N(
363 fileEntry.getGroupId(), fileEntry.getFolderId(),
364 fileEntry.getName());
365
366 for (DLFileVersion fileVersion : fileVersions) {
367 dlFileVersionPersistence.remove(fileVersion);
368 }
369
370
372 assetEntryLocalService.deleteEntry(
373 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
374
375
377 expandoValueLocalService.deleteValues(
378 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
379
380
382 mbMessageLocalService.deleteDiscussionMessages(
383 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
384
385
387 ratingsStatsLocalService.deleteStats(
388 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
389
390
392 socialActivityLocalService.deleteActivities(
393 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
394
395
397 try {
398 dlService.deleteFile(
399 fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
400 fileEntry.getRepositoryId(), fileEntry.getName());
401 }
402 catch (Exception e) {
403 if (_log.isWarnEnabled()) {
404 _log.warn(e, e);
405 }
406 }
407 }
408
409 public void deleteFileEntry(long groupId, long folderId, String name)
410 throws PortalException, SystemException {
411
412 deleteFileEntry(groupId, folderId, name, null);
413 }
414
415 public void deleteFileEntry(
416 long groupId, long folderId, String name, String version)
417 throws PortalException, SystemException {
418
419 DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
420 groupId, folderId, name);
421
422 if (Validator.isNotNull(version)) {
423 try {
424 dlService.deleteFile(
425 fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
426 fileEntry.getRepositoryId(), fileEntry.getName(), version);
427 }
428 catch (Exception e) {
429 if (_log.isWarnEnabled()) {
430 _log.warn(e, e);
431 }
432 }
433
434 long fileVersionsCount = dlFileVersionPersistence.countByG_F_N(
435 groupId, folderId, name);
436
437 dlFileVersionPersistence.removeByG_F_N_V(
438 groupId, folderId, name, version);
439
440 if (fileVersionsCount == 1) {
441 dlFileEntryPersistence.remove(fileEntry);
442 }
443 else {
444 if (version.equals(fileEntry.getVersion())) {
445 try {
446 DLFileVersion fileVersion =
447 dlFileVersionLocalService.getLatestFileVersion(
448 groupId, folderId, name);
449
450 fileEntry.setVersion(fileVersion.getVersion());
451 fileEntry.setSize(fileVersion.getSize());
452 }
453 catch (NoSuchFileVersionException nsfve) {
454 }
455 }
456
457 dlFileEntryPersistence.update(fileEntry, false);
458 }
459 }
460 else {
461 deleteFileEntry(fileEntry);
462 }
463 }
464
465 public List<DLFileEntry> getCompanyFileEntries(
466 long companyId, int start, int end)
467 throws SystemException {
468
469 return dlFileEntryPersistence.findByCompanyId(companyId, start, end);
470 }
471
472 public List<DLFileEntry> getCompanyFileEntries(
473 long companyId, int start, int end, OrderByComparator obc)
474 throws SystemException {
475
476 return dlFileEntryPersistence.findByCompanyId(
477 companyId, start, end, obc);
478 }
479
480 public int getCompanyFileEntriesCount(long companyId)
481 throws SystemException {
482
483 return dlFileEntryPersistence.countByCompanyId(companyId);
484 }
485
486 public InputStream getFileAsStream(
487 long companyId, long userId, long groupId, long folderId,
488 String name)
489 throws PortalException, SystemException {
490
491 return getFileAsStream(
492 companyId, userId, groupId, folderId, name, StringPool.BLANK);
493 }
494
495 public InputStream getFileAsStream(
496 long companyId, long userId, long groupId, long folderId,
497 String name, String version)
498 throws PortalException, SystemException {
499
500 DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
501 groupId, folderId, name);
502
503 if (userId > 0) {
504 dlFileRankLocalService.updateFileRank(
505 groupId, companyId, userId, folderId, name,
506 new ServiceContext());
507 }
508
509 if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED) {
510 fileEntry.setReadCount(fileEntry.getReadCount() + 1);
511
512 dlFileEntryPersistence.update(fileEntry, false);
513
514 assetEntryLocalService.incrementViewCounter(
515 userId, DLFileEntry.class.getName(),
516 fileEntry.getFileEntryId());
517
518 List<DLFileShortcut> fileShortcuts =
519 dlFileShortcutPersistence.findByG_TF_TN(
520 groupId, folderId, name);
521
522 for (DLFileShortcut fileShortcut : fileShortcuts) {
523 assetEntryLocalService.incrementViewCounter(
524 userId, DLFileShortcut.class.getName(),
525 fileShortcut.getFileShortcutId());
526 }
527 }
528
529 if (Validator.isNotNull(version)) {
530 return dlLocalService.getFileAsStream(
531 companyId, fileEntry.getRepositoryId(), name, version);
532 }
533 else {
534 return dlLocalService.getFileAsStream(
535 companyId, fileEntry.getRepositoryId(), name,
536 fileEntry.getVersion());
537 }
538 }
539
540 public List<DLFileEntry> getFileEntries(long groupId, long folderId)
541 throws SystemException {
542
543 return dlFileEntryPersistence.findByG_F(groupId, folderId);
544 }
545
546 public List<DLFileEntry> getFileEntries(
547 long groupId, long folderId, int start, int end)
548 throws SystemException {
549
550 return dlFileEntryPersistence.findByG_F(groupId, folderId, start, end);
551 }
552
553 public List<DLFileEntry> getFileEntries(
554 long groupId, long folderId, int start, int end,
555 OrderByComparator obc)
556 throws SystemException {
557
558 return dlFileEntryPersistence.findByG_F(
559 groupId, folderId, start, end, obc);
560 }
561
562 public int getFileEntriesCount(long groupId, long folderId)
563 throws SystemException {
564
565 return dlFileEntryPersistence.countByG_F(groupId, folderId);
566 }
567
568 public DLFileEntry getFileEntry(long fileEntryId)
569 throws PortalException, SystemException {
570
571 return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
572 }
573
574 public DLFileEntry getFileEntry(long groupId, long folderId, String name)
575 throws PortalException, SystemException {
576
577 return dlFileEntryPersistence.findByG_F_N(groupId, folderId, name);
578 }
579
580 public DLFileEntry getFileEntryByTitle(
581 long groupId, long folderId, String title)
582 throws PortalException, SystemException {
583
584 return dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
585 }
586
587 public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
588 throws PortalException, SystemException {
589
590 return dlFileEntryPersistence.findByUUID_G(uuid, groupId);
591 }
592
593 public int getFoldersFileEntriesCount(
594 long groupId, List<Long> folderIds, int status)
595 throws SystemException {
596
597 if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
598 return dlFileEntryFinder.countByG_F_S(groupId, folderIds, status);
599 }
600 else {
601 int start = 0;
602 int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
603
604 int filesCount = dlFileEntryFinder.countByG_F_S(
605 groupId, folderIds.subList(start, end), status);
606
607 folderIds.subList(start, end).clear();
608
609 filesCount += getFoldersFileEntriesCount(
610 groupId, folderIds, status);
611
612 return filesCount;
613 }
614 }
615
616 public List<DLFileEntry> getGroupFileEntries(
617 long groupId, int start, int end)
618 throws SystemException {
619
620 return getGroupFileEntries(
621 groupId, start, end, new FileEntryModifiedDateComparator());
622 }
623
624 public List<DLFileEntry> getGroupFileEntries(
625 long groupId, int start, int end, OrderByComparator obc)
626 throws SystemException {
627
628 return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
629 }
630
631 public List<DLFileEntry> getGroupFileEntries(
632 long groupId, long userId, int start, int end)
633 throws SystemException {
634
635 return getGroupFileEntries(
636 groupId, userId, start, end, new FileEntryModifiedDateComparator());
637 }
638
639 public List<DLFileEntry> getGroupFileEntries(
640 long groupId, long userId, int start, int end,
641 OrderByComparator obc)
642 throws SystemException {
643
644 if (userId <= 0) {
645 return dlFileEntryPersistence.findByGroupId(
646 groupId, start, end, obc);
647 }
648 else {
649 return dlFileEntryPersistence.findByG_U(
650 groupId, userId, start, end, obc);
651 }
652 }
653
654 public int getGroupFileEntriesCount(long groupId) throws SystemException {
655 return dlFileEntryPersistence.countByGroupId(groupId);
656 }
657
658 public int getGroupFileEntriesCount(long groupId, long userId)
659 throws SystemException {
660
661 if (userId <= 0) {
662 return dlFileEntryPersistence.countByGroupId(groupId);
663 }
664 else {
665 return dlFileEntryPersistence.countByG_U(groupId, userId);
666 }
667 }
668
669 public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
670 return dlFileEntryFinder.findByNoAssets();
671 }
672
673 public void updateAsset(
674 long userId, DLFileEntry fileEntry, DLFileVersion fileVersion,
675 long[] assetCategoryIds, String[] assetTagNames)
676 throws PortalException, SystemException {
677
678 String mimeType = MimeTypesUtil.getContentType(fileEntry.getTitle());
679
680 boolean addDraftAssetEntry = false;
681
682 if ((fileVersion != null) && !fileVersion.isApproved() &&
683 (fileVersion.getVersion() !=
684 DLFileEntryConstants.DEFAULT_VERSION)) {
685
686 int approvedArticlesCount =
687 dlFileVersionPersistence.countByG_F_N_S(
688 fileEntry.getGroupId(), fileEntry.getFolderId(),
689 fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
690
691 if (approvedArticlesCount > 0) {
692 addDraftAssetEntry = true;
693 }
694 }
695
696 if (addDraftAssetEntry) {
697 assetEntryLocalService.updateEntry(
698 userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
699 fileVersion.getFileVersionId(), assetCategoryIds, assetTagNames,
700 false, null, null, null, null, mimeType, fileEntry.getTitle(),
701 fileEntry.getDescription(), null, null, 0, 0, null, false);
702 }
703 else {
704 assetEntryLocalService.updateEntry(
705 userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
706 fileEntry.getFileEntryId(), assetCategoryIds, assetTagNames,
707 true, null, null, null, null, mimeType, fileEntry.getTitle(),
708 fileEntry.getDescription(), null, null, 0, 0, null, false);
709
710 List<DLFileShortcut> fileShortcuts =
711 dlFileShortcutPersistence.findByG_TF_TN(
712 fileEntry.getGroupId(), fileEntry.getFolderId(),
713 fileEntry.getName());
714
715 for (DLFileShortcut fileShortcut : fileShortcuts) {
716 assetEntryLocalService.updateEntry(
717 userId, fileShortcut.getGroupId(),
718 DLFileShortcut.class.getName(),
719 fileShortcut.getFileShortcutId(), assetCategoryIds,
720 assetTagNames, true, null, null, null, null, mimeType,
721 fileEntry.getTitle(), fileEntry.getDescription(), null,
722 null, 0, 0, null, false);
723 }
724 }
725 }
726
727 public DLFileEntry updateFileEntry(
728 long userId, long groupId, long folderId, long newFolderId,
729 String name, String sourceFileName, String title,
730 String description, String versionDescription, boolean majorVersion,
731 String extraSettings, byte[] bytes, ServiceContext serviceContext)
732 throws PortalException, SystemException {
733
734 InputStream is = null;
735 long size = 0;
736
737 if (bytes != null) {
738 is = new UnsyncByteArrayInputStream(bytes);
739 size = bytes.length;
740 }
741
742 return updateFileEntry(
743 userId, groupId, folderId, newFolderId, name, sourceFileName, title,
744 description, versionDescription, majorVersion, extraSettings, is,
745 size, serviceContext);
746 }
747
748 public DLFileEntry updateFileEntry(
749 long userId, long groupId, long folderId, long newFolderId,
750 String name, String sourceFileName, String title,
751 String description, String versionDescription, boolean majorVersion,
752 String extraSettings, File file, ServiceContext serviceContext)
753 throws PortalException, SystemException {
754
755 try {
756 InputStream is = null;
757 long size = 0;
758
759 if ((file != null) && file.exists()) {
760 is = new UnsyncBufferedInputStream(new FileInputStream(file));
761 size = file.length();
762 }
763
764 return updateFileEntry(
765 userId, groupId, folderId, newFolderId, name, sourceFileName,
766 title, description, versionDescription, majorVersion,
767 extraSettings, is, size, serviceContext);
768 }
769 catch (FileNotFoundException fnfe) {
770 throw new NoSuchFileException();
771 }
772 }
773
774 public DLFileEntry updateFileEntry(
775 long userId, long groupId, long folderId, long newFolderId,
776 String name, String sourceFileName, String title,
777 String description, String versionDescription, boolean majorVersion,
778 String extraSettings, InputStream is, long size,
779 ServiceContext serviceContext)
780 throws PortalException, SystemException {
781
782
784 User user = userPersistence.findByPrimaryKey(userId);
785
786 if (Validator.isNull(title)) {
787 title = sourceFileName;
788
789 if (Validator.isNull(title)) {
790 title = name;
791 }
792 }
793
794 Date now = new Date();
795
796 DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
797 groupId, folderId, name);
798
799 validate(
800 groupId, folderId, newFolderId, name, title, sourceFileName, is);
801
802 fileEntry.setTitle(title);
803 fileEntry.setDescription(description);
804 fileEntry.setExtraSettings(extraSettings);
805 fileEntry.setExpandoBridgeAttributes(serviceContext);
806
807
809 if (folderId != newFolderId) {
810 long oldFileEntryId = fileEntry.getFileEntryId();
811
812 if (dlLocalService.hasFile(
813 user.getCompanyId(),
814 DLFileEntryImpl.getRepositoryId(groupId, newFolderId),
815 name, StringPool.BLANK)) {
816
817 throw new DuplicateFileException(name);
818 }
819
820 long newFileEntryId = counterLocalService.increment();
821
822 DLFileEntry newFileEntry = dlFileEntryPersistence.create(
823 newFileEntryId);
824
825 newFileEntry.setGroupId(fileEntry.getGroupId());
826 newFileEntry.setCompanyId(fileEntry.getCompanyId());
827 newFileEntry.setUserId(fileEntry.getUserId());
828 newFileEntry.setUserName(fileEntry.getUserName());
829 newFileEntry.setVersionUserId(fileEntry.getVersionUserId());
830 newFileEntry.setVersionUserName(fileEntry.getVersionUserName());
831 newFileEntry.setCreateDate(fileEntry.getCreateDate());
832 newFileEntry.setModifiedDate(fileEntry.getModifiedDate());
833 newFileEntry.setFolderId(newFolderId);
834 newFileEntry.setName(name);
835 newFileEntry.setTitle(fileEntry.getTitle());
836 newFileEntry.setDescription(fileEntry.getDescription());
837 newFileEntry.setVersion(fileEntry.getVersion());
838 newFileEntry.setSize(fileEntry.getSize());
839 newFileEntry.setReadCount(fileEntry.getReadCount());
840 newFileEntry.setExtraSettings(extraSettings);
841
842 dlFileEntryPersistence.update(newFileEntry, false);
843
844 dlFileEntryPersistence.remove(fileEntry);
845
846 workflowInstanceLinkLocalService.updateClassPK(
847 fileEntry.getCompanyId(), fileEntry.getGroupId(),
848 DLFileEntry.class.getName(), oldFileEntryId, newFileEntryId);
849
850 List<DLFileVersion> fileVersions =
851 dlFileVersionPersistence.findByG_F_N(
852 groupId, folderId, name);
853
854 for (DLFileVersion fileVersion : fileVersions) {
855 long newFileVersionId = counterLocalService.increment();
856
857 DLFileVersion newFileVersion = dlFileVersionPersistence.create(
858 newFileVersionId);
859
860 newFileVersion.setGroupId(fileVersion.getGroupId());
861 newFileVersion.setCompanyId(fileVersion.getCompanyId());
862 newFileVersion.setUserId(fileVersion.getUserId());
863 newFileVersion.setUserName(fileVersion.getUserName());
864 newFileVersion.setCreateDate(fileVersion.getCreateDate());
865 newFileVersion.setFolderId(newFolderId);
866 newFileVersion.setName(name);
867 newFileVersion.setVersion(fileVersion.getVersion());
868 newFileVersion.setSize(fileVersion.getSize());
869 newFileVersion.setStatus(fileVersion.getStatus());
870 newFileVersion.setStatusByUserId(userId);
871 newFileVersion.setStatusByUserName(user.getFullName());
872 newFileVersion.setStatusDate(
873 serviceContext.getModifiedDate(now));
874
875 dlFileVersionPersistence.update(newFileVersion, false);
876
877 dlFileVersionPersistence.remove(fileVersion);
878 }
879
880 dlFileShortcutLocalService.updateFileShortcuts(
881 groupId, folderId, name, newFolderId, name);
882
883
885 Resource resource = resourceLocalService.getResource(
886 fileEntry.getCompanyId(), DLFileEntry.class.getName(),
887 ResourceConstants.SCOPE_INDIVIDUAL,
888 String.valueOf(fileEntry.getFileEntryId()));
889
890 resource.setPrimKey(String.valueOf(newFileEntryId));
891
892 resourcePersistence.update(resource, false);
893
894
896 assetEntryLocalService.deleteEntry(
897 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
898
899 List<DLFileShortcut> fileShortcuts =
900 dlFileShortcutPersistence.findByG_TF_TN(
901 groupId, folderId, name);
902
903 for (DLFileShortcut fileShortcut : fileShortcuts) {
904 assetEntryLocalService.deleteEntry(
905 DLFileShortcut.class.getName(),
906 fileShortcut.getFileShortcutId());
907 }
908
909
911 expandoValueLocalService.deleteValues(
912 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
913
914
916 RatingsStats stats = ratingsStatsLocalService.getStats(
917 DLFileEntry.class.getName(), oldFileEntryId);
918
919 stats.setClassPK(newFileEntryId);
920
921 ratingsStatsPersistence.update(stats, false);
922
923 long classNameId = PortalUtil.getClassNameId(
924 DLFileEntry.class.getName());
925
926 List<RatingsEntry> entries = ratingsEntryPersistence.findByC_C(
927 classNameId, oldFileEntryId);
928
929 for (RatingsEntry entry : entries) {
930 entry.setClassPK(newFileEntryId);
931
932 ratingsEntryPersistence.update(entry, false);
933 }
934
935
937 MBDiscussion discussion = mbDiscussionPersistence.fetchByC_C(
938 classNameId, oldFileEntryId);
939
940 if (discussion != null) {
941 discussion.setClassPK(newFileEntryId);
942
943 mbDiscussionPersistence.update(discussion, false);
944 }
945
946
948 socialActivityLocalService.deleteActivities(
949 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
950
951
953 dlService.updateFile(
954 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
955 newFileEntry.getGroupId(), fileEntry.getRepositoryId(),
956 newFileEntry.getRepositoryId(), name, newFileEntryId);
957
958 folderId = newFolderId;
959 fileEntry = newFileEntry;
960 }
961
962
964 String version = getNextVersion(
965 fileEntry, majorVersion, serviceContext.getWorkflowAction());
966
967 DLFileVersion fileVersion = null;
968
969 try {
970 DLFileVersion latestFileVersion =
971 dlFileVersionLocalService.getLatestFileVersion(
972 groupId, folderId, name);
973
974 if (size == 0) {
975 size = latestFileVersion.getSize();
976 }
977
978 if (latestFileVersion.getStatus() !=
979 WorkflowConstants.STATUS_APPROVED) {
980
981 updateFileVersion(
982 user, latestFileVersion,
983 serviceContext.getModifiedDate(now), version,
984 versionDescription, size, latestFileVersion.getStatus());
985 }
986 else if (is != null) {
987 fileVersion = addFileVersion(
988 user, fileEntry, serviceContext.getModifiedDate(now),
989 version, versionDescription, size,
990 WorkflowConstants.STATUS_DRAFT);
991 }
992 else {
993 version = fileEntry.getVersion();
994 }
995
996 if (fileVersion == null) {
997 fileVersion = latestFileVersion;
998 }
999 }
1000 catch (NoSuchFileVersionException nsfve) {
1001 fileVersion = addFileVersion(
1002 user, fileEntry, serviceContext.getModifiedDate(now), version,
1003 versionDescription, size, WorkflowConstants.STATUS_DRAFT);
1004 }
1005
1006 if ((is == null) && !version.equals(fileEntry.getVersion())) {
1007 int fetchFailures = 0;
1008
1009 while (is == null) {
1010 try {
1011 is = dlLocalService.getFileAsStream(
1012 user.getCompanyId(), fileEntry.getRepositoryId(), name);
1013 }
1014 catch (NoSuchFileException nsfe) {
1015 fetchFailures++;
1016
1017 if (PropsValues.DL_HOOK_IMPL.equals(
1018 JCRHook.class.getName()) &&
1019 (fetchFailures <
1020 PropsValues.DL_HOOK_JCR_FETCH_MAX_FAILURES)) {
1021
1022 try {
1023 Thread.sleep(PropsValues.DL_HOOK_JCR_FETCH_DELAY);
1024 }
1025 catch (InterruptedException ie) {
1026 }
1027 }
1028 else {
1029 throw nsfe;
1030 }
1031 }
1032 }
1033 }
1034
1035
1037 updateAsset(
1038 userId, fileEntry, fileVersion,
1039 serviceContext.getAssetCategoryIds(),
1040 serviceContext.getAssetTagNames());
1041
1042
1044 fileEntry.setVersionUserId(user.getUserId());
1045 fileEntry.setVersionUserName(user.getFullName());
1046 fileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
1047 fileEntry.setSize(size);
1048 fileEntry.setExpandoBridgeAttributes(serviceContext);
1049
1050 dlFileEntryPersistence.update(fileEntry, false);
1051
1052
1054 if (fileEntry.getFolderId() !=
1055 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1056
1057 DLFolder folder = dlFolderPersistence.findByPrimaryKey(
1058 fileEntry.getFolderId());
1059
1060 folder.setLastPostDate(fileEntry.getModifiedDate());
1061
1062 dlFolderPersistence.update(folder, false);
1063 }
1064
1065
1067 if (is != null) {
1068 try {
1069 dlService.deleteFile(
1070 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1071 fileEntry.getRepositoryId(), fileEntry.getName(), version);
1072 }
1073 catch (NoSuchFileException nsfe) {
1074 }
1075
1076 dlLocalService.updateFile(
1077 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1078 fileEntry.getGroupId(), fileEntry.getRepositoryId(), name,
1079 false, version, sourceFileName, fileEntry.getFileEntryId(),
1080 fileEntry.getLuceneProperties(), fileEntry.getModifiedDate(),
1081 serviceContext, is);
1082 }
1083
1084
1086 WorkflowHandlerRegistryUtil.startWorkflowInstance(
1087 user.getCompanyId(), groupId, userId, DLFileEntry.class.getName(),
1088 fileEntry.getFileEntryId(), fileEntry, serviceContext);
1089
1090 return fileEntry;
1091 }
1092
1093 public DLFileEntry updateStatus(
1094 long userId, long fileEntryId, int status,
1095 ServiceContext serviceContext)
1096 throws PortalException, SystemException {
1097
1098
1100 User user = userPersistence.findByPrimaryKey(userId);
1101
1102 DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
1103 fileEntryId);
1104
1105
1107 DLFileVersion latestFileVersion =
1108 dlFileVersionLocalService.getLatestFileVersion(
1109 fileEntry.getGroupId(), fileEntry.getFolderId(),
1110 fileEntry.getName());
1111
1112 int oldStatus = latestFileVersion.getStatus();
1113
1114 latestFileVersion.setStatus(status);
1115 latestFileVersion.setStatusByUserId(user.getUserId());
1116 latestFileVersion.setStatusByUserName(user.getFullName());
1117 latestFileVersion.setStatusDate(new Date());
1118
1119 dlFileVersionPersistence.update(latestFileVersion, false);
1120
1121 if (status == WorkflowConstants.STATUS_APPROVED) {
1122
1123
1125 if (DLUtil.compareVersions(
1126 fileEntry.getVersion(),
1127 latestFileVersion.getVersion()) < 0) {
1128
1129 fileEntry.setVersion(latestFileVersion.getVersion());
1130
1131 dlFileEntryPersistence.update(fileEntry, false);
1132 }
1133
1134
1136 if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1137 if ((oldStatus != WorkflowConstants.STATUS_APPROVED) &&
1138 (latestFileVersion.getVersion() !=
1139 DLFileEntryConstants.DEFAULT_VERSION)) {
1140
1141 AssetEntry draftAssetEntry = null;
1142
1143 try {
1144 draftAssetEntry = assetEntryLocalService.getEntry(
1145 DLFileEntry.class.getName(),
1146 latestFileVersion.getPrimaryKey());
1147
1148 long[] assetCategoryIds =
1149 draftAssetEntry.getCategoryIds();
1150 String[] assetTagNames = draftAssetEntry.getTagNames();
1151
1152 assetEntryLocalService.updateEntry(
1153 userId, fileEntry.getGroupId(),
1154 DLFileEntry.class.getName(),
1155 fileEntry.getFileEntryId(), assetCategoryIds,
1156 assetTagNames, true, null, null, null, null,
1157 draftAssetEntry.getMimeType(), fileEntry.getTitle(),
1158 fileEntry.getDescription(), null, null, 0, 0, null,
1159 false);
1160
1161 assetEntryLocalService.deleteEntry(
1162 draftAssetEntry.getEntryId());
1163 }
1164 catch (NoSuchEntryException nsee) {
1165 }
1166 }
1167
1168 assetEntryLocalService.updateVisible(
1169 DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1170 true);
1171 }
1172
1173
1175 socialActivityLocalService.addUniqueActivity(
1176 latestFileVersion.getUserId(), latestFileVersion.getGroupId(),
1177 latestFileVersion.getCreateDate(), DLFileEntry.class.getName(),
1178 fileEntryId, DLActivityKeys.ADD_FILE_ENTRY,
1179 StringPool.BLANK, 0);
1180
1181
1183 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
1184
1185 indexer.reindex(fileEntry);
1186 }
1187 else {
1188
1189
1191 if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1192 String newVersion = DLFileEntryConstants.DEFAULT_VERSION;
1193
1194 List<DLFileVersion> approvedFileVersions =
1195 dlFileVersionPersistence.findByG_F_N_S(
1196 fileEntry.getGroupId(), fileEntry.getFolderId(),
1197 fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
1198
1199 if (!approvedFileVersions.isEmpty()) {
1200 newVersion = approvedFileVersions.get(0).getVersion();
1201 }
1202
1203 fileEntry.setVersion(newVersion);
1204
1205 dlFileEntryPersistence.update(fileEntry, false);
1206 }
1207
1208
1210 if (Validator.isNull(fileEntry.getVersion())) {
1211 assetEntryLocalService.updateVisible(
1212 DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1213 false);
1214 }
1215
1216
1218 if (latestFileVersion.getVersion().equals(
1219 DLFileEntryConstants.DEFAULT_VERSION)) {
1220
1221 Indexer indexer = IndexerRegistryUtil.getIndexer(
1222 DLFileEntry.class);
1223
1224 indexer.delete(fileEntry);
1225 }
1226 }
1227
1228 return fileEntry;
1229 }
1230
1231 protected DLFileVersion addFileVersion(
1232 User user, DLFileEntry fileEntry, Date modifiedDate, String version,
1233 String description, long size, int status)
1234 throws SystemException {
1235
1236 long fileVersionId = counterLocalService.increment();
1237
1238 DLFileVersion fileVersion = dlFileVersionPersistence.create(
1239 fileVersionId);
1240
1241 long versionUserId = fileEntry.getVersionUserId();
1242
1243 if (versionUserId <= 0) {
1244 versionUserId = fileEntry.getUserId();
1245 }
1246
1247 String versionUserName = GetterUtil.getString(
1248 fileEntry.getVersionUserName(), fileEntry.getUserName());
1249
1250 fileVersion.setGroupId(fileEntry.getGroupId());
1251 fileVersion.setCompanyId(fileEntry.getCompanyId());
1252 fileVersion.setUserId(versionUserId);
1253 fileVersion.setUserName(versionUserName);
1254 fileVersion.setCreateDate(modifiedDate);
1255 fileVersion.setFolderId(fileEntry.getFolderId());
1256 fileVersion.setName(fileEntry.getName());
1257 fileVersion.setDescription(description);
1258 fileVersion.setVersion(version);
1259 fileVersion.setSize(size);
1260 fileVersion.setStatus(status);
1261 fileVersion.setStatusByUserId(user.getUserId());
1262 fileVersion.setStatusByUserName(user.getFullName());
1263 fileVersion.setStatusDate(fileEntry.getModifiedDate());
1264
1265 dlFileVersionPersistence.update(fileVersion, false);
1266
1267 return fileVersion;
1268 }
1269
1270 protected long getFolderId(long companyId, long folderId)
1271 throws SystemException {
1272
1273 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1274
1275
1277 DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
1278
1279 if ((folder == null) || (companyId != folder.getCompanyId())) {
1280 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1281 }
1282 }
1283
1284 return folderId;
1285 }
1286
1287 protected String getNextVersion(
1288 DLFileEntry fileEntry, boolean majorVersion, int workflowAction) {
1289
1290 if (Validator.isNull(fileEntry.getVersion())) {
1291 return DLFileEntryConstants.DEFAULT_VERSION;
1292 }
1293
1294 if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1295 majorVersion = false;
1296 }
1297
1298 int[] versionParts = StringUtil.split(
1299 fileEntry.getVersion(), StringPool.PERIOD, 0);
1300
1301 if (majorVersion) {
1302 versionParts[0]++;
1303 versionParts[1] = 0;
1304 }
1305 else {
1306 versionParts[1]++;
1307 }
1308
1309 return versionParts[0] + StringPool.PERIOD + versionParts[1];
1310 }
1311
1312 protected void updateFileVersion(
1313 User user, DLFileVersion fileVersion, Date modifiedDate,
1314 String version, String description, long size, int status)
1315 throws SystemException {
1316
1317 fileVersion.setDescription(description);
1318 fileVersion.setVersion(version);
1319 fileVersion.setSize(size);
1320 fileVersion.setStatus(status);
1321 fileVersion.setStatusByUserId(user.getUserId());
1322 fileVersion.setStatusByUserName(user.getFullName());
1323 fileVersion.setStatusDate(modifiedDate);
1324
1325 dlFileVersionPersistence.update(fileVersion, false);
1326 }
1327
1328 protected void validate(
1329 long groupId, long folderId, long newFolderId, String name,
1330 String title, String sourceFileName, InputStream is)
1331 throws PortalException, SystemException {
1332
1333 if (Validator.isNotNull(sourceFileName)) {
1334 dlLocalService.validate(title, sourceFileName, is);
1335 }
1336
1337 if (folderId != newFolderId) {
1338 folderId = newFolderId;
1339 }
1340
1341 validate(groupId, folderId, name, title);
1342 }
1343
1344 protected void validate(
1345 long groupId, long folderId, String title, InputStream is)
1346 throws PortalException, SystemException {
1347
1348 dlLocalService.validate(title, true, is);
1349
1350 validate(groupId, folderId, null, title);
1351 }
1352
1353 protected void validate(
1354 long groupId, long folderId, String name, String title)
1355 throws PortalException, SystemException {
1356
1357 try {
1358 dlFolderLocalService.getFolder(groupId, folderId, title);
1359
1360 throw new DuplicateFolderNameException();
1361 }
1362 catch (NoSuchFolderException nsfe) {
1363 }
1364
1365 try {
1366 DLFileEntry fileEntry =
1367 dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
1368
1369 if (!fileEntry.getName().equals(name)) {
1370 throw new DuplicateFileException(title);
1371 }
1372 }
1373 catch (NoSuchFileEntryException nsfee) {
1374 }
1375 }
1376
1377 private static Log _log = LogFactoryUtil.getLog(
1378 DLFileEntryLocalServiceImpl.class);
1379
1380}