KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > taglibs > PackageTag


1 /*
2  * PackageTag is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/taglibs/PackageTag.java,v 1.5.2.1 2006/12/11 16:27:38 fxrobin Exp $
21  */

22 package org.cofax.taglibs;
23
24 import javax.servlet.*;
25 import javax.servlet.jsp.*;
26 import javax.servlet.jsp.tagext.*;
27 import java.util.*;
28 import org.cofax.*;
29
30 /**
31  * Parent/Top level taglib that query database and setup result for display This
32  * PackageTag is replacing the cofax WYSIWYG html templates system.
33  *
34  * HeaderTag, FooterTag, NoRowTag, ParameterTag, and ExecuteTag are children of
35  * this tag.
36  *
37  * ValueTag, PrintIfTag, and PrintTag, are used within the children tags to get
38  * output.
39  *
40  * @author Hung Dao
41  * @author Karl Martino
42  * @author Matt Ericson
43  * @created August 01, 2001
44  *
45  *
46  * Set require params and get record from database base on action name.
47  *
48  * @param action
49  * required - lookup name in the tblpackagetags
50  * @param nameSpace
51  * option - short name for "action".
52  * @param startRow
53  * option - start at row
54  * @param numberRows
55  * option - number of row display
56  * @param addToGlossary
57  * added start row result to cofaxPage glossary bean
58  * @param showPackageTag
59  * used for debugging.
60  */

