View Javadoc

1   package org.paneris.jal.model;
2   
3   import java.sql.Connection;
4   import java.sql.ResultSet;
5   import java.sql.Statement;
6   
7   /**
8    * A FieldMetaData encapsulates meta data for a single field in the datadictionary. 
9    */
10  public class FieldMetaData {
11  
12      private String fieldName;
13      private SQLMetaData SQLMetaData;
14      private Integer fieldNumber;
15      private Integer tableNumber;
16      private String tableName;
17      private String displayName;
18      private String defaultValue;
19      private String type;
20      private int displayOrder;
21      private boolean mandatory;
22      private String relationshipTable;
23      private String relationshipField;
24      private boolean listDisplay;
25      private int size;
26      private int maxLength;
27      private int height;
28      private boolean enforceUnique;
29      private boolean selectDisplay;
30      private String database;
31      private DBConnectionManager connMgr = DBConnectionManager.getInstance();
32  
33  /**
34   * construct meta data for a field in the datadictionary
35   */
36      public FieldMetaData(String db, Integer fn) throws Exception {
37          database = db;
38          fieldNumber = fn;
39          String sqlString = "";
40          try {
41              Connection conn = connMgr.getConnection("FieldMetaData",database);
42              Statement s = conn.createStatement();
43              Statement s2 = conn.createStatement();
44              sqlString = "SELECT * FROM datadictionary WHERE id = ";
45              sqlString = sqlString.concat(fieldNumber.toString());
46              ResultSet rs = s.executeQuery(sqlString);
47              if (rs.next()) {
48                  fieldName = rs.getString("fieldname").trim();
49                  displayName = rs.getString("displayname").trim();
50                  int typeInt = rs.getInt("type");
51                  if (typeInt != 0) {
52                      sqlString = new String("SELECT type FROM datadictionarytypes WHERE id = " + typeInt);
53                      ResultSet rs2 = s2.executeQuery(sqlString);
54                      rs2.next();
55                      type = rs2.getString("type").trim();
56                  } else {
57                      type = "";
58                  }
59              // set up the sql metadata for this field
60                  SQLMetaData = new SQLMetaData(db,fn);
61                  displayOrder = rs.getInt("displayorder");
62                  mandatory = rs.getBoolean("mandatory");
63  
64                  // get the name of the table for this field
65                  tableNumber = new Integer(rs.getInt("tablename"));
66                  sqlString = new String("SELECT tablename FROM datadictionarytables WHERE id = " + tableNumber);
67                  ResultSet rs3 = s2.executeQuery(sqlString);
68                  if (rs3.next()) {
69                      tableName = rs3.getString("tablename").trim();
70                  }
71  
72                  int relationshipTableInt = rs.getInt("relationshiptable");
73                  if (relationshipTableInt != 0) {
74                      sqlString = new String("SELECT tablename FROM datadictionarytables WHERE id = " + relationshipTableInt);
75                      rs3 = s2.executeQuery(sqlString);
76                      if (rs3.next()) {
77                          relationshipTable = rs3.getString("tablename").trim();
78                      } else {
79                          relationshipTable = "";
80                      }
81                  } else {
82                      relationshipTable = "";
83                  }
84                  String rf = rs.getString("relationshipfield");
85                  if (rf != null) {
86                      relationshipField = rf.trim();
87                  } else {
88                      relationshipField = "";
89                  }
90                  try {
91                      String dv = rs.getString("defaultvalue");
92                      if (dv != null) {
93                          defaultValue = dv.trim();
94                      } else {
95                          defaultValue = "";
96                      }
97                  } catch (Exception e) {
98                      defaultValue = "";
99                  }
100                 listDisplay = rs.getBoolean("listdisplay");
101                 size = rs.getInt("size");
102                 try {
103                     height = rs.getInt("height");
104                 } catch (Exception e) {
105                     height = 0;
106                 }
107                 try {
108                     maxLength = rs.getInt("maxlength");
109                 } catch (Exception e) {
110                     maxLength = 0;
111                 }
112                 enforceUnique = rs.getBoolean("enforceunique");
113                 selectDisplay = rs.getBoolean("selectdisplay");
114                 s.close();
115                 s2.close();
116             } else {
117                 throw new Exception("Field number: " + fieldNumber + " is not present in the datadictionary table");
118             }
119             connMgr.freeConnection(database, conn);
120         } catch (Exception e) {
121             throw new Exception("Failed to build FieldMetaData: " + e.toString() + " FieldMetaData SQL: " + sqlString);
122         }
123     }
124 
125 
126 /**
127 returns the field's number
128 **/
129     public Integer getFieldNumber() {
130         return fieldNumber;
131     }
132 
133 /**
134 returns the field's name
135 **/
136     public String getFieldName() {
137         return fieldName;
138     }
139 
140 /**
141 returns the field's default value
142 **/
143     public String getDefaultValue() {
144         return defaultValue;
145     }
146 
147 /**
148 returns the field's display name
149 **/
150     public String getDisplayName() {
151         return displayName;
152     }
153 
154 /**
155 returns the field's type
156 **/
157     public String getType() {
158         return type;
159     }
160 
161 /**
162 returns the field's tablenumber
163 **/
164     public Integer getTableNumber() {
165         return tableNumber;
166     }
167 /**
168 returns the field's tablename
169 **/
170     public String getTableName() {
171         return tableName;
172     }
173 
174 /**
175 returns the field's display order
176 **/
177     public int getDisplayOrder() {
178         return displayOrder;
179     }
180 
181 /**
182 returns true if the field is mandatory
183 **/
184     public boolean getMandatory() {
185         return mandatory;
186     }
187 
188 /**
189 returns null if the field is not mandatory.  this will not be required in webmacro 0.90
190 where we can properly evaluate boolean expressions in the template
191 **/
192     public Object getMandatoryNull() {
193         if (mandatory) {
194             return Boolean.TRUE;
195         } else {
196             return null;
197         }
198     }
199 
200 /**
201 returns the tablename if this field is a look-up field
202 **/
203     public String getRelationshipTable() {
204         return relationshipTable;
205     }
206 
207 /**
208 returns the fieldname if this field is a look-up field
209 **/
210     public String getRelationshipField() {
211         return relationshipField;
212     }
213 
214 /**
215 returns true if this field should appear on the list page
216 **/
217     public boolean getListDisplay() {
218         return listDisplay;
219     }
220 
221 
222 /**
223 returns the size of the input box
224 **/
225     public int getSize() {
226         return size;
227     }
228 
229 /**
230 returns the maximum length of text in an input box
231 **/
232     public int getMaxLength() {
233         return maxLength;
234     }
235 
236 /**
237 returns the height of a textarea input box
238 **/
239     public int getHeight() {
240         return height;
241     }
242 
243 
244 /**
245 returns true if this field should contain a unique value.
246 **/
247     public boolean getEnforceUnique() {
248         return enforceUnique;
249     }
250 
251 
252 /**
253 returns true if this field should as a selection option on the list page
254 **/
255     public boolean getSelectDisplay() {
256         return selectDisplay;
257     }
258 
259 
260 /**
261 gets the type of this field as an java.sql.Type.  this is useful when setting null
262 values in a prepared statement, where the type of the object is important
263 **/
264     public SQLMetaData getSQLMetaData() {
265         return SQLMetaData;
266     }
267 
268 }