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
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
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
60 SQLMetaData = new SQLMetaData(db,fn);
61 displayOrder = rs.getInt("displayorder");
62 mandatory = rs.getBoolean("mandatory");
63
64
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
128
129 public Integer getFieldNumber() {
130 return fieldNumber;
131 }
132
133
134
135
136 public String getFieldName() {
137 return fieldName;
138 }
139
140
141
142
143 public String getDefaultValue() {
144 return defaultValue;
145 }
146
147
148
149
150 public String getDisplayName() {
151 return displayName;
152 }
153
154
155
156
157 public String getType() {
158 return type;
159 }
160
161
162
163
164 public Integer getTableNumber() {
165 return tableNumber;
166 }
167
168
169
170 public String getTableName() {
171 return tableName;
172 }
173
174
175
176
177 public int getDisplayOrder() {
178 return displayOrder;
179 }
180
181
182
183
184 public boolean getMandatory() {
185 return mandatory;
186 }
187
188
189
190
191
192 public Object getMandatoryNull() {
193 if (mandatory) {
194 return Boolean.TRUE;
195 } else {
196 return null;
197 }
198 }
199
200
201
202
203 public String getRelationshipTable() {
204 return relationshipTable;
205 }
206
207
208
209
210 public String getRelationshipField() {
211 return relationshipField;
212 }
213
214
215
216
217 public boolean getListDisplay() {
218 return listDisplay;
219 }
220
221
222
223
224
225 public int getSize() {
226 return size;
227 }
228
229
230
231
232 public int getMaxLength() {
233 return maxLength;
234 }
235
236
237
238
239 public int getHeight() {
240 return height;
241 }
242
243
244
245
246
247 public boolean getEnforceUnique() {
248 return enforceUnique;
249 }
250
251
252
253
254
255 public boolean getSelectDisplay() {
256 return selectDisplay;
257 }
258
259
260
261
262
263
264 public SQLMetaData getSQLMetaData() {
265 return SQLMetaData;
266 }
267
268 }