01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/BindExceptionBase.java $
03: * $Id: BindExceptionBase.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl;
21:
22: import java.util.Map;
23:
24: import org.springframework.beans.BeanWrapper;
25: import org.springframework.validation.BindException;
26: import org.springframework.validation.FieldError;
27:
28: public class BindExceptionBase extends BindException {
29:
30: private BeanWrapper wrapper = null;
31:
32: public BindExceptionBase(Object target, String name) {
33: super (target, name);
34: }
35:
36: protected BeanWrapper getBeanWrapper() {
37: if (wrapper == null) {
38: if (getTarget() instanceof Map) {
39: wrapper = new MapWrapper(getTarget());
40: } else {
41: wrapper = new MixedBeanWrapper(getTarget());
42: }
43: }
44: return wrapper;
45: }
46:
47: public String[] resolveMessageCodes(String errorCode, String field) {
48: String fixedField = fixedField(field);
49: Class fieldType = this .getBeanWrapper().getPropertyType(
50: fixedField);
51: if (fieldType == null) {
52: fieldType = String.class;
53: }
54: return this .getMessageCodesResolver().resolveMessageCodes(
55: errorCode, this .getObjectName(), fixedField, fieldType);
56: }
57:
58: public void rejectValue(String field, String errorCode,
59: Object[] errorArgs, String defaultMessage) {
60: String fixedField = fixedField(field);
61: Object newVal = getBeanWrapper().getPropertyValue(fixedField);
62: if (newVal == null) {
63: newVal = "";
64: }
65: FieldError fe = new FieldError(this .getObjectName(),
66: fixedField, newVal, false, resolveMessageCodes(
67: errorCode, field), errorArgs, defaultMessage);
68: addError(fe);
69: }
70:
71: }
|