KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > codewiz > FormGenerator


1 package codewiz;
2
3 import java.sql.*;
4 import dinamica.*;
5
6 /**
7  * Generates html form and record xml given a table metadata
8  *
9  * <br>
10  * Creation date: 12/10/2003<br>
11  * Last Update: 12/10/2003<br>
12  * (c) 2003 Martin Cordova<br>
13  * This code is released under the LGPL license<br>
14  * @author Martin Cordova
15  * */

16 public class FormGenerator extends GenericTransaction
17 {
18
19     /* (non-Javadoc)
20      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
21      */

22     public int service(Recordset inputs) throws Throwable JavaDoc
23     {
24         
25         int rc = super.service(inputs);
26         
27         DatabaseMetaData md = getConnection().getMetaData();
28         ResultSet cols = md.getColumns(null, (String JavaDoc)inputs.getValue("schema"), (String JavaDoc)inputs.getValue("table"), "%");
29         Recordset rs = new Recordset(cols);
30         cols.close();
31
32         Recordset data = new Recordset();
33         data.append("field", java.sql.Types.VARCHAR);
34         data.append("maxlength", java.sql.Types.VARCHAR);
35         data.append("type", java.sql.Types.VARCHAR);
36         data.append("nativetype", java.sql.Types.VARCHAR);
37         data.append("required", java.sql.Types.VARCHAR);
38         
39         rs.top();
40         while (rs.next())
41         {
42             data.addNew();
43
44             data.setValue("field", String.valueOf(rs.getValue("column_name")).toLowerCase());
45             String JavaDoc decimals = rs.getString("decimal_digits");
46             String JavaDoc type = String.valueOf(rs.getValue("data_type"));
47             String JavaDoc combo = getDataTypeCombo(Integer.parseInt(type), decimals);
48             data.setValue("type", combo);
49
50             String JavaDoc maxlength = String.valueOf(rs.getValue("column_size"));
51             String JavaDoc typeName = (String JavaDoc)rs.getValue("type_name");
52             typeName = typeName.toUpperCase();
53             data.setValue("nativetype", typeName + "(" + maxlength + ")" );
54             if (typeName.indexOf("CHAR")<0)
55                 maxlength = "10";
56             data.setValue("maxlength", maxlength);
57
58             String JavaDoc required = (String JavaDoc)rs.getValue("is_nullable");
59             if (required!=null && required.equals("NO"))
60                 required = "<select name='required'><option selected value='true'>Y</option><option value='false'>N</option></select>";
61                 //required = "<input name='required' type='checkbox' checked value='true'";
62
else
63                 required = "<select name='required'><option value='true'>Y</option><option selected value='false'>N</option></select>";
64                 //required = "<input name='required' type='checkbox' value='true'";
65
data.setValue("required", required);
66
67         }
68
69         publish("cols", data);
70                 
71         return rc;
72         
73     }
74
75     /**
76      * Generates a combobox with the datatypes
77      * accepted by the framework validator.xml file,
78      * with the best match datatype selected for the "type" argument
79      * @param type Column data type (java.sql.Type)
80      * @return HTML code for combobox
81      * @throws Throwable
82      */

83     String JavaDoc getDataTypeCombo(int type, String JavaDoc decimals) throws Throwable JavaDoc
84     {
85
86         String JavaDoc sqlTypeName="varchar";
87         
88         switch (type)
89         {
90             case Types.INTEGER:
91             case Types.TINYINT:
92             case Types.BIGINT:
93             case Types.SMALLINT:
94                 sqlTypeName = "integer";
95                 break;
96                 
97             case Types.LONGVARCHAR:
98             case Types.VARCHAR:
99             case Types.CHAR:
100             case Types.CLOB:
101                 sqlTypeName = "varchar";
102                 break;
103
104             case Types.DATE:
105             case Types.TIMESTAMP:
106                 sqlTypeName = "date";
107                 break;
108
109             case Types.REAL:
110             case Types.FLOAT:
111             case Types.DOUBLE:
112             case Types.NUMERIC:
113             case Types.DECIMAL:
114                 if (decimals!=null && decimals.equals("0"))
115                     sqlTypeName = "integer";
116                 else
117                     sqlTypeName = "double";
118                 break;
119
120         }
121
122         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(250);
123         String JavaDoc[] cbo = {"varchar", "integer","double","date"};
124         buf.append("<select name='type' size='1'>");
125         for (int i=0;i<cbo.length;i++)
126         {
127             if (cbo[i].equals(sqlTypeName))
128                 buf.append( "<option selected value='" + cbo[i] + "'>" + cbo[i] + "</option>" );
129             else
130                 buf.append( "<option value='" + cbo[i] + "'>" + cbo[i] + "</option>" );
131             
132         }
133         buf.append( "</select>" );
134         
135         return buf.toString();
136         
137     }
138     
139 }
140
Popular Tags