001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.form.handler;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import org.apache.struts.action.ActionForm;
023: import org.strecks.bind.internal.BindAnnotationReader;
024: import org.strecks.bind.internal.BindConvertInfo;
025: import org.strecks.form.AnnotatedForm;
026: import org.strecks.form.controller.BindingForm;
027: import org.strecks.form.controller.DelegatingForm;
028: import org.strecks.form.controller.ValidForm;
029: import org.strecks.form.controller.WrappingForm;
030: import org.strecks.util.Assert;
031: import org.strecks.validator.internal.ValidationAnnotationReader;
032: import org.strecks.validator.internal.ValidationInfo;
033:
034: /**
035: * Delegate which carries out form-related Strecks-specific request processing on behalf of the
036: * <code>ControllerRequestProcessor</code> class. Note that <code>handleBindingForm()</code> must be called before
037: * <code>handleValidForm()</code>
038: * @author Phil Zoio
039: */
040: public class ValidateBindFormWrapper implements FormWrapper {
041:
042: private Map<Class, ValidationInfo> validationHandlerMap = new HashMap<Class, ValidationInfo>();
043:
044: private Map<Class, BindConvertInfo> bindConvertMap = new HashMap<Class, BindConvertInfo>();
045:
046: public ActionForm wrapForm(ActionForm form,
047: HttpServletRequest request) {
048: if (form instanceof AnnotatedForm) {
049: //FIXME add check to determine whether validation should be deferred
050:
051: DelegatingForm delegatingForm = new DelegatingForm(form);
052: return delegatingForm;
053: }
054: return form;
055: }
056:
057: public void handleBindingForm(BindingForm form,
058: HttpServletRequest request) {
059:
060: Assert.notNull(form);
061: Assert.notNull(request);
062:
063: Class formClass = getFormClass(form);
064:
065: BindConvertInfo bindConvertInfo = null;
066: synchronized (bindConvertMap) {
067: bindConvertInfo = bindConvertMap.get(formClass);
068: if (bindConvertInfo == null) {
069: BindAnnotationReader bindablesReader = new BindAnnotationReader();
070: Object readable = getReadableObject(form);
071: bindConvertInfo = bindablesReader
072: .readBindables(readable);
073:
074: bindConvertMap.put(formClass, bindConvertInfo);
075: }
076: }
077:
078: form.setBindConvertInfo(bindConvertInfo);
079: }
080:
081: public void handleValidForm(ValidForm form,
082: HttpServletRequest request) {
083: Assert.notNull(form);
084: Assert.notNull(request);
085:
086: Class formClass = getFormClass(form);
087:
088: ValidationInfo validationInfo = null;
089:
090: synchronized (validationHandlerMap) {
091: validationInfo = validationHandlerMap.get(formClass);
092: if (validationInfo == null) {
093: ValidationAnnotationReader reader = new ValidationAnnotationReader();
094: Object readable = getReadableObject(form);
095: validationInfo = reader.readValidationHandlers(
096: readable, bindConvertMap.get(formClass));
097: validationHandlerMap.put(formClass, validationInfo);
098: }
099: }
100:
101: form.setValidationInfo(validationInfo);
102: }
103:
104: /* ********************************** default getters and setters *********************************** */
105:
106: Class getFormClass(Object form) {
107: Class formClass;
108: if (form instanceof WrappingForm) {
109: WrappingForm w = (WrappingForm) form;
110: formClass = w.getWrappedForm().getClass();
111: } else
112: formClass = form.getClass();
113: return formClass;
114: }
115:
116: Object getReadableObject(Object form) {
117: Object formClass;
118: if (form instanceof WrappingForm) {
119: WrappingForm w = (WrappingForm) form;
120: formClass = w.getWrappedForm();
121: } else
122: formClass = form;
123: return formClass;
124: }
125:
126: /* ********************************** default getters and setters *********************************** */
127:
128: public Map<Class, ValidationInfo> getValidationHandlerMap() {
129: return validationHandlerMap;
130: }
131:
132: public Map<Class, BindConvertInfo> getBindConvertMap() {
133: return bindConvertMap;
134: }
135:
136: }
|