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 */
022package org.granite.tide.spring;
023
024import java.util.List;
025import java.util.Locale;
026import java.util.Map;
027
028import javax.servlet.ServletContext;
029
030import org.granite.context.GraniteContext;
031import org.granite.messaging.service.ExceptionConverter;
032import org.granite.messaging.service.ServiceException;
033import org.granite.messaging.webapp.HttpGraniteContext;
034import org.springframework.context.ApplicationContext;
035import org.springframework.validation.BindingResult;
036import org.springframework.validation.Errors;
037import org.springframework.validation.FieldError;
038import org.springframework.validation.ObjectError;
039import org.springframework.web.context.support.WebApplicationContextUtils;
040
041
042public class SpringValidationExceptionConverter implements ExceptionConverter {
043    
044    public static final String VALIDATION_FAILED = "Validation.Failed";
045    
046
047    public boolean accepts(Throwable t, Throwable finalException) {
048        return t.getClass().equals(SpringValidationException.class);
049    }
050
051    public ServiceException convert(Throwable t, String detail, Map<String, Object> extendedData) {
052        Errors errors = ((SpringValidationException)t).getErrors();
053        extendedData.put("invalidValues", convertErrors(errors));
054        
055        ServiceException se = new ServiceException(VALIDATION_FAILED, t.getMessage(), detail, t);
056        se.getExtendedData().putAll(extendedData);
057        return se;
058    }
059
060    
061    public static org.granite.tide.validators.InvalidValue[] convertErrors(Errors errors) {
062        Object bean = null;
063        GraniteContext context = GraniteContext.getCurrentInstance();
064        ServletContext sc = ((HttpGraniteContext)context).getServletContext();
065        ApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(sc);
066        
067        BindingResult bindingResult = null;
068        if (errors instanceof BindingResult) {
069                bindingResult = (BindingResult)errors;
070                bean = bindingResult.getTarget();
071        }
072        
073        org.granite.tide.validators.InvalidValue[] converted = new org.granite.tide.validators.InvalidValue[errors.getErrorCount()];
074        List<? extends ObjectError> allErrors = errors.getAllErrors();
075        int i = 0;
076        for (ObjectError error : allErrors) {
077                if (error instanceof FieldError) {
078                        FieldError ferror = (FieldError)error;
079                        converted[i++] = new org.granite.tide.validators.InvalidValue(bean != null ? bean : ferror.getObjectName(), 
080                                        ferror.getField(),
081                                        ferror.getRejectedValue(),
082                                        springContext.getMessage(ferror, Locale.FRENCH));                                       
083                }
084                else {
085                        converted[i++] = new org.granite.tide.validators.InvalidValue(bean != null ? bean : error.getObjectName(), 
086                                        null,
087                                        null,
088                                        springContext.getMessage(error, Locale.FRENCH));                                        
089                }
090        }
091        return converted;
092    }
093}