1   /**
2    * Copyright (c) 2000-2007 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.counter.service.CounterLocalServiceUtil;
26  import com.liferay.documentlibrary.DuplicateFileException;
27  import com.liferay.documentlibrary.FileSizeException;
28  import com.liferay.documentlibrary.NoSuchFileException;
29  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
30  import com.liferay.documentlibrary.service.DLServiceUtil;
31  import com.liferay.portal.PortalException;
32  import com.liferay.portal.SystemException;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.User;
38  import com.liferay.portal.model.impl.ResourceImpl;
39  import com.liferay.portal.service.ResourceLocalServiceUtil;
40  import com.liferay.portal.service.persistence.UserUtil;
41  import com.liferay.portal.util.MimeTypesUtil;
42  import com.liferay.portal.util.PortletKeys;
43  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
44  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
45  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
46  import com.liferay.portlet.documentlibrary.model.DLFolder;
47  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
48  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
49  import com.liferay.portlet.documentlibrary.service.DLFileRankLocalServiceUtil;
50  import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
51  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
52  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryAndShortcutFinder;
53  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryFinder;
54  import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
55  import com.liferay.portlet.documentlibrary.service.persistence.DLFileVersionUtil;
56  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
57  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
58  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
59  import com.liferay.portlet.tags.service.TagsAssetLocalServiceUtil;
60  import com.liferay.util.MathUtil;
61  
62  import java.io.BufferedInputStream;
63  import java.io.ByteArrayInputStream;
64  import java.io.File;
65  import java.io.FileInputStream;
66  import java.io.FileNotFoundException;
67  import java.io.IOException;
68  import java.io.InputStream;
69  
70  import java.rmi.RemoteException;
71  
72  import java.util.ArrayList;
73  import java.util.Date;
74  import java.util.Iterator;
75  import java.util.List;
76  
77  import org.apache.commons.logging.Log;
78  import org.apache.commons.logging.LogFactory;
79  
80  /**
81   * <a href="DLFileEntryLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
82   *
83   * @author Brian Wing Shun Chan
84   * @author Harry Mark
85   *
86   */
87  public class DLFileEntryLocalServiceImpl
88      extends DLFileEntryLocalServiceBaseImpl {
89  
90      public DLFileEntry addFileEntry(
91              long userId, long folderId, String name, String title,
92              String description, String[] tagsEntries, String extraSettings,
93              File file, boolean addCommunityPermissions,
94              boolean addGuestPermissions)
95          throws PortalException, SystemException {
96  
97          return addFileEntry(
98              userId, folderId, name, title, description, tagsEntries,
99              extraSettings, file, Boolean.valueOf(addCommunityPermissions),
100             Boolean.valueOf(addGuestPermissions), null, null);
101     }
102 
103     public DLFileEntry addFileEntry(
104             long userId, long folderId, String name, String title,
105             String description, String[] tagsEntries, String extraSettings,
106             byte[] byteArray, boolean addCommunityPermissions,
107             boolean addGuestPermissions)
108         throws PortalException, SystemException {
109 
110         return addFileEntry(
111             userId, folderId, name, title, description, tagsEntries,
112             extraSettings, byteArray, Boolean.valueOf(addCommunityPermissions),
113             Boolean.valueOf(addGuestPermissions), null, null);
114     }
115 
116     public DLFileEntry addFileEntry(
117             long userId, long folderId, String name, String title,
118             String description, String[] tagsEntries, String extraSettings,
119             File file, String[] communityPermissions, String[] guestPermissions)
120         throws PortalException, SystemException {
121 
122         return addFileEntry(
123             userId, folderId, name, title, description, tagsEntries,
124             extraSettings, file, null, null, communityPermissions,
125             guestPermissions);
126     }
127 
128     public DLFileEntry addFileEntry(
129             long userId, long folderId, String name, String title,
130             String description, String[] tagsEntries, String extraSettings,
131             byte[] byteArray, String[] communityPermissions,
132             String[] guestPermissions)
133         throws PortalException, SystemException {
134 
135         return addFileEntry(
136             userId, folderId, name, title, description, tagsEntries,
137             extraSettings, byteArray, null, null, communityPermissions,
138             guestPermissions);
139     }
140 
141     public DLFileEntry addFileEntry(
142             long userId, long folderId, String name, String title,
143             String description, String[] tagsEntries, String extraSettings,
144             File file, Boolean addCommunityPermissions,
145             Boolean addGuestPermissions, String[] communityPermissions,
146             String[] guestPermissions)
147         throws PortalException, SystemException {
148 
149         if ((file == null) || (file.length() == 0)) {
150             throw new FileSizeException();
151         }
152 
153         InputStream is = null;
154 
155         try {
156             is = new BufferedInputStream(new FileInputStream(file));
157 
158             return addFileEntry(
159                 userId, folderId, name, title, description, tagsEntries,
160                 extraSettings, is, file.length(), addCommunityPermissions,
161                 addGuestPermissions, communityPermissions, guestPermissions);
162         }
163         catch (FileNotFoundException fnfe) {
164             throw new FileSizeException();
165         }
166         finally {
167             try {
168                 if (is != null) {
169                     is.close();
170                 }
171             }
172             catch (IOException ioe) {
173                 _log.error(ioe);
174             }
175         }
176     }
177 
178     public DLFileEntry addFileEntry(
179             long userId, long folderId, String name, String title,
180             String description, String[] tagsEntries, String extraSettings,
181             byte[] byteArray, Boolean addCommunityPermissions,
182             Boolean addGuestPermissions, String[] communityPermissions,
183             String[] guestPermissions)
184         throws PortalException, SystemException {
185 
186         if ((byteArray == null) || (byteArray.length == 0)) {
187             throw new FileSizeException();
188         }
189 
190         InputStream is = new ByteArrayInputStream(byteArray);
191 
192         return addFileEntry(
193             userId, folderId, name, title, description, tagsEntries,
194             extraSettings, is, byteArray.length, addCommunityPermissions,
195             addGuestPermissions, communityPermissions, guestPermissions);
196     }
197 
198     public DLFileEntry addFileEntry(
199             long userId, long folderId, String name, String title,
200             String description, String[] tagsEntries, String extraSettings,
201             InputStream is, long size, Boolean addCommunityPermissions,
202             Boolean addGuestPermissions, String[] communityPermissions,
203             String[] guestPermissions)
204         throws PortalException, SystemException {
205 
206         // File entry
207 
208         User user = UserUtil.findByPrimaryKey(userId);
209         folderId = getFolderId(user.getCompanyId(), folderId);
210         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
211         Date now = new Date();
212 
213         if (Validator.isNull(title)) {
214             title = name;
215         }
216 
217         DLLocalServiceUtil.validate(name, is);
218 
219         name = getName(name);
220 
221         if (DLLocalServiceUtil.hasFile(
222                 user.getCompanyId(), folderId, name, 0)) {
223 
224             throw new DuplicateFileException(name);
225         }
226 
227         long fileEntryId = CounterLocalServiceUtil.increment();
228 
229         DLFileEntry fileEntry = DLFileEntryUtil.create(fileEntryId);
230 
231         fileEntry.setCompanyId(user.getCompanyId());
232         fileEntry.setUserId(user.getUserId());
233         fileEntry.setUserName(user.getFullName());
234         fileEntry.setVersionUserId(user.getUserId());
235         fileEntry.setVersionUserName(user.getFullName());
236         fileEntry.setCreateDate(now);
237         fileEntry.setModifiedDate(now);
238         fileEntry.setFolderId(folderId);
239         fileEntry.setName(name);
240         fileEntry.setTitle(title);
241         fileEntry.setDescription(description);
242         fileEntry.setVersion(DLFileEntryImpl.DEFAULT_VERSION);
243         fileEntry.setSize((int)size);
244         fileEntry.setReadCount(DLFileEntryImpl.DEFAULT_READ_COUNT);
245         fileEntry.setExtraSettings(extraSettings);
246 
247         DLFileEntryUtil.update(fileEntry);
248 
249         // File
250 
251         DLLocalServiceUtil.addFile(
252             user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
253             folder.getGroupId(), folderId, name,
254             fileEntry.getLuceneProperties(), is);
255 
256         // Resources
257 
258         if ((addCommunityPermissions != null) &&
259             (addGuestPermissions != null)) {
260 
261             addFileEntryResources(
262                 folder, fileEntry, addCommunityPermissions.booleanValue(),
263                 addGuestPermissions.booleanValue());
264         }
265         else {
266             addFileEntryResources(
267                 folder, fileEntry, communityPermissions, guestPermissions);
268         }
269 
270         // Tags
271 
272         updateTagsAsset(userId, fileEntry, tagsEntries);
273 
274         // Folder
275 
276         folder.setLastPostDate(fileEntry.getModifiedDate());
277 
278         DLFolderUtil.update(folder);
279 
280         return fileEntry;
281     }
282 
283     public void addFileEntryResources(
284             long folderId, String name, boolean addCommunityPermissions,
285             boolean addGuestPermissions)
286         throws PortalException, SystemException {
287 
288         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
289         DLFileEntry fileEntry = DLFileEntryUtil.findByF_N(folderId, name);
290 
291         addFileEntryResources(
292             folder, fileEntry, addCommunityPermissions, addGuestPermissions);
293     }
294 
295     public void addFileEntryResources(
296             DLFolder folder, DLFileEntry fileEntry,
297             boolean addCommunityPermissions, boolean addGuestPermissions)
298         throws PortalException, SystemException {
299 
300         ResourceLocalServiceUtil.addResources(
301             fileEntry.getCompanyId(), folder.getGroupId(),
302             fileEntry.getUserId(), DLFileEntry.class.getName(),
303             fileEntry.getFileEntryId(), false, addCommunityPermissions,
304             addGuestPermissions);
305     }
306 
307     public void addFileEntryResources(
308             long folderId, String name, String[] communityPermissions,
309             String[] guestPermissions)
310         throws PortalException, SystemException {
311 
312         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
313         DLFileEntry fileEntry = DLFileEntryUtil.findByF_N(folderId, name);
314 
315         addFileEntryResources(
316             folder, fileEntry, communityPermissions, guestPermissions);
317     }
318 
319     public void addFileEntryResources(
320             DLFolder folder, DLFileEntry fileEntry,
321             String[] communityPermissions, String[] guestPermissions)
322         throws PortalException, SystemException {
323 
324         ResourceLocalServiceUtil.addModelResources(
325             fileEntry.getCompanyId(), folder.getGroupId(),
326             fileEntry.getUserId(), DLFileEntry.class.getName(),
327             fileEntry.getFileEntryId(), communityPermissions, guestPermissions);
328     }
329 
330     public void deleteFileEntries(long folderId)
331         throws PortalException, SystemException {
332 
333         Iterator itr = DLFileEntryUtil.findByFolderId(folderId).iterator();
334 
335         while (itr.hasNext()) {
336             DLFileEntry fileEntry = (DLFileEntry)itr.next();
337 
338             deleteFileEntry(fileEntry);
339         }
340     }
341 
342     public void deleteFileEntry(long folderId, String name)
343         throws PortalException, SystemException {
344 
345         deleteFileEntry(folderId, name, -1);
346     }
347 
348     public void deleteFileEntry(long folderId, String name, double version)
349         throws PortalException, SystemException {
350 
351         DLFileEntry fileEntry = DLFileEntryUtil.findByF_N(folderId, name);
352 
353         if (version > 0) {
354             try {
355                 DLServiceUtil.deleteFile(
356                     fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
357                     fileEntry.getFolderId(), fileEntry.getName(), version);
358             }
359             catch (Exception e) {
360                 if (_log.isWarnEnabled()) {
361                     _log.warn(e, e);
362                 }
363             }
364 
365             DLFileVersionUtil.removeByF_N_V(folderId, name, version);
366         }
367         else {
368             deleteFileEntry(fileEntry);
369         }
370     }
371 
372     public void deleteFileEntry(DLFileEntry fileEntry)
373         throws PortalException, SystemException {
374 
375         // File
376 
377         try {
378             DLServiceUtil.deleteFile(
379                 fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
380                 fileEntry.getFolderId(), fileEntry.getName());
381         }
382         catch (Exception e) {
383             if (_log.isWarnEnabled()) {
384                 _log.warn(e, e);
385             }
386         }
387 
388         // File ranks
389 
390         DLFileRankLocalServiceUtil.deleteFileRanks(
391             fileEntry.getFolderId(), fileEntry.getName());
392 
393         // File shortcuts
394 
395         DLFileShortcutLocalServiceUtil.deleteFileShortcuts(
396             fileEntry.getFolderId(), fileEntry.getName());
397 
398         // File versions
399 
400         Iterator itr = DLFileVersionUtil.findByF_N(
401             fileEntry.getFolderId(), fileEntry.getName()).iterator();
402 
403         while (itr.hasNext()) {
404             DLFileVersion fileVersion = (DLFileVersion)itr.next();
405 
406             DLFileVersionUtil.remove(fileVersion.getPrimaryKey());
407         }
408 
409         // Tags
410 
411         TagsAssetLocalServiceUtil.deleteAsset(
412             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
413 
414         // Ratings
415 
416         RatingsStatsLocalServiceUtil.deleteStats(
417             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
418 
419         // Message boards
420 
421         MBMessageLocalServiceUtil.deleteDiscussionMessages(
422             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
423 
424         // Resources
425 
426         ResourceLocalServiceUtil.deleteResource(
427             fileEntry.getCompanyId(), DLFileEntry.class.getName(),
428             ResourceImpl.SCOPE_INDIVIDUAL, fileEntry.getFileEntryId());
429 
430         // File entry
431 
432         DLFileEntryUtil.remove(fileEntry.getPrimaryKey());
433     }
434 
435     public InputStream getFileAsStream(
436             long companyId, long userId, long folderId, String name)
437         throws PortalException, SystemException {
438 
439         return getFileAsStream(companyId, userId, folderId, name, 0);
440     }
441 
442     public InputStream getFileAsStream(
443             long companyId, long userId, long folderId, String name,
444             double version)
445         throws PortalException, SystemException {
446 
447         if (userId > 0) {
448             DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
449 
450             DLFileRankLocalServiceUtil.updateFileRank(
451                 folder.getGroupId(), companyId, userId, folderId, name);
452         }
453 
454         DLFileEntry fileEntry = DLFileEntryUtil.findByF_N(folderId, name);
455 
456         fileEntry.setReadCount(fileEntry.getReadCount() + 1);
457 
458         DLFileEntryUtil.update(fileEntry);
459 
460         if ((version > 0) && (fileEntry.getVersion() != version)) {
461             return DLLocalServiceUtil.getFileAsStream(
462                 companyId, folderId, name, version);
463         }
464         else {
465             return DLLocalServiceUtil.getFileAsStream(
466                 companyId, folderId, name);
467         }
468     }
469 
470     public DLFileEntry getFileEntry(long fileEntryId)
471         throws PortalException, SystemException {
472 
473         return DLFileEntryUtil.findByPrimaryKey(fileEntryId);
474     }
475 
476     public DLFileEntry getFileEntry(long folderId, String name)
477         throws PortalException, SystemException {
478 
479         return DLFileEntryUtil.findByF_N(folderId, name);
480     }
481 
482     public List getFileEntries(long folderId) throws SystemException {
483         return DLFileEntryUtil.findByFolderId(folderId);
484     }
485 
486     public List getFileEntries(long folderId, int begin, int end)
487         throws SystemException {
488 
489         return DLFileEntryUtil.findByFolderId(folderId, begin, end);
490     }
491 
492     public List getFileEntries(
493             long folderId, int begin, int end, OrderByComparator obc)
494         throws SystemException {
495 
496         return DLFileEntryUtil.findByFolderId(folderId, begin, end, obc);
497     }
498 
499     public List getFileEntriesAndShortcuts(long folderId, int begin, int end)
500         throws SystemException {
501 
502         List folderIds = new ArrayList();
503 
504         folderIds.add(new Long(folderId));
505 
506         return DLFileEntryAndShortcutFinder.findByFolderIds(
507             folderIds, begin, end);
508     }
509 
510     public List getFileEntriesAndShortcuts(List folderIds, int begin, int end)
511         throws SystemException {
512 
513         return DLFileEntryAndShortcutFinder.findByFolderIds(
514             folderIds, begin, end);
515     }
516 
517     public int getFileEntriesAndShortcutsCount(long folderId)
518         throws SystemException {
519 
520         List folderIds = new ArrayList();
521 
522         folderIds.add(new Long(folderId));
523 
524         return DLFileEntryAndShortcutFinder.countByFolderIds(folderIds);
525     }
526 
527     public int getFileEntriesAndShortcutsCount(List folderIds)
528         throws SystemException {
529 
530         return DLFileEntryAndShortcutFinder.countByFolderIds(folderIds);
531     }
532 
533     public int getFileEntriesCount(long folderId) throws SystemException {
534         return DLFileEntryUtil.countByFolderId(folderId);
535     }
536 
537     public int getFoldersFileEntriesCount(List folderIds)
538         throws SystemException {
539 
540         return DLFileEntryFinder.countByFolderIds(folderIds);
541     }
542 
543     public List getGroupFileEntries(long groupId, int begin, int end)
544         throws SystemException {
545 
546         return DLFileEntryFinder.findByGroupId(groupId, begin, end);
547     }
548 
549     public List getGroupFileEntries(
550             long groupId, int begin, int end, OrderByComparator obc)
551         throws SystemException {
552 
553         return DLFileEntryFinder.findByGroupId(groupId, begin, end, obc);
554     }
555 
556     public List getGroupFileEntries(
557             long groupId, long userId, int begin, int end)
558         throws SystemException {
559 
560         if (userId <= 0) {
561             return DLFileEntryFinder.findByGroupId(groupId, begin, end);
562         }
563         else {
564             return DLFileEntryFinder.findByG_U(groupId, userId, begin, end);
565         }
566     }
567 
568     public List getGroupFileEntries(
569             long groupId, long userId, int begin, int end,
570             OrderByComparator obc)
571         throws SystemException {
572 
573         if (userId <= 0) {
574             return DLFileEntryFinder.findByGroupId(groupId, begin, end, obc);
575         }
576         else {
577             return DLFileEntryFinder.findByG_U(
578                 groupId, userId, begin, end, obc);
579         }
580     }
581 
582     public int getGroupFileEntriesCount(long groupId) throws SystemException {
583         return DLFileEntryFinder.countByGroupId(groupId);
584     }
585 
586     public int getGroupFileEntriesCount(long groupId, long userId)
587         throws SystemException {
588 
589         if (userId <= 0) {
590             return DLFileEntryFinder.countByGroupId(groupId);
591         }
592         else {
593             return DLFileEntryFinder.countByG_U(groupId, userId);
594         }
595     }
596 
597     public List getNoAssetFileEntries() throws SystemException {
598         return DLFileEntryFinder.findByNoAssets();
599     }
600 
601     public DLFileEntry updateFileEntry(
602             long userId, long folderId, long newFolderId, String name,
603             String sourceFileName, String title, String description,
604             String[] tagsEntries, String extraSettings, File file)
605         throws PortalException, SystemException {
606 
607         InputStream is = null;
608 
609         try {
610             long size = 0;
611 
612             if ((file != null) && (file.length() > 0)) {
613                 is = new BufferedInputStream(new FileInputStream(file));
614                 size = file.length();
615             }
616 
617             return updateFileEntry(
618                 userId, folderId, newFolderId, name, sourceFileName, title,
619                 description, tagsEntries, extraSettings, is, size);
620         }
621         catch (FileNotFoundException fnfe) {
622             throw new NoSuchFileException();
623         }
624         finally {
625             try {
626                 if (is != null) {
627                     is.close();
628                 }
629             }
630             catch (IOException ioe) {
631                 _log.error(ioe);
632             }
633         }
634     }
635 
636     public DLFileEntry updateFileEntry(
637             long userId, long folderId, long newFolderId, String name,
638             String sourceFileName, String title, String description,
639             String[] tagsEntries, String extraSettings, byte[] byteArray)
640         throws PortalException, SystemException {
641 
642         InputStream is = null;
643         long size = 0;
644 
645         if ((byteArray != null) && (byteArray.length > 0)) {
646             is = new ByteArrayInputStream(byteArray);
647             size = byteArray.length;
648         }
649 
650         return updateFileEntry(
651             userId, folderId, newFolderId, name, sourceFileName, title,
652             description, tagsEntries, extraSettings, is, size);
653     }
654 
655     public DLFileEntry updateFileEntry(
656             long userId, long folderId, long newFolderId, String name,
657             String sourceFileName, String title, String description,
658             String[] tagsEntries, String extraSettings, InputStream is,
659             long size)
660         throws PortalException, SystemException {
661 
662         // File entry
663 
664         User user = UserUtil.findByPrimaryKey(userId);
665         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
666 
667         if (Validator.isNull(title)) {
668             title = name;
669         }
670 
671         DLLocalServiceUtil.validate(name, sourceFileName, is);
672 
673         DLFileEntry fileEntry = DLFileEntryUtil.findByF_N(folderId, name);
674 
675         fileEntry.setTitle(title);
676         fileEntry.setDescription(description);
677         fileEntry.setExtraSettings(extraSettings);
678 
679         DLFileEntryUtil.update(fileEntry);
680 
681         // Move file entry
682 
683         if ((newFolderId > 0) && (folderId != newFolderId)) {
684             DLFolder newFolder = DLFolderUtil.findByPrimaryKey(newFolderId);
685 
686             if (folder.getGroupId() != newFolder.getGroupId()) {
687                 throw new NoSuchFolderException();
688             }
689 
690             if (DLLocalServiceUtil.hasFile(
691                     user.getCompanyId(), newFolderId, name, 0)) {
692 
693                 throw new DuplicateFileException(name);
694             }
695 
696             long newFileEntryId = CounterLocalServiceUtil.increment();
697 
698             DLFileEntry newFileEntry = DLFileEntryUtil.create(newFileEntryId);
699 
700             newFileEntry.setCompanyId(fileEntry.getCompanyId());
701             newFileEntry.setUserId(fileEntry.getUserId());
702             newFileEntry.setUserName(fileEntry.getUserName());
703             newFileEntry.setVersionUserId(fileEntry.getVersionUserId());
704             newFileEntry.setVersionUserName(fileEntry.getVersionUserName());
705             newFileEntry.setCreateDate(fileEntry.getCreateDate());
706             newFileEntry.setModifiedDate(fileEntry.getModifiedDate());
707             newFileEntry.setFolderId(newFolderId);
708             newFileEntry.setName(name);
709             newFileEntry.setTitle(fileEntry.getTitle());
710             newFileEntry.setDescription(fileEntry.getDescription());
711             newFileEntry.setVersion(fileEntry.getVersion());
712             newFileEntry.setSize(fileEntry.getSize());
713             newFileEntry.setReadCount(fileEntry.getReadCount());
714             newFileEntry.setExtraSettings(extraSettings);
715 
716             DLFileEntryUtil.update(newFileEntry);
717 
718             DLFileEntryUtil.remove(fileEntry);
719 
720             fileEntry = newFileEntry;
721 
722             Iterator itr = DLFileVersionUtil.findByF_N(
723                 folderId, name).iterator();
724 
725             while (itr.hasNext()) {
726                 DLFileVersion fileVersion = (DLFileVersion)itr.next();
727 
728                 long newFileVersionId = CounterLocalServiceUtil.increment();
729 
730                 DLFileVersion newFileVersion = DLFileVersionUtil.create(
731                     newFileVersionId);
732 
733                 newFileVersion.setCompanyId(fileVersion.getCompanyId());
734                 newFileVersion.setUserId(fileVersion.getUserId());
735                 newFileVersion.setUserName(fileVersion.getUserName());
736                 newFileVersion.setCreateDate(fileVersion.getCreateDate());
737                 newFileVersion.setFolderId(newFolderId);
738                 newFileVersion.setName(name);
739                 newFileVersion.setVersion(fileVersion.getVersion());
740                 newFileVersion.setSize(fileVersion.getSize());
741 
742                 DLFileVersionUtil.update(newFileVersion);
743 
744                 DLFileVersionUtil.remove(fileVersion);
745             }
746 
747             DLFileShortcutLocalServiceUtil.updateFileShortcuts(
748                 folderId, name, newFolderId, name);
749 
750             try {
751                 DLServiceUtil.updateFile(
752                     user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
753                     folder.getGroupId(), folderId, newFolderId, name);
754             }
755             catch (RemoteException re) {
756                 throw new SystemException(re);
757             }
758 
759             folderId = newFolderId;
760             folder = newFolder;
761         }
762 
763         // Tags
764 
765         updateTagsAsset(userId, fileEntry, tagsEntries);
766 
767         // File version
768 
769         if (is == null) {
770             return fileEntry;
771         }
772 
773         double oldVersion = fileEntry.getVersion();
774         double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);
775 
776         long fileVersionId = CounterLocalServiceUtil.increment();
777 
778         DLFileVersion fileVersion = DLFileVersionUtil.create(fileVersionId);
779 
780         long versionUserId = fileEntry.getVersionUserId();
781 
782         if (versionUserId <= 0) {
783             versionUserId = fileEntry.getUserId();
784         }
785 
786         String versionUserName = GetterUtil.getString(
787             fileEntry.getVersionUserName(), fileEntry.getUserName());
788 
789         fileVersion.setCompanyId(fileEntry.getCompanyId());
790         fileVersion.setUserId(versionUserId);
791         fileVersion.setUserName(versionUserName);
792         fileVersion.setCreateDate(fileEntry.getModifiedDate());
793         fileVersion.setFolderId(folderId);
794         fileVersion.setName(name);
795         fileVersion.setVersion(oldVersion);
796         fileVersion.setSize(fileEntry.getSize());
797 
798         DLFileVersionUtil.update(fileVersion);
799 
800         // File entry
801 
802         fileEntry.setVersionUserId(user.getUserId());
803         fileEntry.setVersionUserName(user.getFullName());
804         fileEntry.setModifiedDate(new Date());
805         fileEntry.setVersion(newVersion);
806         fileEntry.setSize((int)size);
807 
808         DLFileEntryUtil.update(fileEntry);
809 
810         // File
811 
812         DLLocalServiceUtil.updateFile(
813             user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
814             folder.getGroupId(), folderId, name, newVersion, sourceFileName,
815             fileEntry.getLuceneProperties(), is);
816 
817         // Folder
818 
819         folder.setLastPostDate(fileEntry.getModifiedDate());
820 
821         DLFolderUtil.update(folder);
822 
823         return fileEntry;
824     }
825 
826     public void updateTagsAsset(
827             long userId, DLFileEntry fileEntry, String[] tagsEntries)
828         throws PortalException, SystemException {
829 
830         String mimeType = MimeTypesUtil.getContentType(fileEntry.getName());
831 
832         TagsAssetLocalServiceUtil.updateAsset(
833             userId, DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
834             tagsEntries, null, null, null, null, mimeType, fileEntry.getTitle(),
835             fileEntry.getDescription(), fileEntry.getDescription(), null, 0, 0);
836     }
837 
838     protected long getFolderId(long companyId, long folderId)
839         throws PortalException, SystemException {
840 
841         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
842 
843             // Ensure folder exists and belongs to the proper company
844 
845             try {
846                 DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
847 
848                 if (companyId != folder.getCompanyId()) {
849                     folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
850                 }
851             }
852             catch (NoSuchFolderException nsfe) {
853                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
854             }
855         }
856 
857         return folderId;
858     }
859 
860     protected String getName(String name) throws SystemException {
861         String extension = StringPool.BLANK;
862 
863         int pos = name.lastIndexOf(StringPool.PERIOD);
864 
865         if (pos != -1) {
866             extension = name.substring(pos + 1, name.length()).toLowerCase();
867         }
868 
869         name = String.valueOf(CounterLocalServiceUtil.increment(
870             DLFileEntry.class.getName()));
871 
872         if (Validator.isNotNull(extension)) {
873             name = "DLFE-" + name + StringPool.PERIOD + extension;
874         }
875 
876         return name;
877     }
878 
879     private static Log _log =
880         LogFactory.getLog(DLFileEntryLocalServiceImpl.class);
881 
882 }