001    /**
002     * Copyright (c) 2000-2013 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.dao.orm.QueryDefinition;
022    import com.liferay.portal.kernel.dao.orm.QueryUtil;
023    import com.liferay.portal.kernel.dao.orm.Session;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.image.ImageBag;
027    import com.liferay.portal.kernel.image.ImageToolUtil;
028    import com.liferay.portal.kernel.increment.BufferedIncrement;
029    import com.liferay.portal.kernel.increment.NumberIncrement;
030    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
031    import com.liferay.portal.kernel.log.Log;
032    import com.liferay.portal.kernel.log.LogFactoryUtil;
033    import com.liferay.portal.kernel.search.Field;
034    import com.liferay.portal.kernel.search.Hits;
035    import com.liferay.portal.kernel.search.Indexable;
036    import com.liferay.portal.kernel.search.IndexableType;
037    import com.liferay.portal.kernel.search.Indexer;
038    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
039    import com.liferay.portal.kernel.search.SearchContext;
040    import com.liferay.portal.kernel.search.SearchException;
041    import com.liferay.portal.kernel.search.Sort;
042    import com.liferay.portal.kernel.systemevent.SystemEvent;
043    import com.liferay.portal.kernel.util.ArrayUtil;
044    import com.liferay.portal.kernel.util.DateRange;
045    import com.liferay.portal.kernel.util.DigesterUtil;
046    import com.liferay.portal.kernel.util.FileUtil;
047    import com.liferay.portal.kernel.util.GetterUtil;
048    import com.liferay.portal.kernel.util.OrderByComparator;
049    import com.liferay.portal.kernel.util.ParamUtil;
050    import com.liferay.portal.kernel.util.PropsKeys;
051    import com.liferay.portal.kernel.util.StreamUtil;
052    import com.liferay.portal.kernel.util.StringBundler;
053    import com.liferay.portal.kernel.util.StringPool;
054    import com.liferay.portal.kernel.util.StringUtil;
055    import com.liferay.portal.kernel.util.TreePathUtil;
056    import com.liferay.portal.kernel.util.UnicodeProperties;
057    import com.liferay.portal.kernel.util.Validator;
058    import com.liferay.portal.kernel.workflow.WorkflowConstants;
059    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
060    import com.liferay.portal.model.Group;
061    import com.liferay.portal.model.Image;
062    import com.liferay.portal.model.Lock;
063    import com.liferay.portal.model.ModelHintsUtil;
064    import com.liferay.portal.model.Repository;
065    import com.liferay.portal.model.ResourceConstants;
066    import com.liferay.portal.model.SystemEventConstants;
067    import com.liferay.portal.model.User;
068    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
069    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
070    import com.liferay.portal.security.auth.PrincipalThreadLocal;
071    import com.liferay.portal.service.ServiceContext;
072    import com.liferay.portal.util.PortalUtil;
073    import com.liferay.portal.util.PrefsPropsUtil;
074    import com.liferay.portal.util.PropsValues;
075    import com.liferay.portlet.documentlibrary.DuplicateFileException;
076    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
077    import com.liferay.portlet.documentlibrary.FileExtensionException;
078    import com.liferay.portlet.documentlibrary.FileNameException;
079    import com.liferay.portlet.documentlibrary.ImageSizeException;
080    import com.liferay.portlet.documentlibrary.InvalidFileEntryTypeException;
081    import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
082    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
083    import com.liferay.portlet.documentlibrary.NoSuchFileEntryMetadataException;
084    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
085    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
086    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
087    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
088    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
089    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
090    import com.liferay.portlet.documentlibrary.model.DLFolder;
091    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
092    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
093    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
094    import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryModelImpl;
095    import com.liferay.portlet.documentlibrary.model.impl.DLFolderModelImpl;
096    import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
097    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
098    import com.liferay.portlet.documentlibrary.util.DL;
099    import com.liferay.portlet.documentlibrary.util.DLAppUtil;
100    import com.liferay.portlet.documentlibrary.util.DLUtil;
101    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator;
102    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
103    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
104    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
105    import com.liferay.portlet.expando.NoSuchRowException;
106    import com.liferay.portlet.expando.NoSuchTableException;
107    import com.liferay.portlet.expando.model.ExpandoBridge;
108    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
109    import com.liferay.portlet.expando.model.ExpandoRow;
110    import com.liferay.portlet.expando.model.ExpandoTable;
111    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
112    
113    import java.awt.image.RenderedImage;
114    
115    import java.io.File;
116    import java.io.IOException;
117    import java.io.InputStream;
118    import java.io.Serializable;
119    
120    import java.util.ArrayList;
121    import java.util.Date;
122    import java.util.HashMap;
123    import java.util.List;
124    import java.util.Map;
125    
126    /**
127     * Provides the local service for accessing, adding, checking in/out, deleting,
128     * locking/unlocking, moving, reverting, updating, and verifying document
129     * library file entries.
130     *
131     * <p>
132     * Due to legacy code, the names of some file entry properties are not
133     * intuitive. Each file entry has both a name and title. The <code>name</code>
134     * is a unique identifier for a given file and is generally numeric, whereas the
135     * <code>title</code> is the actual name specified by the user (such as
136     * &quot;Budget.xls&quot;).
137     * </p>
138     *
139     * @author Brian Wing Shun Chan
140     * @author Harry Mark
141     * @author Alexander Chow
142     * @author Manuel de la Peña
143     */
144    public class DLFileEntryLocalServiceImpl
145            extends DLFileEntryLocalServiceBaseImpl {
146    
147            @Override
148            public DLFileEntry addFileEntry(
149                            long userId, long groupId, long repositoryId, long folderId,
150                            String sourceFileName, String mimeType, String title,
151                            String description, String changeLog, long fileEntryTypeId,
152                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
153                            ServiceContext serviceContext)
154                    throws PortalException, SystemException {
155    
156                    if (Validator.isNull(title)) {
157                            if (Validator.isNull(sourceFileName)) {
158                                    throw new FileNameException();
159                            }
160                            else {
161                                    title = sourceFileName;
162                            }
163                    }
164    
165                    // File entry
166    
167                    User user = userPersistence.findByPrimaryKey(userId);
168                    folderId = dlFolderLocalService.getFolderId(
169                            user.getCompanyId(), folderId);
170                    String name = String.valueOf(
171                            counterLocalService.increment(DLFileEntry.class.getName()));
172                    String extension = DLAppUtil.getExtension(title, sourceFileName);
173    
174                    if (fileEntryTypeId == -1) {
175                            fileEntryTypeId =
176                                    dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
177                    }
178    
179                    Date now = new Date();
180    
181                    validateFileEntryTypeId(
182                            PortalUtil.getSiteAndCompanyGroupIds(groupId), folderId,
183                            fileEntryTypeId);
184    
185                    validateFile(
186                            groupId, folderId, 0, title, extension, sourceFileName, file, is);
187    
188                    long fileEntryId = counterLocalService.increment();
189    
190                    DLFileEntry dlFileEntry = dlFileEntryPersistence.create(fileEntryId);
191    
192                    dlFileEntry.setUuid(serviceContext.getUuid());
193                    dlFileEntry.setGroupId(groupId);
194                    dlFileEntry.setCompanyId(user.getCompanyId());
195                    dlFileEntry.setUserId(user.getUserId());
196                    dlFileEntry.setUserName(user.getFullName());
197                    dlFileEntry.setCreateDate(serviceContext.getCreateDate(now));
198                    dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
199    
200                    DLFolder repositoryDLFolder = null;
201    
202                    if (repositoryId != groupId) {
203                            Repository repository = repositoryLocalService.getRepository(
204                                    repositoryId);
205    
206                            repositoryDLFolder = dlFolderPersistence.findByPrimaryKey(
207                                    repository.getDlFolderId());
208                    }
209    
210                    long classNameId = 0;
211                    long classPK = 0;
212    
213                    if ((repositoryDLFolder != null) && repositoryDLFolder.isHidden()) {
214                            classNameId = PortalUtil.getClassNameId(
215                                    (String)serviceContext.getAttribute("className"));
216                            classPK = ParamUtil.getLong(serviceContext, "classPK");
217                    }
218    
219                    dlFileEntry.setClassNameId(classNameId);
220                    dlFileEntry.setClassPK(classPK);
221                    dlFileEntry.setRepositoryId(repositoryId);
222                    dlFileEntry.setFolderId(folderId);
223                    dlFileEntry.setTreePath(dlFileEntry.buildTreePath());
224                    dlFileEntry.setName(name);
225                    dlFileEntry.setExtension(extension);
226                    dlFileEntry.setMimeType(mimeType);
227                    dlFileEntry.setTitle(title);
228                    dlFileEntry.setDescription(description);
229                    dlFileEntry.setFileEntryTypeId(fileEntryTypeId);
230                    dlFileEntry.setVersion(DLFileEntryConstants.VERSION_DEFAULT);
231                    dlFileEntry.setSize(size);
232                    dlFileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
233    
234                    dlFileEntryPersistence.update(dlFileEntry);
235    
236                    // File version
237    
238                    addFileVersion(
239                            user, dlFileEntry, serviceContext.getModifiedDate(now), extension,
240                            mimeType, title, description, changeLog, StringPool.BLANK,
241                            fileEntryTypeId, fieldsMap, DLFileEntryConstants.VERSION_DEFAULT,
242                            size, WorkflowConstants.STATUS_DRAFT, serviceContext);
243    
244                    // Folder
245    
246                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
247                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
248                                    dlFileEntry.getFolderId());
249    
250                            dlFolder.setLastPostDate(dlFileEntry.getModifiedDate());
251    
252                            dlFolderPersistence.update(dlFolder);
253                    }
254    
255                    // File
256    
257                    if (file != null) {
258                            DLStoreUtil.addFile(
259                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
260                                    false, file);
261                    }
262                    else {
263                            DLStoreUtil.addFile(
264                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
265                                    false, is);
266                    }
267    
268                    return dlFileEntry;
269            }
270    
271            @Override
272            public DLFileVersion cancelCheckOut(long userId, long fileEntryId)
273                    throws PortalException, SystemException {
274    
275                    if (!isFileEntryCheckedOut(fileEntryId)) {
276                            return null;
277                    }
278    
279                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
280                            fileEntryId);
281    
282                    DLFileVersion dlFileVersion =
283                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
284    
285                    removeFileVersion(dlFileEntry, dlFileVersion);
286    
287                    return dlFileVersion;
288            }
289    
290            @Override
291            public void checkInFileEntry(
292                            long userId, long fileEntryId, boolean majorVersion,
293                            String changeLog, ServiceContext serviceContext)
294                    throws PortalException, SystemException {
295    
296                    if (!isFileEntryCheckedOut(fileEntryId)) {
297                            return;
298                    }
299    
300                    User user = userPersistence.findByPrimaryKey(userId);
301    
302                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
303                            fileEntryId);
304    
305                    boolean webDAVCheckInMode = GetterUtil.getBoolean(
306                            serviceContext.getAttribute(DL.WEBDAV_CHECK_IN_MODE));
307    
308                    boolean manualCheckInRequired = dlFileEntry.getManualCheckInRequired();
309    
310                    if (!webDAVCheckInMode && manualCheckInRequired) {
311                            dlFileEntry.setManualCheckInRequired(false);
312    
313                            dlFileEntryPersistence.update(dlFileEntry);
314                    }
315    
316                    DLFileVersion lastDLFileVersion =
317                            dlFileVersionLocalService.getFileVersion(
318                                    dlFileEntry.getFileEntryId(), dlFileEntry.getVersion());
319    
320                    DLFileVersion latestDLFileVersion =
321                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
322    
323                    boolean keepFileVersionLabel = false;
324    
325                    if (!majorVersion) {
326                            keepFileVersionLabel = isKeepFileVersionLabel(
327                                    dlFileEntry, lastDLFileVersion, latestDLFileVersion,
328                                    serviceContext.getWorkflowAction());
329                    }
330    
331                    if (keepFileVersionLabel) {
332                            if (lastDLFileVersion.getSize() != latestDLFileVersion.getSize()) {
333    
334                                    // File entry
335    
336                                    dlFileEntry.setExtension(latestDLFileVersion.getExtension());
337                                    dlFileEntry.setMimeType(latestDLFileVersion.getMimeType());
338                                    dlFileEntry.setSize(latestDLFileVersion.getSize());
339    
340                                    dlFileEntryPersistence.update(dlFileEntry);
341    
342                                    // File version
343    
344                                    lastDLFileVersion.setExtension(
345                                            latestDLFileVersion.getExtension());
346                                    lastDLFileVersion.setMimeType(
347                                            latestDLFileVersion.getMimeType());
348                                    lastDLFileVersion.setSize(latestDLFileVersion.getSize());
349    
350                                    dlFileVersionPersistence.update(lastDLFileVersion);
351    
352                                    // File
353    
354                                    try {
355                                            DLStoreUtil.deleteFile(
356                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
357                                                    dlFileEntry.getName(), lastDLFileVersion.getVersion());
358                                    }
359                                    catch (NoSuchModelException nsme) {
360                                    }
361    
362                                    DLStoreUtil.copyFileVersion(
363                                            user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
364                                            dlFileEntry.getName(),
365                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
366                                            lastDLFileVersion.getVersion());
367                            }
368    
369                            // Latest file version
370    
371                            removeFileVersion(dlFileEntry, latestDLFileVersion);
372    
373                            latestDLFileVersion = lastDLFileVersion;
374                    }
375                    else {
376    
377                            // File version
378    
379                            String version = getNextVersion(
380                                    dlFileEntry, majorVersion, serviceContext.getWorkflowAction());
381    
382                            latestDLFileVersion.setVersion(version);
383                            latestDLFileVersion.setChangeLog(changeLog);
384    
385                            dlFileVersionPersistence.update(latestDLFileVersion);
386    
387                            // Folder
388    
389                            if (dlFileEntry.getFolderId() !=
390                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
391    
392                                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
393                                            dlFileEntry.getFolderId());
394    
395                                    dlFolder.setLastPostDate(latestDLFileVersion.getModifiedDate());
396    
397                                    dlFolderPersistence.update(dlFolder);
398                            }
399    
400                            // File
401    
402                            DLStoreUtil.updateFileVersion(
403                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
404                                    dlFileEntry.getName(),
405                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION, version);
406                    }
407    
408                    // Workflow
409    
410                    if ((serviceContext.getWorkflowAction() ==
411                                    WorkflowConstants.ACTION_PUBLISH) && !keepFileVersionLabel) {
412    
413                            startWorkflowInstance(
414                                    userId, serviceContext, latestDLFileVersion,
415                                    DLSyncConstants.EVENT_UPDATE);
416                    }
417    
418                    unlockFileEntry(fileEntryId);
419            }
420    
421            /**
422             * @deprecated As of 6.2.0, replaced by {@link #checkInFileEntry(long, long,
423             *             String, ServiceContext)}
424             */
425            @Override
426            public void checkInFileEntry(long userId, long fileEntryId, String lockUuid)
427                    throws PortalException, SystemException {
428    
429                    checkInFileEntry(userId, fileEntryId, lockUuid, new ServiceContext());
430            }
431    
432            @Override
433            public void checkInFileEntry(
434                            long userId, long fileEntryId, String lockUuid,
435                            ServiceContext serviceContext)
436                    throws PortalException, SystemException {
437    
438                    if (Validator.isNotNull(lockUuid)) {
439                            try {
440                                    Lock lock = lockLocalService.getLock(
441                                            DLFileEntry.class.getName(), fileEntryId);
442    
443                                    if (!lock.getUuid().equals(lockUuid)) {
444                                            throw new InvalidLockException("UUIDs do not match");
445                                    }
446                            }
447                            catch (PortalException pe) {
448                                    if ((pe instanceof ExpiredLockException) ||
449                                            (pe instanceof NoSuchLockException)) {
450                                    }
451                                    else {
452                                            throw pe;
453                                    }
454                            }
455                    }
456    
457                    checkInFileEntry(
458                            userId, fileEntryId, false, StringPool.BLANK, serviceContext);
459            }
460    
461            /**
462             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
463             *             long, ServiceContext)}
464             */
465            @Override
466            public DLFileEntry checkOutFileEntry(long userId, long fileEntryId)
467                    throws PortalException, SystemException {
468    
469                    return checkOutFileEntry(userId, fileEntryId, new ServiceContext());
470            }
471    
472            @Override
473            public DLFileEntry checkOutFileEntry(
474                            long userId, long fileEntryId, ServiceContext serviceContext)
475                    throws PortalException, SystemException {
476    
477                    return checkOutFileEntry(
478                            userId, fileEntryId, StringPool.BLANK,
479                            DLFileEntryImpl.LOCK_EXPIRATION_TIME, serviceContext);
480            }
481    
482            /**
483             * @deprecated As of 6.2.0, replaced by {@link #checkOutFileEntry(long,
484             *             long, String, long, ServiceContext)}
485             */
486            @Override
487            public DLFileEntry checkOutFileEntry(
488                            long userId, long fileEntryId, String owner, long expirationTime)
489                    throws PortalException, SystemException {
490    
491                    return checkOutFileEntry(
492                            userId, fileEntryId, owner, expirationTime, new ServiceContext());
493            }
494    
495            @Override
496            public DLFileEntry checkOutFileEntry(
497                            long userId, long fileEntryId, String owner, long expirationTime,
498                            ServiceContext serviceContext)
499                    throws PortalException, SystemException {
500    
501                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
502                            fileEntryId);
503    
504                    boolean hasLock = hasFileEntryLock(userId, fileEntryId);
505    
506                    if (!hasLock) {
507                            if ((expirationTime <= 0) ||
508                                    (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
509    
510                                    expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
511                            }
512    
513                            lockLocalService.lock(
514                                    userId, DLFileEntry.class.getName(), fileEntryId, owner, false,
515                                    expirationTime);
516                    }
517    
518                    User user = userPersistence.findByPrimaryKey(userId);
519    
520                    serviceContext.setCompanyId(user.getCompanyId());
521    
522                    DLFileVersion dlFileVersion =
523                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
524    
525                    long dlFileVersionId = dlFileVersion.getFileVersionId();
526    
527                    Map<String, Serializable> expandoBridgeAttributes =
528                            serviceContext.getExpandoBridgeAttributes();
529    
530                    if (expandoBridgeAttributes.isEmpty()) {
531                            ExpandoBridge expandoBridge =
532                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
533                                            serviceContext.getCompanyId(), DLFileEntry.class.getName(),
534                                            dlFileVersionId);
535    
536                            serviceContext.setExpandoBridgeAttributes(
537                                    expandoBridge.getAttributes());
538                    }
539    
540                    serviceContext.setUserId(userId);
541    
542                    boolean manualCheckinRequired = GetterUtil.getBoolean(
543                            serviceContext.getAttribute(DL.MANUAL_CHECK_IN_REQUIRED));
544    
545                    dlFileEntry.setManualCheckInRequired(manualCheckinRequired);
546    
547                    dlFileEntryPersistence.update(dlFileEntry);
548    
549                    String version = dlFileVersion.getVersion();
550    
551                    if (!version.equals(
552                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
553    
554                            long existingDLFileVersionId = ParamUtil.getLong(
555                                    serviceContext, "existingDLFileVersionId");
556    
557                            if (existingDLFileVersionId > 0) {
558                                    DLFileVersion existingDLFileVersion =
559                                            dlFileVersionPersistence.findByPrimaryKey(
560                                                    existingDLFileVersionId);
561    
562                                    dlFileVersion = updateFileVersion(
563                                            user, existingDLFileVersion, null,
564                                            existingDLFileVersion.getExtension(),
565                                            existingDLFileVersion.getMimeType(),
566                                            existingDLFileVersion.getTitle(),
567                                            existingDLFileVersion.getDescription(),
568                                            existingDLFileVersion.getChangeLog(),
569                                            existingDLFileVersion.getExtraSettings(),
570                                            existingDLFileVersion.getFileEntryTypeId(), null,
571                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
572                                            existingDLFileVersion.getSize(),
573                                            WorkflowConstants.STATUS_DRAFT,
574                                            serviceContext.getModifiedDate(null), serviceContext);
575                            }
576                            else {
577                                    long oldDLFileVersionId = dlFileVersion.getFileVersionId();
578    
579                                    dlFileVersion = addFileVersion(
580                                            user, dlFileEntry, serviceContext.getModifiedDate(null),
581                                            dlFileVersion.getExtension(), dlFileVersion.getMimeType(),
582                                            dlFileVersion.getTitle(), dlFileVersion.getDescription(),
583                                            dlFileVersion.getChangeLog(),
584                                            dlFileVersion.getExtraSettings(),
585                                            dlFileVersion.getFileEntryTypeId(), null,
586                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
587                                            dlFileVersion.getSize(), WorkflowConstants.STATUS_DRAFT,
588                                            serviceContext);
589    
590                                    copyExpandoRowModifiedDate(
591                                            dlFileEntry.getCompanyId(), oldDLFileVersionId,
592                                            dlFileVersion.getFileVersionId());
593                            }
594    
595                            try {
596                                    DLStoreUtil.deleteFile(
597                                            dlFileEntry.getCompanyId(),
598                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
599                                            DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
600                            }
601                            catch (NoSuchModelException nsme) {
602                            }
603    
604                            DLStoreUtil.copyFileVersion(
605                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
606                                    dlFileEntry.getName(), version,
607                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
608    
609                            copyFileEntryMetadata(
610                                    dlFileEntry.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
611                                    fileEntryId, dlFileVersionId, dlFileVersion.getFileVersionId(),
612                                    serviceContext);
613                    }
614    
615                    return dlFileEntry;
616            }
617    
618            @Override
619            public void convertExtraSettings(String[] keys)
620                    throws PortalException, SystemException {
621    
622                    int count = dlFileEntryFinder.countByExtraSettings();
623    
624                    int pages = count / Indexer.DEFAULT_INTERVAL;
625    
626                    for (int i = 0; i <= pages; i++) {
627                            int start = (i * Indexer.DEFAULT_INTERVAL);
628                            int end = start + Indexer.DEFAULT_INTERVAL;
629    
630                            List<DLFileEntry> dlFileEntries =
631                                    dlFileEntryFinder.findByExtraSettings(start, end);
632    
633                            for (DLFileEntry dlFileEntry : dlFileEntries) {
634                                    convertExtraSettings(dlFileEntry, keys);
635                            }
636                    }
637            }
638    
639            @Override
640            public void copyFileEntryMetadata(
641                            long companyId, long fileEntryTypeId, long fileEntryId,
642                            long fromFileVersionId, long toFileVersionId,
643                            ServiceContext serviceContext)
644                    throws PortalException, SystemException {
645    
646                    Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
647    
648                    List<DDMStructure> ddmStructures = null;
649    
650                    if (fileEntryTypeId > 0) {
651                            DLFileEntryType dlFileEntryType =
652                                    dlFileEntryTypeLocalService.getFileEntryType(fileEntryTypeId);
653    
654                            ddmStructures = dlFileEntryType.getDDMStructures();
655                    }
656                    else {
657                            long classNameId = PortalUtil.getClassNameId(
658                                    DLFileEntryMetadata.class);
659    
660                            ddmStructures = ddmStructureLocalService.getClassStructures(
661                                    companyId, classNameId);
662                    }
663    
664                    copyFileEntryMetadata(
665                            companyId, fileEntryTypeId, fileEntryId, fromFileVersionId,
666                            toFileVersionId, serviceContext, fieldsMap, ddmStructures);
667            }
668    
669            @Override
670            public void deleteFileEntries(long groupId, long folderId)
671                    throws PortalException, SystemException {
672    
673                    deleteFileEntries(groupId, folderId, true);
674            }
675    
676            @Override
677            public void deleteFileEntries(
678                            long groupId, long folderId, boolean includeTrashedEntries)
679                    throws PortalException, SystemException {
680    
681                    int count = dlFileEntryPersistence.countByG_F(groupId, folderId);
682    
683                    int pages = count / _DELETE_INTERVAL;
684    
685                    for (int i = 0; i <= pages; i++) {
686                            int start = (i * _DELETE_INTERVAL);
687                            int end = start + _DELETE_INTERVAL;
688    
689                            List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
690                                    groupId, folderId, start, end);
691    
692                            for (DLFileEntry dlFileEntry : dlFileEntries) {
693                                    if (includeTrashedEntries ||
694                                            !dlFileEntry.isInTrashExplicitly()) {
695    
696                                            dlAppHelperLocalService.deleteFileEntry(
697                                                    new LiferayFileEntry(dlFileEntry));
698    
699                                            dlFileEntryLocalService.deleteFileEntry(dlFileEntry);
700                                    }
701                            }
702                    }
703            }
704    
705            @Indexable(type = IndexableType.DELETE)
706            @Override
707            @SystemEvent(
708                    action = SystemEventConstants.ACTION_SKIP,
709                    type = SystemEventConstants.TYPE_DELETE)
710            public DLFileEntry deleteFileEntry(DLFileEntry dlFileEntry)
711                    throws PortalException, SystemException {
712    
713                    // File entry
714    
715                    dlFileEntryPersistence.remove(dlFileEntry);
716    
717                    // Resources
718    
719                    resourceLocalService.deleteResource(
720                            dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
721                            ResourceConstants.SCOPE_INDIVIDUAL, dlFileEntry.getFileEntryId());
722    
723                    // WebDAVProps
724    
725                    webDAVPropsLocalService.deleteWebDAVProps(
726                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
727    
728                    // File entry metadata
729    
730                    dlFileEntryMetadataLocalService.deleteFileEntryMetadata(
731                            dlFileEntry.getFileEntryId());
732    
733                    // File versions
734    
735                    List<DLFileVersion> dlFileVersions =
736                            dlFileVersionPersistence.findByFileEntryId(
737                                    dlFileEntry.getFileEntryId());
738    
739                    for (DLFileVersion dlFileVersion : dlFileVersions) {
740                            dlFileVersionPersistence.remove(dlFileVersion);
741    
742                            expandoRowLocalService.deleteRows(dlFileVersion.getFileVersionId());
743    
744                            workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
745                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
746                                    DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
747                    }
748    
749                    // Expando
750    
751                    expandoRowLocalService.deleteRows(dlFileEntry.getFileEntryId());
752    
753                    // Lock
754    
755                    unlockFileEntry(dlFileEntry.getFileEntryId());
756    
757                    // File
758    
759                    try {
760                            DLStoreUtil.deleteFile(
761                                    dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
762                                    dlFileEntry.getName());
763                    }
764                    catch (Exception e) {
765                            if (_log.isWarnEnabled()) {
766                                    _log.warn(e, e);
767                            }
768                    }
769    
770                    return dlFileEntry;
771            }
772    
773            @Indexable(type = IndexableType.DELETE)
774            @Override
775            public DLFileEntry deleteFileEntry(long fileEntryId)
776                    throws PortalException, SystemException {
777    
778                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
779    
780                    return dlFileEntryLocalService.deleteFileEntry(dlFileEntry);
781            }
782    
783            @Indexable(type = IndexableType.DELETE)
784            @Override
785            public DLFileEntry deleteFileEntry(long userId, long fileEntryId)
786                    throws PortalException, SystemException {
787    
788                    if (!hasFileEntryLock(userId, fileEntryId)) {
789                            lockFileEntry(userId, fileEntryId);
790                    }
791    
792                    try {
793                            return dlFileEntryLocalService.deleteFileEntry(fileEntryId);
794                    }
795                    finally {
796                            unlockFileEntry(fileEntryId);
797                    }
798            }
799    
800            @Indexable(type = IndexableType.REINDEX)
801            @Override
802            public DLFileEntry deleteFileVersion(
803                            long userId, long fileEntryId, String version)
804                    throws PortalException, SystemException {
805    
806                    if (Validator.isNull(version) ||
807                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
808    
809                            throw new InvalidFileVersionException();
810                    }
811    
812                    if (!hasFileEntryLock(userId, fileEntryId)) {
813                            lockFileEntry(userId, fileEntryId);
814                    }
815    
816                    boolean latestVersion = false;
817    
818                    DLFileEntry dlFileEntry = null;
819    
820                    try {
821                            DLFileVersion dlFileVersion = dlFileVersionPersistence.findByF_V(
822                                    fileEntryId, version);
823    
824                            if (!dlFileVersion.isApproved()) {
825                                    throw new InvalidFileVersionException(
826                                            "Cannot delete an unapproved file version");
827                            }
828                            else {
829                                    int count = dlFileVersionPersistence.countByF_S(
830                                            fileEntryId, WorkflowConstants.STATUS_APPROVED);
831    
832                                    if (count <= 1) {
833                                            throw new InvalidFileVersionException(
834                                                    "Cannot delete the only approved file version");
835                                    }
836                            }
837    
838                            dlFileVersionPersistence.remove(dlFileVersion);
839    
840                            expandoRowLocalService.deleteRows(dlFileVersion.getFileVersionId());
841    
842                            dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
843    
844                            latestVersion = version.equals(dlFileEntry.getVersion());
845    
846                            if (latestVersion) {
847                                    try {
848                                            DLFileVersion dlLatestFileVersion =
849                                                    dlFileVersionLocalService.getLatestFileVersion(
850                                                            dlFileEntry.getFileEntryId(), true);
851    
852                                            dlFileEntry.setModifiedDate(
853                                                    dlLatestFileVersion.getCreateDate());
854                                            dlFileEntry.setExtension(
855                                                    dlLatestFileVersion.getExtension());
856                                            dlFileEntry.setMimeType(dlLatestFileVersion.getMimeType());
857                                            dlFileEntry.setTitle(dlLatestFileVersion.getTitle());
858                                            dlFileEntry.setDescription(
859                                                    dlLatestFileVersion.getDescription());
860                                            dlFileEntry.setExtraSettings(
861                                                    dlLatestFileVersion.getExtraSettings());
862                                            dlFileEntry.setFileEntryTypeId(
863                                                    dlLatestFileVersion.getFileEntryTypeId());
864                                            dlFileEntry.setVersion(dlLatestFileVersion.getVersion());
865                                            dlFileEntry.setSize(dlLatestFileVersion.getSize());
866    
867                                            dlFileEntry = dlFileEntryPersistence.update(dlFileEntry);
868                                    }
869                                    catch (NoSuchFileVersionException nsfve) {
870                                    }
871                            }
872    
873                            try {
874                                    DLStoreUtil.deleteFile(
875                                            dlFileEntry.getCompanyId(),
876                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
877                                            version);
878                            }
879                            catch (NoSuchModelException nsme) {
880                            }
881                    }
882                    finally {
883                            unlockFileEntry(fileEntryId);
884                    }
885    
886                    if (latestVersion) {
887                            dlAppHelperLocalService.registerDLSyncEventCallback(
888                                    DLSyncConstants.EVENT_UPDATE,
889                                    new LiferayFileEntry(dlFileEntry));
890    
891                            return dlFileEntry;
892                    }
893    
894                    return null;
895            }
896    
897            @Override
898            public DLFileEntry fetchFileEntry(long groupId, long folderId, String title)
899                    throws SystemException {
900    
901                    return dlFileEntryPersistence.fetchByG_F_T(groupId, folderId, title);
902            }
903    
904            @Override
905            public DLFileEntry fetchFileEntryByAnyImageId(long imageId)
906                    throws SystemException {
907    
908                    return dlFileEntryFinder.fetchByAnyImageId(imageId);
909            }
910    
911            @Override
912            public DLFileEntry fetchFileEntryByName(
913                            long groupId, long folderId, String name)
914                    throws SystemException {
915    
916                    return dlFileEntryPersistence.fetchByG_F_N(groupId, folderId, name);
917            }
918    
919            @Override
920            public List<DLFileEntry> getDDMStructureFileEntries(
921                            long groupId, long[] ddmStructureIds)
922                    throws SystemException {
923    
924                    return dlFileEntryFinder.findByDDMStructureIds(
925                            groupId, ddmStructureIds, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
926            }
927    
928            @Override
929            public List<DLFileEntry> getDDMStructureFileEntries(long[] ddmStructureIds)
930                    throws SystemException {
931    
932                    return dlFileEntryFinder.findByDDMStructureIds(
933                            ddmStructureIds, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
934            }
935    
936            @Override
937            public List<DLFileEntry> getExtraSettingsFileEntries(int start, int end)
938                    throws SystemException {
939    
940                    return dlFileEntryFinder.findByExtraSettings(start, end);
941            }
942    
943            @Override
944            public int getExtraSettingsFileEntriesCount() throws SystemException {
945                    return dlFileEntryFinder.countByExtraSettings();
946            }
947    
948            @Override
949            public File getFile(
950                            long userId, long fileEntryId, String version,
951                            boolean incrementCounter)
952                    throws PortalException, SystemException {
953    
954                    return getFile(userId, fileEntryId, version, incrementCounter, 1);
955            }
956    
957            @Override
958            public File getFile(
959                            long userId, long fileEntryId, String version,
960                            boolean incrementCounter, int increment)
961                    throws PortalException, SystemException {
962    
963                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
964                            fileEntryId);
965    
966                    if (incrementCounter) {
967                            dlFileEntryLocalService.incrementViewCounter(
968                                    dlFileEntry, increment);
969                    }
970    
971                    dlAppHelperLocalService.getFileAsStream(
972                            userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
973    
974                    return DLStoreUtil.getFile(
975                            dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
976                            dlFileEntry.getName(), version);
977            }
978    
979            @Override
980            public InputStream getFileAsStream(
981                            long userId, long fileEntryId, String version)
982                    throws PortalException, SystemException {
983    
984                    return getFileAsStream(userId, fileEntryId, version, true, 1);
985            }
986    
987            @Override
988            public InputStream getFileAsStream(
989                            long userId, long fileEntryId, String version,
990                            boolean incrementCounter)
991                    throws PortalException, SystemException {
992    
993                    return getFileAsStream(
994                            userId, fileEntryId, version, incrementCounter, 1);
995            }
996    
997            @Override
998            public InputStream getFileAsStream(
999                            long userId, long fileEntryId, String version,
1000                            boolean incrementCounter, int increment)
1001                    throws PortalException, SystemException {
1002    
1003                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1004                            fileEntryId);
1005    
1006                    if (incrementCounter) {
1007                            dlFileEntryLocalService.incrementViewCounter(
1008                                    dlFileEntry, increment);
1009                    }
1010    
1011                    dlAppHelperLocalService.getFileAsStream(
1012                            userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
1013    
1014                    return DLStoreUtil.getFileAsStream(
1015                            dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1016                            dlFileEntry.getName(), version);
1017            }
1018    
1019            @Override
1020            public List<DLFileEntry> getFileEntries(int start, int end)
1021                    throws SystemException {
1022    
1023                    return dlFileEntryPersistence.findAll(start, end);
1024            }
1025    
1026            @Override
1027            public List<DLFileEntry> getFileEntries(long groupId, long folderId)
1028                    throws SystemException {
1029    
1030                    return dlFileEntryPersistence.findByG_F(groupId, folderId);
1031            }
1032    
1033            @Override
1034            public List<DLFileEntry> getFileEntries(
1035                            long groupId, long folderId, int status, int start, int end,
1036                            OrderByComparator obc)
1037                    throws SystemException {
1038    
1039                    List<Long> folderIds = new ArrayList<Long>();
1040    
1041                    folderIds.add(folderId);
1042    
1043                    QueryDefinition queryDefinition = new QueryDefinition(
1044                            status, false, start, end, obc);
1045    
1046                    return dlFileEntryFinder.findByG_F(groupId, folderIds, queryDefinition);
1047            }
1048    
1049            @Override
1050            public List<DLFileEntry> getFileEntries(
1051                            long groupId, long folderId, int start, int end,
1052                            OrderByComparator obc)
1053                    throws SystemException {
1054    
1055                    return dlFileEntryPersistence.findByG_F(
1056                            groupId, folderId, start, end, obc);
1057            }
1058    
1059            @Override
1060            public List<DLFileEntry> getFileEntries(
1061                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
1062                            QueryDefinition queryDefinition)
1063                    throws Exception {
1064    
1065                    return dlFileEntryFinder.findByG_U_F_M(
1066                            groupId, userId, folderIds, mimeTypes, queryDefinition);
1067            }
1068    
1069            @Override
1070            public List<DLFileEntry> getFileEntries(long folderId, String name)
1071                    throws SystemException {
1072    
1073                    return dlFileEntryPersistence.findByF_N(folderId, name);
1074            }
1075    
1076            @Override
1077            public int getFileEntriesCount() throws SystemException {
1078                    return dlFileEntryPersistence.countAll();
1079            }
1080    
1081            @Override
1082            public int getFileEntriesCount(
1083                            long groupId, DateRange dateRange, long repositoryId,
1084                            QueryDefinition queryDefinition)
1085                    throws SystemException {
1086    
1087                    return dlFileEntryFinder.countByG_M_R(
1088                            groupId, dateRange, repositoryId, queryDefinition);
1089            }
1090    
1091            @Override
1092            public int getFileEntriesCount(long groupId, long folderId)
1093                    throws SystemException {
1094    
1095                    return dlFileEntryPersistence.countByG_F(groupId, folderId);
1096            }
1097    
1098            @Override
1099            public int getFileEntriesCount(long groupId, long folderId, int status)
1100                    throws SystemException {
1101    
1102                    List<Long> folderIds = new ArrayList<Long>();
1103    
1104                    folderIds.add(folderId);
1105    
1106                    return dlFileEntryFinder.countByG_F(
1107                            groupId, folderIds, new QueryDefinition(status));
1108            }
1109    
1110            @Override
1111            public int getFileEntriesCount(
1112                            long groupId, long userId, List<Long> folderIds, String[] mimeTypes,
1113                            QueryDefinition queryDefinition)
1114                    throws Exception {
1115    
1116                    return dlFileEntryFinder.countByG_U_F_M(
1117                            groupId, userId, folderIds, mimeTypes, queryDefinition);
1118            }
1119    
1120            @Override
1121            public DLFileEntry getFileEntry(long fileEntryId)
1122                    throws PortalException, SystemException {
1123    
1124                    return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
1125            }
1126    
1127            @Override
1128            public DLFileEntry getFileEntry(long groupId, long folderId, String title)
1129                    throws PortalException, SystemException {
1130    
1131                    DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
1132                            groupId, folderId, title);
1133    
1134                    if (dlFileEntry != null) {
1135                            return dlFileEntry;
1136                    }
1137    
1138                    List<DLFileVersion> dlFileVersions =
1139                            dlFileVersionPersistence.findByG_F_T_V(
1140                                    groupId, folderId, title,
1141                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
1142    
1143                    long userId = PrincipalThreadLocal.getUserId();
1144    
1145                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1146                            if (hasFileEntryLock(userId, dlFileVersion.getFileEntryId())) {
1147                                    return dlFileVersion.getFileEntry();
1148                            }
1149                    }
1150    
1151                    StringBundler sb = new StringBundler(8);
1152    
1153                    sb.append("No DLFileEntry exists with the key {");
1154                    sb.append("groupId=");
1155                    sb.append(groupId);
1156                    sb.append(", folderId=");
1157                    sb.append(folderId);
1158                    sb.append(", title=");
1159                    sb.append(title);
1160                    sb.append(StringPool.CLOSE_CURLY_BRACE);
1161    
1162                    throw new NoSuchFileEntryException(sb.toString());
1163            }
1164    
1165            @Override
1166            public DLFileEntry getFileEntryByName(
1167                            long groupId, long folderId, String name)
1168                    throws PortalException, SystemException {
1169    
1170                    return dlFileEntryPersistence.findByG_F_N(groupId, folderId, name);
1171            }
1172    
1173            @Override
1174            public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
1175                    throws PortalException, SystemException {
1176    
1177                    return dlFileEntryPersistence.findByUUID_G(uuid, groupId);
1178            }
1179    
1180            @Override
1181            public List<DLFileEntry> getGroupFileEntries(
1182                            long groupId, int start, int end)
1183                    throws SystemException {
1184    
1185                    return getGroupFileEntries(
1186                            groupId, start, end, new RepositoryModelModifiedDateComparator());
1187            }
1188    
1189            @Override
1190            public List<DLFileEntry> getGroupFileEntries(
1191                            long groupId, int start, int end, OrderByComparator obc)
1192                    throws SystemException {
1193    
1194                    return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
1195            }
1196    
1197            @Override
1198            public List<DLFileEntry> getGroupFileEntries(
1199                            long groupId, long userId, int start, int end)
1200                    throws SystemException {
1201    
1202                    return getGroupFileEntries(
1203                            groupId, userId, start, end,
1204                            new RepositoryModelModifiedDateComparator());
1205            }
1206    
1207            @Override
1208            public List<DLFileEntry> getGroupFileEntries(
1209                            long groupId, long userId, int start, int end,
1210                            OrderByComparator obc)
1211                    throws SystemException {
1212    
1213                    if (userId <= 0) {
1214                            return dlFileEntryPersistence.findByGroupId(
1215                                    groupId, start, end, obc);
1216                    }
1217                    else {
1218                            return dlFileEntryPersistence.findByG_U(
1219                                    groupId, userId, start, end, obc);
1220                    }
1221            }
1222    
1223            @Override
1224            public int getGroupFileEntriesCount(long groupId) throws SystemException {
1225                    return dlFileEntryPersistence.countByGroupId(groupId);
1226            }
1227    
1228            @Override
1229            public int getGroupFileEntriesCount(long groupId, long userId)
1230                    throws SystemException {
1231    
1232                    if (userId <= 0) {
1233                            return dlFileEntryPersistence.countByGroupId(groupId);
1234                    }
1235                    else {
1236                            return dlFileEntryPersistence.countByG_U(groupId, userId);
1237                    }
1238            }
1239    
1240            @Override
1241            public List<DLFileEntry> getMisversionedFileEntries()
1242                    throws SystemException {
1243    
1244                    return dlFileEntryFinder.findByMisversioned();
1245            }
1246    
1247            @Override
1248            public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
1249                    return dlFileEntryFinder.findByNoAssets();
1250            }
1251    
1252            @Override
1253            public List<DLFileEntry> getOrphanedFileEntries() throws SystemException {
1254                    return dlFileEntryFinder.findByOrphanedFileEntries();
1255            }
1256    
1257            @Override
1258            public boolean hasExtraSettings() throws SystemException {
1259                    if (dlFileEntryFinder.countByExtraSettings() > 0) {
1260                            return true;
1261                    }
1262                    else {
1263                            return false;
1264                    }
1265            }
1266    
1267            @Override
1268            public boolean hasFileEntryLock(long userId, long fileEntryId)
1269                    throws PortalException, SystemException {
1270    
1271                    boolean checkedOut = isFileEntryCheckedOut(fileEntryId);
1272    
1273                    DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
1274    
1275                    long folderId = dlFileEntry.getFolderId();
1276    
1277                    boolean hasLock = lockLocalService.hasLock(
1278                            userId, DLFileEntry.class.getName(), fileEntryId);
1279    
1280                    if (!hasLock &&
1281                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
1282    
1283                            hasLock = dlFolderService.hasInheritableLock(folderId);
1284                    }
1285    
1286                    if (checkedOut != hasLock) {
1287                            dlAppHelperLocalService.registerDLSyncEventCallback(
1288                                    DLSyncConstants.EVENT_UPDATE,
1289                                    new LiferayFileEntry(dlFileEntry));
1290                    }
1291    
1292                    return hasLock;
1293            }
1294    
1295            @BufferedIncrement(
1296                    configuration = "DLFileEntry", incrementClass = NumberIncrement.class)
1297            @Override
1298            public void incrementViewCounter(DLFileEntry dlFileEntry, int increment)
1299                    throws SystemException {
1300    
1301                    if (ExportImportThreadLocal.isImportInProcess()) {
1302                            return;
1303                    }
1304    
1305                    dlFileEntry.setReadCount(dlFileEntry.getReadCount() + increment);
1306    
1307                    dlFileEntryPersistence.update(dlFileEntry);
1308            }
1309    
1310            @Override
1311            public boolean isFileEntryCheckedOut(long fileEntryId)
1312                    throws PortalException, SystemException {
1313    
1314                    DLFileVersion dlFileVersion =
1315                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
1316    
1317                    String version = dlFileVersion.getVersion();
1318    
1319                    if (version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
1320                            return true;
1321                    }
1322                    else {
1323                            return false;
1324                    }
1325            }
1326    
1327            @Override
1328            public Lock lockFileEntry(long userId, long fileEntryId)
1329                    throws PortalException, SystemException {
1330    
1331                    if (hasFileEntryLock(userId, fileEntryId)) {
1332                            return lockLocalService.getLock(
1333                                    DLFileEntry.class.getName(), fileEntryId);
1334                    }
1335    
1336                    return lockLocalService.lock(
1337                            userId, DLFileEntry.class.getName(), fileEntryId, null, false,
1338                            DLFileEntryImpl.LOCK_EXPIRATION_TIME);
1339            }
1340    
1341            @Indexable(type = IndexableType.REINDEX)
1342            @Override
1343            public DLFileEntry moveFileEntry(
1344                            long userId, long fileEntryId, long newFolderId,
1345                            ServiceContext serviceContext)
1346                    throws PortalException, SystemException {
1347    
1348                    if (!hasFileEntryLock(userId, fileEntryId)) {
1349                            lockFileEntry(userId, fileEntryId);
1350                    }
1351    
1352                    try {
1353                            DLFileEntry dlFileEntry = moveFileEntryImpl(
1354                                    userId, fileEntryId, newFolderId, serviceContext);
1355    
1356                            dlAppHelperLocalService.moveFileEntry(
1357                                    new LiferayFileEntry(dlFileEntry));
1358    
1359                            return dlFileEntryTypeLocalService.updateFileEntryFileEntryType(
1360                                    dlFileEntry, serviceContext);
1361                    }
1362                    finally {
1363                            if (!isFileEntryCheckedOut(fileEntryId)) {
1364                                    unlockFileEntry(fileEntryId);
1365                            }
1366                    }
1367            }
1368    
1369            @Override
1370            public void rebuildTree(long companyId) throws SystemException {
1371                    dlFolderLocalService.rebuildTree(companyId);
1372    
1373                    Session session = dlFileEntryPersistence.openSession();
1374    
1375                    try {
1376                            TreePathUtil.rebuildTree(
1377                                    session, companyId, DLFileEntryModelImpl.TABLE_NAME,
1378                                    DLFolderModelImpl.TABLE_NAME, "folderId", false);
1379                    }
1380                    finally {
1381                            dlFileEntryPersistence.closeSession(session);
1382    
1383                            dlFileEntryPersistence.clearCache();
1384                    }
1385            }
1386    
1387            @Override
1388            public void revertFileEntry(
1389                            long userId, long fileEntryId, String version,
1390                            ServiceContext serviceContext)
1391                    throws PortalException, SystemException {
1392    
1393                    if (Validator.isNull(version) ||
1394                            version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
1395    
1396                            throw new InvalidFileVersionException();
1397                    }
1398    
1399                    DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
1400                            fileEntryId, version);
1401    
1402                    if (!dlFileVersion.isApproved()) {
1403                            throw new InvalidFileVersionException(
1404                                    "Cannot revert from an unapproved file version");
1405                    }
1406    
1407                    DLFileVersion latestDLFileVersion =
1408                            dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
1409    
1410                    if (version.equals(latestDLFileVersion.getVersion())) {
1411                            throw new InvalidFileVersionException(
1412                                    "Cannot revert from the latest file version");
1413                    }
1414    
1415                    String sourceFileName = dlFileVersion.getTitle();
1416                    String extension = dlFileVersion.getExtension();
1417                    String mimeType = dlFileVersion.getMimeType();
1418                    String title = dlFileVersion.getTitle();
1419                    String description = dlFileVersion.getDescription();
1420                    String changeLog = "Reverted to " + version;
1421                    boolean majorVersion = true;
1422                    String extraSettings = dlFileVersion.getExtraSettings();
1423                    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
1424                    Map<String, Fields> fieldsMap = null;
1425                    InputStream is = getFileAsStream(userId, fileEntryId, version);
1426                    long size = dlFileVersion.getSize();
1427    
1428                    DLFileEntry dlFileEntry = updateFileEntry(
1429                            userId, fileEntryId, sourceFileName, extension, mimeType, title,
1430                            description, changeLog, majorVersion, extraSettings,
1431                            fileEntryTypeId, fieldsMap, null, is, size, serviceContext);
1432    
1433                    DLFileVersion newDlFileVersion =
1434                            dlFileVersionLocalService.getFileVersion(
1435                                    fileEntryId, dlFileEntry.getVersion());
1436    
1437                    copyFileEntryMetadata(
1438                            dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
1439                            fileEntryId, dlFileVersion.getFileVersionId(),
1440                            newDlFileVersion.getFileVersionId(), serviceContext);
1441            }
1442    
1443            @Override
1444            public Hits search(
1445                            long groupId, long userId, long creatorUserId, int status,
1446                            int start, int end)
1447                    throws PortalException, SystemException {
1448    
1449                    return search(
1450                            groupId, userId, creatorUserId,
1451                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, null, status, start,
1452                            end);
1453            }
1454    
1455            @Override
1456            public Hits search(
1457                            long groupId, long userId, long creatorUserId, long folderId,
1458                            String[] mimeTypes, int status, int start, int end)
1459                    throws PortalException, SystemException {
1460    
1461                    Indexer indexer = IndexerRegistryUtil.getIndexer(
1462                            DLFileEntryConstants.getClassName());
1463    
1464                    SearchContext searchContext = new SearchContext();
1465    
1466                    searchContext.setAttribute(Field.STATUS, status);
1467    
1468                    if (creatorUserId > 0) {
1469                            searchContext.setAttribute(
1470                                    Field.USER_ID, String.valueOf(creatorUserId));
1471                    }
1472    
1473                    if (ArrayUtil.isNotEmpty(mimeTypes)) {
1474                            searchContext.setAttribute("mimeTypes", mimeTypes);
1475                    }
1476    
1477                    searchContext.setAttribute("paginationType", "none");
1478    
1479                    Group group = groupLocalService.getGroup(groupId);
1480    
1481                    searchContext.setCompanyId(group.getCompanyId());
1482    
1483                    searchContext.setEnd(end);
1484    
1485                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1486                            List<Long> folderIds = dlFolderService.getFolderIds(
1487                                    groupId, folderId);
1488    
1489                            searchContext.setFolderIds(folderIds);
1490                    }
1491    
1492                    searchContext.setGroupIds(new long[] {groupId});
1493                    searchContext.setSorts(new Sort(Field.MODIFIED_DATE, true));
1494                    searchContext.setStart(start);
1495                    searchContext.setUserId(userId);
1496    
1497                    return indexer.search(searchContext);
1498            }
1499    
1500            @Override
1501            public void unlockFileEntry(long fileEntryId) throws SystemException {
1502                    lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
1503            }
1504    
1505            @Override
1506            public DLFileEntry updateFileEntry(
1507                            long userId, long fileEntryId, String sourceFileName,
1508                            String mimeType, String title, String description, String changeLog,
1509                            boolean majorVersion, long fileEntryTypeId,
1510                            Map<String, Fields> fieldsMap, File file, InputStream is, long size,
1511                            ServiceContext serviceContext)
1512                    throws PortalException, SystemException {
1513    
1514                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1515                            fileEntryId);
1516    
1517                    String extension = DLAppUtil.getExtension(title, sourceFileName);
1518    
1519                    String extraSettings = StringPool.BLANK;
1520    
1521                    if (fileEntryTypeId == -1) {
1522                            fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
1523                    }
1524    
1525                    validateFileEntryTypeId(
1526                            PortalUtil.getSiteAndCompanyGroupIds(dlFileEntry.getGroupId()),
1527                            dlFileEntry.getFolderId(), fileEntryTypeId);
1528    
1529                    return updateFileEntry(
1530                            userId, fileEntryId, sourceFileName, extension, mimeType, title,
1531                            description, changeLog, majorVersion, extraSettings,
1532                            fileEntryTypeId, fieldsMap, file, is, size, serviceContext);
1533            }
1534    
1535            @Override
1536            public void updateSmallImage(long smallImageId, long largeImageId)
1537                    throws PortalException, SystemException {
1538    
1539                    try {
1540                            RenderedImage renderedImage = null;
1541    
1542                            Image largeImage = imageLocalService.getImage(largeImageId);
1543    
1544                            byte[] bytes = largeImage.getTextObj();
1545                            String contentType = largeImage.getType();
1546    
1547                            if (bytes != null) {
1548                                    ImageBag imageBag = ImageToolUtil.read(bytes);
1549    
1550                                    renderedImage = imageBag.getRenderedImage();
1551    
1552                                    //validate(bytes);
1553                            }
1554    
1555                            if (renderedImage != null) {
1556                                    int height = PrefsPropsUtil.getInteger(
1557                                            PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT);
1558                                    int width = PrefsPropsUtil.getInteger(
1559                                            PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH);
1560    
1561                                    RenderedImage thumbnailRenderedImage = ImageToolUtil.scale(
1562                                            renderedImage, height, width);
1563    
1564                                    imageLocalService.updateImage(
1565                                            smallImageId,
1566                                            ImageToolUtil.getBytes(
1567                                                    thumbnailRenderedImage, contentType));
1568                            }
1569                    }
1570                    catch (IOException ioe) {
1571                            throw new ImageSizeException(ioe);
1572                    }
1573            }
1574    
1575            @Override
1576            public DLFileEntry updateStatus(
1577                            long userId, long fileVersionId, int status,
1578                            Map<String, Serializable> workflowContext,
1579                            ServiceContext serviceContext)
1580                    throws PortalException, SystemException {
1581    
1582                    // File version
1583    
1584                    User user = userPersistence.findByPrimaryKey(userId);
1585    
1586                    DLFileVersion dlFileVersion = dlFileVersionPersistence.findByPrimaryKey(
1587                            fileVersionId);
1588    
1589                    int oldStatus = dlFileVersion.getStatus();
1590    
1591                    dlFileVersion.setStatus(status);
1592                    dlFileVersion.setStatusByUserId(user.getUserId());
1593                    dlFileVersion.setStatusByUserName(user.getFullName());
1594                    dlFileVersion.setStatusDate(new Date());
1595    
1596                    dlFileVersionPersistence.update(dlFileVersion);
1597    
1598                    // File entry
1599    
1600                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1601                            dlFileVersion.getFileEntryId());
1602    
1603                    if (status == WorkflowConstants.STATUS_APPROVED) {
1604                            if (DLUtil.compareVersions(
1605                                            dlFileEntry.getVersion(),
1606                                            dlFileVersion.getVersion()) <= 0) {
1607    
1608                                    dlFileEntry.setExtension(dlFileVersion.getExtension());
1609                                    dlFileEntry.setMimeType(dlFileVersion.getMimeType());
1610                                    dlFileEntry.setTitle(dlFileVersion.getTitle());
1611                                    dlFileEntry.setDescription(dlFileVersion.getDescription());
1612                                    dlFileEntry.setExtraSettings(dlFileVersion.getExtraSettings());
1613                                    dlFileEntry.setFileEntryTypeId(
1614                                            dlFileVersion.getFileEntryTypeId());
1615                                    dlFileEntry.setVersion(dlFileVersion.getVersion());
1616                                    dlFileEntry.setModifiedDate(dlFileVersion.getCreateDate());
1617                                    dlFileEntry.setSize(dlFileVersion.getSize());
1618    
1619                                    dlFileEntryPersistence.update(dlFileEntry);
1620                            }
1621                    }
1622                    else {
1623    
1624                            // File entry
1625    
1626                            if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
1627                                    dlFileEntry.getVersion().equals(dlFileVersion.getVersion())) {
1628    
1629                                    String newVersion = DLFileEntryConstants.VERSION_DEFAULT;
1630    
1631                                    List<DLFileVersion> approvedFileVersions =
1632                                            dlFileVersionPersistence.findByF_S(
1633                                                    dlFileEntry.getFileEntryId(),
1634                                                    WorkflowConstants.STATUS_APPROVED);
1635    
1636                                    if (!approvedFileVersions.isEmpty()) {
1637                                            newVersion = approvedFileVersions.get(0).getVersion();
1638                                    }
1639    
1640                                    dlFileEntry.setVersion(newVersion);
1641    
1642                                    dlFileEntryPersistence.update(dlFileEntry);
1643                            }
1644    
1645                            // Indexer
1646    
1647                            if (dlFileVersion.getVersion().equals(
1648                                            DLFileEntryConstants.VERSION_DEFAULT)) {
1649    
1650                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1651                                            DLFileEntry.class);
1652    
1653                                    indexer.delete(dlFileEntry);
1654                            }
1655                    }
1656    
1657                    // App helper
1658    
1659                    dlAppHelperLocalService.updateStatus(
1660                            userId, new LiferayFileEntry(dlFileEntry),
1661                            new LiferayFileVersion(dlFileVersion), oldStatus, status,
1662                            workflowContext, serviceContext);
1663    
1664                    // Indexer
1665    
1666                    if (((status == WorkflowConstants.STATUS_APPROVED) ||
1667                             (status == WorkflowConstants.STATUS_IN_TRASH) ||
1668                             (oldStatus == WorkflowConstants.STATUS_IN_TRASH)) &&
1669                            ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1670    
1671                            reindex(dlFileEntry);
1672                    }
1673    
1674                    return dlFileEntry;
1675            }
1676    
1677            @Override
1678            public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
1679                    throws PortalException, SystemException {
1680    
1681                    if (verifyFileEntryLock(fileEntryId, lockUuid) &&
1682                            isFileEntryCheckedOut(fileEntryId)) {
1683    
1684                            return true;
1685                    }
1686                    else {
1687                            return false;
1688                    }
1689            }
1690    
1691            @Override
1692            public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
1693                    throws PortalException, SystemException {
1694    
1695                    boolean lockVerified = false;
1696    
1697                    try {
1698                            Lock lock = lockLocalService.getLock(
1699                                    DLFileEntry.class.getName(), fileEntryId);
1700    
1701                            if (lock.getUuid().equals(lockUuid)) {
1702                                    lockVerified = true;
1703                            }
1704                    }
1705                    catch (PortalException pe) {
1706                            if ((pe instanceof ExpiredLockException) ||
1707                                    (pe instanceof NoSuchLockException)) {
1708    
1709                                    DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
1710                                            fileEntryId);
1711    
1712                                    lockVerified = dlFolderService.verifyInheritableLock(
1713                                            dlFileEntry.getFolderId(), lockUuid);
1714                            }
1715                            else {
1716                                    throw pe;
1717                            }
1718                    }
1719    
1720                    return lockVerified;
1721            }
1722    
1723            protected DLFileVersion addFileVersion(
1724                            User user, DLFileEntry dlFileEntry, Date modifiedDate,
1725                            String extension, String mimeType, String title, String description,
1726                            String changeLog, String extraSettings, long fileEntryTypeId,
1727                            Map<String, Fields> fieldsMap, String version, long size,
1728                            int status, ServiceContext serviceContext)
1729                    throws PortalException, SystemException {
1730    
1731                    long fileVersionId = counterLocalService.increment();
1732    
1733                    DLFileVersion dlFileVersion = dlFileVersionPersistence.create(
1734                            fileVersionId);
1735    
1736                    String uuid = ParamUtil.getString(
1737                            serviceContext, "fileVersionUuid", serviceContext.getUuid());
1738    
1739                    dlFileVersion.setUuid(uuid);
1740    
1741                    dlFileVersion.setGroupId(dlFileEntry.getGroupId());
1742                    dlFileVersion.setCompanyId(dlFileEntry.getCompanyId());
1743                    dlFileVersion.setUserId(user.getUserId());
1744                    dlFileVersion.setUserName(user.getFullName());
1745                    dlFileVersion.setCreateDate(modifiedDate);
1746                    dlFileVersion.setModifiedDate(modifiedDate);
1747                    dlFileVersion.setRepositoryId(dlFileEntry.getRepositoryId());
1748                    dlFileVersion.setFolderId(dlFileEntry.getFolderId());
1749                    dlFileVersion.setFileEntryId(dlFileEntry.getFileEntryId());
1750                    dlFileVersion.setTreePath(dlFileVersion.buildTreePath());
1751                    dlFileVersion.setExtension(extension);
1752                    dlFileVersion.setMimeType(mimeType);
1753                    dlFileVersion.setTitle(title);
1754                    dlFileVersion.setDescription(description);
1755                    dlFileVersion.setChangeLog(changeLog);
1756                    dlFileVersion.setExtraSettings(extraSettings);
1757                    dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
1758                    dlFileVersion.setVersion(version);
1759                    dlFileVersion.setSize(size);
1760                    dlFileVersion.setStatus(status);
1761                    dlFileVersion.setStatusByUserId(user.getUserId());
1762                    dlFileVersion.setStatusByUserName(user.getFullName());
1763                    dlFileVersion.setStatusDate(dlFileEntry.getModifiedDate());
1764                    dlFileVersion.setExpandoBridgeAttributes(serviceContext);
1765    
1766                    dlFileVersionPersistence.update(dlFileVersion);
1767    
1768                    if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
1769                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
1770                                    fileEntryTypeId, dlFileEntry.getFileEntryId(), fileVersionId,
1771                                    fieldsMap, serviceContext);
1772                    }
1773    
1774                    return dlFileVersion;
1775            }
1776    
1777            protected void convertExtraSettings(
1778                            DLFileEntry dlFileEntry, DLFileVersion dlFileVersion, String[] keys)
1779                    throws PortalException, SystemException {
1780    
1781                    UnicodeProperties extraSettingsProperties =
1782                            dlFileVersion.getExtraSettingsProperties();
1783    
1784                    ExpandoBridge expandoBridge = dlFileVersion.getExpandoBridge();
1785    
1786                    convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1787    
1788                    dlFileVersion.setExtraSettingsProperties(extraSettingsProperties);
1789    
1790                    dlFileVersionPersistence.update(dlFileVersion);
1791    
1792                    int status = dlFileVersion.getStatus();
1793    
1794                    if ((status == WorkflowConstants.STATUS_APPROVED) &&
1795                            (DLUtil.compareVersions(
1796                                    dlFileEntry.getVersion(), dlFileVersion.getVersion()) <= 0)) {
1797    
1798                            reindex(dlFileEntry);
1799                    }
1800            }
1801    
1802            protected void convertExtraSettings(DLFileEntry dlFileEntry, String[] keys)
1803                    throws PortalException, SystemException {
1804    
1805                    UnicodeProperties extraSettingsProperties =
1806                            dlFileEntry.getExtraSettingsProperties();
1807    
1808                    ExpandoBridge expandoBridge = dlFileEntry.getExpandoBridge();
1809    
1810                    convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1811    
1812                    dlFileEntry.setExtraSettingsProperties(extraSettingsProperties);
1813    
1814                    dlFileEntryPersistence.update(dlFileEntry);
1815    
1816                    List<DLFileVersion> dlFileVersions =
1817                            dlFileVersionLocalService.getFileVersions(
1818                                    dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1819    
1820                    for (DLFileVersion dlFileVersion : dlFileVersions) {
1821                            convertExtraSettings(dlFileEntry, dlFileVersion, keys);
1822                    }
1823            }
1824    
1825            protected void convertExtraSettings(
1826                    UnicodeProperties extraSettingsProperties, ExpandoBridge expandoBridge,
1827                    String[] keys) {
1828    
1829                    for (String key : keys) {
1830                            String value = extraSettingsProperties.remove(key);
1831    
1832                            if (Validator.isNull(value)) {
1833                                    continue;
1834                            }
1835    
1836                            int type = expandoBridge.getAttributeType(key);
1837    
1838                            Serializable serializable = ExpandoColumnConstants.getSerializable(
1839                                    type, value);
1840    
1841                            expandoBridge.setAttribute(key, serializable);
1842                    }
1843            }
1844    
1845            protected void copyExpandoRowModifiedDate(
1846                            long companyId, long sourceFileVersionId,
1847                            long destinationFileVersionId)
1848                    throws PortalException, SystemException {
1849    
1850                    ExpandoTable expandoTable = null;
1851    
1852                    try {
1853                            expandoTable = expandoTableLocalService.getDefaultTable(
1854                                    companyId, DLFileEntry.class.getName());
1855                    }
1856                    catch (NoSuchTableException nste) {
1857                            return;
1858                    }
1859    
1860                    Date sourceModifiedDate = null;
1861    
1862                    try {
1863                            ExpandoRow sourceExpandoRow = expandoRowLocalService.getRow(
1864                                    expandoTable.getTableId(), sourceFileVersionId);
1865    
1866                            sourceModifiedDate = sourceExpandoRow.getModifiedDate();
1867                    }
1868                    catch (NoSuchRowException nsre) {
1869                            return;
1870                    }
1871    
1872                    try {
1873                            ExpandoRow destinationExpandoRow = expandoRowLocalService.getRow(
1874                                    expandoTable.getTableId(), destinationFileVersionId);
1875    
1876                            destinationExpandoRow.setModifiedDate(sourceModifiedDate);
1877    
1878                            expandoRowLocalService.updateExpandoRow(destinationExpandoRow);
1879                    }
1880                    catch (NoSuchRowException nsre) {
1881                    }
1882            }
1883    
1884            protected void copyFileEntryMetadata(
1885                            long companyId, long fileEntryTypeId, long fileEntryId,
1886                            long fromFileVersionId, long toFileVersionId,
1887                            ServiceContext serviceContext, Map<String, Fields> fieldsMap,
1888                            List<DDMStructure> ddmStructures)
1889                    throws PortalException, SystemException {
1890    
1891                    for (DDMStructure ddmStructure : ddmStructures) {
1892                            DLFileEntryMetadata dlFileEntryMetadata =
1893                                    dlFileEntryMetadataLocalService.fetchFileEntryMetadata(
1894                                            ddmStructure.getStructureId(), fromFileVersionId);
1895    
1896                            if (dlFileEntryMetadata == null) {
1897                                    continue;
1898                            }
1899    
1900                            Fields fields = StorageEngineUtil.getFields(
1901                                    dlFileEntryMetadata.getDDMStorageId());
1902    
1903                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
1904                    }
1905    
1906                    if (!fieldsMap.isEmpty()) {
1907                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
1908                                    companyId, ddmStructures, fileEntryTypeId, fileEntryId,
1909                                    toFileVersionId, fieldsMap, serviceContext);
1910                    }
1911            }
1912    
1913            protected String getNextVersion(
1914                            DLFileEntry dlFileEntry, boolean majorVersion, int workflowAction)
1915                    throws PortalException, SystemException {
1916    
1917                    String version = dlFileEntry.getVersion();
1918    
1919                    try {
1920                            DLFileVersion dlFileVersion =
1921                                    dlFileVersionLocalService.getLatestFileVersion(
1922                                            dlFileEntry.getFileEntryId(), true);
1923    
1924                            version = dlFileVersion.getVersion();
1925                    }
1926                    catch (NoSuchFileVersionException nsfve) {
1927                    }
1928    
1929                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1930                            majorVersion = false;
1931                    }
1932    
1933                    int[] versionParts = StringUtil.split(version, StringPool.PERIOD, 0);
1934    
1935                    if (majorVersion) {
1936                            versionParts[0]++;
1937                            versionParts[1] = 0;
1938                    }
1939                    else {
1940                            versionParts[1]++;
1941                    }
1942    
1943                    return versionParts[0] + StringPool.PERIOD + versionParts[1];
1944            }
1945    
1946            protected boolean isKeepFileVersionLabel(
1947                            DLFileEntry dlFileEntry, DLFileVersion lastDLFileVersion,
1948                            DLFileVersion latestDLFileVersion, int workflowAction)
1949                    throws PortalException, SystemException {
1950    
1951                    if (PropsValues.DL_FILE_ENTRY_VERSION_POLICY != 1) {
1952                            return false;
1953                    }
1954    
1955                    if (!Validator.equals(
1956                                    lastDLFileVersion.getTitle(), latestDLFileVersion.getTitle())) {
1957    
1958                            return false;
1959                    }
1960    
1961                    if (!Validator.equals(
1962                                    lastDLFileVersion.getDescription(),
1963                                    latestDLFileVersion.getDescription())) {
1964    
1965                            return false;
1966                    }
1967    
1968                    if (lastDLFileVersion.getFileEntryTypeId() !=
1969                                    latestDLFileVersion.getFileEntryTypeId()) {
1970    
1971                            return false;
1972                    }
1973    
1974                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1975                            return false;
1976                    }
1977    
1978                    // File entry type
1979    
1980                    DLFileEntryType dlFileEntryType =
1981                            dlFileEntryTypeLocalService.getFileEntryType(
1982                                    lastDLFileVersion.getFileEntryTypeId());
1983    
1984                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
1985    
1986                    for (DDMStructure ddmStructure : ddmStructures) {
1987                            DLFileEntryMetadata lastFileEntryMetadata = null;
1988    
1989                            try {
1990                                    lastFileEntryMetadata =
1991                                            dlFileEntryMetadataLocalService.getFileEntryMetadata(
1992                                                    ddmStructure.getStructureId(),
1993                                                    lastDLFileVersion.getFileVersionId());
1994                            }
1995                            catch (NoSuchFileEntryMetadataException nsfeme) {
1996                                    return false;
1997                            }
1998    
1999                            DLFileEntryMetadata latestFileEntryMetadata =
2000                                    dlFileEntryMetadataLocalService.getFileEntryMetadata(
2001                                            ddmStructure.getStructureId(),
2002                                            latestDLFileVersion.getFileVersionId());
2003    
2004                            Fields lastFields = StorageEngineUtil.getFields(
2005                                    lastFileEntryMetadata.getDDMStorageId());
2006                            Fields latestFields = StorageEngineUtil.getFields(
2007                                    latestFileEntryMetadata.getDDMStorageId());
2008    
2009                            if (!Validator.equals(lastFields, latestFields)) {
2010                                    return false;
2011                            }
2012                    }
2013    
2014                    // Expando
2015    
2016                    ExpandoBridge lastExpandoBridge = lastDLFileVersion.getExpandoBridge();
2017                    ExpandoBridge latestExpandoBridge =
2018                            latestDLFileVersion.getExpandoBridge();
2019    
2020                    Map<String, Serializable> lastAttributes =
2021                            lastExpandoBridge.getAttributes();
2022                    Map<String, Serializable> latestAttributes =
2023                            latestExpandoBridge.getAttributes();
2024    
2025                    if (!Validator.equals(lastAttributes, latestAttributes)) {
2026                            return false;
2027                    }
2028    
2029                    // Size
2030    
2031                    long lastSize = lastDLFileVersion.getSize();
2032                    long latestSize = latestDLFileVersion.getSize();
2033    
2034                    if ((lastSize == 0) && (latestSize >= 0)) {
2035                            return true;
2036                    }
2037    
2038                    if (lastSize != latestSize) {
2039                            return false;
2040                    }
2041    
2042                    // Checksum
2043    
2044                    InputStream lastInputStream = null;
2045                    InputStream latestInputStream = null;
2046    
2047                    try {
2048                            String lastChecksum = lastDLFileVersion.getChecksum();
2049    
2050                            if (Validator.isNull(lastChecksum)) {
2051                                    lastInputStream = DLStoreUtil.getFileAsStream(
2052                                            dlFileEntry.getCompanyId(),
2053                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
2054                                            lastDLFileVersion.getVersion());
2055    
2056                                    lastChecksum = DigesterUtil.digestBase64(lastInputStream);
2057    
2058                                    lastDLFileVersion.setChecksum(lastChecksum);
2059    
2060                                    dlFileVersionPersistence.update(lastDLFileVersion);
2061                            }
2062    
2063                            latestInputStream = DLStoreUtil.getFileAsStream(
2064                                    dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2065                                    dlFileEntry.getName(), latestDLFileVersion.getVersion());
2066    
2067                            String latestChecksum = DigesterUtil.digestBase64(
2068                                    latestInputStream);
2069    
2070                            if (lastChecksum.equals(latestChecksum)) {
2071                                    return true;
2072                            }
2073    
2074                            latestDLFileVersion.setChecksum(latestChecksum);
2075    
2076                            dlFileVersionPersistence.update(latestDLFileVersion);
2077                    }
2078                    catch (Exception e) {
2079                            if (_log.isWarnEnabled()) {
2080                                    _log.warn(e, e);
2081                            }
2082                    }
2083                    finally {
2084                            StreamUtil.cleanUp(lastInputStream);
2085                            StreamUtil.cleanUp(latestInputStream);
2086                    }
2087    
2088                    return false;
2089            }
2090    
2091            protected DLFileEntry moveFileEntryImpl(
2092                            long userId, long fileEntryId, long newFolderId,
2093                            ServiceContext serviceContext)
2094                    throws PortalException, SystemException {
2095    
2096                    // File entry
2097    
2098                    User user = userPersistence.findByPrimaryKey(userId);
2099                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
2100                            fileEntryId);
2101    
2102                    long oldDataRepositoryId = dlFileEntry.getDataRepositoryId();
2103    
2104                    validateFile(
2105                            dlFileEntry.getGroupId(), newFolderId, dlFileEntry.getFileEntryId(),
2106                            dlFileEntry.getTitle(), dlFileEntry.getExtension());
2107    
2108                    if (DLStoreUtil.hasFile(
2109                                    user.getCompanyId(),
2110                                    DLFolderConstants.getDataRepositoryId(
2111                                            dlFileEntry.getGroupId(), newFolderId),
2112                                    dlFileEntry.getName(), StringPool.BLANK)) {
2113    
2114                            throw new DuplicateFileException(dlFileEntry.getName());
2115                    }
2116    
2117                    dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(null));
2118                    dlFileEntry.setFolderId(newFolderId);
2119                    dlFileEntry.setTreePath(dlFileEntry.buildTreePath());
2120    
2121                    dlFileEntryPersistence.update(dlFileEntry);
2122    
2123                    // File version
2124    
2125                    List<DLFileVersion> dlFileVersions =
2126                            dlFileVersionPersistence.findByFileEntryId(fileEntryId);
2127    
2128                    for (DLFileVersion dlFileVersion : dlFileVersions) {
2129                            dlFileVersion.setFolderId(newFolderId);
2130                            dlFileVersion.setTreePath(dlFileVersion.buildTreePath());
2131    
2132                            dlFileVersionPersistence.update(dlFileVersion);
2133                    }
2134    
2135                    // Folder
2136    
2137                    if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
2138                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
2139                                    newFolderId);
2140    
2141                            dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
2142    
2143                            dlFolderPersistence.update(dlFolder);
2144                    }
2145    
2146                    // File
2147    
2148                    DLStoreUtil.updateFile(
2149                            user.getCompanyId(), oldDataRepositoryId,
2150                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
2151    
2152                    return dlFileEntry;
2153            }
2154    
2155            protected void reindex(DLFileEntry dlFileEntry) throws SearchException {
2156                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
2157                            DLFileEntry.class);
2158    
2159                    indexer.reindex(dlFileEntry);
2160            }
2161    
2162            protected void removeFileVersion(
2163                            DLFileEntry dlFileEntry, DLFileVersion dlFileVersion)
2164                    throws PortalException, SystemException {
2165    
2166                    dlFileVersionPersistence.remove(dlFileVersion);
2167    
2168                    expandoRowLocalService.deleteRows(dlFileVersion.getFileVersionId());
2169    
2170                    dlFileEntryMetadataLocalService.deleteFileVersionFileEntryMetadata(
2171                            dlFileVersion.getFileVersionId());
2172    
2173                    try {
2174                            DLStoreUtil.deleteFile(
2175                                    dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2176                                    dlFileEntry.getName(),
2177                                    DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
2178                    }
2179                    catch (NoSuchModelException nsme) {
2180                    }
2181    
2182                    unlockFileEntry(dlFileEntry.getFileEntryId());
2183            }
2184    
2185            protected void startWorkflowInstance(
2186                            long userId, ServiceContext serviceContext,
2187                            DLFileVersion dlFileVersion, String syncEventType)
2188                    throws PortalException, SystemException {
2189    
2190                    Map<String, Serializable> workflowContext =
2191                            new HashMap<String, Serializable>();
2192    
2193                    workflowContext.put("event", syncEventType);
2194    
2195                    WorkflowHandlerRegistryUtil.startWorkflowInstance(
2196                            dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(), userId,
2197                            DLFileEntry.class.getName(), dlFileVersion.getFileVersionId(),
2198                            dlFileVersion, serviceContext, workflowContext);
2199            }
2200    
2201            protected DLFileEntry updateFileEntry(
2202                            long userId, long fileEntryId, String sourceFileName,
2203                            String extension, String mimeType, String title, String description,
2204                            String changeLog, boolean majorVersion, String extraSettings,
2205                            long fileEntryTypeId, Map<String, Fields> fieldsMap, File file,
2206                            InputStream is, long size, ServiceContext serviceContext)
2207                    throws PortalException, SystemException {
2208    
2209                    User user = userPersistence.findByPrimaryKey(userId);
2210                    DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
2211                            fileEntryId);
2212    
2213                    boolean checkedOut = dlFileEntry.isCheckedOut();
2214    
2215                    DLFileVersion dlFileVersion =
2216                            dlFileVersionLocalService.getLatestFileVersion(
2217                                    fileEntryId, !checkedOut);
2218    
2219                    boolean autoCheckIn = !checkedOut && dlFileVersion.isApproved();
2220    
2221                    if (autoCheckIn) {
2222                            dlFileEntry = checkOutFileEntry(
2223                                    userId, fileEntryId, serviceContext);
2224                    }
2225                    else if (!checkedOut) {
2226                            lockFileEntry(userId, fileEntryId);
2227                    }
2228    
2229                    if (!hasFileEntryLock(userId, fileEntryId)) {
2230                            lockFileEntry(userId, fileEntryId);
2231                    }
2232    
2233                    if (checkedOut || autoCheckIn) {
2234                            dlFileVersion = dlFileVersionLocalService.getLatestFileVersion(
2235                                    fileEntryId, false);
2236                    }
2237    
2238                    try {
2239                            if (Validator.isNull(extension)) {
2240                                    extension = dlFileEntry.getExtension();
2241                            }
2242    
2243                            if (Validator.isNull(mimeType)) {
2244                                    mimeType = dlFileEntry.getMimeType();
2245                            }
2246    
2247                            if (Validator.isNull(title)) {
2248                                    title = sourceFileName;
2249    
2250                                    if (Validator.isNull(title)) {
2251                                            title = dlFileEntry.getTitle();
2252                                    }
2253                            }
2254    
2255                            Date now = new Date();
2256    
2257                            validateFile(
2258                                    dlFileEntry.getGroupId(), dlFileEntry.getFolderId(),
2259                                    dlFileEntry.getFileEntryId(), title, extension, sourceFileName,
2260                                    file, is);
2261    
2262                            // File version
2263    
2264                            String version = dlFileVersion.getVersion();
2265    
2266                            if (size == 0) {
2267                                    size = dlFileVersion.getSize();
2268                            }
2269    
2270                            updateFileVersion(
2271                                    user, dlFileVersion, sourceFileName, extension, mimeType, title,
2272                                    description, changeLog, extraSettings, fileEntryTypeId,
2273                                    fieldsMap, version, size, dlFileVersion.getStatus(),
2274                                    serviceContext.getModifiedDate(now), serviceContext);
2275    
2276                            // Folder
2277    
2278                            if (!checkedOut &&
2279                                    (dlFileEntry.getFolderId() !=
2280                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
2281    
2282                                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
2283                                            dlFileEntry.getFolderId());
2284    
2285                                    dlFolder.setLastPostDate(serviceContext.getModifiedDate(now));
2286    
2287                                    dlFolderPersistence.update(dlFolder);
2288                            }
2289    
2290                            // App helper
2291    
2292                            dlAppHelperLocalService.updateAsset(
2293                                    userId, new LiferayFileEntry(dlFileEntry),
2294                                    new LiferayFileVersion(dlFileVersion),
2295                                    serviceContext.getAssetCategoryIds(),
2296                                    serviceContext.getAssetTagNames(),
2297                                    serviceContext.getAssetLinkEntryIds());
2298    
2299                            // File
2300    
2301                            if ((file != null) || (is != null)) {
2302                                    try {
2303                                            DLStoreUtil.deleteFile(
2304                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2305                                                    dlFileEntry.getName(), version);
2306                                    }
2307                                    catch (NoSuchModelException nsme) {
2308                                    }
2309    
2310                                    if (file != null) {
2311                                            DLStoreUtil.updateFile(
2312                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2313                                                    dlFileEntry.getName(), dlFileEntry.getExtension(),
2314                                                    false, version, sourceFileName, file);
2315                                    }
2316                                    else {
2317                                            DLStoreUtil.updateFile(
2318                                                    user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2319                                                    dlFileEntry.getName(), dlFileEntry.getExtension(),
2320                                                    false, version, sourceFileName, is);
2321                                    }
2322                            }
2323    
2324                            if (autoCheckIn) {
2325                                    if (ExportImportThreadLocal.isImportInProcess()) {
2326                                            checkInFileEntry(
2327                                                    userId, fileEntryId, majorVersion, changeLog,
2328                                                    serviceContext);
2329                                    }
2330                                    else {
2331                                            dlFileEntryService.checkInFileEntry(
2332                                                    fileEntryId, majorVersion, changeLog, serviceContext);
2333                                    }
2334                            }
2335                            else if (!checkedOut &&
2336                                             (serviceContext.getWorkflowAction() ==
2337                                                    WorkflowConstants.ACTION_PUBLISH)) {
2338    
2339                                    String syncEvent = DLSyncConstants.EVENT_UPDATE;
2340    
2341                                    if (dlFileVersion.getVersion().equals(
2342                                                    DLFileEntryConstants.VERSION_DEFAULT)) {
2343    
2344                                            syncEvent = DLSyncConstants.EVENT_ADD;
2345                                    }
2346    
2347                                    startWorkflowInstance(
2348                                            userId, serviceContext, dlFileVersion, syncEvent);
2349                            }
2350                    }
2351                    catch (PortalException pe) {
2352                            if (autoCheckIn) {
2353                                    try {
2354                                            if (ExportImportThreadLocal.isImportInProcess()) {
2355                                                    cancelCheckOut(userId, fileEntryId);
2356                                            }
2357                                            else {
2358                                                    dlFileEntryService.cancelCheckOut(fileEntryId);
2359                                            }
2360                                    }
2361                                    catch (Exception e) {
2362                                            _log.error(e, e);
2363                                    }
2364                            }
2365    
2366                            throw pe;
2367                    }
2368                    catch (SystemException se) {
2369                            if (autoCheckIn) {
2370                                    try {
2371                                            if (ExportImportThreadLocal.isImportInProcess()) {
2372                                                    cancelCheckOut(userId, fileEntryId);
2373                                            }
2374                                            else {
2375                                                    dlFileEntryService.cancelCheckOut(fileEntryId);
2376                                            }
2377                                    }
2378                                    catch (Exception e) {
2379                                            _log.error(e, e);
2380                                    }
2381                            }
2382    
2383                            throw se;
2384                    }
2385                    finally {
2386                            if (!autoCheckIn && !checkedOut) {
2387                                    unlockFileEntry(fileEntryId);
2388                            }
2389                    }
2390    
2391                    return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
2392            }
2393    
2394            protected DLFileVersion updateFileVersion(
2395                            User user, DLFileVersion dlFileVersion, String sourceFileName,
2396                            String extension, String mimeType, String title, String description,
2397                            String changeLog, String extraSettings, long fileEntryTypeId,
2398                            Map<String, Fields> fieldsMap, String version, long size,
2399                            int status, Date statusDate, ServiceContext serviceContext)
2400                    throws PortalException, SystemException {
2401    
2402                    dlFileVersion.setUserId(user.getUserId());
2403                    dlFileVersion.setUserName(user.getFullName());
2404                    dlFileVersion.setModifiedDate(statusDate);
2405    
2406                    if (Validator.isNotNull(sourceFileName)) {
2407                            dlFileVersion.setExtension(extension);
2408                            dlFileVersion.setMimeType(mimeType);
2409                    }
2410    
2411                    dlFileVersion.setTitle(title);
2412                    dlFileVersion.setDescription(description);
2413                    dlFileVersion.setChangeLog(changeLog);
2414                    dlFileVersion.setExtraSettings(extraSettings);
2415                    dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
2416                    dlFileVersion.setVersion(version);
2417                    dlFileVersion.setSize(size);
2418                    dlFileVersion.setStatus(status);
2419                    dlFileVersion.setStatusByUserId(user.getUserId());
2420                    dlFileVersion.setStatusByUserName(user.getFullName());
2421                    dlFileVersion.setStatusDate(statusDate);
2422                    dlFileVersion.setExpandoBridgeAttributes(serviceContext);
2423    
2424                    dlFileVersion = dlFileVersionPersistence.update(dlFileVersion);
2425    
2426                    if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
2427                            dlFileEntryMetadataLocalService.updateFileEntryMetadata(
2428                                    fileEntryTypeId, dlFileVersion.getFileEntryId(),
2429                                    dlFileVersion.getFileVersionId(), fieldsMap, serviceContext);
2430                    }
2431    
2432                    return dlFileVersion;
2433            }
2434    
2435            @Override
2436            public void validateFile(
2437                            long groupId, long folderId, long fileEntryId, String title,
2438                            String extension)
2439                    throws PortalException, SystemException {
2440    
2441                    DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
2442                            groupId, folderId, title);
2443    
2444                    if (dlFolder != null) {
2445                            throw new DuplicateFolderNameException(title);
2446                    }
2447    
2448                    DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
2449                            groupId, folderId, title);
2450    
2451                    if ((dlFileEntry != null) &&
2452                            (dlFileEntry.getFileEntryId() != fileEntryId)) {
2453    
2454                            throw new DuplicateFileException(title);
2455                    }
2456    
2457                    if (Validator.isNull(extension)) {
2458                            return;
2459                    }
2460    
2461                    String periodAndExtension = StringPool.PERIOD.concat(extension);
2462    
2463                    if (title.endsWith(periodAndExtension)) {
2464                            title = FileUtil.stripExtension(title);
2465                    }
2466                    else {
2467                            title += periodAndExtension;
2468                    }
2469    
2470                    dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
2471                            groupId, folderId, title);
2472    
2473                    if ((dlFileEntry != null) &&
2474                            (dlFileEntry.getFileEntryId() != fileEntryId) &&
2475                            extension.equals(dlFileEntry.getExtension())) {
2476    
2477                            throw new DuplicateFileException(title);
2478                    }
2479            }
2480    
2481            protected void validateFile(
2482                            long groupId, long folderId, long fileEntryId, String title,
2483                            String extension, String sourceFileName, File file, InputStream is)
2484                    throws PortalException, SystemException {
2485    
2486                    if (Validator.isNotNull(sourceFileName)) {
2487                            if (file != null) {
2488                                    DLStoreUtil.validate(
2489                                            sourceFileName, extension, sourceFileName, true, file);
2490                            }
2491                            else {
2492                                    DLStoreUtil.validate(
2493                                            sourceFileName, extension, sourceFileName, true, is);
2494                            }
2495                    }
2496    
2497                    validateFileExtension(extension);
2498                    validateFileName(title);
2499    
2500                    DLStoreUtil.validate(title, false);
2501    
2502                    validateFile(groupId, folderId, fileEntryId, title, extension);
2503            }
2504    
2505            protected void validateFileEntryTypeId(
2506                            long[] groupIds, long folderId, long fileEntryTypeId)
2507                    throws PortalException, SystemException {
2508    
2509                    List<DLFileEntryType> dlFileEntryTypes =
2510                            dlFileEntryTypeLocalService.getFolderFileEntryTypes(
2511                                    groupIds, folderId, true);
2512    
2513                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
2514                            if (dlFileEntryType.getFileEntryTypeId() == fileEntryTypeId) {
2515                                    return;
2516                            }
2517                    }
2518    
2519                    throw new InvalidFileEntryTypeException(
2520                            "Invalid file entry type " + fileEntryTypeId + " for folder " +
2521                                    folderId);
2522            }
2523    
2524            protected void validateFileExtension(String extension)
2525                    throws PortalException {
2526    
2527                    if (Validator.isNotNull(extension)) {
2528                            int maxLength = ModelHintsUtil.getMaxLength(
2529                                    DLFileEntry.class.getName(), "extension");
2530    
2531                            if (extension.length() > maxLength) {
2532                                    throw new FileExtensionException();
2533                            }
2534                    }
2535            }
2536    
2537            protected void validateFileName(String fileName) throws PortalException {
2538                    if (fileName.contains(StringPool.SLASH)) {
2539                            throw new FileNameException(fileName);
2540                    }
2541            }
2542    
2543            private static final int _DELETE_INTERVAL = 100;
2544    
2545            private static Log _log = LogFactoryUtil.getLog(
2546                    DLFileEntryLocalServiceImpl.class);
2547    
2548    }