KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbLinkURLTag.java,v 1.30 2004/08/18 12:26:07 hkollmann Exp $
3  * $Revision: 1.30 $
4  * $Date: 2004/08/18 12:26:07 $
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.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.Field;
30 import org.dbforms.config.FieldValue;
31 import org.dbforms.config.FieldValues;
32 import org.dbforms.config.Table;
33
34 import org.dbforms.util.Util;
35
36 import java.io.UnsupportedEncodingException JavaDoc;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41
42
43
44 /**
45  * <p>
46  * the 3 examples below produce all the same result
47  * </p>
48  *
49  * <p>
50  * &lt;linkURL HREF="customer.jsp" table="customer" position="1:2:12-3:4:1992"
51  * /&gt;
52  * </p>
53  *
54  * <p>
55  * &lt;linkURL HREF="customer.jsp" table="customer" position="&lt;%= currentKey
56  * %&gt;" /&gt;
57  * </p>
58  *
59  * <p>
60  * &lt;linkURL HREF="customer.jsp" table="customer" /&gt; &lt;position
61  * fieldName="id" value="103" /&gt; &lt;position fieldName="cust_lang"
62  * value="2" /&gt; &lt;/linkURL&gt;
63  * </p>
64  *
65  * <p>
66  * result (off course without the line feeds)
67  * </p>
68  * <pre>/servlet/control?
69  * ac_goto_x=t&
70  * data_ac_goto_x_fu=/customer.jsp&
71  * data_ac_goto_x_destTable=17&
72  * data_ac_goto_x_destPos=103~2</pre>
73  *
74  * <p>
75  * Use it like this:
76  * </p>
77  * <pre><a HREF="&lt;linkURL HREF="customer.jsp" tableName="customer" position="103~2" /&gt;"> some text </a></pre>
78  *
79  * @author Joachim Peer
80  */

81 public class DbLinkURLTag extends DbBaseHandlerTag
82    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
83    private static Log logCat = LogFactory.getLog(DbLinkURLTag.class
84                                                             .getName()); // logging category for this class
85
private transient FieldValues positionFv; // fields and their values, provided by embedded DbLinkPositionItem-Elements
86

87    /**
88     * used if parentTable is different to tableName: field(s) in this forme
89     * that is/are linked to the parent form
90     */

91    private String JavaDoc childField;
92
93    // -- properties
94
private String JavaDoc href;
95    private String JavaDoc keyToDestPos;
96    private String JavaDoc keyToKeyToDestPos;
97
98    /**
99     * used if parentTable is different to tableName: field(s) in the main form
100     * that is/are linked to this form
101     */

102    private String JavaDoc parentField;
103    private String JavaDoc position;
104    private String JavaDoc singleRow = "false";
105    private String JavaDoc tableName;
106
107    /**
108     * Sets the childField.
109     *
110     * @param childField The childField to set
111     */

112    public void setChildField(String JavaDoc childField) {
113       this.childField = childField;
114    }
115
116
117    /**
118     * Returns the childField.
119     *
120     * @return String
121     */

122    public String JavaDoc getChildField() {
123       return childField;
124    }
125
126
127    /**
128     * DOCUMENT ME!
129     *
130     * @param position DOCUMENT ME!
131     */

132    public void setDestPos(String JavaDoc position) {
133       this.position = position;
134    }
135
136
137    /**
138     * DOCUMENT ME!
139     *
140     * @return DOCUMENT ME!
141     */

142    public String JavaDoc getDestPos() {
143       return position;
144    }
145
146
147    /**
148     * DOCUMENT ME!
149     *
150     * @param href DOCUMENT ME!
151     */

152    public void setHref(String JavaDoc href) {
153       this.href = href;
154    }
155
156
157    /**
158     * DOCUMENT ME!
159     *
160     * @return DOCUMENT ME!
161     */

162    public String JavaDoc getHref() {
163       return href;
164    }
165
166
167    /**
168     * Sets the keyToDestPos.
169     *
170     * @param keyToDestPos The keyToDestPos to set
171     */

172    public void setKeyToDestPos(String JavaDoc keyToDestPos) {
173       this.keyToDestPos = keyToDestPos;
174    }
175
176
177    /**
178     * Returns the keyToDestPos.
179     *
180     * @return String
181     */

182    public String JavaDoc getKeyToDestPos() {
183       return keyToDestPos;
184    }
185
186
187    /**
188     * Sets the keyToKeyToDestPos.
189     *
190     * @param keyToKeyToDestPos The keyToKeyToDestPos to set
191     */

