/*
* 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.struts.forms.taglib;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.taglib.html.FormTag;
import de.odysseus.calyxo.forms.Form;
import de.odysseus.calyxo.forms.FormData;
import de.odysseus.calyxo.forms.FormInput;
import de.odysseus.calyxo.forms.FormInputResult;
import de.odysseus.calyxo.forms.FormResult;
import de.odysseus.calyxo.forms.FormsSupport;
import de.odysseus.calyxo.struts.forms.legacy.FlushableForm;
/**
* Flush form data into form properties.
* Use this tag inside a struts form <strong>before</strong> any
* input tags.
* The form bean associated with the action must be an instance of
* {@link de.odysseus.calyxo.struts.forms.legacy.FlushableForm}.
*
* @author Christoph Beck
*/
public class FlushTag extends TagSupport {
public int doEndTag() throws JspException {
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
FormsSupport support = FormsSupport.getInstance(request);
FormTag formTag = (FormTag)findAncestorWithClass(this, FormTag.class);
if (formTag == null) {
throw new JspException("Cannot get enclosing form tag");
}
String action = formTag.getAction();
FormData formData = null;
try {
formData = support.getFormData(request, action, false);
} catch (Exception e) {
throw new JspException("Cannot get form data for action '" + action + "'!");
}
if (formData instanceof FlushableForm) {
FlushableForm values = (FlushableForm)formData;
FormResult result = support.getFormResult(request, action);
if (result != null) {
try {
Iterator inputs = result.getFormInputResults();
while (inputs.hasNext()) {
FormInputResult inputResult = (FormInputResult)inputs.next();
FormInput input = inputResult.getFormInput();
Object value = inputResult.format(request);
if (input.isArray()) {
values._setInputs(input.getName(), (String[])value);
} else {
values._setInput(input.getName(), (String)value);
}
}
} catch (Exception e) {
throw new JspException(e);
}
} else {
Form form = support.getForm(request, action);
if (form == null) {
throw new JspException("Cannot find form for action '" + action + "'!");
}
try {
Iterator inputs = form.getFormInputs();
while (inputs.hasNext()) {
FormInput input = (FormInput)inputs.next();
Object value = input.format(request, formData);
if (input.isArray()) {
values._setInputs(input.getName(), (String[])value);
} else {
values._setInput(input.getName(), (String)value);
}
}
} catch (Exception e) {
throw new JspException(e);
}
}
}
return EVAL_PAGE;
}
}