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