192    public void setKeyToKeyToDestPos(String JavaDoc keyToKeyToDestPos) {
193       this.keyToKeyToDestPos = keyToKeyToDestPos;
194    }
195
196
197    /**
198     * Returns the keyToKeyToDestPos.
199     *
200     * @return String
201     */

202    public String JavaDoc getKeyToKeyToDestPos() {
203       return keyToKeyToDestPos;
204    }
205
206
207    /**
208     * Sets the parentField.
209     *
210     * @param parentField The parentField to set
211     */

212    public void setParentField(String JavaDoc parentField) {
213       this.parentField = parentField;
214    }
215
216
217    /**
218     * Returns the parentField.
219     *
220     * @return String
221     */

222    public String JavaDoc getParentField() {
223       return parentField;
224    }
225
226
227    /**
228     * DOCUMENT ME!
229     *
230     * @param position DOCUMENT ME!
231     */

232    public void setPosition(String JavaDoc position) {
233       this.position = position;
234    }
235
236
237    /**
238     * DOCUMENT ME!
239     *
240     * @return DOCUMENT ME!
241     */

242    public String JavaDoc getPosition() {
243       return position;
244    }
245
246
247    /**
248     * DOCUMENT ME!
249     *
250     * @param string
251     */

252    public void setSingleRow(String JavaDoc string) {
253       singleRow = string;
254    }
255
256
257    /**
258     * DOCUMENT ME!
259     *
260     * @return the attribute
261     */

262    public String JavaDoc getSingleRow() {
263       return singleRow;
264    }
265
266
267    /**
268     * DOCUMENT ME!
269     *
270     * @return DOCUMENT ME!
271     */

272    public Table getTable() {
273       if (!Util.isNull(tableName)) {
274          return getConfig()
275                    .getTableByName(tableName);
276       } else if (getParentForm() != null) { // we must try if we get info from parentForm
277

278          return getParentForm()
279                    .getTable();
280       } else {
281          throw new IllegalArgumentException JavaDoc("no table specified. either you define expliclty the attribute \"tableName\" or you put this tag inside a db:form!");
282       }
283    }
284
285
286    /**
287     * DOCUMENT ME!
288     *
289     * @param tableName DOCUMENT ME!
290     */

291    public void setTableName(String JavaDoc tableName) {
292       this.tableName = tableName;
293    }
294
295
296    /**
297     * to be called by DbLinkPositonItems
298     *
299     * @param field DOCUMENT ME!
300     * @param value DOCUMENT ME!
301     */

302    public void addPositionPart(Field field,
303                                String JavaDoc value) {
304       if (positionFv == null) {
305          positionFv = new FieldValues();
306       }
307
308       // 2003-03-29 HKK: Change from Hashtable to FieldValueTable
309
FieldValue fv = new FieldValue(field, value);
310       positionFv.put(fv);
311    }
312
313
314    /**
315     * DOCUMENT ME!
316     *
317     * @return DOCUMENT ME!
318     *
319     * @throws JspException thrown when error occurs in processing the body of
320     * this method
321     */

322    public int doBodyEndTag() throws javax.servlet.jsp.JspException JavaDoc {
323       return SKIP_BODY;
324    }
325
326
327    /**
328     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
329     */

330    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
331       throw t;
332    }
333
334
335    /**
336     * DOCUMENT ME!
337     *
338     * @return DOCUMENT ME!
339     *
340     * @throws JspException thrown when error occurs in processing the body of
341     * this method
342     */

343    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
344       try {
345          HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext
346                                         .getResponse();
347
348          String JavaDoc s = makeUrl();
349          s = response.encodeURL(s);
350          pageContext.getOut()
351                     .write(s);
352       } catch (java.io.IOException JavaDoc ioe) {
353          throw new JspException JavaDoc("IO Error: " + ioe.getMessage());
354       } catch (Exception JavaDoc e) {
355          throw new JspException JavaDoc("Error: " + e.getMessage());
356       }
357
358       return EVAL_PAGE;
359    }
360
361
362    /**
363     * DOCUMENT ME!
364     */

