001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.struts.forms.taglib;
017:
018: import java.util.Iterator;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.jsp.JspException;
022: import javax.servlet.jsp.tagext.TagSupport;
023:
024: import org.apache.struts.taglib.html.FormTag;
025:
026: import de.odysseus.calyxo.forms.Form;
027: import de.odysseus.calyxo.forms.FormData;
028: import de.odysseus.calyxo.forms.FormInput;
029: import de.odysseus.calyxo.forms.FormInputResult;
030: import de.odysseus.calyxo.forms.FormResult;
031: import de.odysseus.calyxo.forms.FormsSupport;
032: import de.odysseus.calyxo.struts.forms.legacy.FlushableForm;
033:
034: /**
035: * Flush form data into form properties.
036: * Use this tag inside a struts form <strong>before</strong> any
037: * input tags.
038: * The form bean associated with the action must be an instance of
039: * {@link de.odysseus.calyxo.struts.forms.legacy.FlushableForm}.
040: *
041: * @author Christoph Beck
042: */
043: public class FlushTag extends TagSupport {
044:
045: public int doEndTag() throws JspException {
046: HttpServletRequest request = (HttpServletRequest) pageContext
047: .getRequest();
048: FormsSupport support = FormsSupport.getInstance(request);
049:
050: FormTag formTag = (FormTag) findAncestorWithClass(this ,
051: FormTag.class);
052: if (formTag == null) {
053: throw new JspException("Cannot get enclosing form tag");
054: }
055: String action = formTag.getAction();
056:
057: FormData formData = null;
058: try {
059: formData = support.getFormData(request, action, false);
060: } catch (Exception e) {
061: throw new JspException("Cannot get form data for action '"
062: + action + "'!");
063: }
064: if (formData instanceof FlushableForm) {
065: FlushableForm values = (FlushableForm) formData;
066: FormResult result = support.getFormResult(request, action);
067: if (result != null) {
068: try {
069: Iterator inputs = result.getFormInputResults();
070: while (inputs.hasNext()) {
071: FormInputResult inputResult = (FormInputResult) inputs
072: .next();
073: FormInput input = inputResult.getFormInput();
074: Object value = inputResult.format(request);
075: if (input.isArray()) {
076: values._setInputs(input.getName(),
077: (String[]) value);
078: } else {
079: values._setInput(input.getName(),
080: (String) value);
081: }
082: }
083: } catch (Exception e) {
084: throw new JspException(e);
085: }
086: } else {
087: Form form = support.getForm(request, action);
088: if (form == null) {
089: throw new JspException(
090: "Cannot find form for action '" + action
091: + "'!");
092: }
093: try {
094: Iterator inputs = form.getFormInputs();
095: while (inputs.hasNext()) {
096: FormInput input = (FormInput) inputs.next();
097: Object value = input.format(request, formData);
098: if (input.isArray()) {
099: values._setInputs(input.getName(),
100: (String[]) value);
101: } else {
102: values._setInput(input.getName(),
103: (String) value);
104: }
105: }
106: } catch (Exception e) {
107: throw new JspException(e);
108: }
109: }
110: }
111: return EVAL_PAGE;
112: }
113: }
|