/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.form.handler;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.strecks.bind.internal.BindAnnotationReader;
import org.strecks.bind.internal.BindConvertInfo;
import org.strecks.form.AnnotatedForm;
import org.strecks.form.controller.BindingForm;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.form.controller.ValidForm;
import org.strecks.form.controller.WrappingForm;
import org.strecks.util.Assert;
import org.strecks.validator.internal.ValidationAnnotationReader;
import org.strecks.validator.internal.ValidationInfo;
/**
* Delegate which carries out form-related Strecks-specific request processing on behalf of the
* <code>ControllerRequestProcessor</code> class. Note that <code>handleBindingForm()</code> must be called before
* <code>handleValidForm()</code>
* @author Phil Zoio
*/
public class ValidateBindFormWrapper implements FormWrapper
{
private Map<Class, ValidationInfo> validationHandlerMap = new HashMap<Class, ValidationInfo>();
private Map<Class, BindConvertInfo> bindConvertMap = new HashMap<Class, BindConvertInfo>();
public ActionForm wrapForm(ActionForm form, HttpServletRequest request)
{
if (form instanceof AnnotatedForm)
{
//FIXME add check to determine whether validation should be deferred
DelegatingForm delegatingForm = new DelegatingForm(form);
return delegatingForm;
}
return form;
}
public void handleBindingForm(BindingForm form, HttpServletRequest request)
{
Assert.notNull(form);
Assert.notNull(request);
Class formClass = getFormClass(form);
BindConvertInfo bindConvertInfo = null;
synchronized (bindConvertMap)
{
bindConvertInfo = bindConvertMap.get(formClass);
if (bindConvertInfo == null)
{
BindAnnotationReader bindablesReader = new BindAnnotationReader();
Object readable = getReadableObject(form);
bindConvertInfo = bindablesReader.readBindables(readable);
bindConvertMap.put(formClass, bindConvertInfo);
}
}
form.setBindConvertInfo(bindConvertInfo);
}
public void handleValidForm(ValidForm form, HttpServletRequest request)
{
Assert.notNull(form);
Assert.notNull(request);
Class formClass = getFormClass(form);
ValidationInfo validationInfo = null;
synchronized (validationHandlerMap)
{
validationInfo = validationHandlerMap.get(formClass);
if (validationInfo == null)
{
ValidationAnnotationReader reader = new ValidationAnnotationReader();
Object readable = getReadableObject(form);
validationInfo = reader.readValidationHandlers(readable, bindConvertMap.get(formClass));
validationHandlerMap.put(formClass, validationInfo);
}
}
form.setValidationInfo(validationInfo);
}
/* ********************************** default getters and setters *********************************** */
Class getFormClass(Object form)
{
Class formClass;
if (form instanceof WrappingForm)
{
WrappingForm w = (WrappingForm) form;
formClass = w.getWrappedForm().getClass();
}
else
formClass = form.getClass();
return formClass;
}
Object getReadableObject(Object form)
{
Object formClass;
if (form instanceof WrappingForm)
{
WrappingForm w = (WrappingForm) form;
formClass = w.getWrappedForm();
}
else
formClass = form;
return formClass;
}
/* ********************************** default getters and setters *********************************** */
public Map<Class, ValidationInfo> getValidationHandlerMap()
{
return validationHandlerMap;
}
public Map<Class, BindConvertInfo> getBindConvertMap()
{
return bindConvertMap;
}
}