365    public void doFinally() {
366       logCat.info("doFinally called");
367       position = null;
368
369       if (positionFv != null) {
370          positionFv.clear();
371       }
372
373       positionFv = null;
374       href = null;
375       tableName = null;
376       position = null;
377       keyToDestPos = null;
378       keyToKeyToDestPos = null;
379       singleRow = "false";
380
381       super.doFinally();
382    }
383
384
385    /**
386     * DOCUMENT ME!
387     *
388     * @return DOCUMENT ME!
389     *
390     * @throws JspException thrown when error occurs in processing the body of
391     * this method
392     * @throws IllegalArgumentException thrown when some parameters are missing.
393     */

394    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
395       if (Util.isNull(getPosition())) { // if position was not set explicitly,
396

397          return EVAL_BODY_BUFFERED; // we have to evaluate body and hopefully find DbLinkPositionItems there
398
} else {
399          return SKIP_BODY; // if position was provided we don't need to look into body
400
}
401    }
402
403
404    /**
405     * DOCUMENT ME!
406     *
407     * @return DOCUMENT ME!
408     *
409     * @throws UnsupportedEncodingException DOCUMENT ME!
410     */

411    protected String JavaDoc makeUrl() throws UnsupportedEncodingException JavaDoc {
412       // determinate position inside table (key)
413
if (this.position == null) { // not explic. def. by attribute
414

415          if (positionFv != null) { // but (maybe) defined by sub-elements (DbLinkPositionItem)
416
position = getTable()
417                           .getKeyPositionString(positionFv);
418          }
419       }
420
421       // build tag
422
StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc(200);
423       HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext
424                                        .getRequest();
425       String JavaDoc contextPath = request.getContextPath();
426       tagBuf.append(contextPath);
427
428       // 2002-01-17 Fix contributed by Dirk Kraemer and Bertram Gong//
429
if (!contextPath.endsWith("/")) {
430          tagBuf.append("/");
431       }
432
433       tagBuf.append("servlet/control?");
434
435       String JavaDoc tagName = "ac_goto";
436       tagBuf.append(getDataTag(tagName, "x", "t"));
437
438       tagName = "data" + tagName + "_x";
439       tagBuf.append(getDataTag(tagName, "fu", href));
440
441       // table is required. we force to define a valid table.
442
// because we do not want the developer to use this tag instead of
443
// normal <a HREF="">-tags to arbitrary (static) ressources, as this would slow down the application.
444
tagBuf.append(getDataTag(tagName, "destTable", getTable().getName()));
445
446       // position within table is not required.
447
// if no position was provided/determinated, dbForm will navigate to the first row
448
// 2002-11-20 HKK: Fixed encoding bug!
449
tagBuf.append(getDataTag(tagName, "destPos",
450                                Util.encode(position,
451                                            pageContext.getRequest().getCharacterEncoding())));
452
453       // 2002-11-21 HKK: Allow same keys as in dbgotobutton
454
tagBuf.append(getDataTag(tagName, "keyToDestPos",
455                                Util.encode(keyToDestPos,
456                                            pageContext.getRequest().getCharacterEncoding())));
457       tagBuf.append(getDataTag(tagName, "keyToKeyDestPos",
458                                Util.encode(keyToKeyToDestPos,
459                                            pageContext.getRequest().getCharacterEncoding())));
460
461       // 2002-11-21 HKK: New: send parent table name as parameter if it is different to table
462
if (getTable() != getParentForm()
463                                  .getTable()) {
464          tagBuf.append(getDataTag(tagName, "srcTable",
465                                   getParentForm().getTable().getName()));
466          tagBuf.append(getDataTag(tagName, "childField",
467                                   Util.encode(childField,
468                                               pageContext.getRequest().getCharacterEncoding())));
469          tagBuf.append(getDataTag(tagName, "parentField",
470                                   Util.encode(parentField,
471                                               pageContext.getRequest().getCharacterEncoding())));
472       }
473
474       tagBuf.append(getDataTag(tagName, "singleRow", getSingleRow()));
475
476       String JavaDoc s = tagBuf.toString();
477       s = s.substring(0, s.length() - 1);
478
479       return s;
480    }
481
482
483    private String JavaDoc getDataTag(String JavaDoc primaryTagName,
484                              String JavaDoc dataKey,
485                              String JavaDoc dataValue) {
486       String JavaDoc s = "";
487
488       if (!Util.isNull(dataValue)) {
489          StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
490          tagBuf.append(primaryTagName);
491          tagBuf.append("_");
492          tagBuf.append(dataKey);
493          tagBuf.append("=");
494          tagBuf.append(dataValue);
495          tagBuf.append("&");
496          s = tagBuf.toString();
497       }
498
499       return s;
500    }
501 }
502
Popular Tags