/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* 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 de.odysseus.calyxo.forms.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.VariableResolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
import de.odysseus.calyxo.forms.Form;
import de.odysseus.calyxo.forms.FormInput;
import de.odysseus.calyxo.forms.FormInputValues;
import de.odysseus.calyxo.forms.FormResult;
import de.odysseus.calyxo.forms.conf.AssertConfig;
import de.odysseus.calyxo.forms.conf.FieldConfig;
import de.odysseus.calyxo.forms.conf.FormConfig;
import de.odysseus.calyxo.forms.conf.InputConfig;
/**
* Form implementation.
*
* @author Christoph Beck
*/
public class FormImpl implements Form {
private static final Log log = LogFactory.getLog(FormImpl.class);
private final FormConfig config;
private final Locale locale;
private final HashMap inputs;
private final ArrayList asserts;
/**
* Constructor.
*/
public FormImpl(Factory factory, FormConfig config, Locale locale) {
super();
this.config = config;
this.locale = locale;
this.inputs = new HashMap();
Iterator fieldConfigs = config.getFieldConfigs();
if (fieldConfigs.hasNext()) {
while (fieldConfigs.hasNext()) {
FieldConfig fieldConfig = (FieldConfig)fieldConfigs.next();
inputs.put(fieldConfig.getName(), new Field(factory, fieldConfig, locale));
}
}
Iterator inputConfigs = config.getInputConfigs();
if (inputConfigs.hasNext()) {
while (inputConfigs.hasNext()) {
InputConfig inputConfig = (InputConfig)inputConfigs.next();
inputs.put(inputConfig.getName(), new Input(factory, inputConfig, locale));
}
}
Iterator assertConfigs = config.getAssertConfigs();
if (assertConfigs.hasNext()) {
asserts = new ArrayList();
while (assertConfigs.hasNext()) {
AssertConfig assertConfig = (AssertConfig)assertConfigs.next();
asserts.add(new Assert(assertConfig));
}
} else {
asserts = null;
}
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Form#getFormConfig()
*/
public FormConfig getFormConfig() {
return config;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Form#getFormInputs()
*/
public Iterator getFormInputs() {
return inputs.values().iterator();
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Form#getFormInput(java.lang.String)
*/
public FormInput getFormInput(String name) {
return (FormInput)inputs.get(name);
}
/**
* Validate form inputs
*
* @param request the request we process
* @param params input parameters
*/
public FormResult validate(HttpServletRequest request, FormInputValues params) {
log.trace("validate()");
FormResultImpl result = new FormResultImpl(this);
if (this.inputs != null) {
VariableResolver resolver = new CalyxoVariableResolver(request);
Iterator inputs = getFormInputs();
while (inputs.hasNext()) {
FormInput input = (FormInput)inputs.next();
if (input.isIgnored(resolver)) {
result.add(new IgnoredFormInputResult(input, params));
} else {
result.add(input.validate(request, params, resolver));
}
}
}
if (asserts != null) {
AssertVariableResolver resolver =
new AssertVariableResolver(request, params, result);
Iterator asserts = this.asserts.iterator();
while (asserts.hasNext()) {
Assert azzert = (Assert)asserts.next();
resolver.reset();
boolean asserted = azzert.eval(resolver);
if (!asserted && !resolver.isInvalidPropertyReferenced()) {
result.assertionFailed(resolver, azzert.getMessage());
}
}
}
return result;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.Form#getLocale()
*/
public Locale getLocale() {
return locale;
}
public String toString() {
return getClass().getName() + " [formName '" + config.getName() + "', locale '" + locale + "']";
}
}