/*
* 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.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.VariableResolver;
import de.odysseus.calyxo.base.Message;
import de.odysseus.calyxo.forms.FormData;
import de.odysseus.calyxo.forms.FormInput;
import de.odysseus.calyxo.forms.FormInputResult;
/**
* Input validation result.
*
* @author Christoph Beck
*/
public class InputResult implements FormInputResult {
private Input input;
private InputFieldResult selected;
private boolean relaxed;
/**
* Constructor
*/
public InputResult(Input input, InputFieldResult selected, VariableResolver resolver) {
super();
this.input = input;
this.selected = selected;
this.relaxed = selected.isValid() ? false : input.isRelaxed(resolver);
}
public int getSize() {
return selected.getSize();
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#format(javax.servlet.http.HttpServletRequest)
*/
public Object format(HttpServletRequest request) {
return selected.format(request);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#format(javax.servlet.http.HttpServletRequest, int)
*/
public String format(HttpServletRequest request, int index) {
return selected.format(request, index);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#populate(de.odysseus.calyxo.forms.FormData)
*/
public void populate(FormData bean) throws Exception {
if (!isValid()) {
return;
}
Iterator iter = input.getFields();
while (iter.hasNext()) {
InputField field = (InputField)iter.next();
String property = field.getFieldConfig().getProperty();
if (property.equals(selected.getProperty())) {
selected.populate(bean);
} else if (input.isArray()) {
bean._setProperty(property, null);
} else {
bean._setProperty(property, field.getFieldConfig().getNull());
}
}
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormResultValue#getInput()
*/
public FormInput getFormInput() {
return input;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormResultValue#getProperty()
*/
public String getProperty() {
return selected.isValid() ? selected.getProperty() : null;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormResultValue#getValue()
*/
public Object getValue() {
return selected.getValue();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#getValue(int)
*/
public Object getValue(int index) {
return selected.getValue(index);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#getInputValue()
*/
public Object getInputValue() {
return selected.getInputValue();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#getInputValue(int)
*/
public String getInputValue(int index) {
return selected.getInputValue(index);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormResultValue#isValid()
*/
public boolean isValid() {
return selected.isValid();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#isValid(int)
*/
public boolean isValid(int index) {
return selected.isValid(index);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormResultValue#getMessage()
*/
public Message getMessage(int index) {
if (!isValid(index)) {
Message message = input.getMessage();
if (message == null) {
message = selected.getMessage(index);
}
return message;
}
return null;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#getFirstMessage()
*/
public Message getFirstMessage() {
Message message = input.getMessage();
if (message != null) {
return message;
}
return selected.getFirstMessage();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#getMessages()
*/
public Iterator getLastMessages() {
return selected.getLastMessages();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#isIgnored()
*/
public boolean isIgnored() {
return false;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.forms.FormInputResult#isRelaxed()
*/
public boolean isRelaxed() {
return relaxed;
}
}