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.kernel.repository.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.model.FileContentReference;
019    import com.liferay.portal.kernel.repository.model.ModelValidator;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portlet.documentlibrary.util.DLValidatorUtil;
022    
023    /**
024     * @author Adolfo P??rez
025     */
026    public class ModelValidatorUtil {
027    
028            public static final <T> ModelValidator<T> compose(
029                    ModelValidator<T>... modelValidators) {
030    
031                    return new CompositeModelValidator<>(modelValidators);
032            }
033    
034            public static final ModelValidator<FileContentReference>
035                    getDefaultDLFileEntryModelValidator() {
036    
037                    return compose(
038                            getDefaultFileNameModelValidator(),
039                            getDefaultFileExtensionModelValidator(),
040                            getDefaultFileSizeModelValidator());
041            }
042    
043            public static final ModelValidator<FileContentReference>
044                    getDefaultFileExtensionModelValidator() {
045    
046                    return _defaultFileExtensionModelValidator;
047            }
048    
049            public static final ModelValidator<FileContentReference>
050                    getDefaultFileNameModelValidator() {
051    
052                    return _defaultFileNameModelValidator;
053            }
054    
055            public static final ModelValidator<FileContentReference>
056                    getDefaultFileSizeModelValidator() {
057    
058                    return _defaultFileSizeModelValidator;
059            }
060    
061            private static final ModelValidator<FileContentReference>
062                    _defaultFileExtensionModelValidator =
063                            new ModelValidator<FileContentReference>() {
064    
065                                    @Override
066                                    public void validate(FileContentReference fileContentReference)
067                                            throws PortalException {
068    
069                                            DLValidatorUtil.validateFileExtension(
070                                                    fileContentReference.getSourceFileName());
071    
072                                            DLValidatorUtil.validateSourceFileExtension(
073                                                    fileContentReference.getExtension(),
074                                                    fileContentReference.getSourceFileName());
075                                    }
076    
077                            };
078    
079            private static final ModelValidator<FileContentReference>
080                    _defaultFileNameModelValidator =
081                            new ModelValidator<FileContentReference>() {
082    
083                                    @Override
084                                    public void validate(FileContentReference fileContentReference)
085                                            throws PortalException {
086    
087                                            if (!Validator.isNull(
088                                                            fileContentReference.getSourceFileName())) {
089    
090                                                    DLValidatorUtil.validateFileName(
091                                                            fileContentReference.getSourceFileName());
092                                            }
093                                    }
094    
095                            };
096    
097            private static final ModelValidator<FileContentReference>
098                    _defaultFileSizeModelValidator =
099                            new ModelValidator<FileContentReference>() {
100    
101                                    @Override
102                                    public void validate(FileContentReference fileContentReference)
103                                            throws PortalException {
104    
105                                            DLValidatorUtil.validateFileSize(
106                                                    fileContentReference.getSourceFileName(),
107                                                    fileContentReference.getSize());
108                                    }
109    
110                            };
111    
112            private static class CompositeModelValidator<T>
113                    implements ModelValidator<T> {
114    
115                    public CompositeModelValidator(ModelValidator<T>... modelValidators) {
116                            _modelValidators = modelValidators;
117                    }
118    
119                    @Override
120                    public void validate(T t) throws PortalException {
121                            for (ModelValidator<T> modelValidator : _modelValidators) {
122                                    modelValidator.validate(t);
123                            }
124                    }
125    
126                    private final ModelValidator<T>[] _modelValidators;
127    
128            }
129    
130    }