KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > transactions > Projects


1 package transactions;
2
3 import dinamica.*;
4
5 /**
6  * This class shows how to use the framework class TemplateEngine
7  * to produce fragments of HTML code that wil be stored as normal
8  * record column values and later printed into the template using
9  * the regular data binding facilities for recordsets. it is used to
10  * create ditable grids, in this case where a cell of the grid may
11  * contain a combobox that must be positioned on the corresponding item
12  * according to the record column's value.
13  * @author Martin Cordova (dinamica@martincordova.com)
14  */

15 public class Projects extends GenericTransaction
16 {
17
18     /**
19      * The main recordset (query.sql) contains all the required
20      * data except for the combobox, for this, a dummy column was
21      * included in the SQL query. This method will create a combobox
22      * for every record and will store the html code into the recordset
23      * "combobox" column, then it can be printed like any table.
24      */

25     public int service(Recordset inputParams) throws Throwable JavaDoc
26     {
27         
28         super.service(inputParams);
29         
30         //get reference to auto-created recordsets
31
Recordset rsData = getRecordset("query.sql");
32         Recordset rsEmployees = getRecordset("employees.sql");
33
34         //generate pre-filled combobox html code with employee data
35
TemplateEngine comboBox = new TemplateEngine( getContext(), getRequest(), getResource("combo.htm") );
36         comboBox.replace(rsEmployees,"","rows");
37         String JavaDoc template = comboBox.toString();
38         
39         //for every record generate a value for the dummy "combobox" column
40
rsData.top();
41         while (rsData.next())
42         {
43             //generate html code using the pre-filled combobox
44
//and the current record value (manager_id) used to select
45
//the corresponding combobox item
46
String JavaDoc c = getComboBox(template, rsData);
47             
48             //save it in the "artificial" column
49
rsData.setValue("combobox", c);
50         }
51         
52         return 0;
53         
54     }
55
56     /**
57      * Create a combobox with a selected item
58      * according to the value of the column "manager_id"
59      * @param template HTML template containing the base pre-fillde combobox
60      * @param rs Recordset positioned on the current record used to set the combo item position
61      * @return ComboBox html code
62      * @throws Throwable
63      */

64     String JavaDoc getComboBox(String JavaDoc template, Recordset rs) throws Throwable JavaDoc
65     {
66         TemplateEngine t = new TemplateEngine(getContext(), getRequest(), template);
67         t.setComboValue("manager_id", rs.getString("manager_id"));
68         return t.toString();
69     }
70
71 }
72
Popular Tags