001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.web.bind;
017
018import org.kuali.rice.core.api.util.RiceKeyConstants;
019import org.kuali.rice.core.web.format.FormatException;
020import org.kuali.rice.krad.util.GlobalVariables;
021import org.springframework.beans.PropertyAccessException;
022import org.springframework.validation.BindingResult;
023import org.springframework.validation.DefaultBindingErrorProcessor;
024
025/**
026 * UIF handler for binding processing errors
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class UifBindingErrorProcessor extends DefaultBindingErrorProcessor {
031
032    /**
033     * Adds an entry to the {@link org.kuali.rice.krad.util.GlobalVariables#getMessageMap()} for the given
034     * binding processing error
035     *
036     * @param ex exception that was thrown
037     * @param bindingResult binding result containing the results of the binding process
038     */
039    @Override
040    public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) {
041        // Create field error with the exceptions's code, e.g. "typeMismatch".
042        super.processPropertyAccessException(ex, bindingResult);
043
044        Object rejectedValue = ex.getValue();
045        if (!(rejectedValue == null || rejectedValue.equals(""))) {
046            if (ex.getCause() instanceof FormatException) {
047                GlobalVariables.getMessageMap().putError(ex.getPropertyName(),
048                        ((FormatException) ex.getCause()).getErrorKey(),
049                        new String[] {rejectedValue.toString()});
050            } else {
051                GlobalVariables.getMessageMap().putError(ex.getPropertyName(), RiceKeyConstants.ERROR_CUSTOM,
052                        new String[] {"Invalid format"});
053            }
054        }
055    }
056}