001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.tide.validation;
023    
024    import java.util.Set;
025    
026    import javax.naming.InitialContext;
027    import javax.naming.NamingException;
028    import javax.validation.ConstraintViolation;
029    import javax.validation.Validation;
030    import javax.validation.Validator;
031    import javax.validation.ValidatorFactory;
032    
033    import org.granite.logging.Logger;
034    import org.granite.tide.validators.EntityValidator;
035    import org.granite.tide.validators.InvalidValue;
036    
037    /**
038     * @author William DRAI
039     */
040    public class BeanValidation implements EntityValidator {
041            
042            private static final Logger log = Logger.getLogger(BeanValidation.class);
043        
044        private ValidatorFactory validatorFactory = null;
045        
046        
047        public BeanValidation() {
048            try {
049                    InitialContext ic = new InitialContext();
050                    this.validatorFactory = (ValidatorFactory)ic.lookup("java:comp/ValidatorFactory");
051            }
052            catch (NamingException e) {
053                    log.info("ValidatorFactory not found in JNDI, build default");
054                    this.validatorFactory = Validation.buildDefaultValidatorFactory();
055            }
056        }
057        
058        public BeanValidation(ValidatorFactory validatorFactory) {
059            this.validatorFactory = validatorFactory;
060        }
061        
062        
063        @SuppressWarnings("unchecked")
064        public InvalidValue[] getPotentialInvalidValues(Class<?> entityClass, String propertyName, Object value) {
065            Validator validator = validatorFactory.getValidator();
066            
067            Set<?> constraintViolations = validator.validateValue(entityClass, propertyName, value);
068            return convertConstraintViolations((Set<ConstraintViolation<?>>)constraintViolations);
069        }
070    
071    
072        public static InvalidValue[] convertConstraintViolations(Set<ConstraintViolation<?>> constraintViolations) {
073            InvalidValue[] converted = new org.granite.tide.validators.InvalidValue[constraintViolations.size()];
074            int i = 0;
075            for (ConstraintViolation<?> cv : constraintViolations) {
076                if (cv.getRootBean() == null) {
077                    converted[i++] = new InvalidValue(
078                        cv.getRootBeanClass(),
079                        cv.getPropertyPath().toString(),
080                        cv.getInvalidValue(),
081                        cv.getMessage()
082                    );
083                }
084                else {
085                    converted[i++] = new InvalidValue(
086                        cv.getRootBean(),
087                        cv.getLeafBean() != null ? cv.getLeafBean() : cv.getRootBean(),
088                        cv.getPropertyPath().toString(),
089                        cv.getInvalidValue(),
090                        cv.getMessage()
091                    );
092                }
093            }
094            return converted;
095        }
096    }