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