001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.documentlibrary.DuplicateFileException;
018    import com.liferay.documentlibrary.FileSizeException;
019    import com.liferay.documentlibrary.NoSuchFileException;
020    import com.liferay.documentlibrary.util.JCRHook;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
024    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.search.Indexer;
028    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.MimeTypesUtil;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.kernel.workflow.WorkflowConstants;
037    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
038    import com.liferay.portal.model.ResourceConstants;
039    import com.liferay.portal.model.User;
040    import com.liferay.portal.service.ServiceContext;
041    import com.liferay.portal.util.PortalUtil;
042    import com.liferay.portal.util.PortletKeys;
043    import com.liferay.portal.util.PropsValues;
044    import com.liferay.portlet.asset.NoSuchEntryException;
045    import com.liferay.portlet.asset.model.AssetEntry;
046    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
047    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
048    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
049    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
050    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
051    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
052    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
053    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
054    import com.liferay.portlet.documentlibrary.model.DLFolder;
055    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
056    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
057    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
058    import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
059    import com.liferay.portlet.documentlibrary.util.DLUtil;
060    import com.liferay.portlet.documentlibrary.util.comparator.FileEntryModifiedDateComparator;
061    import com.liferay.portlet.messageboards.model.MBDiscussion;
062    import com.liferay.portlet.ratings.model.RatingsEntry;
063    import com.liferay.portlet.ratings.model.RatingsStats;
064    
065    import java.io.File;
066    import java.io.FileInputStream;
067    import java.io.FileNotFoundException;
068    import java.io.InputStream;
069    
070    import java.util.Date;
071    import java.util.List;
072    
073    /**
074     * <p>
075     * For DLFileEntries, the naming convention for some of the variables is not
076     * very informative, due to legacy code. Each DLFileEntry has a corresponding
077     * name and title. The "name" is a unique identifier for a given file and
078     * usually follows the format "1234" whereas the "title" is the actual name
079     * specified by the user (e.g., "Budget.xls").
080     * </p>
081     *
082     * @author Brian Wing Shun Chan
083     * @author Harry Mark
084     */
085    public class DLFileEntryLocalServiceImpl
086            extends DLFileEntryLocalServiceBaseImpl {
087    
088            public DLFileEntry addFileEntry(
089                            long userId, long groupId, long folderId, String name, String title,
090                            String description, String changeLog, String extraSettings,
091                            byte[] bytes, ServiceContext serviceContext)
092                    throws PortalException, SystemException {
093    
094                    if (bytes == null) {
095                            throw new FileSizeException();
096                    }
097    
098                    InputStream is = new UnsyncByteArrayInputStream(bytes);
099    
100                    return addFileEntry(
101                            userId, groupId, folderId, name, title, description,
102                            changeLog, extraSettings, is, bytes.length, serviceContext);
103            }
104    
105            public DLFileEntry addFileEntry(
106                            long userId, long groupId, long folderId, String name, String title,
107                            String description, String changeLog, String extraSettings,
108                            File file, ServiceContext serviceContext)
109                    throws PortalException, SystemException {
110    
111                    if (file == null) {
112                            throw new FileSizeException();
113                    }
114    
115                    try {
116                            InputStream is = new UnsyncBufferedInputStream(
117                                    new FileInputStream(file));
118    
119                            return addFileEntry(
120                                    userId, groupId, folderId, name, title, description,
121                                    changeLog, extraSettings, is, file.length(), serviceContext);
122                    }
123                    catch (FileNotFoundException fnfe) {
124                            throw new FileSizeException();
125                    }
126            }
127    
128            public DLFileEntry addFileEntry(
129                            long userId, long groupId, long folderId, String name, String title,
130                            String description, String changeLog, String extraSettings,
131                            InputStream is, long size, ServiceContext serviceContext)
132                    throws PortalException, SystemException {
133    
134                    // File entry
135    
136                    User user = userPersistence.findByPrimaryKey(userId);
137                    folderId = getFolderId(user.getCompanyId(), folderId);
138    
139                    String extension = FileUtil.getExtension(name);
140    
141                    if (Validator.isNull(title)) {
142                            title = name;
143                    }
144    
145                    name = String.valueOf(
146                            counterLocalService.increment(DLFileEntry.class.getName()));
147    
148                    Date now = new Date();
149    
150                    validate(groupId, folderId, title, is);
151    
152                    long fileEntryId = counterLocalService.increment();
153    
154                    DLFileEntry fileEntry = dlFileEntryPersistence.create(fileEntryId);
155    
156                    fileEntry.setUuid(serviceContext.getUuid());
157                    fileEntry.setGroupId(groupId);
158                    fileEntry.setCompanyId(user.getCompanyId());
159                    fileEntry.setUserId(user.getUserId());
160                    fileEntry.setUserName(user.getFullName());
161                    fileEntry.setVersionUserId(user.getUserId());
162                    fileEntry.setVersionUserName(user.getFullName());
163                    fileEntry.setCreateDate(serviceContext.getCreateDate(now));
164                    fileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
165                    fileEntry.setFolderId(folderId);
166                    fileEntry.setName(name);
167                    fileEntry.setExtension(extension);
168                    fileEntry.setTitle(title);
169                    fileEntry.setDescription(description);
170                    fileEntry.setExtraSettings(extraSettings);
171                    fileEntry.setVersion(DLFileEntryConstants.DEFAULT_VERSION);
172                    fileEntry.setSize(size);
173                    fileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
174    
175                    dlFileEntryPersistence.update(fileEntry, false);
176    
177                    // Resources
178    
179                    if (serviceContext.getAddCommunityPermissions() ||
180                            serviceContext.getAddGuestPermissions()) {
181    
182                            addFileEntryResources(
183                                    fileEntry, serviceContext.getAddCommunityPermissions(),
184                                    serviceContext.getAddGuestPermissions());
185                    }
186                    else {
187                            addFileEntryResources(
188                                    fileEntry, serviceContext.getCommunityPermissions(),
189                                    serviceContext.getGuestPermissions());
190                    }
191    
192                    // File version
193    
194                    DLFileVersion fileVersion = addFileVersion(
195                            user, fileEntry, serviceContext.getModifiedDate(now),
196                            extension, title, description, null, extraSettings,
197                            DLFileEntryConstants.DEFAULT_VERSION, size,
198                            WorkflowConstants.STATUS_DRAFT, serviceContext);
199    
200                    // Folder
201    
202                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
203                            DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
204    
205                            folder.setLastPostDate(fileEntry.getModifiedDate());
206    
207                            dlFolderPersistence.update(folder, false);
208                    }
209    
210                    // Asset
211    
212                    updateAsset(
213                            userId, fileEntry, fileVersion,
214                            serviceContext.getAssetCategoryIds(),
215                            serviceContext.getAssetTagNames());
216    
217                    // Message boards
218    
219                    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
220                            mbMessageLocalService.addDiscussionMessage(
221                                    userId, fileEntry.getUserName(), groupId,
222                                    DLFileEntry.class.getName(), fileEntryId,
223                                    WorkflowConstants.ACTION_PUBLISH);
224                    }
225    
226                    // File
227    
228                    dlLocalService.addFile(
229                            user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
230                            fileEntry.getGroupId(), fileEntry.getRepositoryId(), name, false,
231                            fileEntryId, fileEntry.getLuceneProperties(),
232                            fileEntry.getModifiedDate(), serviceContext, is);
233    
234                    // Workflow
235    
236                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
237                            user.getCompanyId(), groupId, userId, DLFileEntry.class.getName(),
238                            fileEntryId, fileEntry, serviceContext);
239    
240                    return fileEntry;
241            }
242    
243            public void addFileEntryResources(
244                            DLFileEntry fileEntry, boolean addCommunityPermissions,
245                            boolean addGuestPermissions)
246                    throws PortalException, SystemException {
247    
248                    resourceLocalService.addResources(
249                            fileEntry.getCompanyId(), fileEntry.getGroupId(),
250                            fileEntry.getUserId(), DLFileEntry.class.getName(),
251                            fileEntry.getFileEntryId(), false, addCommunityPermissions,
252                            addGuestPermissions);
253            }
254    
255            public void addFileEntryResources(
256                            DLFileEntry fileEntry, String[] communityPermissions,
257                            String[] guestPermissions)
258                    throws PortalException, SystemException {
259    
260                    resourceLocalService.addModelResources(
261                            fileEntry.getCompanyId(), fileEntry.getGroupId(),
262                            fileEntry.getUserId(), DLFileEntry.class.getName(),
263                            fileEntry.getFileEntryId(), communityPermissions, guestPermissions);
264            }
265    
266            public void addFileEntryResources(
267                            long fileEntryId, boolean addCommunityPermissions,
268                            boolean addGuestPermissions)
269                    throws PortalException, SystemException {
270    
271                    DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
272                            fileEntryId);
273    
274                    addFileEntryResources(
275                            fileEntry, addCommunityPermissions, addGuestPermissions);
276            }
277    
278            public void addFileEntryResources(
279                            long fileEntryId, String[] communityPermissions,
280                            String[] guestPermissions)
281                    throws PortalException, SystemException {
282    
283                    DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
284                            fileEntryId);
285    
286                    addFileEntryResources(
287                            fileEntry, communityPermissions, guestPermissions);
288            }
289    
290            public DLFileEntry addOrOverwriteFileEntry(
291                            long userId, long groupId, long folderId, String name,
292                            String sourceName, String title, String description,
293                            String changeLog, String extraSettings, File file,
294                            ServiceContext serviceContext)
295                    throws PortalException, SystemException {
296    
297                    try {
298                            dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
299    
300                            return updateFileEntry(
301                                    userId, groupId, folderId, name, sourceName, title, description,
302                                    changeLog, false, extraSettings, file, serviceContext);
303                    }
304                    catch (NoSuchFileEntryException nsfee) {
305                            return addFileEntry(
306                                    userId, groupId, folderId, name, title, description, changeLog,
307                                    extraSettings, file, serviceContext);
308                    }
309            }
310    
311            public void deleteFileEntries(long groupId, long folderId)
312                    throws PortalException, SystemException {
313    
314                    List<DLFileEntry> fileEntries = dlFileEntryPersistence.findByG_F(
315                            groupId, folderId);
316    
317                    for (DLFileEntry fileEntry : fileEntries) {
318                            deleteFileEntry(fileEntry);
319                    }
320            }
321    
322            public void deleteFileEntry(DLFileEntry fileEntry)
323                    throws PortalException, SystemException {
324    
325                    // File entry
326    
327                    dlFileEntryPersistence.remove(fileEntry);
328    
329                    // Resources
330    
331                    resourceLocalService.deleteResource(
332                            fileEntry.getCompanyId(), DLFileEntry.class.getName(),
333                            ResourceConstants.SCOPE_INDIVIDUAL, fileEntry.getFileEntryId());
334    
335                    // WebDAVProps
336    
337                    webDAVPropsLocalService.deleteWebDAVProps(
338                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
339    
340                    // Workflow
341    
342                    workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
343                            fileEntry.getCompanyId(), fileEntry.getGroupId(),
344                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
345    
346                    // File ranks
347    
348                    dlFileRankLocalService.deleteFileRanks(
349                            fileEntry.getFolderId(), fileEntry.getName());
350    
351                    // File shortcuts
352    
353                    dlFileShortcutLocalService.deleteFileShortcuts(
354                            fileEntry.getGroupId(), fileEntry.getFolderId(),
355                            fileEntry.getName());
356    
357                    // File versions
358    
359                    List<DLFileVersion> fileVersions = dlFileVersionPersistence.findByG_F_N(
360                            fileEntry.getGroupId(), fileEntry.getFolderId(),
361                            fileEntry.getName());
362    
363                    for (DLFileVersion fileVersion : fileVersions) {
364                            dlFileVersionPersistence.remove(fileVersion);
365                    }
366    
367                    // Asset
368    
369                    assetEntryLocalService.deleteEntry(
370                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
371    
372                    // Expando
373    
374                    expandoValueLocalService.deleteValues(
375                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
376    
377                    // Lock
378    
379                    String lockId = DLUtil.getLockId(
380                            fileEntry.getGroupId(), fileEntry.getFolderId(),
381                            fileEntry.getName());
382    
383                    lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
384    
385                    // Message boards
386    
387                    mbMessageLocalService.deleteDiscussionMessages(
388                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
389    
390                    // Ratings
391    
392                    ratingsStatsLocalService.deleteStats(
393                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
394    
395                    // Social
396    
397                    socialActivityLocalService.deleteActivities(
398                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
399    
400                    // File
401    
402                    try {
403                            dlService.deleteFile(
404                                    fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
405                                    fileEntry.getRepositoryId(), fileEntry.getName());
406                    }
407                    catch (Exception e) {
408                            if (_log.isWarnEnabled()) {
409                                    _log.warn(e, e);
410                            }
411                    }
412            }
413    
414            public void deleteFileEntry(long groupId, long folderId, String name)
415                    throws PortalException, SystemException {
416    
417                    deleteFileEntry(groupId, folderId, name, null);
418            }
419    
420            public void deleteFileEntry(
421                            long groupId, long folderId, String name, String version)
422                    throws PortalException, SystemException {
423    
424                    DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
425                            groupId, folderId, name);
426    
427                    if (Validator.isNotNull(version)) {
428                            try {
429                                    dlService.deleteFile(
430                                            fileEntry.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
431                                            fileEntry.getRepositoryId(), fileEntry.getName(), version);
432                            }
433                            catch (Exception e) {
434                                    if (_log.isWarnEnabled()) {
435                                            _log.warn(e, e);
436                                    }
437                            }
438    
439                            long fileVersionsCount = dlFileVersionPersistence.countByG_F_N(
440                                    groupId, folderId, name);
441    
442                            dlFileVersionPersistence.removeByG_F_N_V(
443                                    groupId, folderId, name, version);
444    
445                            if (fileVersionsCount == 1) {
446                                    dlFileEntryPersistence.remove(fileEntry);
447                            }
448                            else {
449                                    if (version.equals(fileEntry.getVersion())) {
450                                            try {
451                                                    DLFileVersion fileVersion =
452                                                            dlFileVersionLocalService.getLatestFileVersion(
453                                                                    groupId, folderId, name);
454    
455                                                    fileEntry.setVersion(fileVersion.getVersion());
456                                                    fileEntry.setSize(fileVersion.getSize());
457                                            }
458                                            catch (NoSuchFileVersionException nsfve) {
459                                            }
460                                    }
461    
462                                    dlFileEntryPersistence.update(fileEntry, false);
463                            }
464                    }
465                    else {
466                            deleteFileEntry(fileEntry);
467                    }
468            }
469    
470            public List<DLFileEntry> getCompanyFileEntries(
471                            long companyId, int start, int end)
472                    throws SystemException {
473    
474                    return dlFileEntryPersistence.findByCompanyId(companyId, start, end);
475            }
476    
477            public List<DLFileEntry> getCompanyFileEntries(
478                            long companyId, int start, int end, OrderByComparator obc)
479                    throws SystemException {
480    
481                    return dlFileEntryPersistence.findByCompanyId(
482                            companyId, start, end, obc);
483            }
484    
485            public int getCompanyFileEntriesCount(long companyId)
486                    throws SystemException {
487    
488                    return dlFileEntryPersistence.countByCompanyId(companyId);
489            }
490    
491            public InputStream getFileAsStream(
492                            long companyId, long userId, long groupId, long folderId,
493                            String name)
494                    throws PortalException, SystemException {
495    
496                    return getFileAsStream(
497                            companyId, userId, groupId, folderId, name, StringPool.BLANK);
498            }
499    
500            public InputStream getFileAsStream(
501                            long companyId, long userId, long groupId, long folderId,
502                            String name, String version)
503                    throws PortalException, SystemException {
504    
505                    DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
506                            groupId, folderId, name);
507    
508                    if (userId > 0) {
509                            dlFileRankLocalService.updateFileRank(
510                                    groupId, companyId, userId, folderId, name,
511                                    new ServiceContext());
512                    }
513    
514                    if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED) {
515                            fileEntry.setReadCount(fileEntry.getReadCount() + 1);
516    
517                            dlFileEntryPersistence.update(fileEntry, false);
518    
519                            assetEntryLocalService.incrementViewCounter(
520                                    userId, DLFileEntry.class.getName(),
521                                    fileEntry.getFileEntryId());
522    
523                            List<DLFileShortcut> fileShortcuts =
524                                    dlFileShortcutPersistence.findByG_TF_TN(
525                                            groupId, folderId, name);
526    
527                            for (DLFileShortcut fileShortcut : fileShortcuts) {
528                                    assetEntryLocalService.incrementViewCounter(
529                                            userId, DLFileShortcut.class.getName(),
530                                            fileShortcut.getFileShortcutId());
531                            }
532                    }
533    
534                    if (Validator.isNotNull(version)) {
535                            return dlLocalService.getFileAsStream(
536                                    companyId, fileEntry.getRepositoryId(), name, version);
537                    }
538                    else {
539                            return dlLocalService.getFileAsStream(
540                                    companyId, fileEntry.getRepositoryId(), name,
541                                    fileEntry.getVersion());
542                    }
543            }
544    
545            public List<DLFileEntry> getFileEntries(long groupId, long folderId)
546                    throws SystemException {
547    
548                    return dlFileEntryPersistence.findByG_F(groupId, folderId);
549            }
550    
551            public List<DLFileEntry> getFileEntries(
552                            long groupId, long folderId, int start, int end)
553                    throws SystemException {
554    
555                    return dlFileEntryPersistence.findByG_F(groupId, folderId, start, end);
556            }
557    
558            public List<DLFileEntry> getFileEntries(
559                            long groupId, long folderId, int start, int end,
560                            OrderByComparator obc)
561                    throws SystemException {
562    
563                    return dlFileEntryPersistence.findByG_F(
564                            groupId, folderId, start, end, obc);
565            }
566    
567            public int getFileEntriesCount(long groupId, long folderId)
568                    throws SystemException {
569    
570                    return dlFileEntryPersistence.countByG_F(groupId, folderId);
571            }
572    
573            public DLFileEntry getFileEntry(long fileEntryId)
574                    throws PortalException, SystemException {
575    
576                    return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
577            }
578    
579            public DLFileEntry getFileEntry(long groupId, long folderId, String name)
580                    throws PortalException, SystemException {
581    
582                    return dlFileEntryPersistence.findByG_F_N(groupId, folderId, name);
583            }
584    
585            public DLFileEntry getFileEntryByTitle(
586                            long groupId, long folderId, String title)
587                    throws PortalException, SystemException {
588    
589                    return dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
590            }
591    
592            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
593                    throws PortalException, SystemException {
594    
595                    return dlFileEntryPersistence.findByUUID_G(uuid, groupId);
596            }
597    
598            public int getFoldersFileEntriesCount(
599                            long groupId, List<Long> folderIds, int status)
600                    throws SystemException {
601    
602                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
603                            return dlFileEntryFinder.countByG_F_S(groupId, folderIds, status);
604                    }
605                    else {
606                            int start = 0;
607                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
608    
609                            int filesCount = dlFileEntryFinder.countByG_F_S(
610                                    groupId, folderIds.subList(start, end), status);
611    
612                            folderIds.subList(start, end).clear();
613    
614                            filesCount += getFoldersFileEntriesCount(
615                                    groupId, folderIds, status);
616    
617                            return filesCount;
618                    }
619            }
620    
621            public List<DLFileEntry> getGroupFileEntries(
622                            long groupId, int start, int end)
623                    throws SystemException {
624    
625                    return getGroupFileEntries(
626                            groupId, start, end, new FileEntryModifiedDateComparator());
627            }
628    
629            public List<DLFileEntry> getGroupFileEntries(
630                            long groupId, int start, int end, OrderByComparator obc)
631                    throws SystemException {
632    
633                    return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
634            }
635    
636            public List<DLFileEntry> getGroupFileEntries(
637                            long groupId, long userId, int start, int end)
638                    throws SystemException {
639    
640                    return getGroupFileEntries(
641                            groupId, userId, start, end, new FileEntryModifiedDateComparator());
642            }
643    
644            public List<DLFileEntry> getGroupFileEntries(
645                            long groupId, long userId, int start, int end,
646                            OrderByComparator obc)
647                    throws SystemException {
648    
649                    if (userId <= 0) {
650                            return dlFileEntryPersistence.findByGroupId(
651                                    groupId, start, end, obc);
652                    }
653                    else {
654                            return dlFileEntryPersistence.findByG_U(
655                                    groupId, userId, start, end, obc);
656                    }
657            }
658    
659            public int getGroupFileEntriesCount(long groupId) throws SystemException {
660                    return dlFileEntryPersistence.countByGroupId(groupId);
661            }
662    
663            public int getGroupFileEntriesCount(long groupId, long userId)
664                    throws SystemException {
665    
666                    if (userId <= 0) {
667                            return dlFileEntryPersistence.countByGroupId(groupId);
668                    }
669                    else {
670                            return dlFileEntryPersistence.countByG_U(groupId, userId);
671                    }
672            }
673    
674            public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
675                    return dlFileEntryFinder.findByNoAssets();
676            }
677    
678            public DLFileEntry moveFileEntry(
679                            long userId, long groupId, long folderId, long newFolderId,
680                            String name, ServiceContext serviceContext)
681                    throws PortalException, SystemException {
682    
683                    User user = userPersistence.findByPrimaryKey(userId);
684    
685                    Date now = new Date();
686    
687                    DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
688                            groupId, folderId, name);
689    
690                    long oldFileEntryId = fileEntry.getFileEntryId();
691    
692                    if (dlLocalService.hasFile(
693                                    user.getCompanyId(),
694                                    DLFileEntryImpl.getRepositoryId(groupId, newFolderId), name,
695                                    StringPool.BLANK)) {
696    
697                            throw new DuplicateFileException(name);
698                    }
699    
700                    long newFileEntryId = counterLocalService.increment();
701    
702                    DLFileEntry newFileEntry = dlFileEntryPersistence.create(
703                            newFileEntryId);
704    
705                    newFileEntry.setGroupId(fileEntry.getGroupId());
706                    newFileEntry.setCompanyId(fileEntry.getCompanyId());
707                    newFileEntry.setUserId(fileEntry.getUserId());
708                    newFileEntry.setUserName(fileEntry.getUserName());
709                    newFileEntry.setVersionUserId(fileEntry.getVersionUserId());
710                    newFileEntry.setVersionUserName(fileEntry.getVersionUserName());
711                    newFileEntry.setCreateDate(fileEntry.getCreateDate());
712                    newFileEntry.setModifiedDate(fileEntry.getModifiedDate());
713                    newFileEntry.setFolderId(newFolderId);
714                    newFileEntry.setName(name);
715                    newFileEntry.setExtension(fileEntry.getExtension());
716                    newFileEntry.setTitle(fileEntry.getTitle());
717                    newFileEntry.setDescription(fileEntry.getDescription());
718                    newFileEntry.setExtraSettings(fileEntry.getExtraSettings());
719                    newFileEntry.setVersion(fileEntry.getVersion());
720                    newFileEntry.setSize(fileEntry.getSize());
721                    newFileEntry.setReadCount(fileEntry.getReadCount());
722    
723                    dlFileEntryPersistence.update(newFileEntry, false);
724    
725                    dlFileEntryPersistence.remove(fileEntry);
726    
727                    workflowInstanceLinkLocalService.updateClassPK(
728                            fileEntry.getCompanyId(), fileEntry.getGroupId(),
729                            DLFileEntry.class.getName(), oldFileEntryId, newFileEntryId);
730    
731                    List<DLFileVersion> fileVersions = dlFileVersionPersistence.findByG_F_N(
732                            groupId, folderId, name);
733    
734                    for (DLFileVersion fileVersion : fileVersions) {
735                            long newFileVersionId = counterLocalService.increment();
736    
737                            DLFileVersion newFileVersion = dlFileVersionPersistence.create(
738                                    newFileVersionId);
739    
740                            newFileVersion.setGroupId(fileVersion.getGroupId());
741                            newFileVersion.setCompanyId(fileVersion.getCompanyId());
742                            newFileVersion.setUserId(fileVersion.getUserId());
743                            newFileVersion.setUserName(fileVersion.getUserName());
744                            newFileVersion.setCreateDate(fileVersion.getCreateDate());
745                            newFileVersion.setFolderId(newFolderId);
746                            newFileVersion.setName(name);
747                            newFileVersion.setExtension(fileVersion.getExtension());
748                            newFileVersion.setTitle(fileVersion.getTitle());
749                            newFileVersion.setDescription(fileVersion.getDescription());
750                            newFileVersion.setChangeLog(fileVersion.getChangeLog());
751                            newFileVersion.setExtraSettings(fileVersion.getExtraSettings());
752                            newFileVersion.setVersion(fileVersion.getVersion());
753                            newFileVersion.setSize(fileVersion.getSize());
754                            newFileVersion.setStatus(fileVersion.getStatus());
755                            newFileVersion.setStatusByUserId(userId);
756                            newFileVersion.setStatusByUserName(user.getFullName());
757                            newFileVersion.setStatusDate(serviceContext.getModifiedDate(now));
758    
759                            dlFileVersionPersistence.update(newFileVersion, false);
760    
761                            dlFileVersionPersistence.remove(fileVersion);
762                    }
763    
764                    dlFileShortcutLocalService.updateFileShortcuts(
765                            groupId, folderId, name, newFolderId, name);
766    
767                    // Resources
768    
769                    resourceLocalService.updateResources(
770                            fileEntry.getCompanyId(), DLFileEntry.class.getName(),
771                            ResourceConstants.SCOPE_INDIVIDUAL,
772                            String.valueOf(fileEntry.getFileEntryId()),
773                            String.valueOf(newFileEntryId));
774    
775                    // Asset
776    
777                    assetEntryLocalService.deleteEntry(
778                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
779    
780                    List<DLFileShortcut> fileShortcuts =
781                            dlFileShortcutPersistence.findByG_TF_TN(
782                                    groupId, folderId, name);
783    
784                    for (DLFileShortcut fileShortcut : fileShortcuts) {
785                            assetEntryLocalService.deleteEntry(
786                                    DLFileShortcut.class.getName(),
787                                    fileShortcut.getFileShortcutId());
788                    }
789    
790                    // Expando
791    
792                    expandoValueLocalService.deleteValues(
793                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
794    
795                    // Ratings
796    
797                    RatingsStats stats = ratingsStatsLocalService.getStats(
798                            DLFileEntry.class.getName(), oldFileEntryId);
799    
800                    stats.setClassPK(newFileEntryId);
801    
802                    ratingsStatsPersistence.update(stats, false);
803    
804                    long classNameId = PortalUtil.getClassNameId(
805                            DLFileEntry.class.getName());
806    
807                    List<RatingsEntry> entries = ratingsEntryPersistence.findByC_C(
808                            classNameId, oldFileEntryId);
809    
810                    for (RatingsEntry entry : entries) {
811                            entry.setClassPK(newFileEntryId);
812    
813                            ratingsEntryPersistence.update(entry, false);
814                    }
815    
816                    // Message boards
817    
818                    MBDiscussion discussion = mbDiscussionPersistence.fetchByC_C(
819                            classNameId, oldFileEntryId);
820    
821                    if (discussion != null) {
822                            discussion.setClassPK(newFileEntryId);
823    
824                            mbDiscussionPersistence.update(discussion, false);
825                    }
826    
827                    // Social
828    
829                    socialActivityLocalService.deleteActivities(
830                            DLFileEntry.class.getName(), fileEntry.getFileEntryId());
831    
832                    // File
833    
834                    dlService.updateFile(
835                            user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
836                            newFileEntry.getGroupId(), fileEntry.getRepositoryId(),
837                            newFileEntry.getRepositoryId(), name, newFileEntryId);
838    
839                    return newFileEntry;
840            }
841    
842            public void updateAsset(
843                            long userId, DLFileEntry fileEntry, DLFileVersion fileVersion,
844                            long[] assetCategoryIds, String[] assetTagNames)
845                    throws PortalException, SystemException {
846    
847                    String mimeType = MimeTypesUtil.getContentType(fileEntry.getTitle());
848    
849                    boolean addDraftAssetEntry = false;
850    
851                    String version = fileVersion.getVersion();
852    
853                    if ((fileVersion != null) && !fileVersion.isApproved() &&
854                            !version.equals(DLFileEntryConstants.DEFAULT_VERSION)) {
855    
856                            int approvedArticlesCount =
857                                    dlFileVersionPersistence.countByG_F_N_S(
858                                            fileEntry.getGroupId(), fileEntry.getFolderId(),
859                                            fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
860    
861                            if (approvedArticlesCount > 0) {
862                                    addDraftAssetEntry = true;
863                            }
864                    }
865    
866                    if (addDraftAssetEntry) {
867                            assetEntryLocalService.updateEntry(
868                                    userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
869                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
870                                    assetCategoryIds, assetTagNames, false, null, null, null, null,
871                                    mimeType, fileEntry.getTitle(), fileEntry.getDescription(),
872                                    null, null, 0, 0, null, false);
873                    }
874                    else {
875                            boolean visible = true;
876    
877                            if ((fileVersion != null) && !fileVersion.isApproved()) {
878                                    visible = false;
879                            }
880    
881                            assetEntryLocalService.updateEntry(
882                                    userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
883                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
884                                    assetCategoryIds, assetTagNames, visible, null, null, null,
885                                    null, mimeType, fileEntry.getTitle(),
886                                    fileEntry.getDescription(), null, null, 0, 0, null, false);
887    
888                            List<DLFileShortcut> fileShortcuts =
889                                    dlFileShortcutPersistence.findByG_TF_TN(
890                                            fileEntry.getGroupId(), fileEntry.getFolderId(),
891                                            fileEntry.getName());
892    
893                            for (DLFileShortcut fileShortcut : fileShortcuts) {
894                                    assetEntryLocalService.updateEntry(
895                                            userId, fileShortcut.getGroupId(),
896                                            DLFileShortcut.class.getName(),
897                                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(),
898                                            assetCategoryIds, assetTagNames, true, null, null, null,
899                                            null, mimeType, fileEntry.getTitle(),
900                                            fileEntry.getDescription(), null, null, 0, 0, null, false);
901                            }
902                    }
903            }
904    
905            public DLFileEntry updateFileEntry(
906                            long userId, long groupId, long folderId, String name,
907                            String sourceFileName, String title, String description,
908                            String changeLog, boolean majorVersion, String extraSettings,
909                            byte[] bytes, ServiceContext serviceContext)
910                    throws PortalException, SystemException {
911    
912                    InputStream is = null;
913                    long size = 0;
914    
915                    if (bytes != null) {
916                            is = new UnsyncByteArrayInputStream(bytes);
917                            size = bytes.length;
918                    }
919    
920                    return updateFileEntry(
921                            userId, groupId, folderId, name, sourceFileName, title, description,
922                            changeLog, majorVersion, extraSettings, is, size, serviceContext);
923            }
924    
925            public DLFileEntry updateFileEntry(
926                            long userId, long groupId, long folderId, String name,
927                            String sourceFileName, String title, String description,
928                            String changeLog, boolean majorVersion, String extraSettings,
929                            File file, ServiceContext serviceContext)
930                    throws PortalException, SystemException {
931    
932                    try {
933                            InputStream is = null;
934                            long size = 0;
935    
936                            if ((file != null) && file.exists()) {
937                                    is = new UnsyncBufferedInputStream(new FileInputStream(file));
938                                    size = file.length();
939                            }
940    
941                            return updateFileEntry(
942                                    userId, groupId, folderId, name, sourceFileName, title,
943                                    description, changeLog, majorVersion, extraSettings, is, size,
944                                    serviceContext);
945                    }
946                    catch (FileNotFoundException fnfe) {
947                            throw new NoSuchFileException();
948                    }
949            }
950    
951            public DLFileEntry updateFileEntry(
952                            long userId, long groupId, long folderId, String name,
953                            String sourceFileName, String title, String description,
954                            String changeLog, boolean majorVersion, String extraSettings,
955                            InputStream is, long size, ServiceContext serviceContext)
956                    throws PortalException, SystemException {
957    
958                    // File entry
959    
960                    User user = userPersistence.findByPrimaryKey(userId);
961    
962                    if (Validator.isNull(title)) {
963                            title = sourceFileName;
964    
965                            if (Validator.isNull(title)) {
966                                    title = name;
967                            }
968                    }
969    
970                    Date now = new Date();
971    
972                    DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
973                            groupId, folderId, name);
974    
975                    validate(
976                            groupId, folderId, name, fileEntry.getExtension(), title,
977                            sourceFileName, is);
978    
979                    // File version
980    
981                    String version = getNextVersion(
982                            fileEntry, majorVersion, serviceContext.getWorkflowAction());
983    
984                    DLFileVersion fileVersion = null;
985    
986                    String extension = null;
987    
988                    if (Validator.isNotNull(sourceFileName)) {
989                            extension = FileUtil.getExtension(sourceFileName);
990                    }
991                    else {
992                            extension = fileEntry.getExtension();
993                    }
994    
995                    boolean updatedFileVersion = false;
996    
997                    try {
998                            DLFileVersion latestFileVersion = fileEntry.getLatestFileVersion();
999    
1000                            if (size == 0) {
1001                                    size = latestFileVersion.getSize();
1002                            }
1003    
1004                            if (!latestFileVersion.isApproved()) {
1005                                    if (!PropsValues.DL_FILE_ENTRY_DRAFTS_ENABLED) {
1006                                            serviceContext.setWorkflowAction(
1007                                                    WorkflowConstants.ACTION_SAVE_DRAFT);
1008    
1009                                            version = latestFileVersion.getVersion();
1010                                    }
1011    
1012                                    if (version.equals(latestFileVersion.getVersion())) {
1013                                            updatedFileVersion = true;
1014                                    }
1015    
1016                                    updateFileVersion(
1017                                            user, latestFileVersion, sourceFileName, extension, title,
1018                                            description, changeLog, extraSettings, version, size,
1019                                            latestFileVersion.getStatus(),
1020                                            serviceContext.getModifiedDate(now), serviceContext);
1021                            }
1022                            else {
1023                                    fileVersion = addFileVersion(
1024                                            user, fileEntry, serviceContext.getModifiedDate(now),
1025                                            extension, title, description, changeLog, extraSettings,
1026                                            version, size, WorkflowConstants.STATUS_DRAFT,
1027                                            serviceContext);
1028                            }
1029    
1030                            if (fileVersion == null) {
1031                                    fileVersion = latestFileVersion;
1032                            }
1033                    }
1034                    catch (NoSuchFileVersionException nsfve) {
1035                            fileVersion = addFileVersion(
1036                                    user, fileEntry, serviceContext.getModifiedDate(now),
1037                                    extension, title, description, changeLog, extraSettings,
1038                                    version, size, WorkflowConstants.STATUS_DRAFT, serviceContext);
1039                    }
1040    
1041                    if ((is == null) && !updatedFileVersion) {
1042                            int fetchFailures = 0;
1043    
1044                            while (is == null) {
1045                                    try {
1046                                            is = dlLocalService.getFileAsStream(
1047                                                    user.getCompanyId(), fileEntry.getRepositoryId(), name);
1048                                    }
1049                                    catch (NoSuchFileException nsfe) {
1050                                            fetchFailures++;
1051    
1052                                            if (PropsValues.DL_HOOK_IMPL.equals(
1053                                                            JCRHook.class.getName()) &&
1054                                                    (fetchFailures <
1055                                                            PropsValues.DL_HOOK_JCR_FETCH_MAX_FAILURES)) {
1056    
1057                                                    try {
1058                                                            Thread.sleep(PropsValues.DL_HOOK_JCR_FETCH_DELAY);
1059                                                    }
1060                                                    catch (InterruptedException ie) {
1061                                                    }
1062                                            }
1063                                            else {
1064                                                    throw nsfe;
1065                                            }
1066                                    }
1067                            }
1068                    }
1069    
1070                    // Asset
1071    
1072                    updateAsset(
1073                            userId, fileEntry, fileVersion,
1074                            serviceContext.getAssetCategoryIds(),
1075                            serviceContext.getAssetTagNames());
1076    
1077                    // Folder
1078    
1079                    if (fileEntry.getFolderId() !=
1080                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1081    
1082                            DLFolder folder = dlFolderPersistence.findByPrimaryKey(
1083                                    fileEntry.getFolderId());
1084    
1085                            folder.setLastPostDate(fileEntry.getModifiedDate());
1086    
1087                            dlFolderPersistence.update(folder, false);
1088                    }
1089    
1090                    // File
1091    
1092                    if (is != null) {
1093                            try {
1094                                    dlService.deleteFile(
1095                                            user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1096                                            fileEntry.getRepositoryId(), fileEntry.getName(), version);
1097                            }
1098                            catch (NoSuchFileException nsfe) {
1099                            }
1100    
1101                            dlLocalService.updateFile(
1102                                    user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1103                                    fileEntry.getGroupId(), fileEntry.getRepositoryId(), name,
1104                                    fileEntry.getExtension(), false, version, sourceFileName,
1105                                    fileEntry.getFileEntryId(), fileEntry.getLuceneProperties(),
1106                                    fileEntry.getModifiedDate(), serviceContext, is);
1107                    }
1108    
1109                    // Workflow
1110    
1111                    if (serviceContext.getWorkflowAction() ==
1112                                    WorkflowConstants.ACTION_PUBLISH) {
1113    
1114                            WorkflowHandlerRegistryUtil.startWorkflowInstance(
1115                                    user.getCompanyId(), groupId, userId,
1116                                    DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1117                                    fileEntry, serviceContext);
1118                    }
1119    
1120                    return fileEntry;
1121            }
1122    
1123            public DLFileEntry updateStatus(
1124                            long userId, long fileEntryId, int status,
1125                            ServiceContext serviceContext)
1126                    throws PortalException, SystemException {
1127    
1128                    // File entry
1129    
1130                    User user = userPersistence.findByPrimaryKey(userId);
1131    
1132                    DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
1133                            fileEntryId);
1134    
1135                    // File version
1136    
1137                    DLFileVersion latestFileVersion =
1138                            dlFileVersionLocalService.getLatestFileVersion(
1139                                    fileEntry.getGroupId(), fileEntry.getFolderId(),
1140                                    fileEntry.getName());
1141    
1142                    latestFileVersion.setStatus(status);
1143                    latestFileVersion.setStatusByUserId(user.getUserId());
1144                    latestFileVersion.setStatusByUserName(user.getFullName());
1145                    latestFileVersion.setStatusDate(new Date());
1146    
1147                    dlFileVersionPersistence.update(latestFileVersion, false);
1148    
1149                    if (status == WorkflowConstants.STATUS_APPROVED) {
1150    
1151                            // File entry
1152    
1153                            if (DLUtil.compareVersions(
1154                                            fileEntry.getVersion(),
1155                                            latestFileVersion.getVersion()) <= 0) {
1156    
1157                                    fileEntry.setTitle(latestFileVersion.getTitle());
1158                                    fileEntry.setDescription(latestFileVersion.getDescription());
1159                                    fileEntry.setExtraSettings(
1160                                            latestFileVersion.getExtraSettings());
1161                                    fileEntry.setVersion(latestFileVersion.getVersion());
1162                                    fileEntry.setVersionUserId(latestFileVersion.getUserId());
1163                                    fileEntry.setVersionUserName(latestFileVersion.getUserName());
1164                                    fileEntry.setModifiedDate(latestFileVersion.getCreateDate());
1165                                    fileEntry.setSize(latestFileVersion.getSize());
1166    
1167                                    dlFileEntryPersistence.update(fileEntry, false);
1168                            }
1169    
1170                            // Asset
1171    
1172                            if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1173                                    if ((latestFileVersion.getVersion() !=
1174                                                    DLFileEntryConstants.DEFAULT_VERSION)) {
1175    
1176                                            AssetEntry draftAssetEntry = null;
1177    
1178                                            try {
1179                                                    draftAssetEntry = assetEntryLocalService.getEntry(
1180                                                            DLFileEntry.class.getName(),
1181                                                            latestFileVersion.getPrimaryKey());
1182    
1183                                                    long[] assetCategoryIds =
1184                                                            draftAssetEntry.getCategoryIds();
1185                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
1186    
1187                                                    assetEntryLocalService.updateEntry(
1188                                                            userId, fileEntry.getGroupId(),
1189                                                            DLFileEntry.class.getName(),
1190                                                            fileEntry.getFileEntryId(), fileEntry.getUuid(),
1191                                                            assetCategoryIds, assetTagNames, true, null, null,
1192                                                            null, null, draftAssetEntry.getMimeType(),
1193                                                            fileEntry.getTitle(), fileEntry.getDescription(),
1194                                                            null, null, 0, 0, null, false);
1195    
1196                                                    assetEntryLocalService.deleteEntry(
1197                                                            draftAssetEntry.getEntryId());
1198                                            }
1199                                            catch (NoSuchEntryException nsee) {
1200                                            }
1201                                    }
1202    
1203                                    assetEntryLocalService.updateVisible(
1204                                            DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1205                                            true);
1206                            }
1207    
1208                            // Social
1209    
1210                            socialActivityLocalService.addUniqueActivity(
1211                                    latestFileVersion.getUserId(), latestFileVersion.getGroupId(),
1212                                    latestFileVersion.getCreateDate(), DLFileEntry.class.getName(),
1213                                    fileEntryId, DLActivityKeys.ADD_FILE_ENTRY,
1214                                    StringPool.BLANK, 0);
1215    
1216                            // Indexer
1217    
1218                            Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
1219    
1220                            indexer.reindex(fileEntry);
1221                    }
1222                    else {
1223    
1224                            // File entry
1225    
1226                            if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1227                                    String newVersion = DLFileEntryConstants.DEFAULT_VERSION;
1228    
1229                                    List<DLFileVersion> approvedFileVersions =
1230                                            dlFileVersionPersistence.findByG_F_N_S(
1231                                                    fileEntry.getGroupId(), fileEntry.getFolderId(),
1232                                                    fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
1233    
1234                                    if (!approvedFileVersions.isEmpty()) {
1235                                            newVersion = approvedFileVersions.get(0).getVersion();
1236                                    }
1237    
1238                                    fileEntry.setVersion(newVersion);
1239    
1240                                    dlFileEntryPersistence.update(fileEntry, false);
1241                            }
1242    
1243                            // Asset
1244    
1245                            if (Validator.isNull(fileEntry.getVersion())) {
1246                                    assetEntryLocalService.updateVisible(
1247                                            DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1248                                            false);
1249                            }
1250    
1251                            // Indexer
1252    
1253                            if (latestFileVersion.getVersion().equals(
1254                                            DLFileEntryConstants.DEFAULT_VERSION)) {
1255    
1256                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
1257                                            DLFileEntry.class);
1258    
1259                                    indexer.delete(fileEntry);
1260                            }
1261                    }
1262    
1263                    return fileEntry;
1264            }
1265    
1266            protected DLFileVersion addFileVersion(
1267                            User user, DLFileEntry fileEntry, Date modifiedDate,
1268                            String extension, String title, String description,
1269                            String changeLog, String extraSettings, String version, long size,
1270                            int status, ServiceContext serviceContext)
1271                    throws SystemException {
1272    
1273                    long fileVersionId = counterLocalService.increment();
1274    
1275                    DLFileVersion fileVersion = dlFileVersionPersistence.create(
1276                            fileVersionId);
1277    
1278                    long versionUserId = fileEntry.getVersionUserId();
1279    
1280                    if (versionUserId <= 0) {
1281                            versionUserId = fileEntry.getUserId();
1282                    }
1283    
1284                    String versionUserName = GetterUtil.getString(
1285                            fileEntry.getVersionUserName(), fileEntry.getUserName());
1286    
1287                    fileVersion.setGroupId(fileEntry.getGroupId());
1288                    fileVersion.setCompanyId(fileEntry.getCompanyId());
1289                    fileVersion.setUserId(versionUserId);
1290                    fileVersion.setUserName(versionUserName);
1291                    fileVersion.setCreateDate(modifiedDate);
1292                    fileVersion.setFolderId(fileEntry.getFolderId());
1293                    fileVersion.setName(fileEntry.getName());
1294                    fileVersion.setExtension(extension);
1295                    fileVersion.setTitle(title);
1296                    fileVersion.setDescription(description);
1297                    fileVersion.setChangeLog(changeLog);
1298                    fileVersion.setExtraSettings(extraSettings);
1299                    fileVersion.setVersion(version);
1300                    fileVersion.setSize(size);
1301                    fileVersion.setStatus(status);
1302                    fileVersion.setStatusByUserId(user.getUserId());
1303                    fileVersion.setStatusByUserName(user.getFullName());
1304                    fileVersion.setStatusDate(fileEntry.getModifiedDate());
1305                    fileVersion.setExpandoBridgeAttributes(serviceContext);
1306    
1307                    dlFileVersionPersistence.update(fileVersion, false);
1308    
1309                    return fileVersion;
1310            }
1311    
1312            protected long getFolderId(long companyId, long folderId)
1313                    throws SystemException {
1314    
1315                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1316    
1317                            // Ensure folder exists and belongs to the proper company
1318    
1319                            DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
1320    
1321                            if ((folder == null) || (companyId != folder.getCompanyId())) {
1322                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1323                            }
1324                    }
1325    
1326                    return folderId;
1327            }
1328    
1329            protected String getNextVersion(
1330                            DLFileEntry fileEntry, boolean majorVersion, int workflowAction)
1331                    throws PortalException, SystemException {
1332    
1333                    if (Validator.isNull(fileEntry.getVersion())) {
1334                            return DLFileEntryConstants.DEFAULT_VERSION;
1335                    }
1336    
1337                    try {
1338                            DLFileVersion fileVersion = fileEntry.getLatestFileVersion();
1339    
1340                            String version = fileVersion.getVersion();
1341    
1342                            if (!fileVersion.isApproved() &&
1343                                    version.equals(DLFileEntryConstants.DEFAULT_VERSION)) {
1344    
1345                                    return DLFileEntryConstants.DEFAULT_VERSION;
1346                            }
1347                    }
1348                    catch (NoSuchFileVersionException nsfve) {
1349                    }
1350    
1351                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1352                            majorVersion = false;
1353                    }
1354    
1355                    int[] versionParts = StringUtil.split(
1356                            fileEntry.getVersion(), StringPool.PERIOD, 0);
1357    
1358                    if (majorVersion) {
1359                            versionParts[0]++;
1360                            versionParts[1] = 0;
1361                    }
1362                    else {
1363                            versionParts[1]++;
1364                    }
1365    
1366                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
1367            }
1368    
1369            protected void updateFileVersion(
1370                            User user, DLFileVersion fileVersion, String sourceFileName,
1371                            String extension, String title, String description,
1372                            String changeLog, String extraSettings, String version, long size,
1373                            int status, Date statusDate, ServiceContext serviceContext)
1374                    throws SystemException {
1375    
1376                    if (Validator.isNotNull(sourceFileName)) {
1377                            fileVersion.setExtension(extension);
1378                    }
1379    
1380                    fileVersion.setTitle(title);
1381                    fileVersion.setDescription(description);
1382                    fileVersion.setChangeLog(changeLog);
1383                    fileVersion.setExtraSettings(extraSettings);
1384                    fileVersion.setVersion(version);
1385                    fileVersion.setSize(size);
1386                    fileVersion.setStatus(status);
1387                    fileVersion.setStatusByUserId(user.getUserId());
1388                    fileVersion.setStatusByUserName(user.getFullName());
1389                    fileVersion.setStatusDate(statusDate);
1390                    fileVersion.setExpandoBridgeAttributes(serviceContext);
1391    
1392                    dlFileVersionPersistence.update(fileVersion, false);
1393            }
1394    
1395            protected void validate(
1396                            long groupId, long folderId, String title, InputStream is)
1397                    throws PortalException, SystemException {
1398    
1399                    dlLocalService.validate(title, true, is);
1400    
1401                    validate(groupId, folderId, null, title);
1402            }
1403    
1404            protected void validate(
1405                            long groupId, long folderId, String name, String title)
1406                    throws PortalException, SystemException {
1407    
1408                    try {
1409                            dlFolderLocalService.getFolder(groupId, folderId, title);
1410    
1411                            throw new DuplicateFolderNameException();
1412                    }
1413                    catch (NoSuchFolderException nsfe) {
1414                    }
1415    
1416                    try {
1417                            DLFileEntry fileEntry =
1418                                    dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
1419    
1420                            if (!fileEntry.getName().equals(name)) {
1421                                    throw new DuplicateFileException(title);
1422                            }
1423                    }
1424                    catch (NoSuchFileEntryException nsfee) {
1425                    }
1426            }
1427    
1428            protected void validate(
1429                            long groupId, long folderId, String name, String extension,
1430                            String title, String sourceFileName, InputStream is)
1431                    throws PortalException, SystemException {
1432    
1433                    if (Validator.isNotNull(sourceFileName)) {
1434                            dlLocalService.validate(
1435                                    sourceFileName, extension, sourceFileName, true, is);
1436                    }
1437    
1438                    validate(groupId, folderId, name, title);
1439            }
1440    
1441            private static Log _log = LogFactoryUtil.getLog(
1442                    DLFileEntryLocalServiceImpl.class);
1443    
1444    }