001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.LocalRepository;
019    import com.liferay.portal.kernel.repository.model.FileContentReference;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.ModelValidator;
022    import com.liferay.portal.kernel.service.ServiceContext;
023    import com.liferay.portal.repository.util.LocalRepositoryWrapper;
024    import com.liferay.portlet.documentlibrary.util.DLAppUtil;
025    
026    import java.io.File;
027    import java.io.InputStream;
028    
029    /**
030     * @author Adolfo P??rez
031     */
032    public class ModelValidatorLocalRepositoryWrapper
033            extends LocalRepositoryWrapper {
034    
035            public ModelValidatorLocalRepositoryWrapper(
036                    LocalRepository localRepository,
037                    ModelValidator<FileContentReference> modelValidator) {
038    
039                    super(localRepository);
040    
041                    _modelValidator = modelValidator;
042            }
043    
044            @Override
045            public FileEntry addFileEntry(
046                            long userId, long folderId, String sourceFileName, String mimeType,
047                            String title, String description, String changeLog, File file,
048                            ServiceContext serviceContext)
049                    throws PortalException {
050    
051                    FileContentReference fileContentReference =
052                            FileContentReference.fromFile(
053                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
054                                    mimeType, file);
055    
056                    _modelValidator.validate(fileContentReference);
057    
058                    return super.addFileEntry(
059                            userId, folderId, sourceFileName, mimeType, title, description,
060                            changeLog, file, serviceContext);
061            }
062    
063            @Override
064            public FileEntry addFileEntry(
065                            long userId, long folderId, String sourceFileName, String mimeType,
066                            String title, String description, String changeLog, InputStream is,
067                            long size, ServiceContext serviceContext)
068                    throws PortalException {
069    
070                    FileContentReference fileContentReference =
071                            FileContentReference.fromInputStream(
072                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
073                                    mimeType, is, size);
074    
075                    _modelValidator.validate(fileContentReference);
076    
077                    return super.addFileEntry(
078                            userId, folderId, sourceFileName, mimeType, title, description,
079                            changeLog, is, size, serviceContext);
080            }
081    
082            @Override
083            public FileEntry updateFileEntry(
084                            long userId, long fileEntryId, String sourceFileName,
085                            String mimeType, String title, String description, String changeLog,
086                            boolean majorVersion, File file, ServiceContext serviceContext)
087                    throws PortalException {
088    
089                    FileContentReference fileContentReference =
090                            FileContentReference.fromFile(
091                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
092                                    mimeType, file);
093    
094                    _modelValidator.validate(fileContentReference);
095    
096                    return super.updateFileEntry(
097                            userId, fileEntryId, sourceFileName, mimeType, title, description,
098                            changeLog, majorVersion, file, serviceContext);
099            }
100    
101            @Override
102            public FileEntry updateFileEntry(
103                            long userId, long fileEntryId, String sourceFileName,
104                            String mimeType, String title, String description, String changeLog,
105                            boolean majorVersion, InputStream is, long size,
106                            ServiceContext serviceContext)
107                    throws PortalException {
108    
109                    FileContentReference fileContentReference =
110                            FileContentReference.fromInputStream(
111                                    sourceFileName, DLAppUtil.getExtension(title, sourceFileName),
112                                    mimeType, is, size);
113    
114                    _modelValidator.validate(fileContentReference);
115    
116                    return super.updateFileEntry(
117                            userId, fileEntryId, sourceFileName, mimeType, title, description,
118                            changeLog, majorVersion, is, size, serviceContext);
119            }
120    
121            private final ModelValidator<FileContentReference> _modelValidator;
122    
123    }