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