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