1
22
23 package com.liferay.portlet.documentlibrary.service.impl;
24
25 import com.liferay.documentlibrary.DuplicateFileException;
26 import com.liferay.documentlibrary.FileSizeException;
27 import com.liferay.documentlibrary.NoSuchFileException;
28 import com.liferay.portal.PortalException;
29 import com.liferay.portal.SystemException;
30 import com.liferay.portal.kernel.log.Log;
31 import com.liferay.portal.kernel.log.LogFactoryUtil;
32 import com.liferay.portal.kernel.util.FileUtil;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.MathUtil;
35 import com.liferay.portal.kernel.util.MimeTypesUtil;
36 import com.liferay.portal.kernel.util.OrderByComparator;
37 import com.liferay.portal.kernel.util.StringPool;
38 import com.liferay.portal.kernel.util.Validator;
39 import com.liferay.portal.model.Resource;
40 import com.liferay.portal.model.ResourceConstants;
41 import com.liferay.portal.model.User;
42 import com.liferay.portal.util.PortalUtil;
43 import com.liferay.portal.util.PortletKeys;
44 import com.liferay.portal.util.PropsValues;
45 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
46 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
47 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
48 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
49 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
50 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
51 import com.liferay.portlet.documentlibrary.model.DLFolder;
52 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
53 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
54 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
55 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
56 import com.liferay.portlet.documentlibrary.util.comparator.FileEntryModifiedDateComparator;
57 import com.liferay.portlet.messageboards.model.MBDiscussion;
58 import com.liferay.portlet.ratings.model.RatingsEntry;
59 import com.liferay.portlet.ratings.model.RatingsStats;
60
61 import java.io.BufferedInputStream;
62 import java.io.ByteArrayInputStream;
63 import java.io.File;
64 import java.io.FileInputStream;
65 import java.io.FileNotFoundException;
66 import java.io.IOException;
67 import java.io.InputStream;
68
69 import java.rmi.RemoteException;
70
71 import java.util.ArrayList;
72 import java.util.Date;
73 import java.util.List;
74
75
90 public class DLFileEntryLocalServiceImpl
91 extends DLFileEntryLocalServiceBaseImpl {
92
93 public DLFileEntry addFileEntry(
94 long userId, long folderId, String name, String title,
95 String description, String[] tagsEntries, String extraSettings,
96 File file, boolean addCommunityPermissions,
97 boolean addGuestPermissions)
98 throws PortalException, SystemException {
99
100 return addFileEntry(
101 userId, folderId, name, title, description, tagsEntries,
102 extraSettings, file, Boolean.valueOf(addCommunityPermissions),
103 Boolean.valueOf(addGuestPermissions), null, null);
104 }
105
106 public DLFileEntry addFileEntry(
107 long userId, long folderId, String name, String title,
108 String description, String[] tagsEntries, String extraSettings,
109 byte[] bytes, boolean addCommunityPermissions,
110 boolean addGuestPermissions)
111 throws PortalException, SystemException {
112
113 return addFileEntry(
114 null, userId, folderId, name, title, description, tagsEntries,
115 extraSettings, bytes, Boolean.valueOf(addCommunityPermissions),
116 Boolean.valueOf(addGuestPermissions), null, null);
117 }
118
119 public DLFileEntry addFileEntry(
120 String uuid, long userId, long folderId, String name, String title,
121 String description, String[] tagsEntries, String extraSettings,
122 byte[] bytes, boolean addCommunityPermissions,
123 boolean addGuestPermissions)
124 throws PortalException, SystemException {
125
126 return addFileEntry(
127 uuid, userId, folderId, name, title, description, tagsEntries,
128 extraSettings, bytes, Boolean.valueOf(addCommunityPermissions),
129 Boolean.valueOf(addGuestPermissions), null, null);
130 }
131
132 public DLFileEntry addFileEntry(
133 long userId, long folderId, String name, String title,
134 String description, String[] tagsEntries, String extraSettings,
135 File file, String[] communityPermissions, String[] guestPermissions)
136 throws PortalException, SystemException {
137
138 return addFileEntry(
139 userId, folderId, name, title, description, tagsEntries,
140 extraSettings, file, null, null, communityPermissions,
141 guestPermissions);
142 }
143
144 public DLFileEntry addFileEntry(
145 long userId, long folderId, String name, String title,
146 String description, String[] tagsEntries, String extraSettings,
147 byte[] bytes, String[] communityPermissions,
148 String[] guestPermissions)
149 throws PortalException, SystemException {
150
151 return addFileEntry(
152 null, userId, folderId, name, title, description, tagsEntries,
153 extraSettings, bytes, null, null, communityPermissions,
154 guestPermissions);
155 }
156
157 public DLFileEntry addFileEntry(
158 long userId, long folderId, String name, String title,
159 String description, String[] tagsEntries, String extraSettings,
160 File file, Boolean addCommunityPermissions,
161 Boolean addGuestPermissions, String[] communityPermissions,
162 String[] guestPermissions)
163 throws PortalException, SystemException {
164
165 if (!PropsValues.WEBDAV_LITMUS) {
166 if (file == null) {
167 throw new FileSizeException();
168 }
169 }
170
171 InputStream is = null;
172
173 try {
174 is = new BufferedInputStream(new FileInputStream(file));
175
176 return addFileEntry(
177 null, userId, folderId, name, title, description, tagsEntries,
178 extraSettings, is, file.length(), addCommunityPermissions,
179 addGuestPermissions, communityPermissions, guestPermissions);
180 }
181 catch (FileNotFoundException fnfe) {
182 throw new FileSizeException();
183 }
184 finally {
185 try {
186 if (is != null) {
187 is.close();
188 }
189 }
190 catch (IOException ioe) {
191 _log.error(ioe);
192 }
193 }
194 }
195
196 public DLFileEntry addFileEntry(
197 String uuid, long userId, long folderId, String name, String title,
198 String description, String[] tagsEntries, String extraSettings,
199 byte[] bytes, Boolean addCommunityPermissions,
200 Boolean addGuestPermissions, String[] communityPermissions,
201 String[] guestPermissions)
202 throws PortalException, SystemException {
203
204 if (!PropsValues.WEBDAV_LITMUS) {
205 if ((bytes == null) || (bytes.length == 0)) {
206 throw new FileSizeException();
207 }
208 }
209
210 InputStream is = new ByteArrayInputStream(bytes);
211
212 return addFileEntry(
213 uuid, userId, folderId, name, title, description, tagsEntries,
214 extraSettings, is, bytes.length, addCommunityPermissions,
215 addGuestPermissions, communityPermissions, guestPermissions);
216 }
217
218 public DLFileEntry addFileEntry(
219 String uuid, long userId, long folderId, String name, String title,
220 String description, String[] tagsEntries, String extraSettings,
221 InputStream is, long size, Boolean addCommunityPermissions,
222 Boolean addGuestPermissions, String[] communityPermissions,
223 String[] guestPermissions)
224 throws PortalException, SystemException {
225
226
228 User user = userPersistence.findByPrimaryKey(userId);
229 folderId = getFolderId(user.getCompanyId(), folderId);
230 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
231 Date now = new Date();
232
233 if (Validator.isNull(title)) {
234 title = name;
235 }
236
237 name = getName(name);
238 title = DLFileEntryImpl.stripExtension(name, title);
239
240 validate(folder.getGroupId(), folderId, name, title, is);
241
242 long fileEntryId = counterLocalService.increment();
243
244 DLFileEntry fileEntry = dlFileEntryPersistence.create(fileEntryId);
245
246 fileEntry.setUuid(uuid);
247 fileEntry.setGroupId(folder.getGroupId());
248 fileEntry.setCompanyId(user.getCompanyId());
249 fileEntry.setUserId(user.getUserId());
250 fileEntry.setUserName(user.getFullName());
251 fileEntry.setVersionUserId(user.getUserId());
252 fileEntry.setVersionUserName(user.getFullName());
253 fileEntry.setCreateDate(now);
254 fileEntry.setModifiedDate(now);
255 fileEntry.setFolderId(folderId);
256 fileEntry.setName(name);
257 fileEntry.setTitle(title);
258 fileEntry.setDescription(description);
259 fileEntry.setVersion(DLFileEntryConstants.DEFAULT_VERSION);
260 fileEntry.setSize((int)size);
261 fileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
262 fileEntry.setExtraSettings(extraSettings);
263
264 dlFileEntryPersistence.update(fileEntry, false);
265
266
268 dlLocalService.addFile(
269 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
270 fileEntry.getGroupId(), folderId, name,
271 fileEntry.getLuceneProperties(), fileEntry.getModifiedDate(),
272 tagsEntries, is);
273
274
276 if ((addCommunityPermissions != null) &&
277 (addGuestPermissions != null)) {
278
279 addFileEntryResources(
280 fileEntry, addCommunityPermissions.booleanValue(),
281 addGuestPermissions.booleanValue());
282 }
283 else {
284 addFileEntryResources(
285 fileEntry, communityPermissions, guestPermissions);
286 }
287
288
290 if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
291 mbMessageLocalService.addDiscussionMessage(
292 userId, fileEntry.getUserName(), DLFileEntry.class.getName(),
293 fileEntryId);
294 }
295
296
298 socialActivityLocalService.addActivity(
299 userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
300 fileEntryId, DLActivityKeys.ADD_FILE_ENTRY, StringPool.BLANK, 0);
301
302
304 updateTagsAsset(userId, fileEntry, tagsEntries);
305
306
308 folder.setLastPostDate(fileEntry.getModifiedDate());
309
310 dlFolderPersistence.update(folder, false);
311
312 return fileEntry;
313 }
314
315 public void addFileEntryResources(
316 long fileEntryId, boolean addCommunityPermissions,
317 boolean addGuestPermissions)
318 throws PortalException, SystemException {
319
320 DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
321 fileEntryId);
322
323 addFileEntryResources(
324 fileEntry, addCommunityPermissions, addGuestPermissions);
325 }
326
327 public void addFileEntryResources(
328 DLFileEntry fileEntry, boolean addCommunityPermissions,
329 boolean addGuestPermissions)
330 throws PortalException, SystemException {
331
332 resourceLocalService.addResources(
333 fileEntry.getCompanyId(), fileEntry.getGroupId(),
334 fileEntry.getUserId(), DLFileEntry.class.getName(),
335 fileEntry.getFileEntryId(), false, addCommunityPermissions,
336 addGuestPermissions);
337 }
338
339 public void addFileEntryResources(
340 long fileEntryId, String[] communityPermissions,
341 String[] guestPermissions)
342 throws PortalException, SystemException {
343
344 DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
345 fileEntryId);
346
347 addFileEntryResources(
348 fileEntry, communityPermissions, guestPermissions);
349 }
350
351 public void addFileEntryResources(
352 DLFileEntry fileEntry, String[] communityPermissions,
353 String[] guestPermissions)
354 throws PortalException, SystemException {
355
356 resourceLocalService.addModelResources(
357 fileEntry.getCompanyId(), fileEntry.getGroupId(),
358 fileEntry.getUserId(), DLFileEntry.class.getName(),
359 fileEntry.getFileEntryId(), communityPermissions, guestPermissions);
360 }
361
362 public DLFileEntry addOrOverwriteFileEntry(
363 long userId, long folderId, String name, String sourceName,
364 String title, String description, String[] tagsEntries,
365 String extraSettings, File file, boolean addCommunityPermissions,
366 boolean addGuestPermissions)
367 throws PortalException, SystemException {
368
369 boolean update = false;
370
371 String extension = FileUtil.getExtension(name);
372
373 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
374 folderId, title);
375
376 for (DLFileEntry fileEntry : fileEntries) {
377 String curExtension = FileUtil.getExtension(fileEntry.getName());
378
379 if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
380 if (Validator.isNull(curExtension)) {
381 update = true;
382
383 name = fileEntry.getName();
384
385 break;
386 }
387 }
388 else if (extension.equals(curExtension)) {
389 update = true;
390
391 break;
392 }
393 }
394
395 if (update) {
396 return updateFileEntry(
397 userId, folderId, folderId, name, sourceName, title,
398 description, tagsEntries, extraSettings, file);
399 }
400 else {
401 return addFileEntry(
402 userId, folderId, name, title, description, tagsEntries,
403 extraSettings, file, addCommunityPermissions,
404 addGuestPermissions);
405 }
406 }
407
408 public void deleteFileEntries(long folderId)
409 throws PortalException, SystemException {
410
411 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByFolderId(
412 folderId);
413
414 for (DLFileEntry fileEntry : fileEntries) {
415 deleteFileEntry(fileEntry);
416 }
417 }
418
419 public void deleteFileEntry(long folderId, String name)
420 throws PortalException, SystemException {
421
422 deleteFileEntry(folderId, name, -1);
423 }
424
425 public void deleteFileEntry(long folderId, String name, double version)
426 throws PortalException, SystemException {
427
428 DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
429 folderId, name);
430
431 if (version > 0) {
432 try {
433 dlService.deleteFile(
434 fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
435 fileEntry.getFolderId(), fileEntry.getName(), version);
436 }
437 catch (Exception e) {
438 if (_log.isWarnEnabled()) {
439 _log.warn(e, e);
440 }
441 }
442
443 dlFileVersionPersistence.removeByF_N_V(folderId, name, version);
444 }
445 else {
446 deleteFileEntry(fileEntry);
447 }
448 }
449
450 public void deleteFileEntry(DLFileEntry fileEntry)
451 throws PortalException, SystemException {
452
453
455 try {
456 dlService.deleteFile(
457 fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
458 fileEntry.getFolderId(), fileEntry.getName());
459 }
460 catch (Exception e) {
461 if (_log.isWarnEnabled()) {
462 _log.warn(e, e);
463 }
464 }
465
466
468 dlFileRankLocalService.deleteFileRanks(
469 fileEntry.getFolderId(), fileEntry.getName());
470
471
473 dlFileShortcutLocalService.deleteFileShortcuts(
474 fileEntry.getFolderId(), fileEntry.getName());
475
476
478 List<DLFileVersion> fileVersions = dlFileVersionPersistence.findByF_N(
479 fileEntry.getFolderId(), fileEntry.getName());
480
481 for (DLFileVersion fileVersion : fileVersions) {
482 dlFileVersionPersistence.remove(fileVersion);
483 }
484
485
487 tagsAssetLocalService.deleteAsset(
488 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
489
490
492 socialActivityLocalService.deleteActivities(
493 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
494
495
497 ratingsStatsLocalService.deleteStats(
498 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
499
500
502 mbMessageLocalService.deleteDiscussionMessages(
503 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
504
505
507 webDAVPropsLocalService.deleteWebDAVProps(
508 DLFileEntry.class.getName(), fileEntry.getPrimaryKey());
509
510
512 resourceLocalService.deleteResource(
513 fileEntry.getCompanyId(), DLFileEntry.class.getName(),
514 ResourceConstants.SCOPE_INDIVIDUAL, fileEntry.getFileEntryId());
515
516
518 dlFileEntryPersistence.remove(fileEntry);
519 }
520
521 public List<DLFileEntry> getCompanyFileEntries(
522 long companyId, int start, int end)
523 throws SystemException {
524
525 return dlFileEntryPersistence.findByCompanyId(companyId, start, end);
526 }
527
528 public List<DLFileEntry> getCompanyFileEntries(
529 long companyId, int start, int end, OrderByComparator obc)
530 throws SystemException {
531
532 return dlFileEntryPersistence.findByCompanyId(
533 companyId, start, end, obc);
534 }
535
536 public int getCompanyFileEntriesCount(long companyId)
537 throws SystemException {
538
539 return dlFileEntryPersistence.countByCompanyId(companyId);
540 }
541
542 public InputStream getFileAsStream(
543 long companyId, long userId, long folderId, String name)
544 throws PortalException, SystemException {
545
546 return getFileAsStream(companyId, userId, folderId, name, 0);
547 }
548
549 public InputStream getFileAsStream(
550 long companyId, long userId, long folderId, String name,
551 double version)
552 throws PortalException, SystemException {
553
554 DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
555 folderId, name);
556
557 if (userId > 0) {
558 dlFileRankLocalService.updateFileRank(
559 fileEntry.getGroupId(), companyId, userId, folderId, name);
560 }
561
562 fileEntry.setReadCount(fileEntry.getReadCount() + 1);
563
564 dlFileEntryPersistence.update(fileEntry, false);
565
566 tagsAssetLocalService.incrementViewCounter(
567 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
568
569 if ((version > 0) && (fileEntry.getVersion() != version)) {
570 return dlLocalService.getFileAsStream(
571 companyId, folderId, name, version);
572 }
573 else {
574 return dlLocalService.getFileAsStream(companyId, folderId, name);
575 }
576 }
577
578 public List<DLFileEntry> getFileEntries(long folderId)
579 throws SystemException {
580
581 return dlFileEntryPersistence.findByFolderId(folderId);
582 }
583
584 public List<DLFileEntry> getFileEntries(long folderId, int start, int end)
585 throws SystemException {
586
587 return dlFileEntryPersistence.findByFolderId(folderId, start, end);
588 }
589
590 public List<DLFileEntry> getFileEntries(
591 long folderId, int start, int end, OrderByComparator obc)
592 throws SystemException {
593
594 return dlFileEntryPersistence.findByFolderId(folderId, start, end, obc);
595 }
596
597 public List<Object> getFileEntriesAndShortcuts(
598 long folderId, int start, int end)
599 throws SystemException {
600
601 List<Long> folderIds = new ArrayList<Long>();
602
603 folderIds.add(folderId);
604
605 return dlFileEntryAndShortcutFinder.findByFolderIds(
606 folderIds, start, end);
607 }
608
609 public List<Object> getFileEntriesAndShortcuts(
610 List<Long> folderIds, int start, int end)
611 throws SystemException {
612
613 return dlFileEntryAndShortcutFinder.findByFolderIds(
614 folderIds, start, end);
615 }
616
617 public int getFileEntriesAndShortcutsCount(long folderId)
618 throws SystemException {
619
620 List<Long> folderIds = new ArrayList<Long>();
621
622 folderIds.add(folderId);
623
624 return dlFileEntryAndShortcutFinder.countByFolderIds(folderIds);
625 }
626
627 public int getFileEntriesAndShortcutsCount(List<Long> folderIds)
628 throws SystemException {
629
630 return dlFileEntryAndShortcutFinder.countByFolderIds(folderIds);
631 }
632
633 public int getFileEntriesCount(long folderId) throws SystemException {
634 return dlFileEntryPersistence.countByFolderId(folderId);
635 }
636
637 public DLFileEntry getFileEntry(long fileEntryId)
638 throws PortalException, SystemException {
639
640 return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
641 }
642
643 public DLFileEntry getFileEntry(long folderId, String name)
644 throws PortalException, SystemException {
645
646 return dlFileEntryPersistence.findByF_N(folderId, name);
647 }
648
649 public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
650 throws PortalException, SystemException {
651
652 return dlFileEntryPersistence.findByUUID_G(uuid, groupId);
653 }
654
655 public DLFileEntry getFileEntryByTitle(
656 long folderId, String titleWithExtension)
657 throws PortalException, SystemException {
658
659 String title = DLFileEntryImpl.stripExtension(
660 titleWithExtension, titleWithExtension);
661 String extension = FileUtil.getExtension(titleWithExtension);
662
663 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
664 folderId, title);
665
666 for (DLFileEntry fileEntry : fileEntries) {
667 String curExtension = FileUtil.getExtension(fileEntry.getName());
668
669 if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
670 if (Validator.isNull(curExtension)) {
671 return fileEntry;
672 }
673 }
674 else if (extension.equals(curExtension)) {
675 return fileEntry;
676 }
677 }
678
679 throw new NoSuchFileEntryException();
680 }
681
682 public int getFoldersFileEntriesCount(List<Long> folderIds)
683 throws SystemException {
684
685 return dlFileEntryFinder.countByFolderIds(folderIds);
686 }
687
688 public List<DLFileEntry> getGroupFileEntries(
689 long groupId, int start, int end)
690 throws SystemException {
691
692 return getGroupFileEntries(
693 groupId, start, end, new FileEntryModifiedDateComparator());
694 }
695
696 public List<DLFileEntry> getGroupFileEntries(
697 long groupId, int start, int end, OrderByComparator obc)
698 throws SystemException {
699
700 return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
701 }
702
703 public List<DLFileEntry> getGroupFileEntries(
704 long groupId, long userId, int start, int end)
705 throws SystemException {
706
707 return getGroupFileEntries(
708 groupId, userId, start, end, new FileEntryModifiedDateComparator());
709 }
710
711 public List<DLFileEntry> getGroupFileEntries(
712 long groupId, long userId, int start, int end,
713 OrderByComparator obc)
714 throws SystemException {
715
716 if (userId <= 0) {
717 return dlFileEntryPersistence.findByGroupId(
718 groupId, start, end, obc);
719 }
720 else {
721 return dlFileEntryPersistence.findByG_U(
722 groupId, userId, start, end, obc);
723 }
724 }
725
726 public int getGroupFileEntriesCount(long groupId) throws SystemException {
727 return dlFileEntryPersistence.countByGroupId(groupId);
728 }
729
730 public int getGroupFileEntriesCount(long groupId, long userId)
731 throws SystemException {
732
733 if (userId <= 0) {
734 return dlFileEntryPersistence.countByGroupId(groupId);
735 }
736 else {
737 return dlFileEntryPersistence.countByG_U(groupId, userId);
738 }
739 }
740
741 public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
742 return dlFileEntryFinder.findByNoAssets();
743 }
744
745 public DLFileEntry updateFileEntry(
746 long userId, long folderId, long newFolderId, String name,
747 String sourceFileName, String title, String description,
748 String[] tagsEntries, String extraSettings, File file)
749 throws PortalException, SystemException {
750
751 InputStream is = null;
752
753 try {
754 long size = 0;
755
756 if ((file != null) && (file.length() > 0)) {
757 is = new BufferedInputStream(new FileInputStream(file));
758 size = file.length();
759 }
760
761 return updateFileEntry(
762 userId, folderId, newFolderId, name, sourceFileName, title,
763 description, tagsEntries, extraSettings, is, size);
764 }
765 catch (FileNotFoundException fnfe) {
766 throw new NoSuchFileException();
767 }
768 finally {
769 try {
770 if (is != null) {
771 is.close();
772 }
773 }
774 catch (IOException ioe) {
775 _log.error(ioe);
776 }
777 }
778 }
779
780 public DLFileEntry updateFileEntry(
781 long userId, long folderId, long newFolderId, String name,
782 String sourceFileName, String title, String description,
783 String[] tagsEntries, String extraSettings, byte[] bytes)
784 throws PortalException, SystemException {
785
786 InputStream is = null;
787 long size = 0;
788
789 if ((bytes != null) && (bytes.length > 0)) {
790 is = new ByteArrayInputStream(bytes);
791 size = bytes.length;
792 }
793
794 return updateFileEntry(
795 userId, folderId, newFolderId, name, sourceFileName, title,
796 description, tagsEntries, extraSettings, is, size);
797 }
798
799 public DLFileEntry updateFileEntry(
800 long userId, long folderId, long newFolderId, String name,
801 String sourceFileName, String title, String description,
802 String[] tagsEntries, String extraSettings, InputStream is,
803 long size)
804 throws PortalException, SystemException {
805
806
808 User user = userPersistence.findByPrimaryKey(userId);
809 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
810
811 if (Validator.isNull(title)) {
812 title = sourceFileName;
813
814 if (Validator.isNull(title)) {
815 title = name;
816 }
817 }
818
819 title = DLFileEntryImpl.stripExtension(name, title);
820
821 validate(
822 folder.getGroupId(), folderId, newFolderId, name, title,
823 sourceFileName, is);
824
825 DLFileEntry fileEntry = dlFileEntryPersistence.findByF_N(
826 folderId, name);
827
828 fileEntry.setTitle(title);
829 fileEntry.setDescription(description);
830 fileEntry.setExtraSettings(extraSettings);
831
832 dlFileEntryPersistence.update(fileEntry, false);
833
834
836 if ((newFolderId > 0) && (folderId != newFolderId)) {
837 long oldFileEntryId = fileEntry.getFileEntryId();
838
839 DLFolder newFolder = dlFolderPersistence.findByPrimaryKey(
840 newFolderId);
841
842 if (folder.getGroupId() != newFolder.getGroupId()) {
843 throw new NoSuchFolderException();
844 }
845
846 if (dlLocalService.hasFile(
847 user.getCompanyId(), newFolderId, name, 0)) {
848
849 throw new DuplicateFileException(name);
850 }
851
852 long newFileEntryId = counterLocalService.increment();
853
854 DLFileEntry newFileEntry = dlFileEntryPersistence.create(
855 newFileEntryId);
856
857 newFileEntry.setGroupId(fileEntry.getGroupId());
858 newFileEntry.setCompanyId(fileEntry.getCompanyId());
859 newFileEntry.setUserId(fileEntry.getUserId());
860 newFileEntry.setUserName(fileEntry.getUserName());
861 newFileEntry.setVersionUserId(fileEntry.getVersionUserId());
862 newFileEntry.setVersionUserName(fileEntry.getVersionUserName());
863 newFileEntry.setCreateDate(fileEntry.getCreateDate());
864 newFileEntry.setModifiedDate(fileEntry.getModifiedDate());
865 newFileEntry.setFolderId(newFolderId);
866 newFileEntry.setName(name);
867 newFileEntry.setTitle(fileEntry.getTitle());
868 newFileEntry.setDescription(fileEntry.getDescription());
869 newFileEntry.setVersion(fileEntry.getVersion());
870 newFileEntry.setSize(fileEntry.getSize());
871 newFileEntry.setReadCount(fileEntry.getReadCount());
872 newFileEntry.setExtraSettings(extraSettings);
873
874 dlFileEntryPersistence.update(newFileEntry, false);
875
876 dlFileEntryPersistence.remove(fileEntry);
877
878 List<DLFileVersion> fileVersions =
879 dlFileVersionPersistence.findByF_N(folderId, name);
880
881 for (DLFileVersion fileVersion : fileVersions) {
882 long newFileVersionId = counterLocalService.increment();
883
884 DLFileVersion newFileVersion = dlFileVersionPersistence.create(
885 newFileVersionId);
886
887 newFileVersion.setGroupId(fileVersion.getGroupId());
888 newFileVersion.setCompanyId(fileVersion.getCompanyId());
889 newFileVersion.setUserId(fileVersion.getUserId());
890 newFileVersion.setUserName(fileVersion.getUserName());
891 newFileVersion.setCreateDate(fileVersion.getCreateDate());
892 newFileVersion.setFolderId(newFolderId);
893 newFileVersion.setName(name);
894 newFileVersion.setVersion(fileVersion.getVersion());
895 newFileVersion.setSize(fileVersion.getSize());
896
897 dlFileVersionPersistence.update(newFileVersion, false);
898
899 dlFileVersionPersistence.remove(fileVersion);
900 }
901
902 dlFileShortcutLocalService.updateFileShortcuts(
903 folderId, name, newFolderId, name);
904
905
907 Resource resource = resourceLocalService.getResource(
908 fileEntry.getCompanyId(), DLFileEntry.class.getName(),
909 ResourceConstants.SCOPE_INDIVIDUAL,
910 String.valueOf(fileEntry.getPrimaryKey()));
911
912 resource.setPrimKey(String.valueOf(newFileEntryId));
913
914 resourcePersistence.update(resource, false);
915
916
918 try {
919 dlService.updateFile(
920 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
921 newFileEntry.getGroupId(), folderId, newFolderId, name);
922 }
923 catch (RemoteException re) {
924 throw new SystemException(re);
925 }
926
927
929 long classNameId = PortalUtil.getClassNameId(
930 DLFileEntry.class.getName());
931
932 MBDiscussion discussion = mbDiscussionPersistence.fetchByC_C(
933 classNameId, oldFileEntryId);
934
935 if (discussion != null) {
936 discussion.setClassPK(newFileEntryId);
937
938 mbDiscussionPersistence.update(discussion, false);
939 }
940
941
943 RatingsStats stats = ratingsStatsLocalService.getStats(
944 DLFileEntry.class.getName(), oldFileEntryId);
945
946 stats.setClassPK(newFileEntryId);
947
948 ratingsStatsPersistence.update(stats, false);
949
950 List<RatingsEntry> entries = ratingsEntryPersistence.findByC_C(
951 classNameId, oldFileEntryId);
952
953 for (RatingsEntry entry : entries) {
954 entry.setClassPK(newFileEntryId);
955
956 ratingsEntryPersistence.update(entry, false);
957 }
958
959
961 socialActivityLocalService.deleteActivities(
962 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
963
964
966 tagsAssetLocalService.deleteAsset(
967 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
968
969 folderId = newFolderId;
970 folder = newFolder;
971 fileEntry = newFileEntry;
972 }
973
974
976 socialActivityLocalService.addActivity(
977 userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
978 fileEntry.getFileEntryId(), DLActivityKeys.UPDATE_FILE_ENTRY,
979 StringPool.BLANK, 0);
980
981
983 updateTagsAsset(userId, fileEntry, tagsEntries);
984
985
987 double oldVersion = fileEntry.getVersion();
988 double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);
989
990 if (is == null) {
991 fileEntry.setVersion(newVersion);
992
993 dlFileEntryPersistence.update(fileEntry, false);
994
995 is = dlLocalService.getFileAsStream(
996 user.getCompanyId(), folderId, name);
997
998 dlLocalService.updateFile(
999 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1000 fileEntry.getGroupId(), folderId, name, newVersion, name,
1001 fileEntry.getLuceneProperties(), fileEntry.getModifiedDate(),
1002 tagsEntries, is);
1003
1004 return fileEntry;
1005 }
1006
1007 long fileVersionId = counterLocalService.increment();
1008
1009 DLFileVersion fileVersion = dlFileVersionPersistence.create(
1010 fileVersionId);
1011
1012 long versionUserId = fileEntry.getVersionUserId();
1013
1014 if (versionUserId <= 0) {
1015 versionUserId = fileEntry.getUserId();
1016 }
1017
1018 String versionUserName = GetterUtil.getString(
1019 fileEntry.getVersionUserName(), fileEntry.getUserName());
1020
1021 fileVersion.setGroupId(fileEntry.getGroupId());
1022 fileVersion.setCompanyId(fileEntry.getCompanyId());
1023 fileVersion.setUserId(versionUserId);
1024 fileVersion.setUserName(versionUserName);
1025 fileVersion.setCreateDate(fileEntry.getModifiedDate());
1026 fileVersion.setFolderId(folderId);
1027 fileVersion.setName(name);
1028 fileVersion.setVersion(oldVersion);
1029 fileVersion.setSize(fileEntry.getSize());
1030
1031 dlFileVersionPersistence.update(fileVersion, false);
1032
1033
1035 fileEntry.setVersionUserId(user.getUserId());
1036 fileEntry.setVersionUserName(user.getFullName());
1037 fileEntry.setModifiedDate(new Date());
1038 fileEntry.setVersion(newVersion);
1039 fileEntry.setSize((int)size);
1040
1041 dlFileEntryPersistence.update(fileEntry, false);
1042
1043
1045 dlLocalService.updateFile(
1046 user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1047 fileEntry.getGroupId(), folderId, name, newVersion, sourceFileName,
1048 fileEntry.getLuceneProperties(), fileEntry.getModifiedDate(),
1049 tagsEntries, is);
1050
1051
1053 folder.setLastPostDate(fileEntry.getModifiedDate());
1054
1055 dlFolderPersistence.update(folder, false);
1056
1057 return fileEntry;
1058 }
1059
1060 public void updateTagsAsset(
1061 long userId, DLFileEntry fileEntry, String[] tagsEntries)
1062 throws PortalException, SystemException {
1063
1064 String mimeType = MimeTypesUtil.getContentType(fileEntry.getName());
1065
1066 tagsAssetLocalService.updateAsset(
1067 userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
1068 fileEntry.getFileEntryId(), tagsEntries, null, null, null, null,
1069 mimeType, fileEntry.getTitle(), fileEntry.getDescription(), null,
1070 null, 0, 0, null, false);
1071 }
1072
1073 protected long getFolderId(long companyId, long folderId)
1074 throws SystemException {
1075
1076 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1077
1078
1080 DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
1081
1082 if ((folder == null) || (companyId != folder.getCompanyId())) {
1083 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1084 }
1085 }
1086
1087 return folderId;
1088 }
1089
1090 protected String getName(String name) throws SystemException {
1091 String extension = StringPool.BLANK;
1092
1093 int pos = name.lastIndexOf(StringPool.PERIOD);
1094
1095 if (pos != -1) {
1096 extension = name.substring(pos + 1, name.length()).toLowerCase();
1097 }
1098
1099 name = String.valueOf(counterLocalService.increment(
1100 DLFileEntry.class.getName()));
1101
1102 if (Validator.isNotNull(extension)) {
1103 name = "DLFE-" + name + StringPool.PERIOD + extension;
1104 }
1105
1106 return name;
1107 }
1108
1109 protected void validate(
1110 long groupId, long folderId, long newFolderId, String name,
1111 String title, String sourceFileName, InputStream is)
1112 throws PortalException, SystemException {
1113
1114 if (Validator.isNotNull(sourceFileName)) {
1115 dlLocalService.validate(name, sourceFileName, is);
1116 }
1117
1118 if (newFolderId > 0 && (folderId != newFolderId)) {
1119 folderId = newFolderId;
1120 }
1121
1122 String extension = FileUtil.getExtension(name);
1123
1124 try {
1125 String titleWithExtension = title;
1126
1127 if (Validator.isNotNull(extension)) {
1128 titleWithExtension += StringPool.PERIOD + extension;
1129 }
1130
1131 dlFolderLocalService.getFolder(
1132 groupId, folderId, titleWithExtension);
1133
1134 throw new DuplicateFolderNameException();
1135 }
1136 catch (NoSuchFolderException nsfe) {
1137 }
1138
1139 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
1140 folderId, title);
1141
1142 for (DLFileEntry fileEntry : fileEntries) {
1143 if (!name.equals(fileEntry.getName())) {
1144 String curExtension = FileUtil.getExtension(
1145 fileEntry.getName());
1146
1147 if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
1148 if (Validator.isNull(curExtension)) {
1149 throw new DuplicateFileException(
1150 fileEntry.getTitleWithExtension());
1151 }
1152 }
1153 else if (extension.equals(curExtension)) {
1154 throw new DuplicateFileException(
1155 fileEntry.getTitleWithExtension());
1156 }
1157 }
1158 }
1159 }
1160
1161 protected void validate(
1162 long groupId, long folderId, String name, String title,
1163 InputStream is)
1164 throws PortalException, SystemException {
1165
1166 dlLocalService.validate(name, is);
1167
1168 String extension = FileUtil.getExtension(name);
1169
1170 try {
1171 String titleWithExtension = title;
1172
1173 if (Validator.isNotNull(extension)) {
1174 titleWithExtension += StringPool.PERIOD + extension;
1175 }
1176
1177 dlFolderLocalService.getFolder(
1178 groupId, folderId, titleWithExtension);
1179
1180 throw new DuplicateFolderNameException();
1181 }
1182 catch (NoSuchFolderException nsfe) {
1183 }
1184
1185 List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
1186 folderId, title);
1187
1188 for (DLFileEntry fileEntry : fileEntries) {
1189 String curExtension = FileUtil.getExtension(fileEntry.getName());
1190
1191 if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
1192 if (Validator.isNull(curExtension)) {
1193 throw new DuplicateFileException(
1194 fileEntry.getTitleWithExtension());
1195 }
1196 }
1197 else if (extension.equals(curExtension)) {
1198 throw new DuplicateFileException(
1199 fileEntry.getTitleWithExtension());
1200 }
1201 }
1202 }
1203
1204 private static Log _log =
1205 LogFactoryUtil.getLog(DLFileEntryLocalServiceImpl.class);
1206
1207}