61 public class PackageTag extends TagSupport {
62
63     // init & set flag for each params
64
private String JavaDoc action;
65
66     // Overrides column key name prefix
67
private String JavaDoc nameSpace;
68
69     // For adding a row to the request's Glossary object
70
private boolean addToGlossary = false;
71
72     // Local glossary copy for proccessing
73
private Glossary myGlossary = new Glossary();
74
75     // Rows returned from the packageTag call
76
private List packageData;
77
78     // Current row that we are operating on
79
private int currentRow = 0;
80
81     // Total number of rows in the result set
82
private int totalRows = 0;
83
84     // Display variables. Start looping with startRow and loop numRow times
85
private int startRow = 1;
86
87     private int numRows = 0;
88
89     // Add additional debug data to this request's Glossary
90
private boolean showPackageTag = false;
91
92     private String JavaDoc noConnection = "Can't get DataStore Connection for this packageTag = ";
93
94     private String JavaDoc noPackageTag = "Store Procedure does not exist for this packageTag = ";
95
96     private HashMap rowData;
97
98     private boolean hasRowData;
99
100     private boolean outOfBound = false; // invalid startRow
101

102     private Iterator rows;
103
104     private int showRows = 0;
105
106     private boolean noRows = false;
107
108     ServletRequest req;
109
110     JspWriter out;
111
112     CofaxPage cofaxPage;
113
114     /**
115      * Get's the flag setting true/false returned from the packageTag call.
116      */

117     public boolean getHasRowData() {
118         return hasRowData;
119     }
120
121     /**
122      * Determines if to add packageTag debug information to the request Glossary
123      */

124     public void setShowPackageTag(String JavaDoc v) {
125         if (v.equalsIgnoreCase("true")) {
126             this.showPackageTag = true;
127         } else {
128             this.showPackageTag = false;
129         }
130     }
131
132     /**
133      * Checks to see if we are adding debug data to the request Glossary
134      */

135     public String JavaDoc getShowPackageTag() {
136         if (showPackageTag) {
137             return "true";
138         } else {
139             return "false";
140         }
141     }
142
143     /**
144      * Determines if we should add the start row to the request Glossary
145      */

146     public void setAddToGlossary(String JavaDoc v) {
147         if (v.equalsIgnoreCase("true")) {
148             this.addToGlossary = true;
149         } else {
150             this.addToGlossary = false;
151         }
152     }
153
154     /**
155      * Checks to see if we are adding the start row to the request Glossary
156      */

157     public String JavaDoc getAddToGlossary() {
158         if (addToGlossary) {
159             return "true";
160         } else {
161             return "false";
162         }
163     }
164
165     /**
166      * Set's the namespace (column prefix) for the values returned.
167      */

168     public void setNameSpace(String JavaDoc nameSpace) {
169         if (!nameSpace.equals("")) {
170             this.nameSpace = nameSpace;
171         } else {
172             this.nameSpace = action;
173         }
174     }
175
176     /**
177      * Get's the namespace (column prefix) for the values returned.
178      */

179     public String JavaDoc getNameSpace() {
180         return (nameSpace);
181     }
182
183     /**
184      * Set's the packageTag name we are executing. To be looked up in execute().
185      */

186     public void setAction(String JavaDoc action) {
187         if (!action.equals("")) {
188             this.action = action;
189         } else {
190             this.action = "";
191         }
192     }
193
194     /**
195      * Get's the packageTag name we are executing.
196      */

197     public String JavaDoc getAction() {
198         return (action);
199     }
200
201     /**
202      * Set's the row we are starting from.
203      */

204     public void setStartrow(String JavaDoc startRow) {
205         try {
206             this.startRow = Integer.parseInt(startRow);
207             if (this.startRow < 1) {
208                 this.startRow = 1;
209             }
210         } catch (NumberFormatException JavaDoc nfe) {
211             this.startRow = 1;
212         }
213     }
214
215     /**
216      * Get's the row we are starting from.
217      *
218      */

219     public int getStartRow() {
220         return (startRow);
221     }
222
223     /**
224      * Set's the number of rows we are displaying.
225      */

226     public void setNumberrows(String JavaDoc numberRows) {
227
228         try {
229             this.numRows = Integer.parseInt(numberRows);
230             if (this.numRows < 1) {
231                 this.numRows = 0; // 1;
232
}
233
234         } catch (NumberFormatException JavaDoc nfe) {
235             this.numRows = 0; // 1 ;
236
}
237     }
238
239     /**
240      * Gets the number of rows we are displaying.
241      */

242     public int getNumberRows() {
243         return (numRows);
244     }
245
246     /**
247      * Set's a glossary value in THIS packageTag's Glossary
248      */

249     public void setGlossaryValue(String JavaDoc key, String JavaDoc value) {
250         myGlossary.putString(key, value);
251     }
252
253     /**
254      * Get's a glossary value in THIS packageTag's Glossary
255      */

256     public String JavaDoc getGlossaryValue(String JavaDoc key) {
257         return myGlossary.getString(key);
258     }
259
260     /**
261      * Get's the total rows returned from the packageTag call.
262      */

263     public int getTotalRows() {
264         return totalRows;
265     }
266
267     /**
268      * Set's the total rows returned from the packageTag call.
269      */

270     public void setTotalRows(int totalRows) {
271         this.totalRows = totalRows;
272     }
273
274     /**
275      * Get's the current rows returned from the packageTag call.
276      */

277     public int getCurrentRow() {
278         return currentRow;
279     }
280
281     /**
282      * Determines if we have an additional row to move to.
283      */

284     public boolean hasNext() {
285         return rows.hasNext();
286     }
287
288     /**
289      * Get's the flag setting true/false returned from the packageTag call.
290      */

291     public boolean getNoRows() {
292         return noRows;
293     }
294
295     /**
296      * A pointer to the next row.
297      */

298     public void nextRow() {
299
300         if (outOfBound) { // || (0 == numRows)
301
hasRowData = false;
302         } else {
303
304             if (currentRow >= startRow) {
305                 showRows++; // init showRow = 0
306
}
307
308             if ((rows.hasNext()) && (showRows <= numRows)) {
309                 rowData = (HashMap) rows.next();
310                 hasRowData = true;
311                 currentRow++;
312             } else {
313                 hasRowData = false;
314             }
315         }
316     } // end nextRow
317

318     /**
319      * Actually executes the SQL operation by calling the CofaxPage's DataStore
320      * object.
321      */

322     public void execute() throws JspException {
323
324         DataStore db = null;
325         String JavaDoc tagDefinition = null;
326         try {
327
328             db = cofaxPage.getDataStore(); // db connecting
329
if (db == null) {
330                 printMessage(noConnection + nameSpace);
331             } else {
332
333                 tagDefinition = db.getPackageTag(action, myGlossary.getKeyValues(), cofaxPage.getServingPage(), cofaxPage.getServingClient());
334
335                 if (tagDefinition == null || tagDefinition.equals("")) {
336                     printMessage(noPackageTag + "action is " + action + ", nameSpace is " + nameSpace);
337
338                 } else {
339                     packageData = db.getPackageData(getNameSpace(), tagDefinition, cofaxPage.getServingPage(), cofaxPage.getServingClient());
340
341                 }
342             }
343
344         } catch (Exception JavaDoc e) {
345             cofaxPage.setErrorMsg("Connection failed in PackageTag = " + nameSpace + " /n Error = " + e.getMessage());
346         } finally {
347
348             cofaxPage.disconnectDataStore(); // db disconect
349
}
350
351         // showPackageTag debug
352
if (showPackageTag) {
353             cofaxPage.putGlossaryValue(getNameSpace() + ":packageTagDefinition", tagDefinition);
354             cofaxPage.putGlossaryValue(getNameSpace() + ":packageTagRowsReturned", packageData.size() + "");
355             cofaxPage.putGlossaryValue(getNameSpace() + ":packageTagErrorsReturned", db.getLastError());
356         }
357
358         if (packageData == null || (packageData.size() == 0)) {
359             noRows = true;
360             setTotalRows(totalRows);
361             cofaxPage.putGlossaryValue(getNameSpace() + ":TOTAL_ROWS", "0");
362         } else {
363
364             totalRows = packageData.size();
365             // add total rows to this package tag glossary
366
cofaxPage.putGlossaryValue(getNameSpace() + ":TOTAL_ROWS", packageData.size() + "");
367
368             rows = packageData.iterator();
369
370             // get number of Record need display
371
if ((totalRows - startRow) > 0) { // eliminate neg startRow
372
if (numRows == 0) {
373                     numRows = totalRows; // show all record from startRow
374
}
375             } else if ((totalRows - startRow) == 0) { // TotalRows = startRow
376
numRows = 1;
377             } else if ((totalRows - startRow) < 0) { // startRow out of bound
378
numRows = 0;
379                 outOfBound = true;
380             }
381
382             if (!outOfBound) {
383                 noRows = false;
384
385                 currentRow = 1;
386                 while ((currentRow <= startRow) && (!outOfBound)) {
387                     if (rows.hasNext()) {
388                         nextRow();
389                     }
390                 }
391
392                 // add to cofaxPage glossary bean
393
if (addToGlossary) {
394                     HashMap oneRow = new HashMap(rowData);
395                     cofaxPage.addToGlossary(oneRow);
396
397                 }
398
399             } else {
400
401                 noRows = true;
402
403             }
404
405         } // totalRows > 0
406

407     } // end execute method
408

409     /**
410      * Determines if a column exists in this row.
411      */

412     public boolean fieldExists(String JavaDoc name) {
413
414         if (packageData != null && totalRows > 0) {
415             // HashMap oneRow = (HashMap) packageData.get( currentRow );
416

417             if (rowData.containsKey(name)) {
418                 return true;
419             } else {
420                 return false;
421             }
422         } else {
423             return false;
424         }
425     }
426
427     /**
428      * Get's the current processing row
429      */

430     public HashMap getRowData() {
431         return rowData;
432     }
433
434     /**
435      * Get's a value from the row.
436      */

437     public String JavaDoc getField(String JavaDoc name) {
438
439         if (packageData != null && totalRows > 0) {
440             // HashMap oneRow = (HashMap) packageData.get( currentRow );
441

442             if (rowData.containsKey(name)) {
443                 String JavaDoc data;
444                 if (rowData.get(name) != null) {
445                     data = rowData.get(name).toString();
446                 } else {
447                     data = "null";
448                 }
449                 return data;
450             } else {
451                 return "NA";
452             }
453         } else {
454             return "NA";
455         }
456     }
457
458     /**
459      * Print messages to clients browser.
460      */

461     public void printMessage(String JavaDoc message) throws JspException {
462
463         try {
464             out.print(message);
465         } catch (Exception JavaDoc ex) {
466             throw new JspException("IO problems printMessage() " + ex);
467         }
468         return;
469     }
470
471     /**
472      * Set's the parameter's for this packageTag run.
473      */

474     public int doStartTag() throws JspException {
475
476         req = pageContext.getRequest();
477         out = pageContext.getOut();
478         cofaxPage = (CofaxPage) req.getAttribute("cofaxPage");
479
480         myGlossary.setKeyValues(new HashMap(cofaxPage.getGlossary().getKeyValues()));
481
482         if ((nameSpace == null) || (nameSpace.equals(""))) {
483             nameSpace = action;
484             setNameSpace(action);
485         }
486
487         return EVAL_BODY_INCLUDE;
488
489     } // end doStartTag
490

491     /**
492      * Ends this packageTag run.
493      */

494     public int doEndTag() {
495         // reset initial variables
496
currentRow = 0;
497         totalRows = 0;
498         startRow = 1;
499         numRows = 0;
500         showRows = 0;
501
502         rowData = null;
503         packageData = null;
504
505         return EVAL_PAGE;
506     }
507
508 } // end PackageTag
509

510
Popular Tags