KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbBodyTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbBodyTag.java,v 1.28 2004/10/26 19:46:06 hkollmann Exp $
3  * $Revision: 1.28 $
4  * $Date: 2004/10/26 19:46:06 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.taglib;
25
26 import org.dbforms.config.ResultSetVector;
27
28 import org.dbforms.util.Util;
29
30 import java.io.UnsupportedEncodingException JavaDoc;
31
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35
36
37
38 /**
39  * <p>
40  * this tag renders a Body-tag. it is supposed to be nested within a DbFormTag.
41  * because this tag is nested within a DbFormTag it is invoked every time the
42  * parent dbFormTag gets evaluated, AND it gets rendered in every
43  * evalation-loop (if there exists data to be rendered)
44  * </p>
45  *
46  * @author Joachim Peer
47  */

48 public class DbBodyTag extends DbBaseHandlerTag
49    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
50    private String JavaDoc allowNew = "true";
51
52    /**
53     * DOCUMENT ME!
54     *
55     * @param allowNew DOCUMENT ME!
56     */

57    public void setAllowNew(String JavaDoc allowNew) {
58       this.allowNew = allowNew;
59    }
60
61
62    /**
63     * DOCUMENT ME!
64     *
65     * @return DOCUMENT ME!
66     */

67    public String JavaDoc getAllowNew() {
68       return allowNew;
69    }
70
71
72    /**
73     * DOCUMENT ME!
74     *
75     * @return DOCUMENT ME!
76     *
77     * @throws JspException DOCUMENT ME!
78     */

79    public int doAfterBody() throws JspException JavaDoc {
80       DbFormTag myParent = getParentForm();
81
82       try {
83          // each rendering loop represents one row of data.
84
// for every row we need to print some data needed by the controller servlet in order to
85
// correctly dispatching and eventually modifying our data.
86
//
87
// now the key of the current dataset is printed out (always)
88
// this key will be used by actions such as delete or update.
89
if (myParent.getTable() != null) {
90             String JavaDoc curKeyString = myParent.getTable()
91                                           .getKeyPositionString(myParent
92                                                                 .getResultSetVector());
93
94             if (!Util.isNull(curKeyString)) {
95                curKeyString = Util.encode(curKeyString,
96                                           pageContext.getRequest().getCharacterEncoding());
97                myParent.appendToChildElementOutput("<input type=\"hidden\" name=\"k_"
98                                                    + myParent.getTable().getId()
99                                                    + "_"
100                                                    + myParent.getPositionPath()
101                                                    + "\" value=\""
102                                                    + curKeyString + "\"/>");
103             }
104          }
105       } catch (UnsupportedEncodingException JavaDoc uee) {
106          throw new JspException JavaDoc(uee.toString());
107       }
108       // field values that have not been rendered by html tags but that is determinated by field
109
// mapping between main- and subform are rendered now:
110
if (myParent.isSubForm() && myParent.isFooterReached() && Util.getTrue(getAllowNew())) {
111          myParent.appendToChildElementOutput(myParent.produceLinkedTags()); // print hidden-fields for missing insert-fields we can determinated
112
}
113
114
115       return SKIP_BODY;
116    }
117
118
119    /**
120     * DOCUMENT ME!
121     *
122     * @param t DOCUMENT ME!
123     *
124     * @throws Throwable DOCUMENT ME!
125     */

126    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
127       throw t;
128    }
129
130
131    /**
132     * DOCUMENT ME!
133     *
134     * @return DOCUMENT ME!
135     *
136     * @throws JspException DOCUMENT ME!
137     */

138    public int doEndTag() throws JspException JavaDoc {
139       try {
140          if (bodyContent != null) {
141             bodyContent.writeOut(bodyContent.getEnclosingWriter());
142             bodyContent.clearBody();
143
144             // workaround for duplicate rows in JRun 3.1
145
}
146       } catch (java.io.IOException JavaDoc e) {
147          throw new JspException JavaDoc("IO Error: " + e.getMessage());
148       }
149
150       return EVAL_PAGE;
151    }
152
153
154    // by default this is "true" - if so, the body is rendered at least 1 time, even if there are no data rows in the table. this enables the user to insert a new data row. - to disable this feature, allowNew has to be set to "false"
155

156    /**
157     * DOCUMENT ME!
158     */

159    public void doFinally() {
160       allowNew = "true";
161    }
162
163
164    /**
165     * DOCUMENT ME!
166     *
167     * @return DOCUMENT ME!
168     */

169    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
170       DbFormTag myParent = getParentForm();
171       ResultSetVector rsv = myParent.getResultSetVector();
172       // the body may be rendered under the following circumstances:
173
// - resultSetVector > 0 && not footer reached => render a row
174
// - resultSetVector == 0 && allowNew==true => insert a new row
175
if (
176             (!ResultSetVector.isNull(rsv) && myParent.isFooterReached())
177             ||
178             (ResultSetVector.isNull(rsv) && Util.getFalse(allowNew))
179           ) {
180             return SKIP_BODY;
181       }
182
183       myParent.updatePositionPath();
184       // to enable access from jsp we provide a variable
185
// #fixme: we need a more convenient data access structure
186
//
187
// #fixme: this is a CRAZY WEIRED ODD SQUARE WORKAROUND
188
//
189

190       if (!ResultSetVector.isNull(rsv)) {
191          Map JavaDoc dbforms = (Map JavaDoc) pageContext.getAttribute("dbforms");
192
193          if (dbforms != null) {
194             DbFormContext dbContext = (DbFormContext) dbforms.get(myParent
195                                                                   .getName());
196             if (dbContext != null) {
197                dbContext.setCurrentRow(rsv.getCurrentRowAsMap());
198                try {
199                   dbContext.setPosition(Util.encode(myParent.getTable().getPositionString(rsv),
200                                                     pageContext.getRequest().getCharacterEncoding()));
201                } catch (Exception JavaDoc e) {
202                   throw new JspException JavaDoc(e.getMessage());
203                }
204             }
205          }
206
207          if (!rsv.isLast()) {
208             rsv.moveNext(); // teleport us to future...
209

210             // This must be done because currentRow_xxx is reread from
211
// the pagecontext after(!) the body of the tag. This means
212
// that the current body contains the currentRow of rsv(i - 1)
213
// # jp 27-06-2001: replacing "." by "_", so that SCHEMATA can be used
214
pageContext.setAttribute("currentRow_"
215                                      + myParent.getTableName().replace('.', '_'),
216                                      rsv.getCurrentRowAsMap());
217
218             try {
219                pageContext.setAttribute("position_"
220                                         + myParent.getTableName().replace('.',
221                                                                           '_'),
222                                         Util.encode(myParent.getTable().getPositionString(rsv),
223                                                     pageContext.getRequest().getCharacterEncoding()));
224             } catch (Exception JavaDoc e) {
225                throw new JspException JavaDoc(e.getMessage());
226             }
227
228             rsv.movePrevious(); // ...and back to present ;=)
229
}
230       }
231
232       return EVAL_BODY_BUFFERED;
233    }
234 }
235
Popular Tags