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.GetterUtil;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.ResourceConstants;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.util.MimeTypesUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portal.util.PropsValues;
39  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
40  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
41  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
42  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
43  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
44  import com.liferay.portlet.documentlibrary.model.DLFolder;
45  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
46  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
47  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
48  import com.liferay.util.FileUtil;
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[] byteArray, boolean addCommunityPermissions,
103             boolean addGuestPermissions)
104         throws PortalException, SystemException {
105 
106         return addFileEntry(
107             null, userId, folderId, name, title, description, tagsEntries,
108             extraSettings, byteArray, 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[] byteArray, boolean addCommunityPermissions,
116             boolean addGuestPermissions)
117         throws PortalException, SystemException {
118 
119         return addFileEntry(
120             uuid, userId, folderId, name, title, description, tagsEntries,
121             extraSettings, byteArray, 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[] byteArray, String[] communityPermissions,
141             String[] guestPermissions)
142         throws PortalException, SystemException {
143 
144         return addFileEntry(
145             null, userId, folderId, name, title, description, tagsEntries,
146             extraSettings, byteArray, 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[] byteArray, Boolean addCommunityPermissions,
191             Boolean addGuestPermissions, String[] communityPermissions,
192             String[] guestPermissions)
193         throws PortalException, SystemException {
194 
195         if ((byteArray == null) || (byteArray.length == 0)) {
196             throw new FileSizeException();
197         }
198 
199         InputStream is = new ByteArrayInputStream(byteArray);
200 
201         return addFileEntry(
202             uuid, userId, folderId, name, title, description, tagsEntries,
203             extraSettings, is, byteArray.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 begin, int end)
493         throws SystemException {
494 
495         return dlFileEntryPersistence.findByCompanyId(companyId, begin, end);
496     }
497 
498     public List<DLFileEntry> getCompanyFileEntries(
499             long companyId, int begin, int end, OrderByComparator obc)
500         throws SystemException {
501 
502         return dlFileEntryPersistence.findByCompanyId(
503             companyId, begin, 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 begin, int end)
554         throws SystemException {
555 
556         return dlFileEntryPersistence.findByFolderId(folderId, begin, end);
557     }
558 
559     public List<DLFileEntry> getFileEntries(
560             long folderId, int begin, int end, OrderByComparator obc)
561         throws SystemException {
562 
563         return dlFileEntryPersistence.findByFolderId(folderId, begin, end, obc);
564     }
565 
566     public List<DLFileEntry> getFileEntriesAndShortcuts(
567             long folderId, int begin, 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, begin, end);
576     }
577 
578     public List<DLFileEntry> getFileEntriesAndShortcuts(
579             List<Long> folderIds, int begin, int end)
580         throws SystemException {
581 
582         return dlFileEntryAndShortcutFinder.findByFolderIds(
583             folderIds, begin, 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 begin, int end)
659         throws SystemException {
660 
661         return dlFileEntryFinder.findByGroupId(groupId, begin, end);
662     }
663 
664     public List<DLFileEntry> getGroupFileEntries(
665             long groupId, int begin, int end, OrderByComparator obc)
666         throws SystemException {
667 
668         return dlFileEntryFinder.findByGroupId(groupId, begin, end, obc);
669     }
670 
671     public List<DLFileEntry> getGroupFileEntries(
672             long groupId, long userId, int begin, int end)
673         throws SystemException {
674 
675         if (userId <= 0) {
676             return dlFileEntryFinder.findByGroupId(groupId, begin, end);
677         }
678         else {
679             return dlFileEntryFinder.findByG_U(groupId, userId, begin, end);
680         }
681     }
682 
683     public List<DLFileEntry> getGroupFileEntries(
684             long groupId, long userId, int begin, int end,
685             OrderByComparator obc)
686         throws SystemException {
687 
688         if (userId <= 0) {
689             return dlFileEntryFinder.findByGroupId(groupId, begin, end, obc);
690         }
691         else {
692             return dlFileEntryFinder.findByG_U(
693                 groupId, userId, begin, 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[] byteArray)
755         throws PortalException, SystemException {
756 
757         InputStream is = null;
758         long size = 0;
759 
760         if ((byteArray != null) && (byteArray.length > 0)) {
761             is = new ByteArrayInputStream(byteArray);
762             size = byteArray.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         if (is == null) {
894             return fileEntry;
895         }
896 
897         double oldVersion = fileEntry.getVersion();
898         double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);
899 
900         long fileVersionId = counterLocalService.increment();
901 
902         DLFileVersion fileVersion = dlFileVersionPersistence.create(
903             fileVersionId);
904 
905         long versionUserId = fileEntry.getVersionUserId();
906 
907         if (versionUserId <= 0) {
908             versionUserId = fileEntry.getUserId();
909         }
910 
911         String versionUserName = GetterUtil.getString(
912             fileEntry.getVersionUserName(), fileEntry.getUserName());
913 
914         fileVersion.setCompanyId(fileEntry.getCompanyId());
915         fileVersion.setUserId(versionUserId);
916         fileVersion.setUserName(versionUserName);
917         fileVersion.setCreateDate(fileEntry.getModifiedDate());
918         fileVersion.setFolderId(folderId);
919         fileVersion.setName(name);
920         fileVersion.setVersion(oldVersion);
921         fileVersion.setSize(fileEntry.getSize());
922 
923         dlFileVersionPersistence.update(fileVersion, false);
924 
925         // File entry
926 
927         fileEntry.setVersionUserId(user.getUserId());
928         fileEntry.setVersionUserName(user.getFullName());
929         fileEntry.setModifiedDate(new Date());
930         fileEntry.setVersion(newVersion);
931         fileEntry.setSize((int)size);
932 
933         dlFileEntryPersistence.update(fileEntry, false);
934 
935         // File
936 
937         dlLocalService.updateFile(
938             user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
939             folder.getGroupId(), folderId, name, newVersion, sourceFileName,
940             fileEntry.getLuceneProperties(), tagsEntries, is);
941 
942         // Folder
943 
944         folder.setLastPostDate(fileEntry.getModifiedDate());
945 
946         dlFolderPersistence.update(folder, false);
947 
948         return fileEntry;
949     }
950 
951     public void updateTagsAsset(
952             long userId, DLFileEntry fileEntry, String[] tagsEntries)
953         throws PortalException, SystemException {
954 
955         String mimeType = MimeTypesUtil.getContentType(fileEntry.getName());
956 
957         tagsAssetLocalService.updateAsset(
958             userId, fileEntry.getFolder().getGroupId(),
959             DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
960             tagsEntries, null, null, null, null, mimeType, fileEntry.getTitle(),
961             fileEntry.getDescription(), null, null, 0, 0, null, false);
962     }
963 
964     protected long getFolderId(long companyId, long folderId)
965         throws PortalException, SystemException {
966 
967         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
968 
969             // Ensure folder exists and belongs to the proper company
970 
971             try {
972                 DLFolder folder = dlFolderPersistence.findByPrimaryKey(
973                     folderId);
974 
975                 if (companyId != folder.getCompanyId()) {
976                     folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
977                 }
978             }
979             catch (NoSuchFolderException nsfe) {
980                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
981             }
982         }
983 
984         return folderId;
985     }
986 
987     protected String getName(String name) throws SystemException {
988         String extension = StringPool.BLANK;
989 
990         int pos = name.lastIndexOf(StringPool.PERIOD);
991 
992         if (pos != -1) {
993             extension = name.substring(pos + 1, name.length()).toLowerCase();
994         }
995 
996         name = String.valueOf(counterLocalService.increment(
997             DLFileEntry.class.getName()));
998 
999         if (Validator.isNotNull(extension)) {
1000            name = "DLFE-" + name + StringPool.PERIOD + extension;
1001        }
1002
1003        return name;
1004    }
1005
1006    protected void validate(
1007            long groupId, long folderId, long newFolderId, String name,
1008            String title, String sourceFileName, InputStream is)
1009        throws PortalException, SystemException {
1010
1011        if (Validator.isNotNull(sourceFileName)) {
1012            dlLocalService.validate(name, sourceFileName, is);
1013        }
1014
1015        if (newFolderId > 0 && (folderId != newFolderId)) {
1016            folderId = newFolderId;
1017        }
1018
1019        String extension = FileUtil.getExtension(name);
1020
1021        try {
1022            String titleWithException = title;
1023
1024            if (Validator.isNotNull(extension)) {
1025                titleWithException += StringPool.PERIOD + extension;
1026            }
1027
1028            dlFolderLocalService.getFolder(
1029                groupId, newFolderId, titleWithException);
1030
1031            throw new DuplicateFolderNameException();
1032        }
1033        catch (NoSuchFolderException nsfe) {
1034        }
1035
1036        List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
1037            folderId, title);
1038
1039        for (DLFileEntry fileEntry : fileEntries) {
1040            if (!name.equals(fileEntry.getName())) {
1041                String curExtension = FileUtil.getExtension(
1042                    fileEntry.getName());
1043
1044                if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
1045                    if (Validator.isNull(curExtension)) {
1046                        throw new DuplicateFileException(
1047                            fileEntry.getTitleWithExtension());
1048                    }
1049                }
1050                else if (extension.equals(curExtension)) {
1051                    throw new DuplicateFileException(
1052                        fileEntry.getTitleWithExtension());
1053                }
1054            }
1055        }
1056    }
1057
1058    protected void validate(
1059            long groupId, long folderId, String name, String title,
1060            InputStream is)
1061        throws PortalException, SystemException {
1062
1063        dlLocalService.validate(name, is);
1064
1065        String extension = FileUtil.getExtension(name);
1066
1067        try {
1068            String titleWithException = title;
1069
1070            if (Validator.isNotNull(extension)) {
1071                titleWithException += StringPool.PERIOD + extension;
1072            }
1073
1074            dlFolderLocalService.getFolder(
1075                groupId, folderId, titleWithException);
1076
1077            throw new DuplicateFolderNameException();
1078        }
1079        catch (NoSuchFolderException nsfe) {
1080        }
1081
1082        List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByF_T(
1083            folderId, title);
1084
1085        for (DLFileEntry fileEntry : fileEntries) {
1086            String curExtension = FileUtil.getExtension(fileEntry.getName());
1087
1088            if (PropsValues.WEBDAV_LITMUS && Validator.isNull(extension)) {
1089                if (Validator.isNull(curExtension)) {
1090                    throw new DuplicateFileException(
1091                        fileEntry.getTitleWithExtension());
1092                }
1093            }
1094            else if (extension.equals(curExtension)) {
1095                throw new DuplicateFileException(
1096                    fileEntry.getTitleWithExtension());
1097            }
1098        }
1099    }
1100
1101    private static Log _log =
1102        LogFactory.getLog(DLFileEntryLocalServiceImpl.class);
1103
1104}