001 /*
002 * Copyright 2002-2005 the original author or authors.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 */
016 package org.springframework.binding.form.support;
017
018 import org.springframework.beans.PropertyAccessException;
019 import org.springframework.binding.form.BindingErrorMessageProvider;
020 import org.springframework.binding.form.FormModel;
021 import org.springframework.binding.format.InvalidFormatException;
022 import org.springframework.binding.validation.ValidationMessage;
023 import org.springframework.binding.validation.support.DefaultValidationMessage;
024 import org.springframework.context.support.MessageSourceAccessor;
025 import org.springframework.richclient.application.ApplicationServicesLocator;
026 import org.springframework.richclient.core.Severity;
027
028 /**
029 * Default implementation of <code>BindingErrorMessageProvider</code>.
030 *
031 * @author Oliver Hutchison
032 */
033 public class DefaultBindingErrorMessageProvider implements BindingErrorMessageProvider {
034
035 private MessageSourceAccessor messageSourceAccessor = null;
036
037 public void setMessageSourceAccessor(MessageSourceAccessor messageSourceAccessor) {
038 this.messageSourceAccessor = messageSourceAccessor;
039 }
040
041 protected MessageSourceAccessor getMessageSourceAccessor() {
042 if (messageSourceAccessor == null) {
043 messageSourceAccessor = (MessageSourceAccessor)ApplicationServicesLocator.services().getService(MessageSourceAccessor.class);
044 }
045 return messageSourceAccessor;
046 }
047
048 public ValidationMessage getErrorMessage(FormModel formModel, String propertyName, Object valueBeingSet, Exception e) {
049 String messageCode = getMessageCodeForException(e);
050 Object[] args = new Object[] {formModel.getFieldFace(propertyName).getDisplayName(),
051 UserMetadata.isFieldProtected(formModel, propertyName) ? "***" : valueBeingSet};
052 String message = getMessageSourceAccessor().getMessage(messageCode, args, messageCode);
053 return new DefaultValidationMessage(propertyName, Severity.ERROR, message);
054 }
055
056 protected String getMessageCodeForException(Exception e) {
057 if (e instanceof PropertyAccessException) {
058 return ((PropertyAccessException)e).getErrorCode();
059 }
060 else if (e instanceof NullPointerException) {
061 return "required";
062 }
063 else if (e instanceof InvalidFormatException) {
064 return "typeMismatch";
065 }
066 else if (e instanceof IllegalArgumentException) {
067 return "typeMismatch";
068 }
069 else if (e.getCause() instanceof Exception) {
070 return getMessageCodeForException((Exception)e.getCause());
071 }
072 else {
073 return "unknown";
074 }
075 }
076 }