001    /**
002     * Copyright (c) 2000-2012 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.portal.ExpiredLockException;
018    import com.liferay.portal.InvalidLockException;
019    import com.liferay.portal.NoSuchLockException;
020    import com.liferay.portal.NoSuchModelException;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.image.ImageBag;
024    import com.liferay.portal.kernel.image.ImageToolUtil;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.search.Indexable;
028    import com.liferay.portal.kernel.search.IndexableType;
029    import com.liferay.portal.kernel.search.Indexer;
030    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
031    import com.liferay.portal.kernel.search.SearchException;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.ParamUtil;
035    import com.liferay.portal.kernel.util.PropsKeys;
036    import com.liferay.portal.kernel.util.StringBundler;
037    import com.liferay.portal.kernel.util.StringPool;
038    import com.liferay.portal.kernel.util.StringUtil;
039    import com.liferay.portal.kernel.util.UnicodeProperties;
040    import com.liferay.portal.kernel.util.Validator;
041    import com.liferay.portal.kernel.workflow.WorkflowConstants;
042    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
043    import com.liferay.portal.model.Image;
044    import com.liferay.portal.model.Lock;
045    import com.liferay.portal.model.ResourceConstants;
046    import com.liferay.portal.model.User;
047    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
048    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
049    import com.liferay.portal.security.auth.PrincipalThreadLocal;
050    import com.liferay.portal.service.ServiceContext;
051    import com.liferay.portal.util.PortalUtil;
052    import com.liferay.portal.util.PrefsPropsUtil;
053    import com.liferay.portal.util.PropsValues;
054    import com.liferay.portlet.documentlibrary.DuplicateFileException;
055    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
056    import com.liferay.portlet.documentlibrary.FileNameException;
057    import com.liferay.portlet.documentlibrary.ImageSizeException;
058    import com.liferay.portlet.documentlibrary.InvalidFileEntryTypeException;
059    import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
060    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
061    import com.liferay.portlet.documentlibrary.NoSuchFileEntryMetadataException;
062    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
063    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
064    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
065    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
066    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
067    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
068    import com.liferay.portlet.documentlibrary.model.DLFolder;
069    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
070    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
071    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
072    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
073    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
074    import com.liferay.portlet.documentlibrary.util.DLAppUtil;
075    import com.liferay.portlet.documentlibrary.util.DLUtil;
076    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator;
077    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
078    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
079    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
080    import com.liferay.portlet.expando.model.ExpandoBridge;
081    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
082    
083    import java.awt.image.RenderedImage;
084    
085    import java.io.File;
086    import java.io.IOException;
087    import java.io.InputStream;
088    import java.io.Serializable;
089    
090    import java.util.Date;
091    import java.util.HashMap;
092    import java.util.List;
093    import java.util.Map;
094    
095    /**
096     * The document library file entry local service.
097     *
098     * <p>
099     * Due to legacy code, the names of some file entry properties are not
100     * intuitive. Each file entry has both a name and title. The <code>name</code>
101     * is a unique identifier for a given file and is generally numeric, whereas the
102     * <code>title</code> is the actual name specified by the user (such as
103     * &quot;Budget.xls&quot;).
104     * </p>
105     *
106     * @author Brian Wing Shun Chan
107     * @author Harry Mark
108     * @author Alexander Chow
109     */
110    public class DLFileEntryLocalServiceImpl
111            extends DLFileEntryLocalServiceBaseImpl {
112    
113            public DLFileEntry addFileEntry(
114                            long userId, long groupId, long repositoryId, long folderId,
115                            String sourceFileName, String mimeType, String title,
116                            String description, String changeLog, long fileEntryTypeId,
117                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
118                            ServiceContext serviceContext)
119                    throws PortalException, SystemException {
120    
121                    if (Validator.isNull(title)) {
122                            if (size == 0) {
123                                    throw new FileNameException();
124                            }
125                            else {
126                                    title = sourceFileName;
127                            }
128                    }
129    
130                    // File entry
131    
132                    User user = userPersistence.findByPrimaryKey(userId);
133                    folderId = dlFolderLocalService.getFolderId(
134                            user.getCompanyId(), folderId);
135                    String name = String.valueOf(
136                            counterLocalService.increment(DLFileEntry.class.getName()));
137                    String extension = DLAppUtil.getExtension(title, sourceFileName);
138                    fileEntryTypeId = getFileEntryTypeId(
139                            DLUtil.getGroupIds(groupId), folderId, fileEntryTypeId);
140                    Date now = new Date();
141    
142                    validateFile(
143                            groupId, folderId, 0, title, extension, sourceFileName, file, is);
144    
145                    long fileEntryId = counterLocalService.increment();
146    
147                    DLFileEntry dlFileEntry = dlFileEntryPersistence.create(fileEntryId);
148    
149                    dlFileEntry.setUuid(serviceContext.getUuid());
150                    dlFileEntry.setGroupId(groupId);
151                    dlFileEntry.setCompanyId(user.getCompanyId());
152                    dlFileEntry.setUserId(user.getUserId());
153                    dlFileEntry.setUserName(user.getFullName());
154                    dlFileEntry.setVersionUserId(user.getUserId());
155                    dlFileEntry.setVersionUserName(user.getFullName());
156                    dlFileEntry.setCreateDate(serviceContext.getCreateDate(now));
157                    dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
158                    dlFileEntry.setRepositoryId(repositoryId);
159                    dlFileEntry.setFolderId(folderId);
160                    dlFileEntry.setName(name);
161                    dlFileEntry.setExtension(extension);
162                    dlFileEntry.setMimeType(mimeType);
163                    dlFileEntry.setTitle(title);
164                    dlFileEntry.setDescription(description);
165                    dlFileEntry.setFileEntryTypeId(fileEntryTypeId);
166                    dlFileEntry.setVersion(DLFileEntryConstants.VERSION_DEFAULT);
167                    dlFileEntry.setSize(size);
168                    dlFileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
169    
170                    dlFileEntryPersistence.update(dlFileEntry, false);
171    
172                    // File version
173    
174                    DLFileVersion dlFileVersion = addFileVersion(
175                            user, dlFileEntry, serviceContext.getModifiedDate(now), extension,
176                            mimeType, title, description, null, StringPool.BLANK,
177                            fileEntryTypeId, fieldsMap, DLFileEntryConstants.VERSION_DEFAULT,
178                            size, WorkflowConstants.STATUS_DRAFT, serviceContext);
179    
180                    dlFileEntry.setFileVersion(dlFileVersion);
181    
182                    // Folder
183    
184                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
185                            dlFolderLocalService.updateLastPostDate(
186                                    dlFileEntry.getFolderId(), dlFileEntry.getModifiedDate());
187                    }
188    
189                    // File
190    
191                    if (file != null) {
192                            DLStoreUtil.addFile(
193                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
194                                    false, file);
195                    }
196                    else {
197                            DLStoreUtil.addFile(
198                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
199                                    false, is);
200                    }
201    
202                    return dlFileEntry;
203            }
204    
205            public void addFileEntryResources(
206                            DLFileEntry dlFileEntry, boolean addGroupPermissions,
207                            boolean addGuestPermissions)
208                    throws PortalException, SystemException {
209    
210                    resourceLocalService.addResources(
211                            dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
212                            dlFileEntry.getUserId(), DLFileEntry.class.getName(),
213                            dlFileEntry.getFileEntryId(), false, addGroupPermissions,
214                            addGuestPermissions);
215            }
216    
217            public void addFileEntryResources(
218                            DLFileEntry dlFileEntry, String[] groupPermissions,
219                            String[] guestPermissions)
220                    throws PortalException, SystemException {
221    
222                    resourceLocalService.addModelResources(
223                            dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
224                            dlFileEntry.getUserId(), DLFileEntry.class.getName(),
225                            dlFileEntry.getFileEntryId(), groupPermissions, guestPermissions);
226            }
227    
228            public DLFileVersion cancelCheckOut(long userId, long fileEntryId)
229                    throws PortalException, SystemException {
230    
231                    if (!isFileEntryCheckedOut(fileEntryId)) {
232                            return null;
233                    }
234    
235                    if (!hasFileEntryLock(userId, fileEntryId)) {
236                            lockFileEntry(userId, fileEntryId);
237                    }
238    
239                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
240                            fileEntryId);
241    
242                    DLFileVersion dlFileVersion =
243                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
244    
245                    dlFileVersionPersistence.remove(dlFileVersion);
246    
247                    try {
248                            DLStoreUtil.deleteFile(
249                                    dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
250                                    dlFileEntry.getName(),
251                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
252                    }
253                    catch (NoSuchModelException nsme) {
254                    }
255    
256                    lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
257    
258                    return dlFileVersion;
259            }
260    
261            public void checkInFileEntry(
262                            long userId, long fileEntryId, boolean majorVersion,
263                            String changeLog, ServiceContext serviceContext)
264                    throws PortalException, SystemException {
265    
266                    if (!isFileEntryCheckedOut(fileEntryId)) {
267                            return;
268                    }
269    
270                    if (!hasFileEntryLock(userId, fileEntryId)) {
271                            lockFileEntry(userId, fileEntryId);
272                    }
273    
274                    User user = userPersistence.findByPrimaryKey(userId);
275                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
276                            fileEntryId);
277    
278                    String version = getNextVersion(
279                            dlFileEntry, majorVersion, serviceContext.getWorkflowAction());
280    
281                    DLFileVersion dlFileVersion =
282                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
283    
284                    dlFileVersion.setVersion(version);
285                    dlFileVersion.setChangeLog(changeLog);
286    
287                    dlFileVersionPersistence.update(dlFileVersion, false);
288    
289                    // Folder
290    
291                    if (dlFileEntry.getFolderId() !=
292                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
293    
294                            dlFolderLocalService.updateLastPostDate(
295                                    dlFileEntry.getFolderId(), dlFileEntry.getModifiedDate());
296                    }
297    
298                    // File
299    
300                    DLStoreUtil.updateFileVersion(
301                            user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
302                            dlFileEntry.getName(),
303                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION, version);
304    
305                    if (serviceContext.getWorkflowAction() ==
306                                    WorkflowConstants.ACTION_PUBLISH) {
307    
308                            startWorkflowInstance(
309                                    userId, serviceContext, dlFileVersion,
310                                    DLSyncConstants.EVENT_UPDATE);
311                    }
312    
313                    lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
314            }
315    
316            public void checkInFileEntry(long userId, long fileEntryId, String lockUuid)
317                    throws PortalException, SystemException {
318    
319                    if (Validator.isNotNull(lockUuid)) {
320                            try {
321                                    Lock lock = lockLocalService.getLock(
322                                            DLFileEntry.class.getName(), fileEntryId);
323    
324                                    if (!lock.getUuid().equals(lockUuid)) {
325                                            throw new InvalidLockException("UUIDs do not match");
326                                    }
327                            }
328                            catch (PortalException pe) {
329                                    if ((pe instanceof ExpiredLockException) ||
330                                            (pe instanceof NoSuchLockException)) {
331                                    }
332                                    else {
333                                            throw pe;
334                                    }
335                            }
336                    }
337    
338                    checkInFileEntry(
339                            userId, fileEntryId, false, StringPool.BLANK, new ServiceContext());
340            }
341    
342            /**
343             * @deprecated {@link #checkOutFileEntry(long, long, ServiceContext)}
344             */
345            public DLFileEntry checkOutFileEntry(long userId, long fileEntryId)
346                    throws PortalException, SystemException {
347    
348                    return checkOutFileEntry(userId, fileEntryId, new ServiceContext());
349            }
350    
351            public DLFileEntry checkOutFileEntry(
352                            long userId, long fileEntryId, ServiceContext serviceContext)
353                    throws PortalException, SystemException {
354    
355                    return checkOutFileEntry(
356                            userId, fileEntryId, StringPool.BLANK,
357                            DLFileEntryImpl.LOCK_EXPIRATION_TIME, serviceContext);
358            }
359    
360            /**
361             * @deprecated {@link #checkOutFileEntry(long, long, String, long,
362             *             ServiceContext)}
363             */
364            public DLFileEntry checkOutFileEntry(
365                            long userId, long fileEntryId, String owner, long expirationTime)
366                    throws PortalException, SystemException {
367    
368                    return checkOutFileEntry(
369                            userId, fileEntryId, owner, expirationTime, new ServiceContext());
370            }
371    
372            public DLFileEntry checkOutFileEntry(
373                            long userId, long fileEntryId, String owner, long expirationTime,
374                            ServiceContext serviceContext)
375                    throws PortalException, SystemException {
376    
377                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
378                            fileEntryId);
379    
380                    boolean hasLock = hasFileEntryLock(userId, fileEntryId);
381    
382                    if (!hasLock) {
383                            if ((expirationTime <= 0) ||
384                                    (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
385    
386                                    expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
387                            }
388    
389                            lockLocalService.lock(
390                                    userId, DLFileEntry.class.getName(), fileEntryId, owner, false,
391                                    expirationTime);
392                    }
393    
394                    User user = userPersistence.findByPrimaryKey(userId);
395    
396                    serviceContext.setCompanyId(user.getCompanyId());
397                    serviceContext.setUserId(userId);
398    
399                    dlFileEntryPersistence.update(dlFileEntry, false);
400    
401                    DLFileVersion dlFileVersion =
402                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
403    
404                    long dlFileVersionId = dlFileVersion.getFileVersionId();
405    
406                    String version = dlFileVersion.getVersion();
407    
408                    if (!version.equals(
409                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
410    
411                            long existingDLFileVersionId = ParamUtil.getLong(
412                                    serviceContext, "existingDLFileVersionId");
413    
414                            if (existingDLFileVersionId > 0) {
415                                    DLFileVersion existingDLFileVersion =
416                                            dlFileVersionPersistence.findByPrimaryKey(
417                                                    existingDLFileVersionId);
418    
419                                    dlFileVersion = updateFileVersion(
420                                            user, existingDLFileVersion, null,
421                                            existingDLFileVersion.getExtension(),
422                                            existingDLFileVersion.getMimeType(),
423                                            existingDLFileVersion.getTitle(),
424                                            existingDLFileVersion.getDescription(),
425                                            existingDLFileVersion.getChangeLog(),
426                                            existingDLFileVersion.getExtraSettings(),
427                                            existingDLFileVersion.getFileEntryTypeId(), null,
428                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
429                                            existingDLFileVersion.getSize(),
430                                            WorkflowConstants.STATUS_DRAFT, new Date(), serviceContext);
431                            }
432                            else {
433                                    dlFileVersion = addFileVersion(
434                                            user, dlFileEntry, new Date(), dlFileVersion.getExtension(),
435                                            dlFileVersion.getMimeType(), dlFileVersion.getTitle(),
436                                            dlFileVersion.getDescription(),
437                                            dlFileVersion.getChangeLog(),
438                                            dlFileVersion.getExtraSettings(),
439                                            dlFileVersion.getFileEntryTypeId(), null,
440                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
441                                            dlFileVersion.getSize(), WorkflowConstants.STATUS_DRAFT,
442                                            serviceContext);
443                            }
444    
445                            try {
446                                    DLStoreUtil.deleteFile(
447                                            dlFileEntry.getCompanyId(),
448                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
449                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
450                            }
451                            catch (NoSuchModelException nsme) {
452                            }
453    
454                            DLStoreUtil.copyFileVersion(
455                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
456                                    dlFileEntry.getName(), version,
457                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
458    
459                            copyFileEntryMetadata(
460                                    dlFileEntry.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
461                                    fileEntryId, dlFileVersionId, dlFileVersion.getFileVersionId(),
462                                    serviceContext);
463                    }
464    
465                    return dlFileEntry;
466            }
467    
468            public void convertExtraSettings(String[] keys)
469                    throws PortalException, SystemException {
470    
471                    int count = dlFileEntryFinder.countByExtraSettings();
472    
473                    int pages = count / Indexer.DEFAULT_INTERVAL;
474    
475                    for (int i = 0; i <= pages; i++) {
476                            int start = (i * Indexer.DEFAULT_INTERVAL);
477                            int end = start + Indexer.DEFAULT_INTERVAL;
478    
479                            List<DLFileEntry> dlFileEntries =
480                                    dlFileEntryFinder.findByExtraSettings(start, end);
481    
482                            for (DLFileEntry dlFileEntry : dlFileEntries) {
483                                    convertExtraSettings(dlFileEntry, keys);
484                            }
485                    }
486            }
487    
488            public void copyFileEntryMetadata(
489                            long companyId, long fileEntryTypeId, long fileEntryId,
490                            long fromFileVersionId, long toFileVersionId,
491                            ServiceContext serviceContext)
492                    throws PortalException, SystemException {
493    
494                    Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
495    
496                    List<DDMStructure> ddmStructures = null;
497    
498                    if (fileEntryTypeId > 0) {
499                            DLFileEntryType dlFileEntryType =
500                                    dlFileEntryTypeLocalService.getFileEntryType(fileEntryTypeId);
501    
502                            ddmStructures = dlFileEntryType.getDDMStructures();
503    
504                            for (DDMStructure ddmStructure : ddmStructures) {
505                                    try {
506                                            DLFileEntryMetadata dlFileEntryMetadata =
507                                                    dlFileEntryMetadataLocalService.getFileEntryMetadata(
508                                                            ddmStructure.getStructureId(), fromFileVersionId);
509    
510                                            Fields fields = StorageEngineUtil.getFields(
511                                                    dlFileEntryMetadata.getDDMStorageId());
512    
513                                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
514                                    }
515                                    catch (NoSuchFileEntryMetadataException nsfeme) {
516                                    }
517                            }
518    
519                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
520                                    companyId, ddmStructures, fileEntryTypeId, fileEntryId,
521                                    toFileVersionId, fieldsMap, serviceContext);
522                    }
523    
524                    long classNameId = PortalUtil.getClassNameId(DLFileEntry.class);
525    
526                    ddmStructures = ddmStructureLocalService.getClassStructures(
527                            classNameId);
528    
529                    for (DDMStructure ddmStructure : ddmStructures) {
530                            try {
531                                    DLFileEntryMetadata fileEntryMetadata =
532                                            dlFileEntryMetadataLocalService.getFileEntryMetadata(
533                                                    ddmStructure.getStructureId(), fromFileVersionId);
534    
535                                    Fields fields = StorageEngineUtil.getFields(
536                                            fileEntryMetadata.getDDMStorageId());
537    
538                                    fieldsMap.put(ddmStructure.getStructureKey(), fields);
539                            }
540                            catch (NoSuchFileEntryMetadataException nsfeme) {
541                            }
542                    }
543    
544                    dlFileEntryMetadataLocalService.updateFileEntryMetadata(
545                            companyId, ddmStructures, fileEntryTypeId, fileEntryId,
546                            toFileVersionId, fieldsMap, serviceContext);
547            }
548    
549            public void deleteFileEntries(long groupId, long folderId)
550                    throws PortalException, SystemException {
551    
552                    int count = dlFileEntryPersistence.countByG_F(groupId, folderId);
553    
554                    int pages = count / _DELETE_INTERVAL;
555    
556                    for (int i = 0; i <= pages; i++) {
557                            int start = (i * _DELETE_INTERVAL);
558                            int end = start + _DELETE_INTERVAL;
559    
560                            List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
561                                    groupId, folderId, start, end);
562    
563                            for (DLFileEntry dlFileEntry : dlFileEntries) {
564                                    dlAppHelperLocalService.deleteFileEntry(
565                                            new LiferayFileEntry(dlFileEntry));
566    
567                                    deleteFileEntry(dlFileEntry);
568                            }
569                    }
570            }
571    
572            public void deleteFileEntry(long fileEntryId)
573                    throws PortalException, SystemException {
574    
575                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
576    
577                    deleteFileEntry(dlFileEntry);
578            }
579    
580            public void deleteFileEntry(long userId, long fileEntryId)
581                    throws PortalException, SystemException {
582    
583                    if (!hasFileEntryLock(userId, fileEntryId)) {
584                            lockFileEntry(userId, fileEntryId);
585                    }
586    
587                    try {
588                            deleteFileEntry(fileEntryId);
589                    }
590                    finally {
591                            unlockFileEntry(fileEntryId);
592                    }
593            }
594    
595            @Indexable(type = IndexableType.DELETE)
596            public void deleteFileVersion(long userId, long fileEntryId, String version)
597                    throws PortalException, SystemException {
598    
599                    if (Validator.isNull(version) ||
600                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
601    
602                            throw new InvalidFileVersionException();
603                    }
604    
605                    if (!hasFileEntryLock(userId, fileEntryId)) {
606                            lockFileEntry(userId, fileEntryId);
607                    }
608    
609                    try {
610                            DLFileVersion dlFileVersion = dlFileVersionPersistence.findByF_V(
611                                    fileEntryId, version);
612    
613                            if (!dlFileVersion.isApproved()) {
614                                    throw new InvalidFileVersionException(
615                                            "Cannot delete an unapproved file version");
616                            }
617                            else {
618                                    int count = dlFileVersionPersistence.countByF_S(
619                                            fileEntryId, WorkflowConstants.STATUS_APPROVED);
620    
621                                    if (count <= 1) {
622                                            throw new InvalidFileVersionException(
623                                                    "Cannot delete the only approved file version");
624                                    }
625                            }
626    
627                            dlFileVersionPersistence.remove(dlFileVersion);
628    
629                            expandoValueLocalService.deleteValues(
630                                    DLFileVersion.class.getName(),
631                                    dlFileVersion.getFileVersionId());
632    
633                            DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
634                                    fileEntryId);
635    
636                            if (version.equals(dlFileEntry.getVersion())) {
637                                    try {
638                                            DLFileVersion dlLatestFileVersion =
639                                                    dlFileVersionLocalService.getLatestFileVersion(
640                                                            dlFileEntry.getFileEntryId(), true);
641    
642                                            dlFileEntry.setVersionUserId(
643                                                    dlLatestFileVersion.getUserId());
644                                            dlFileEntry.setVersionUserName(
645                                                    dlLatestFileVersion.getUserName());
646                                            dlFileEntry.setModifiedDate(
647                                                    dlLatestFileVersion.getCreateDate());
648                                            dlFileEntry.setExtension(
649                                                    dlLatestFileVersion.getExtension());
650                                            dlFileEntry.setTitle(dlLatestFileVersion.getTitle());
651                                            dlFileEntry.setDescription(
652                                                    dlLatestFileVersion.getDescription());
653                                            dlFileEntry.setExtraSettings(
654                                                    dlLatestFileVersion.getExtraSettings());
655                                            dlFileEntry.setFileEntryTypeId(
656                                                    dlLatestFileVersion.getFileEntryTypeId());
657                                            dlFileEntry.setVersion(dlLatestFileVersion.getVersion());
658                                            dlFileEntry.setSize(dlLatestFileVersion.getSize());
659    
660                                            dlFileEntryPersistence.update(dlFileEntry, false);
661                                    }
662                                    catch (NoSuchFileVersionException nsfve) {
663                                    }
664                            }
665    
666                            try {
667                                    DLStoreUtil.deleteFile(
668                                            dlFileEntry.getCompanyId(),
669                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
670                                            version);
671                            }
672                            catch (NoSuchModelException nsme) {
673                            }
674                    }
675                    finally {
676                            unlockFileEntry(fileEntryId);
677                    }
678            }
679    
680            public DLFileEntry fetchFileEntryByAnyImageId(long imageId)
681                    throws SystemException {
682    
683                    return dlFileEntryFinder.fetchByAnyImageId(imageId);
684            }
685    
686            public List<DLFileEntry> getExtraSettingsFileEntries(int start, int end)
687                    throws SystemException {
688    
689                    return dlFileEntryFinder.findByExtraSettings(start, end);
690            }
691    
692            public File getFile(
693                            long userId, long fileEntryId, String version,
694                            boolean incrementCounter)
695                    throws PortalException, SystemException {
696    
697                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
698                            fileEntryId);
699    
700                    if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED && incrementCounter) {
701                            dlFileEntry.setReadCount(dlFileEntry.getReadCount() + 1);
702    
703                            dlFileEntryPersistence.update(dlFileEntry, false);
704                    }
705    
706                    dlAppHelperLocalService.getFileAsStream(
707                            userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
708    
709                    return DLStoreUtil.getFile(
710                            dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
711                            dlFileEntry.getName(), version);
712            }
713    
714            public InputStream getFileAsStream(
715                            long userId, long fileEntryId, String version)
716                    throws PortalException, SystemException {
717    
718                    return getFileAsStream(userId, fileEntryId, version, true);
719            }
720    
721            public InputStream getFileAsStream(
722                            long userId, long fileEntryId, String version,
723                            boolean incrementCounter)
724                    throws PortalException, SystemException {
725    
726                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
727                            fileEntryId);
728    
729                    if (PropsValues.DL_FILE_ENTRY_READ_COUNT_ENABLED && incrementCounter) {
730                            dlFileEntry.setReadCount(dlFileEntry.getReadCount() + 1);
731    
732                            dlFileEntryPersistence.update(dlFileEntry, false);
733                    }
734    
735                    dlAppHelperLocalService.getFileAsStream(
736                            userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
737    
738                    return DLStoreUtil.getFileAsStream(
739                            dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
740                            dlFileEntry.getName(), version);
741            }
742    
743            public List<DLFileEntry> getFileEntries(int start, int end)
744                    throws SystemException {
745    
746                    return dlFileEntryPersistence.findAll(start, end);
747            }
748    
749            public List<DLFileEntry> getFileEntries(
750                            long groupId, long folderId, int start, int end,
751                            OrderByComparator obc)
752                    throws SystemException {
753    
754                    return dlFileEntryPersistence.findByG_F(
755                            groupId, folderId, start, end, obc);
756            }
757    
758            public List<DLFileEntry> getFileEntriesByMimeType(String mimeType)
759                    throws SystemException {
760    
761                    return dlFileEntryPersistence.findByMimeType(mimeType);
762            }
763    
764            public int getFileEntriesCount() throws SystemException {
765                    return dlFileEntryPersistence.countAll();
766            }
767    
768            public int getFileEntriesCount(long groupId, long folderId)
769                    throws SystemException {
770    
771                    return dlFileEntryPersistence.countByG_F(groupId, folderId);
772            }
773    
774            public DLFileEntry getFileEntry(long fileEntryId)
775                    throws PortalException, SystemException {
776    
777                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
778                            fileEntryId);
779    
780                    setFileVersion(dlFileEntry);
781    
782                    return dlFileEntry;
783            }
784    
785            public DLFileEntry getFileEntry(long groupId, long folderId, String title)
786                    throws PortalException, SystemException {
787    
788                    DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
789                            groupId, folderId, title);
790    
791                    if (dlFileEntry != null) {
792                            setFileVersion(dlFileEntry);
793    
794                            return dlFileEntry;
795                    }
796    
797                    List<DLFileVersion> dlFileVersions =
798                            dlFileVersionPersistence.findByG_F_T_V(
799                                    groupId, folderId, title,
800                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
801    
802                    long userId = PrincipalThreadLocal.getUserId();
803    
804                    for (DLFileVersion dlFileVersion : dlFileVersions) {
805                            if (hasFileEntryLock(userId, dlFileVersion.getFileEntryId())) {
806                                    return dlFileVersion.getFileEntry();
807                            }
808                    }
809    
810                    StringBundler sb = new StringBundler(8);
811    
812                    sb.append("No DLFileEntry exists with the key {");
813                    sb.append("groupId=");
814                    sb.append(groupId);
815                    sb.append(", folderId=");
816                    sb.append(folderId);
817                    sb.append(", title=");
818                    sb.append(title);
819                    sb.append(StringPool.CLOSE_CURLY_BRACE);
820    
821                    if (_log.isWarnEnabled()) {
822                            _log.warn(sb.toString());
823                    }
824    
825                    throw new NoSuchFileEntryException(sb.toString());
826            }
827    
828            public DLFileEntry getFileEntryByName(
829                            long groupId, long folderId, String name)
830                    throws PortalException, SystemException {
831    
832                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByG_F_N(
833                            groupId, folderId, name);
834    
835                    setFileVersion(dlFileEntry);
836    
837                    return dlFileEntry;
838            }
839    
840            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
841                    throws PortalException, SystemException {
842    
843                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByUUID_G(
844                            uuid, groupId);
845    
846                    setFileVersion(dlFileEntry);
847    
848                    return dlFileEntry;
849            }
850    
851            public List<DLFileEntry> getGroupFileEntries(
852                            long groupId, int start, int end)
853                    throws SystemException {
854    
855                    return getGroupFileEntries(
856                            groupId, start, end, new RepositoryModelModifiedDateComparator());
857            }
858    
859            public List<DLFileEntry> getGroupFileEntries(
860                            long groupId, int start, int end, OrderByComparator obc)
861                    throws SystemException {
862    
863                    return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
864            }
865    
866            public List<DLFileEntry> getGroupFileEntries(
867                            long groupId, long userId, int start, int end)
868                    throws SystemException {
869    
870                    return getGroupFileEntries(
871                            groupId, userId, start, end,
872                            new RepositoryModelModifiedDateComparator());
873            }
874    
875            public List<DLFileEntry> getGroupFileEntries(
876                            long groupId, long userId, int start, int end,
877                            OrderByComparator obc)
878                    throws SystemException {
879    
880                    if (userId <= 0) {
881                            return dlFileEntryPersistence.findByGroupId(
882                                    groupId, start, end, obc);
883                    }
884                    else {
885                            return dlFileEntryPersistence.findByG_U(
886                                    groupId, userId, start, end, obc);
887                    }
888            }
889    
890            public int getGroupFileEntriesCount(long groupId) throws SystemException {
891                    return dlFileEntryPersistence.countByGroupId(groupId);
892            }
893    
894            public int getGroupFileEntriesCount(long groupId, long userId)
895                    throws SystemException {
896    
897                    if (userId <= 0) {
898                            return dlFileEntryPersistence.countByGroupId(groupId);
899                    }
900                    else {
901                            return dlFileEntryPersistence.countByG_U(groupId, userId);
902                    }
903            }
904    
905            public List<DLFileEntry> getMisversionedFileEntries()
906                    throws SystemException {
907    
908                    return dlFileEntryFinder.findByMisversioned();
909            }
910    
911            public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
912                    return dlFileEntryFinder.findByNoAssets();
913            }
914    
915            public List<DLFileEntry> getOrphanedFileEntries() throws SystemException {
916                    return dlFileEntryFinder.findByOrphanedFileEntries();
917            }
918    
919            public boolean hasExtraSettings() throws SystemException {
920                    if (dlFileEntryFinder.countByExtraSettings() > 0) {
921                            return true;
922                    }
923                    else {
924                            return false;
925                    }
926            }
927    
928            public boolean hasFileEntryLock(long userId, long fileEntryId)
929                    throws PortalException, SystemException {
930    
931                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
932    
933                    long folderId = dlFileEntry.getFolderId();
934    
935                    boolean hasLock = lockLocalService.hasLock(
936                            userId, DLFileEntry.class.getName(), fileEntryId);
937    
938                    if (!hasLock &&
939                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
940    
941                            hasLock = dlFolderService.hasInheritableLock(folderId);
942                    }
943    
944                    return hasLock;
945            }
946    
947            public boolean isFileEntryCheckedOut(long fileEntryId)
948                    throws PortalException, SystemException {
949    
950                    DLFileVersion dlFileVersion =
951                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
952    
953                    String version = dlFileVersion.getVersion();
954    
955                    if (version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
956                            return true;
957                    }
958                    else {
959                            return false;
960                    }
961            }
962    
963            public DLFileEntry moveFileEntry(
964                            long userId, long fileEntryId, long newFolderId,
965                            ServiceContext serviceContext)
966                    throws PortalException, SystemException {
967    
968                    if (!hasFileEntryLock(userId, fileEntryId)) {
969                            lockFileEntry(userId, fileEntryId);
970                    }
971    
972                    try {
973                            DLFileEntry dlFileEntry = moveFileEntryImpl(
974                                    userId, fileEntryId, newFolderId, serviceContext);
975    
976                            dlAppHelperLocalService.moveFileEntry(
977                                    new LiferayFileEntry(dlFileEntry));
978    
979                            return dlFileEntryTypeLocalService.updateFileEntryFileEntryType(
980                                    dlFileEntry, serviceContext);
981                    }
982                    finally {
983                            if (!isFileEntryCheckedOut(fileEntryId)) {
984                                    unlockFileEntry(fileEntryId);
985                            }
986                    }
987            }
988    
989            public void revertFileEntry(
990                            long userId, long fileEntryId, String version,
991                            ServiceContext serviceContext)
992                    throws PortalException, SystemException {
993    
994                    if (Validator.isNull(version) ||
995                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
996    
997                            throw new InvalidFileVersionException();
998                    }
999    
1000                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
1001                            fileEntryId, version);
1002    
1003                    if (!dlFileVersion.isApproved()) {
1004                            throw new InvalidFileVersionException(
1005                                    "Cannot revert from an unapproved file version");
1006                    }
1007    
1008                    DLFileVersion latestDLFileVersion =
1009                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
1010    
1011                    if (version.equals(latestDLFileVersion.getVersion())) {
1012                            throw new InvalidFileVersionException(
1013                                    "Cannot revert from the latest file version");
1014                    }
1015    
1016                    String sourceFileName = dlFileVersion.getTitle();
1017                    String extension = dlFileVersion.getExtension();
1018                    String mimeType = dlFileVersion.getMimeType();
1019                    String title = dlFileVersion.getTitle();
1020                    String description = dlFileVersion.getDescription();
1021                    String changeLog = "Reverted to " + version;
1022                    boolean majorVersion = true;
1023                    String extraSettings = dlFileVersion.getExtraSettings();
1024                    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
1025                    Map<String, Fields> fieldsMap = null;
1026                    InputStream is = getFileAsStream(userId, fileEntryId, version);
1027                    long size = dlFileVersion.getSize();
1028    
1029                    DLFileEntry dlFileEntry = updateFileEntry(
1030                            userId, fileEntryId, sourceFileName, extension, mimeType, title,
1031                            description, changeLog, majorVersion, extraSettings,
1032                            fileEntryTypeId, fieldsMap, null, is, size, serviceContext);
1033    
1034                    DLFileVersion newDlFileVersion =
1035                            dlFileVersionLocalService.getFileVersion(
1036                                    fileEntryId, dlFileEntry.getVersion());
1037    
1038                    copyFileEntryMetadata(
1039                            dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
1040                            fileEntryId, newDlFileVersion.getFileVersionId(),
1041                            dlFileVersion.getFileVersionId(), serviceContext);
1042            }
1043    
1044            public DLFileEntry updateFileEntry(
1045                            long userId, long fileEntryId, String sourceFileName,
1046                            String mimeType, String title, String description, String changeLog,
1047                            boolean majorVersion, long fileEntryTypeId,
1048                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
1049                            ServiceContext serviceContext)
1050                    throws PortalException, SystemException {
1051    
1052                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1053                            fileEntryId);
1054    
1055                    String extension = DLAppUtil.getExtension(title, sourceFileName);
1056    
1057                    String extraSettings = StringPool.BLANK;
1058    
1059                    if (fileEntryTypeId == -1) {
1060                            fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
1061                    }
1062    
1063                    fileEntryTypeId = getFileEntryTypeId(
1064                            DLUtil.getGroupIds(dlFileEntry.getGroupId()),
1065                            dlFileEntry.getFolderId(), fileEntryTypeId);
1066    
1067                    return updateFileEntry(
1068                            userId, fileEntryId, sourceFileName, extension, mimeType, title,
1069                            description, changeLog, majorVersion, extraSettings,
1070                            fileEntryTypeId, fieldsMap, file, is, size, serviceContext);
1071            }
1072    
1073            public void updateSmallImage(long smallImageId, long largeImageId)
1074                    throws PortalException, SystemException {
1075    
1076                    try {
1077                            RenderedImage renderedImage = null;
1078    
1079                            Image largeImage = imageLocalService.getImage(largeImageId);
1080    
1081                            byte[] bytes = largeImage.getTextObj();
1082                            String contentType = largeImage.getType();
1083    
1084                            if (bytes != null) {
1085                                    ImageBag imageBag = ImageToolUtil.read(bytes);
1086    
1087                                    renderedImage = imageBag.getRenderedImage();
1088    
1089                                    //validate(bytes);
1090                            }
1091    
1092                            if (renderedImage != null) {
1093                                    int height = PrefsPropsUtil.getInteger(
1094                                            PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT);
1095                                    int width = PrefsPropsUtil.getInteger(
1096                                            PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH);
1097    
1098                                    RenderedImage thumbnailRenderedImage = ImageToolUtil.scale(
1099                                            renderedImage, height, width);
1100    
1101                                    imageLocalService.updateImage(
1102                                            smallImageId,
1103                                            ImageToolUtil.getBytes(
1104                                                    thumbnailRenderedImage, contentType));
1105                            }
1106                    }
1107                    catch (IOException ioe) {
1108                            throw new ImageSizeException(ioe);
1109                    }
1110            }
1111    
1112            public DLFileEntry updateStatus(
1113                            long userId, long fileVersionId, int status,
1114                            Map<String, Serializable> workflowContext,
1115                            ServiceContext serviceContext)
1116                    throws PortalException, SystemException {
1117    
1118                    // File version
1119    
1120                    User user = userPersistence.findByPrimaryKey(userId);
1121    
1122                    DLFileVersion dlFileVersion = dlFileVersionPersistence.findByPrimaryKey(
1123                            fileVersionId);
1124    
1125                    dlFileVersion.setStatus(status);
1126                    dlFileVersion.setStatusByUserId(user.getUserId());
1127                    dlFileVersion.setStatusByUserName(user.getFullName());
1128                    dlFileVersion.setStatusDate(new Date());
1129    
1130                    dlFileVersionPersistence.update(dlFileVersion, false);
1131    
1132                    // File entry
1133    
1134                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1135                            dlFileVersion.getFileEntryId());
1136    
1137                    if (status == WorkflowConstants.STATUS_APPROVED) {
1138                            if (DLUtil.compareVersions(
1139                                            dlFileEntry.getVersion(),
1140                                            dlFileVersion.getVersion()) <= 0) {
1141    
1142                                    dlFileEntry.setExtension(dlFileVersion.getExtension());
1143                                    dlFileEntry.setTitle(dlFileVersion.getTitle());
1144                                    dlFileEntry.setDescription(dlFileVersion.getDescription());
1145                                    dlFileEntry.setExtraSettings(dlFileVersion.getExtraSettings());
1146                                    dlFileEntry.setFileEntryTypeId(
1147                                            dlFileVersion.getFileEntryTypeId());
1148                                    dlFileEntry.setVersion(dlFileVersion.getVersion());
1149                                    dlFileEntry.setVersionUserId(dlFileVersion.getUserId());
1150                                    dlFileEntry.setVersionUserName(dlFileVersion.getUserName());
1151                                    dlFileEntry.setModifiedDate(dlFileVersion.getCreateDate());
1152                                    dlFileEntry.setSize(dlFileVersion.getSize());
1153    
1154                                    dlFileEntryPersistence.update(dlFileEntry, false);
1155                            }
1156                    }
1157                    else {
1158    
1159                            // File entry
1160    
1161                            if (dlFileEntry.getVersion().equals(dlFileVersion.getVersion())) {
1162                                    String newVersion = DLFileEntryConstants.VERSION_DEFAULT;
1163    
1164                                    List<DLFileVersion> approvedFileVersions =
1165                                            dlFileVersionPersistence.findByF_S(
1166                                                    dlFileEntry.getFileEntryId(),
1167                                                    WorkflowConstants.STATUS_APPROVED);
1168    
1169                                    if (!approvedFileVersions.isEmpty()) {
1170                                            newVersion = approvedFileVersions.get(0).getVersion();
1171                                    }
1172    
1173                                    dlFileEntry.setVersion(newVersion);
1174    
1175                                    dlFileEntryPersistence.update(dlFileEntry, false);
1176                            }
1177    
1178                            // Indexer
1179    
1180                            if (dlFileVersion.getVersion().equals(
1181                                            DLFileEntryConstants.VERSION_DEFAULT)) {
1182    
1183                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1184                                            DLFileEntry.class);
1185    
1186                                    indexer.delete(dlFileEntry);
1187                            }
1188                    }
1189    
1190                    // App helper
1191    
1192                    dlAppHelperLocalService.updateStatus(
1193                            userId, new LiferayFileEntry(dlFileEntry),
1194                            new LiferayFileVersion(dlFileVersion), status, workflowContext);
1195    
1196                    // Indexer
1197    
1198                    if ((status == WorkflowConstants.STATUS_APPROVED) &&
1199                            ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1200    
1201                            reindex(dlFileEntry);
1202                    }
1203    
1204                    return dlFileEntry;
1205            }
1206    
1207            public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
1208                    throws PortalException, SystemException {
1209    
1210                    if (verifyFileEntryLock(fileEntryId, lockUuid) &&
1211                            isFileEntryCheckedOut(fileEntryId)) {
1212    
1213                            return true;
1214                    }
1215                    else {
1216                            return false;
1217                    }
1218            }
1219    
1220            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
1221                    throws PortalException, SystemException {
1222    
1223                    boolean lockVerified = false;
1224    
1225                    try {
1226                            Lock lock = lockLocalService.getLock(
1227                                    DLFileEntry.class.getName(), fileEntryId);
1228    
1229                            if (lock.getUuid().equals(lockUuid)) {
1230                                    lockVerified = true;
1231                            }
1232                    }
1233                    catch (PortalException pe) {
1234                            if ((pe instanceof ExpiredLockException) ||
1235                                    (pe instanceof NoSuchLockException)) {
1236    
1237                                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
1238                                            fileEntryId);
1239    
1240                                    lockVerified = dlFolderService.verifyInheritableLock(
1241                                            dlFileEntry.getFolderId(), lockUuid);
1242                            }
1243                            else {
1244                                    throw pe;
1245                            }
1246                    }
1247    
1248                    return lockVerified;
1249            }
1250    
1251            protected DLFileVersion addFileVersion(
1252                            User user, DLFileEntry dlFileEntry, Date modifiedDate,
1253                            String extension, String mimeType, String title, String description,
1254                            String changeLog, String extraSettings, long fileEntryTypeId,
1255                            Map<String, Fields> fieldsMap, String version, long size,
1256                            int status, ServiceContext serviceContext)
1257                    throws PortalException, SystemException {
1258    
1259                    long fileVersionId = counterLocalService.increment();
1260    
1261                    DLFileVersion dlFileVersion = dlFileVersionPersistence.create(
1262                            fileVersionId);
1263    
1264                    String uuid = ParamUtil.getString(
1265                            serviceContext, "fileVersionUuid", serviceContext.getUuid());
1266    
1267                    dlFileVersion.setUuid(uuid);
1268    
1269                    dlFileVersion.setGroupId(dlFileEntry.getGroupId());
1270                    dlFileVersion.setCompanyId(dlFileEntry.getCompanyId());
1271    
1272                    long versionUserId = dlFileEntry.getVersionUserId();
1273    
1274                    if (versionUserId <= 0) {
1275                            versionUserId = dlFileEntry.getUserId();
1276                    }
1277    
1278                    dlFileVersion.setUserId(versionUserId);
1279    
1280                    String versionUserName = GetterUtil.getString(
1281                            dlFileEntry.getVersionUserName(), dlFileEntry.getUserName());
1282    
1283                    dlFileVersion.setUserName(versionUserName);
1284    
1285                    dlFileVersion.setCreateDate(modifiedDate);
1286                    dlFileVersion.setModifiedDate(modifiedDate);
1287                    dlFileVersion.setRepositoryId(dlFileEntry.getRepositoryId());
1288                    dlFileVersion.setFolderId(dlFileEntry.getFolderId());
1289                    dlFileVersion.setFileEntryId(dlFileEntry.getFileEntryId());
1290                    dlFileVersion.setExtension(extension);
1291                    dlFileVersion.setMimeType(mimeType);
1292                    dlFileVersion.setTitle(title);
1293                    dlFileVersion.setDescription(description);
1294                    dlFileVersion.setChangeLog(changeLog);
1295                    dlFileVersion.setExtraSettings(extraSettings);
1296                    dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
1297                    dlFileVersion.setVersion(version);
1298                    dlFileVersion.setSize(size);
1299                    dlFileVersion.setStatus(status);
1300                    dlFileVersion.setStatusByUserId(user.getUserId());
1301                    dlFileVersion.setStatusByUserName(user.getFullName());
1302                    dlFileVersion.setStatusDate(dlFileEntry.getModifiedDate());
1303                    dlFileVersion.setExpandoBridgeAttributes(serviceContext);
1304    
1305                    dlFileVersionPersistence.update(dlFileVersion, false);
1306    
1307                    if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
1308                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
1309                                    fileEntryTypeId, dlFileEntry.getFileEntryId(), fileVersionId,
1310                                    fieldsMap, serviceContext);
1311                    }
1312    
1313                    return dlFileVersion;
1314            }
1315    
1316            protected void convertExtraSettings(
1317                            DLFileEntry dlFileEntry, DLFileVersion dlFileVersion, String[] keys)
1318                    throws PortalException, SystemException {
1319    
1320                    UnicodeProperties extraSettingsProperties =
1321                            dlFileVersion.getExtraSettingsProperties();
1322    
1323                    ExpandoBridge expandoBridge = dlFileVersion.getExpandoBridge();
1324    
1325                    convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1326    
1327                    dlFileVersion.setExtraSettingsProperties(extraSettingsProperties);
1328    
1329                    dlFileVersionPersistence.update(dlFileVersion, false);
1330    
1331                    int status = dlFileVersion.getStatus();
1332    
1333                    if ((status == WorkflowConstants.STATUS_APPROVED) &&
1334                            (DLUtil.compareVersions(
1335                                    dlFileEntry.getVersion(), dlFileVersion.getVersion()) <= 0)) {
1336    
1337                            reindex(dlFileEntry);
1338                    }
1339            }
1340    
1341            protected void convertExtraSettings(DLFileEntry dlFileEntry, String[] keys)
1342                    throws PortalException, SystemException {
1343    
1344                    UnicodeProperties extraSettingsProperties =
1345                            dlFileEntry.getExtraSettingsProperties();
1346    
1347                    ExpandoBridge expandoBridge = dlFileEntry.getExpandoBridge();
1348    
1349                    convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1350    
1351                    dlFileEntry.setExtraSettingsProperties(extraSettingsProperties);
1352    
1353                    dlFileEntryPersistence.update(dlFileEntry, false);
1354    
1355                    List<DLFileVersion> dlFileVersions =
1356                            dlFileVersionLocalService.getFileVersions(
1357                                    dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1358    
1359                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1360                            convertExtraSettings(dlFileEntry, dlFileVersion, keys);
1361                    }
1362            }
1363    
1364            protected void convertExtraSettings(
1365                    UnicodeProperties extraSettingsProperties, ExpandoBridge expandoBridge,
1366                    String[] keys) {
1367    
1368                    for (String key : keys) {
1369                            String value = extraSettingsProperties.remove(key);
1370    
1371                            if (Validator.isNull(value)) {
1372                                    continue;
1373                            }
1374    
1375                            int type = expandoBridge.getAttributeType(key);
1376    
1377                            Serializable serializable = ExpandoColumnConstants.getSerializable(
1378                                    type, value);
1379    
1380                            expandoBridge.setAttribute(key, serializable);
1381                    }
1382            }
1383    
1384            protected void deleteFileEntry(DLFileEntry dlFileEntry)
1385                    throws PortalException, SystemException {
1386    
1387                    // File entry
1388    
1389                    dlFileEntryPersistence.remove(dlFileEntry);
1390    
1391                    // Resources
1392    
1393                    resourceLocalService.deleteResource(
1394                            dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
1395                            ResourceConstants.SCOPE_INDIVIDUAL, dlFileEntry.getFileEntryId());
1396    
1397                    // WebDAVProps
1398    
1399                    webDAVPropsLocalService.deleteWebDAVProps(
1400                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
1401    
1402                    // File entry metadata
1403    
1404                    dlFileEntryMetadataLocalService.deleteFileEntryMetadata(
1405                            dlFileEntry.getFileEntryId());
1406    
1407                    // File versions
1408    
1409                    List<DLFileVersion> dlFileVersions =
1410                            dlFileVersionPersistence.findByFileEntryId(
1411                                    dlFileEntry.getFileEntryId());
1412    
1413                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1414                            dlFileVersionPersistence.remove(dlFileVersion);
1415    
1416                            expandoValueLocalService.deleteValues(
1417                                    DLFileVersion.class.getName(),
1418                                    dlFileVersion.getFileVersionId());
1419    
1420                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
1421                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
1422                                    DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
1423                    }
1424    
1425                    // Expando
1426    
1427                    expandoValueLocalService.deleteValues(
1428                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
1429    
1430                    // Lock
1431    
1432                    lockLocalService.unlock(
1433                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
1434    
1435                    // File
1436    
1437                    try {
1438                            DLStoreUtil.deleteFile(
1439                                    dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1440                                    dlFileEntry.getName());
1441                    }
1442                    catch (Exception e) {
1443                            if (_log.isWarnEnabled()) {
1444                                    _log.warn(e, e);
1445                            }
1446                    }
1447    
1448                    // Index
1449    
1450                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1451                            DLFileEntry.class);
1452    
1453                    indexer.delete(dlFileEntry);
1454            }
1455    
1456            protected Long getFileEntryTypeId(
1457                            long[] groupIds, long folderId, long fileEntryTypeId)
1458                    throws PortalException, SystemException {
1459    
1460                    if (fileEntryTypeId == -1) {
1461                            fileEntryTypeId =
1462                                    dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
1463                    }
1464                    else {
1465                            List<DLFileEntryType> dlFileEntryTypes =
1466                                    dlFileEntryTypeLocalService.getFolderFileEntryTypes(
1467                                            groupIds, folderId, true);
1468    
1469                            boolean found = false;
1470    
1471                            for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
1472                                    if (dlFileEntryType.getFileEntryTypeId() == fileEntryTypeId) {
1473                                            found = true;
1474    
1475                                            break;
1476                                    }
1477                            }
1478    
1479                            if (!found) {
1480                                    throw new InvalidFileEntryTypeException(
1481                                            "Invalid file entry type " + fileEntryTypeId +
1482                                                    " for folder " + folderId);
1483                            }
1484                    }
1485    
1486                    return fileEntryTypeId;
1487            }
1488    
1489            protected String getNextVersion(
1490                            DLFileEntry dlFileEntry, boolean majorVersion, int workflowAction)
1491                    throws PortalException, SystemException {
1492    
1493                    String version = dlFileEntry.getVersion();
1494    
1495                    try {
1496                            DLFileVersion dlFileVersion =
1497                                    dlFileVersionLocalService.getLatestFileVersion(
1498                                            dlFileEntry.getFileEntryId(), true);
1499    
1500                            version = dlFileVersion.getVersion();
1501                    }
1502                    catch (NoSuchFileVersionException nsfve) {
1503                    }
1504    
1505                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1506                            majorVersion = false;
1507                    }
1508    
1509                    int[] versionParts = StringUtil.split(version, StringPool.PERIOD, 0);
1510    
1511                    if (majorVersion) {
1512                            versionParts[0]++;
1513                            versionParts[1] = 0;
1514                    }
1515                    else {
1516                            versionParts[1]++;
1517                    }
1518    
1519                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
1520            }
1521    
1522            protected Lock lockFileEntry(long userId, long fileEntryId)
1523                    throws PortalException, SystemException {
1524    
1525                    return lockFileEntry(
1526                            userId, fileEntryId, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
1527            }
1528    
1529            protected Lock lockFileEntry(
1530                            long userId, long fileEntryId, String owner, long expirationTime)
1531                    throws PortalException, SystemException {
1532    
1533                    if (hasFileEntryLock(userId, fileEntryId)) {
1534                            return lockLocalService.getLock(
1535                                    DLFileEntry.class.getName(), fileEntryId);
1536                    }
1537    
1538                    if ((expirationTime <= 0) ||
1539                            (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
1540    
1541                            expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
1542                    }
1543    
1544                    return lockLocalService.lock(
1545                            userId, DLFileEntry.class.getName(), fileEntryId, owner, false,
1546                            expirationTime);
1547            }
1548    
1549            protected DLFileEntry moveFileEntryImpl(
1550                            long userId, long fileEntryId, long newFolderId,
1551                            ServiceContext serviceContext)
1552                    throws PortalException, SystemException {
1553    
1554                    // File entry
1555    
1556                    User user = userPersistence.findByPrimaryKey(userId);
1557                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1558                            fileEntryId);
1559    
1560                    long oldDataRepositoryId = dlFileEntry.getDataRepositoryId();
1561    
1562                    validateFile(
1563                            dlFileEntry.getGroupId(), newFolderId, dlFileEntry.getFileEntryId(),
1564                            dlFileEntry.getTitle(), dlFileEntry.getExtension());
1565    
1566                    if (DLStoreUtil.hasFile(
1567                                    user.getCompanyId(),
1568                                    DLFolderConstants.getDataRepositoryId(
1569                                            dlFileEntry.getGroupId(), newFolderId),
1570                                    dlFileEntry.getName(), StringPool.BLANK)) {
1571    
1572                            throw new DuplicateFileException(dlFileEntry.getName());
1573                    }
1574    
1575                    dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(null));
1576                    dlFileEntry.setFolderId(newFolderId);
1577    
1578                    dlFileEntryPersistence.update(dlFileEntry, false);
1579    
1580                    // File version
1581    
1582                    List<DLFileVersion> dlFileVersions =
1583                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
1584    
1585                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1586                            dlFileVersion.setFolderId(newFolderId);
1587    
1588                            dlFileVersionPersistence.update(dlFileVersion, false);
1589                    }
1590    
1591                    // Folder
1592    
1593                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1594                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
1595                                    newFolderId);
1596    
1597                            dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
1598    
1599                            dlFolderPersistence.update(dlFolder, false);
1600                    }
1601    
1602                    // File
1603    
1604                    DLStoreUtil.updateFile(
1605                            user.getCompanyId(), oldDataRepositoryId,
1606                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
1607    
1608                    // Index
1609    
1610                    if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
1611                            reindex(dlFileEntry);
1612                    }
1613    
1614                    return dlFileEntry;
1615            }
1616    
1617            protected void reindex(DLFileEntry dlFileEntry) throws SearchException {
1618                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1619                            DLFileEntry.class);
1620    
1621                    indexer.reindex(dlFileEntry);
1622            }
1623    
1624            protected void setFileVersion(DLFileEntry dlFileEntry)
1625                    throws PortalException, SystemException {
1626    
1627                    try {
1628                            DLFileVersion dlFileVersion =
1629                                    dlFileVersionLocalService.getFileVersion(
1630                                            dlFileEntry.getFileEntryId(), dlFileEntry.getVersion());
1631    
1632                            dlFileEntry.setFileVersion(dlFileVersion);
1633                    }
1634                    catch (NoSuchFileVersionException nsfve) {
1635                    }
1636            }
1637    
1638            protected void startWorkflowInstance(
1639                            long userId, ServiceContext serviceContext,
1640                            DLFileVersion dlFileVersion, String syncEventType)
1641                    throws PortalException, SystemException {
1642    
1643                    Map<String, Serializable> workflowContext =
1644                            new HashMap<String, Serializable>();
1645    
1646                    workflowContext.put("event", syncEventType);
1647    
1648                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
1649                            dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(), userId,
1650                            DLFileEntry.class.getName(), dlFileVersion.getFileVersionId(),
1651                            dlFileVersion, serviceContext, workflowContext);
1652            }
1653    
1654            protected void unlockFileEntry(long fileEntryId) throws SystemException {
1655                    lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
1656            }
1657    
1658            protected void unlockFileEntry(long fileEntryId, String lockUuid)
1659                    throws PortalException, SystemException {
1660    
1661                    if (Validator.isNotNull(lockUuid)) {
1662                            try {
1663                                    Lock lock = lockLocalService.getLock(
1664                                            DLFileEntry.class.getName(), fileEntryId);
1665    
1666                                    if (!lock.getUuid().equals(lockUuid)) {
1667                                            throw new InvalidLockException("UUIDs do not match");
1668                                    }
1669                            }
1670                            catch (PortalException pe) {
1671                                    if ((pe instanceof ExpiredLockException) ||
1672                                            (pe instanceof NoSuchLockException)) {
1673                                    }
1674                                    else {
1675                                            throw pe;
1676                                    }
1677                            }
1678                    }
1679    
1680                    if (!isFileEntryCheckedOut(fileEntryId)) {
1681                            lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
1682                    }
1683            }
1684    
1685            protected DLFileEntry updateFileEntry(
1686                            long userId, long fileEntryId, String sourceFileName,
1687                            String extension, String mimeType, String title, String description,
1688                            String changeLog, boolean majorVersion, String extraSettings,
1689                            long fileEntryTypeId, Map<String, Fields> fieldsMap, File file,
1690                            InputStream is, long size, ServiceContext serviceContext)
1691                    throws PortalException, SystemException {
1692    
1693                    User user = userPersistence.findByPrimaryKey(userId);
1694                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1695                            fileEntryId);
1696    
1697                    boolean checkedOut = dlFileEntry.isCheckedOut();
1698    
1699                    DLFileVersion dlFileVersion =
1700                            dlFileVersionLocalService.getLatestFileVersion(
1701                                    fileEntryId, !checkedOut);
1702    
1703                    boolean autoCheckIn = !checkedOut && dlFileVersion.isApproved();
1704    
1705                    if (autoCheckIn) {
1706                            dlFileEntry = checkOutFileEntry(
1707                                    userId, fileEntryId, serviceContext);
1708                    }
1709                    else if (!checkedOut) {
1710                            lockFileEntry(userId, fileEntryId);
1711                    }
1712    
1713                    if (!hasFileEntryLock(userId, fileEntryId)) {
1714                            lockFileEntry(userId, fileEntryId);
1715                    }
1716    
1717                    if (checkedOut || autoCheckIn) {
1718                            dlFileVersion = dlFileVersionLocalService.getLatestFileVersion(
1719                                    fileEntryId, false);
1720                    }
1721    
1722                    try {
1723                            if (Validator.isNull(extension)) {
1724                                    extension = dlFileEntry.getExtension();
1725                            }
1726    
1727                            if (Validator.isNull(mimeType)) {
1728                                    mimeType = dlFileEntry.getMimeType();
1729                            }
1730    
1731                            if (Validator.isNull(title)) {
1732                                    title = sourceFileName;
1733    
1734                                    if (Validator.isNull(title)) {
1735                                            title = dlFileEntry.getTitle();
1736                                    }
1737                            }
1738    
1739                            Date now = new Date();
1740    
1741                            validateFile(
1742                                    dlFileEntry.getGroupId(), dlFileEntry.getFolderId(),
1743                                    dlFileEntry.getFileEntryId(), title, extension, sourceFileName,
1744                                    file, is);
1745    
1746                            // File version
1747    
1748                            String version = dlFileVersion.getVersion();
1749    
1750                            if (size == 0) {
1751                                    size = dlFileVersion.getSize();
1752                            }
1753    
1754                            updateFileVersion(
1755                                    user, dlFileVersion, sourceFileName, extension, mimeType, title,
1756                                    description, changeLog, extraSettings, fileEntryTypeId,
1757                                    fieldsMap, version, size, dlFileVersion.getStatus(),
1758                                    serviceContext.getModifiedDate(now), serviceContext);
1759    
1760                            // App helper
1761    
1762                            dlAppHelperLocalService.updateAsset(
1763                                    userId, new LiferayFileEntry(dlFileEntry),
1764                                    new LiferayFileVersion(dlFileVersion),
1765                                    serviceContext.getAssetCategoryIds(),
1766                                    serviceContext.getAssetTagNames(),
1767                                    serviceContext.getAssetLinkEntryIds());
1768    
1769                            // File
1770    
1771                            if ((file != null) || (is != null)) {
1772                                    try {
1773                                            DLStoreUtil.deleteFile(
1774                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1775                                                    dlFileEntry.getName(), version);
1776                                    }
1777                                    catch (NoSuchModelException nsme) {
1778                                    }
1779    
1780                                    if (file != null) {
1781                                            DLStoreUtil.updateFile(
1782                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1783                                                    dlFileEntry.getName(), dlFileEntry.getExtension(),
1784                                                    false, version, sourceFileName, file);
1785                                    }
1786                                    else {
1787                                            DLStoreUtil.updateFile(
1788                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1789                                                    dlFileEntry.getName(), dlFileEntry.getExtension(),
1790                                                    false, version, sourceFileName, is);
1791                                    }
1792                            }
1793    
1794                            if (autoCheckIn) {
1795                                    checkInFileEntry(
1796                                            userId, fileEntryId, majorVersion, changeLog,
1797                                            serviceContext);
1798                            }
1799                            else if (!checkedOut &&
1800                                             (serviceContext.getWorkflowAction() ==
1801                                                    WorkflowConstants.ACTION_PUBLISH)) {
1802    
1803                                    String syncEvent = DLSyncConstants.EVENT_UPDATE;
1804    
1805                                    if (dlFileVersion.getVersion().equals(
1806                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
1807    
1808                                            syncEvent = DLSyncConstants.EVENT_ADD;
1809                                    }
1810    
1811                                    startWorkflowInstance(
1812                                            userId, serviceContext, dlFileVersion, syncEvent);
1813                            }
1814                    }
1815                    catch (PortalException pe) {
1816                            if (autoCheckIn) {
1817                                    cancelCheckOut(userId, fileEntryId);
1818                            }
1819    
1820                            throw pe;
1821                    }
1822                    catch (SystemException se) {
1823                            if (autoCheckIn) {
1824                                    cancelCheckOut(userId, fileEntryId);
1825                            }
1826    
1827                            throw se;
1828                    }
1829                    finally {
1830                            if (!autoCheckIn && !checkedOut) {
1831                                    unlockFileEntry(fileEntryId);
1832                            }
1833                    }
1834    
1835                    return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
1836            }
1837    
1838            protected DLFileVersion updateFileVersion(
1839                            User user, DLFileVersion dlFileVersion, String sourceFileName,
1840                            String extension, String mimeType, String title, String description,
1841                            String changeLog, String extraSettings, long fileEntryTypeId,
1842                            Map<String, Fields> fieldsMap, String version, long size,
1843                            int status, Date statusDate, ServiceContext serviceContext)
1844                    throws PortalException, SystemException {
1845    
1846                    dlFileVersion.setModifiedDate(statusDate);
1847    
1848                    if (Validator.isNotNull(sourceFileName)) {
1849                            dlFileVersion.setExtension(extension);
1850                            dlFileVersion.setMimeType(mimeType);
1851                    }
1852    
1853                    dlFileVersion.setTitle(title);
1854                    dlFileVersion.setDescription(description);
1855                    dlFileVersion.setChangeLog(changeLog);
1856                    dlFileVersion.setExtraSettings(extraSettings);
1857                    dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
1858                    dlFileVersion.setVersion(version);
1859                    dlFileVersion.setSize(size);
1860                    dlFileVersion.setStatus(status);
1861                    dlFileVersion.setStatusByUserId(user.getUserId());
1862                    dlFileVersion.setStatusByUserName(user.getFullName());
1863                    dlFileVersion.setStatusDate(statusDate);
1864                    dlFileVersion.setExpandoBridgeAttributes(serviceContext);
1865    
1866                    dlFileVersion = dlFileVersionPersistence.update(dlFileVersion, false);
1867    
1868                    if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
1869                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
1870                                    fileEntryTypeId, dlFileVersion.getFileEntryId(),
1871                                    dlFileVersion.getFileVersionId(), fieldsMap, serviceContext);
1872                    }
1873    
1874                    return dlFileVersion;
1875            }
1876    
1877            protected void validateFile(
1878                            long groupId, long folderId, long fileEntryId, String title,
1879                            String extension)
1880                    throws PortalException, SystemException {
1881    
1882                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
1883                            groupId, folderId, title);
1884    
1885                    if (dlFolder != null) {
1886                            throw new DuplicateFolderNameException(title);
1887                    }
1888    
1889                    DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
1890                            groupId, folderId, title);
1891    
1892                    if ((dlFileEntry != null) &&
1893                            (dlFileEntry.getFileEntryId() != fileEntryId)) {
1894    
1895                            throw new DuplicateFileException(title);
1896                    }
1897    
1898                    String periodAndExtension = StringPool.PERIOD + extension;
1899    
1900                    if (!title.endsWith(periodAndExtension)) {
1901                            title += periodAndExtension;
1902    
1903                            dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
1904                                    groupId, folderId, title);
1905    
1906                            if ((dlFileEntry != null) &&
1907                                    (dlFileEntry.getFileEntryId() != fileEntryId)) {
1908    
1909                                    throw new DuplicateFileException(title);
1910                            }
1911                    }
1912            }
1913    
1914            protected void validateFile(
1915                            long groupId, long folderId, long fileEntryId, String title,
1916                            String extension, String sourceFileName, File file, InputStream is)
1917                    throws PortalException, SystemException {
1918    
1919                    if (Validator.isNotNull(sourceFileName)) {
1920                            if (file != null) {
1921                                    DLStoreUtil.validate(
1922                                            sourceFileName, extension, sourceFileName, true, file);
1923                            }
1924                            else {
1925                                    DLStoreUtil.validate(
1926                                            sourceFileName, extension, sourceFileName, true, is);
1927                            }
1928                    }
1929    
1930                    validateFileName(title);
1931    
1932                    DLStoreUtil.validate(title, false);
1933    
1934                    validateFile(groupId, folderId, fileEntryId, title, extension);
1935            }
1936    
1937            protected void validateFileName(String fileName) throws PortalException {
1938                    if (fileName.contains(StringPool.SLASH)) {
1939                            throw new FileNameException(fileName);
1940                    }
1941            }
1942    
1943            private static final int _DELETE_INTERVAL = 100;
1944    
1945            private static Log _log = LogFactoryUtil.getLog(
1946                    DLFileEntryLocalServiceImpl.class);
1947    
1948    }