001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    if ((fileVersion != null) && !fileVersion.isApproved() &&
852                            (fileVersion.getVersion() !=
853                                    DLFileEntryConstants.DEFAULT_VERSION)) {
854    
855                            int approvedArticlesCount =
856                                    dlFileVersionPersistence.countByG_F_N_S(
857                                            fileEntry.getGroupId(), fileEntry.getFolderId(),
858                                            fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
859    
860                            if (approvedArticlesCount > 0) {
861                                    addDraftAssetEntry = true;
862                            }
863                    }
864    
865                    if (addDraftAssetEntry) {
866                            assetEntryLocalService.updateEntry(
867                                    userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
868                                    fileVersion.getFileVersionId(), fileEntry.getUuid(),
869                                    assetCategoryIds, assetTagNames, false, null, null, null, null,
870                                    mimeType, fileEntry.getTitle(), fileEntry.getDescription(),
871                                    null, null, 0, 0, null, false);
872                    }
873                    else {
874                            boolean visible = true;
875    
876                            if ((fileVersion != null) && !fileVersion.isApproved()) {
877                                    visible = false;
878                            }
879    
880                            assetEntryLocalService.updateEntry(
881                                    userId, fileEntry.getGroupId(), DLFileEntry.class.getName(),
882                                    fileEntry.getFileEntryId(), fileEntry.getUuid(),
883                                    assetCategoryIds, assetTagNames, visible, null, null, null,
884                                    null, mimeType, fileEntry.getTitle(),
885                                    fileEntry.getDescription(), null, null, 0, 0, null, false);
886    
887                            List<DLFileShortcut> fileShortcuts =
888                                    dlFileShortcutPersistence.findByG_TF_TN(
889                                            fileEntry.getGroupId(), fileEntry.getFolderId(),
890                                            fileEntry.getName());
891    
892                            for (DLFileShortcut fileShortcut : fileShortcuts) {
893                                    assetEntryLocalService.updateEntry(
894                                            userId, fileShortcut.getGroupId(),
895                                            DLFileShortcut.class.getName(),
896                                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(),
897                                            assetCategoryIds, assetTagNames, true, null, null, null,
898                                            null, mimeType, fileEntry.getTitle(),
899                                            fileEntry.getDescription(), null, null, 0, 0, null, false);
900                            }
901                    }
902            }
903    
904            public DLFileEntry updateFileEntry(
905                            long userId, long groupId, long folderId, String name,
906                            String sourceFileName, String title, String description,
907                            String changeLog, boolean majorVersion, String extraSettings,
908                            byte[] bytes, ServiceContext serviceContext)
909                    throws PortalException, SystemException {
910    
911                    InputStream is = null;
912                    long size = 0;
913    
914                    if (bytes != null) {
915                            is = new UnsyncByteArrayInputStream(bytes);
916                            size = bytes.length;
917                    }
918    
919                    return updateFileEntry(
920                            userId, groupId, folderId, name, sourceFileName, title, description,
921                            changeLog, majorVersion, extraSettings, is, size, serviceContext);
922            }
923    
924            public DLFileEntry updateFileEntry(
925                            long userId, long groupId, long folderId, String name,
926                            String sourceFileName, String title, String description,
927                            String changeLog, boolean majorVersion, String extraSettings,
928                            File file, ServiceContext serviceContext)
929                    throws PortalException, SystemException {
930    
931                    try {
932                            InputStream is = null;
933                            long size = 0;
934    
935                            if ((file != null) && file.exists()) {
936                                    is = new UnsyncBufferedInputStream(new FileInputStream(file));
937                                    size = file.length();
938                            }
939    
940                            return updateFileEntry(
941                                    userId, groupId, folderId, name, sourceFileName, title,
942                                    description, changeLog, majorVersion, extraSettings, is, size,
943                                    serviceContext);
944                    }
945                    catch (FileNotFoundException fnfe) {
946                            throw new NoSuchFileException();
947                    }
948            }
949    
950            public DLFileEntry updateFileEntry(
951                            long userId, long groupId, long folderId, String name,
952                            String sourceFileName, String title, String description,
953                            String changeLog, boolean majorVersion, String extraSettings,
954                            InputStream is, long size, ServiceContext serviceContext)
955                    throws PortalException, SystemException {
956    
957                    // File entry
958    
959                    User user = userPersistence.findByPrimaryKey(userId);
960    
961                    if (Validator.isNull(title)) {
962                            title = sourceFileName;
963    
964                            if (Validator.isNull(title)) {
965                                    title = name;
966                            }
967                    }
968    
969                    Date now = new Date();
970    
971                    DLFileEntry fileEntry = dlFileEntryPersistence.findByG_F_N(
972                            groupId, folderId, name);
973    
974                    validate(
975                            groupId, folderId, name, fileEntry.getExtension(), title,
976                            sourceFileName, is);
977    
978                    // File version
979    
980                    String version = getNextVersion(
981                            fileEntry, majorVersion, serviceContext.getWorkflowAction());
982    
983                    DLFileVersion fileVersion = null;
984    
985                    String extension = null;
986    
987                    if (Validator.isNotNull(sourceFileName)) {
988                            extension = FileUtil.getExtension(sourceFileName);
989                    }
990                    else {
991                            extension = fileEntry.getExtension();
992                    }
993    
994                    boolean updatedFileVersion = false;
995    
996                    try {
997                            DLFileVersion latestFileVersion =
998                                    dlFileVersionLocalService.getLatestFileVersion(
999                                            groupId, folderId, name);
1000    
1001                            if (size == 0) {
1002                                    size = latestFileVersion.getSize();
1003                            }
1004    
1005                            if (latestFileVersion.getStatus() !=
1006                                            WorkflowConstants.STATUS_APPROVED) {
1007    
1008                                    serviceContext.setWorkflowAction(
1009                                            WorkflowConstants.ACTION_SAVE_DRAFT);
1010    
1011                                    version = latestFileVersion.getVersion();
1012    
1013                                    updatedFileVersion = true;
1014    
1015                                    updateFileVersion(
1016                                            user, latestFileVersion, sourceFileName, extension, title,
1017                                            description, changeLog, extraSettings, version, size,
1018                                            latestFileVersion.getStatus(),
1019                                            serviceContext.getModifiedDate(now), serviceContext);
1020                            }
1021                            else {
1022                                    fileVersion = addFileVersion(
1023                                            user, fileEntry, serviceContext.getModifiedDate(now),
1024                                            extension, title, description, changeLog, extraSettings,
1025                                            version, size, WorkflowConstants.STATUS_DRAFT,
1026                                            serviceContext);
1027                            }
1028    
1029                            if (fileVersion == null) {
1030                                    fileVersion = latestFileVersion;
1031                            }
1032                    }
1033                    catch (NoSuchFileVersionException nsfve) {
1034                            fileVersion = addFileVersion(
1035                                    user, fileEntry, serviceContext.getModifiedDate(now),
1036                                    extension, title, description, changeLog, extraSettings,
1037                                    version, size, WorkflowConstants.STATUS_DRAFT, serviceContext);
1038                    }
1039    
1040                    if ((is == null) && !updatedFileVersion) {
1041                            int fetchFailures = 0;
1042    
1043                            while (is == null) {
1044                                    try {
1045                                            is = dlLocalService.getFileAsStream(
1046                                                    user.getCompanyId(), fileEntry.getRepositoryId(), name);
1047                                    }
1048                                    catch (NoSuchFileException nsfe) {
1049                                            fetchFailures++;
1050    
1051                                            if (PropsValues.DL_HOOK_IMPL.equals(
1052                                                            JCRHook.class.getName()) &&
1053                                                    (fetchFailures <
1054                                                            PropsValues.DL_HOOK_JCR_FETCH_MAX_FAILURES)) {
1055    
1056                                                    try {
1057                                                            Thread.sleep(PropsValues.DL_HOOK_JCR_FETCH_DELAY);
1058                                                    }
1059                                                    catch (InterruptedException ie) {
1060                                                    }
1061                                            }
1062                                            else {
1063                                                    throw nsfe;
1064                                            }
1065                                    }
1066                            }
1067                    }
1068    
1069                    // Asset
1070    
1071                    updateAsset(
1072                            userId, fileEntry, fileVersion,
1073                            serviceContext.getAssetCategoryIds(),
1074                            serviceContext.getAssetTagNames());
1075    
1076                    // Folder
1077    
1078                    if (fileEntry.getFolderId() !=
1079                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1080    
1081                            DLFolder folder = dlFolderPersistence.findByPrimaryKey(
1082                                    fileEntry.getFolderId());
1083    
1084                            folder.setLastPostDate(fileEntry.getModifiedDate());
1085    
1086                            dlFolderPersistence.update(folder, false);
1087                    }
1088    
1089                    // File
1090    
1091                    if (is != null) {
1092                            try {
1093                                    dlService.deleteFile(
1094                                            user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1095                                            fileEntry.getRepositoryId(), fileEntry.getName(), version);
1096                            }
1097                            catch (NoSuchFileException nsfe) {
1098                            }
1099    
1100                            dlLocalService.updateFile(
1101                                    user.getCompanyId(), PortletKeys.DOCUMENT_LIBRARY,
1102                                    fileEntry.getGroupId(), fileEntry.getRepositoryId(), name,
1103                                    fileEntry.getExtension(), false, version, sourceFileName,
1104                                    fileEntry.getFileEntryId(), fileEntry.getLuceneProperties(),
1105                                    fileEntry.getModifiedDate(), serviceContext, is);
1106                    }
1107    
1108                    // Workflow
1109    
1110                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
1111                            user.getCompanyId(), groupId, userId, DLFileEntry.class.getName(),
1112                            fileEntry.getFileEntryId(), fileEntry, serviceContext);
1113    
1114                    return fileEntry;
1115            }
1116    
1117            public DLFileEntry updateStatus(
1118                            long userId, long fileEntryId, int status,
1119                            ServiceContext serviceContext)
1120                    throws PortalException, SystemException {
1121    
1122                    // File entry
1123    
1124                    User user = userPersistence.findByPrimaryKey(userId);
1125    
1126                    DLFileEntry fileEntry = dlFileEntryPersistence.findByPrimaryKey(
1127                            fileEntryId);
1128    
1129                    // File version
1130    
1131                    DLFileVersion latestFileVersion =
1132                            dlFileVersionLocalService.getLatestFileVersion(
1133                                    fileEntry.getGroupId(), fileEntry.getFolderId(),
1134                                    fileEntry.getName());
1135    
1136                    latestFileVersion.setStatus(status);
1137                    latestFileVersion.setStatusByUserId(user.getUserId());
1138                    latestFileVersion.setStatusByUserName(user.getFullName());
1139                    latestFileVersion.setStatusDate(new Date());
1140    
1141                    dlFileVersionPersistence.update(latestFileVersion, false);
1142    
1143                    if (status == WorkflowConstants.STATUS_APPROVED) {
1144    
1145                            // File entry
1146    
1147                            if (DLUtil.compareVersions(
1148                                            fileEntry.getVersion(),
1149                                            latestFileVersion.getVersion()) < 0) {
1150    
1151                                    fileEntry.setTitle(latestFileVersion.getTitle());
1152                                    fileEntry.setDescription(latestFileVersion.getDescription());
1153                                    fileEntry.setExtraSettings(
1154                                            latestFileVersion.getExtraSettings());
1155                                    fileEntry.setVersion(latestFileVersion.getVersion());
1156                                    fileEntry.setVersionUserId(latestFileVersion.getUserId());
1157                                    fileEntry.setVersionUserName(latestFileVersion.getUserName());
1158                                    fileEntry.setModifiedDate(latestFileVersion.getCreateDate());
1159                                    fileEntry.setSize(latestFileVersion.getSize());
1160    
1161                                    dlFileEntryPersistence.update(fileEntry, false);
1162                            }
1163    
1164                            // Asset
1165    
1166                            if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1167                                    if ((latestFileVersion.getVersion() !=
1168                                                    DLFileEntryConstants.DEFAULT_VERSION)) {
1169    
1170                                            AssetEntry draftAssetEntry = null;
1171    
1172                                            try {
1173                                                    draftAssetEntry = assetEntryLocalService.getEntry(
1174                                                            DLFileEntry.class.getName(),
1175                                                            latestFileVersion.getPrimaryKey());
1176    
1177                                                    long[] assetCategoryIds =
1178                                                            draftAssetEntry.getCategoryIds();
1179                                                    String[] assetTagNames = draftAssetEntry.getTagNames();
1180    
1181                                                    assetEntryLocalService.updateEntry(
1182                                                            userId, fileEntry.getGroupId(),
1183                                                            DLFileEntry.class.getName(),
1184                                                            fileEntry.getFileEntryId(), fileEntry.getUuid(),
1185                                                            assetCategoryIds, assetTagNames, true, null, null,
1186                                                            null, null, draftAssetEntry.getMimeType(),
1187                                                            fileEntry.getTitle(), fileEntry.getDescription(),
1188                                                            null, null, 0, 0, null, false);
1189    
1190                                                    assetEntryLocalService.deleteEntry(
1191                                                            draftAssetEntry.getEntryId());
1192                                            }
1193                                            catch (NoSuchEntryException nsee) {
1194                                            }
1195                                    }
1196    
1197                                    assetEntryLocalService.updateVisible(
1198                                            DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1199                                            true);
1200                            }
1201    
1202                            // Social
1203    
1204                            socialActivityLocalService.addUniqueActivity(
1205                                    latestFileVersion.getUserId(), latestFileVersion.getGroupId(),
1206                                    latestFileVersion.getCreateDate(), DLFileEntry.class.getName(),
1207                                    fileEntryId, DLActivityKeys.ADD_FILE_ENTRY,
1208                                    StringPool.BLANK, 0);
1209    
1210                            // Indexer
1211    
1212                            Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
1213    
1214                            indexer.reindex(fileEntry);
1215                    }
1216                    else {
1217    
1218                            // File entry
1219    
1220                            if (fileEntry.getVersion().equals(latestFileVersion.getVersion())) {
1221                                    String newVersion = DLFileEntryConstants.DEFAULT_VERSION;
1222    
1223                                    List<DLFileVersion> approvedFileVersions =
1224                                            dlFileVersionPersistence.findByG_F_N_S(
1225                                                    fileEntry.getGroupId(), fileEntry.getFolderId(),
1226                                                    fileEntry.getName(), WorkflowConstants.STATUS_APPROVED);
1227    
1228                                    if (!approvedFileVersions.isEmpty()) {
1229                                            newVersion = approvedFileVersions.get(0).getVersion();
1230                                    }
1231    
1232                                    fileEntry.setVersion(newVersion);
1233    
1234                                    dlFileEntryPersistence.update(fileEntry, false);
1235                            }
1236    
1237                            // Asset
1238    
1239                            if (Validator.isNull(fileEntry.getVersion())) {
1240                                    assetEntryLocalService.updateVisible(
1241                                            DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
1242                                            false);
1243                            }
1244    
1245                            // Indexer
1246    
1247                            if (latestFileVersion.getVersion().equals(
1248                                            DLFileEntryConstants.DEFAULT_VERSION)) {
1249    
1250                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
1251                                            DLFileEntry.class);
1252    
1253                                    indexer.delete(fileEntry);
1254                            }
1255                    }
1256    
1257                    return fileEntry;
1258            }
1259    
1260            protected DLFileVersion addFileVersion(
1261                            User user, DLFileEntry fileEntry, Date modifiedDate,
1262                            String extension, String title, String description,
1263                            String changeLog, String extraSettings, String version, long size,
1264                            int status, ServiceContext serviceContext)
1265                    throws SystemException {
1266    
1267                    long fileVersionId = counterLocalService.increment();
1268    
1269                    DLFileVersion fileVersion = dlFileVersionPersistence.create(
1270                            fileVersionId);
1271    
1272                    long versionUserId = fileEntry.getVersionUserId();
1273    
1274                    if (versionUserId <= 0) {
1275                            versionUserId = fileEntry.getUserId();
1276                    }
1277    
1278                    String versionUserName = GetterUtil.getString(
1279                            fileEntry.getVersionUserName(), fileEntry.getUserName());
1280    
1281                    fileVersion.setGroupId(fileEntry.getGroupId());
1282                    fileVersion.setCompanyId(fileEntry.getCompanyId());
1283                    fileVersion.setUserId(versionUserId);
1284                    fileVersion.setUserName(versionUserName);
1285                    fileVersion.setCreateDate(modifiedDate);
1286                    fileVersion.setFolderId(fileEntry.getFolderId());
1287                    fileVersion.setName(fileEntry.getName());
1288                    fileVersion.setExtension(extension);
1289                    fileVersion.setTitle(title);
1290                    fileVersion.setDescription(description);
1291                    fileVersion.setChangeLog(changeLog);
1292                    fileVersion.setExtraSettings(extraSettings);
1293                    fileVersion.setVersion(version);
1294                    fileVersion.setSize(size);
1295                    fileVersion.setStatus(status);
1296                    fileVersion.setStatusByUserId(user.getUserId());
1297                    fileVersion.setStatusByUserName(user.getFullName());
1298                    fileVersion.setStatusDate(fileEntry.getModifiedDate());
1299                    fileVersion.setExpandoBridgeAttributes(serviceContext);
1300    
1301                    dlFileVersionPersistence.update(fileVersion, false);
1302    
1303                    return fileVersion;
1304            }
1305    
1306            protected long getFolderId(long companyId, long folderId)
1307                    throws SystemException {
1308    
1309                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1310    
1311                            // Ensure folder exists and belongs to the proper company
1312    
1313                            DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
1314    
1315                            if ((folder == null) || (companyId != folder.getCompanyId())) {
1316                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
1317                            }
1318                    }
1319    
1320                    return folderId;
1321            }
1322    
1323            protected String getNextVersion(
1324                    DLFileEntry fileEntry, boolean majorVersion, int workflowAction) {
1325    
1326                    if (Validator.isNull(fileEntry.getVersion())) {
1327                            return DLFileEntryConstants.DEFAULT_VERSION;
1328                    }
1329    
1330                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1331                            majorVersion = false;
1332                    }
1333    
1334                    int[] versionParts = StringUtil.split(
1335                            fileEntry.getVersion(), StringPool.PERIOD, 0);
1336    
1337                    if (majorVersion) {
1338                            versionParts[0]++;
1339                            versionParts[1] = 0;
1340                    }
1341                    else {
1342                            versionParts[1]++;
1343                    }
1344    
1345                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
1346            }
1347    
1348            protected void updateFileVersion(
1349                            User user, DLFileVersion fileVersion, String sourceFileName,
1350                            String extension, String title, String description,
1351                            String changeLog, String extraSettings, String version, long size,
1352                            int status, Date statusDate, ServiceContext serviceContext)
1353                    throws SystemException {
1354    
1355                    if (Validator.isNotNull(sourceFileName)) {
1356                            fileVersion.setExtension(extension);
1357                    }
1358    
1359                    fileVersion.setTitle(title);
1360                    fileVersion.setDescription(description);
1361                    fileVersion.setChangeLog(changeLog);
1362                    fileVersion.setExtraSettings(extraSettings);
1363                    fileVersion.setVersion(version);
1364                    fileVersion.setSize(size);
1365                    fileVersion.setStatus(status);
1366                    fileVersion.setStatusByUserId(user.getUserId());
1367                    fileVersion.setStatusByUserName(user.getFullName());
1368                    fileVersion.setStatusDate(statusDate);
1369                    fileVersion.setExpandoBridgeAttributes(serviceContext);
1370    
1371                    dlFileVersionPersistence.update(fileVersion, false);
1372            }
1373    
1374            protected void validate(
1375                            long groupId, long folderId, String title, InputStream is)
1376                    throws PortalException, SystemException {
1377    
1378                    dlLocalService.validate(title, true, is);
1379    
1380                    validate(groupId, folderId, null, title);
1381            }
1382    
1383            protected void validate(
1384                            long groupId, long folderId, String name, String title)
1385                    throws PortalException, SystemException {
1386    
1387                    try {
1388                            dlFolderLocalService.getFolder(groupId, folderId, title);
1389    
1390                            throw new DuplicateFolderNameException();
1391                    }
1392                    catch (NoSuchFolderException nsfe) {
1393                    }
1394    
1395                    try {
1396                            DLFileEntry fileEntry =
1397                                    dlFileEntryPersistence.findByG_F_T(groupId, folderId, title);
1398    
1399                            if (!fileEntry.getName().equals(name)) {
1400                                    throw new DuplicateFileException(title);
1401                            }
1402                    }
1403                    catch (NoSuchFileEntryException nsfee) {
1404                    }
1405            }
1406    
1407            protected void validate(
1408                            long groupId, long folderId, String name, String extension,
1409                            String title, String sourceFileName, InputStream is)
1410                    throws PortalException, SystemException {
1411    
1412                    if (Validator.isNotNull(sourceFileName)) {
1413                            dlLocalService.validate(
1414                                    sourceFileName, extension, sourceFileName, true, is);
1415                    }
1416    
1417                    validate(groupId, folderId, name, title);
1418            }
1419    
1420            private static Log _log = LogFactoryUtil.getLog(
1421                    DLFileEntryLocalServiceImpl.class);
1422    
1423    }