001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.hibernate;
022    
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    import org.granite.tide.validators.EntityValidator;
026    import org.hibernate.validator.ClassValidator;
027    import org.hibernate.validator.InvalidValue;
028    
029    
030    public class HibernateValidator implements EntityValidator {
031        
032        private ConcurrentHashMap<Class<?>, ClassValidator<?>> validators = new ConcurrentHashMap<Class<?>, ClassValidator<?>>(20);
033        
034        
035        public HibernateValidator() {
036        }
037        
038        
039        @SuppressWarnings({ "unchecked", "rawtypes" })
040        public org.granite.tide.validators.InvalidValue[] getPotentialInvalidValues(Class<?> entityClass, String propertyName, Object value) {
041            ClassValidator<?> validator = validators.get(entityClass);
042            if (validator == null) {
043                validator = new ClassValidator(entityClass);
044                ClassValidator<?> tmpValidator = validators.putIfAbsent(entityClass, validator); 
045                if (tmpValidator != null) 
046                    validator = tmpValidator; 
047            }
048            
049            org.hibernate.validator.InvalidValue[] invalidValues = validator.getPotentialInvalidValues(propertyName, value);
050            return convertInvalidValues(invalidValues);
051        }
052    
053    
054        public static org.granite.tide.validators.InvalidValue[] convertInvalidValues(InvalidValue[] values) {
055            org.granite.tide.validators.InvalidValue[] converted = new org.granite.tide.validators.InvalidValue[values.length];
056            for (int i = 0; i < values.length; i++) {
057                InvalidValue value = values[i];
058                if (value.getBean() == null) {
059                    converted[i] = new org.granite.tide.validators.InvalidValue(
060                        value.getBeanClass(),
061                        value.getPropertyPath(),
062                        value.getValue(),
063                        value.getMessage()
064                    );
065                }
066                else {
067                    converted[i] = new org.granite.tide.validators.InvalidValue(
068                        value.getRootBean() != null ? value.getRootBean() : value.getBean(),
069                        value.getPropertyPath(),
070                        value.getValue(),
071                        value.getMessage()
072                    );
073                }
074            }
075            return converted;
076        }
077    }