KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > DAOMembers


1
2
3 // Illustrates the use of Jt Data Access Objects
4

5   
6 package Jt.examples;
7
8 import Jt.*;
9 import Jt.xml.*;
10
11
12 import java.util.*;
13 import java.text.*;
14 import java.io.*;
15
16 public class DAOMembers extends JtDAO {
17 private String JavaDoc email;
18 private String JavaDoc firstname;
19 private String JavaDoc lastname;
20 private int status;
21 private String JavaDoc subject;
22 private String JavaDoc comments;
23 private Date tstamp;
24 private int email_flag;
25 private Date mdate;
26 private String JavaDoc location;
27 private long lval;
28 private float fval;
29 private double dval;
30 private byte bval;
31 private boolean booleanv;
32
33   public DAOMembers() {
34   }
35
36   public void setEmail_flag (int newEmail_flag) {
37     email_flag = newEmail_flag;
38   }
39
40   public int getEmail_flag () {
41     return (email_flag);
42   }
43
44   public int getStatus () {
45     return (status);
46   }
47
48
49   public void setStatus (int newStatus) {
50     status = newStatus;
51   }
52
53   public byte getBval () {
54     return (bval);
55   }
56
57   public void setBval (byte bval) {
58     this.bval = bval;
59   }
60
61   public long getLval () {
62     return (lval);
63   }
64
65   public void setLval (long lval) {
66     this.lval = lval;
67   }
68
69   public boolean getBooleanv () {
70     return (booleanv);
71   }
72
73   public void setBooleanv (boolean booleanv) {
74     this.booleanv = booleanv;
75   }
76
77   public float getfval () {
78     return (fval);
79   }
80
81
82   public void setDval (double dval) {
83     this.dval = dval;
84   }
85
86   public double getdval () {
87     return (dval);
88   }
89
90
91   public void setFval (float fval) {
92     this.fval = fval;
93   }
94
95   public String JavaDoc getEmail() {
96     return email;
97   }
98
99   public void setEmail (String JavaDoc newEmail) {
100     email = newEmail;
101   }
102
103   public String JavaDoc getSubject() {
104     return subject;
105   }
106
107   public void setSubject (String JavaDoc newSubject) {
108     subject = newSubject;
109   }
110
111   public String JavaDoc getComments() {
112     return comments;
113   }
114
115   public void setComments (String JavaDoc newComments) {
116     comments = newComments;
117   }
118
119   public void setFirstname (String JavaDoc newFirstname) {
120     firstname = newFirstname;
121   }
122
123   public String JavaDoc getFirstname() {
124     return firstname;
125   }
126
127   public void setLastname (String JavaDoc newLastname) {
128     lastname = newLastname;
129   }
130
131   public String JavaDoc getLastname() {
132     return lastname;
133   }
134
135
136   public void setTstamp (Date tstamp) {
137     this.tstamp = tstamp;
138   }
139
140   public Date getTstamp () {
141     return tstamp;
142   }
143
144   public void setMdate (Date mdate) {
145     this.mdate = mdate;
146   }
147
148   public Date getMdate () {
149     return mdate;
150   }
151
152   public String JavaDoc getLocation() {
153     return location;
154   }
155
156   public void setLocation (String JavaDoc location) {
157     this.location = location;
158   }
159
160   // ConfigFileExist - Verify if the configuration
161
// file exists
162

163   private boolean configFileExists (String JavaDoc configFile) {
164
165     String JavaDoc fname;
166     File file;
167  
168     //fname = (String) getValue (this, configFile);
169
fname = configFile;
170
171     if (fname == null)
172       return false;
173
174     file = new File (fname);
175     return (file.exists ());
176   }
177
178   public void realize () {
179
180     JtMessage msg;
181
182     msg = new JtMessage ();
183
184     // Set attribute/column mappings
185

186     if (getValue (this, "configFile") != null)
187       return;
188
189     // Use the following fallback mappings (attribute/column)
190
// if the config file is not present
191

192     handleWarning ("DAOMembers: config file (DAOMembers.xml) not found.");
193     handleWarning ("DAOMembers: fallback mappings will be used.");
194     setValue (msg, "msgContent", "status");
195     setValue (msg, "msgData", "status");
196     setValue (msg, "msgId", "JtMAP_ATTRIBUTE");
197     sendMessage (this, msg);
198
199     setValue (msg, "msgContent", "email");
200     setValue (msg, "msgData", "email");
201     sendMessage (this, msg);
202
203     setValue (msg, "msgContent", "mdate");
204     setValue (msg, "msgData", "mdate");
205     sendMessage (this, msg);
206
207     setValue (msg, "msgContent", "tstamp");
208     setValue (msg, "msgData", "tstamp");
209     sendMessage (this, msg);
210
211     setValue (msg, "msgContent", "email_flag");
212     setValue (msg, "msgData", "email_flag");
213     sendMessage (this, msg);
214
215     setValue (msg, "msgContent", "booleanv");
216     setValue (msg, "msgData", "booleanv");
217     sendMessage (this, msg);
218
219     setValue (msg, "msgContent", "fval");
220     setValue (msg, "msgData", "fval");
221     sendMessage (this, msg);
222
223     setValue (msg, "msgContent", "lval");
224     setValue (msg, "msgData", "lval");
225     sendMessage (this, msg);
226
227     setValue (msg, "msgContent", "dval");
228     setValue (msg, "msgData", "dval");
229     sendMessage (this, msg);
230
231     setValue (msg, "msgContent", "bval");
232     setValue (msg, "msgData", "bval");
233     sendMessage (this, msg);
234
235   }
236
237   // processMessageEvent: process messages
238

239   public Object JavaDoc processMessage (Object JavaDoc event) {
240   String JavaDoc content;
241   String JavaDoc query;
242   JtMessage e = (JtMessage) event;
243
244
245     if (e == null || (e.getMsgId() == null)) {
246       handleError ("processMessage:invalid message:" +
247         e);
248       return (null);
249     }
250     // Let the superclass handle all the messages
251

252     if (e.getMsgId().equals("JtREALIZE")) {
253         super.processMessage (new JtMessage ("JtREALIZE"));
254         //super.realize ();
255
realize ();
256         return (null);
257     }
258
259     return (super.processMessage (event));
260
261   }
262
263   // Test program
264

265   public static void main(String JavaDoc[] args) {
266
267     JtObject main = new JtObject ();
268     JtMessage msg, msg1;
269     Integer JavaDoc count;
270     Date date = new Date ();
271     DateFormat df = DateFormat.getDateInstance();
272     DAOMembers tmp, member;
273     Exception JavaDoc ex;
274     Object JavaDoc tmp1;
275     File file;
276
277     //main.setObjTrace (1);
278

279     // Create DAO object
280

281     member = (DAOMembers) main.createObject ("Jt.examples.DAOMembers", "member");
282     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
283
284
285     // Set database key and table
286

287     main.setValue (member, "key", "email");
288     main.setValue (member, "table", "members");
289
290     file = new File ("DAOMembers.xml");
291
292     if (file.exists ())
293       main.setValue (member, "configFile", "DAOMembers.xml");
294
295
296     // Realize object
297

298     //main.setValue (msg, "msgId", "JtREALIZE");
299
main.sendMessage (member, new JtMessage ("JtREALIZE"));
300
301     // Set JDBC driver and url
302

303     member.setValue ("db", "driver", "com.mysql.jdbc.Driver");
304     member.setValue ("db", "url", "jdbc:mysql://localhost/test");
305     member.setValue ("db", "user", "root");
306     member.setValue ("db", "password", "123456");
307
308
309
310     //main.setValue ("member", "tstamp", "December 12, 2004");
311
//main.setValue ("member", "tstamp", df.format (date));
312

313     // Set attribute/column mappings
314

315
316     //main.setValue (member, "status", "2005");
317
//main.setValue (member, "fval", "3.1");
318

319
320     main.setValue (member, "email", "user@freedom.com");
321     main.setValue (member, "tstamp", new Date ());
322     main.setValue (member, "status", "2005");
323     main.setValue (member, "email_flag", "100");
324     main.setValue (member, "lval", "300000");
325     main.setValue (member, "fval", "3.1");
326     main.setValue (member, "dval", "4.1");
327     main.setValue (member, "bval", "1");
328     main.setValue (member, "booleanv", "true");
329
330     // Insert object
331

332     //main.setValue (msg, "msgId", "JtINSERT");
333
tmp = (DAOMembers) main.sendMessage (member, new JtMessage ("JtINSERT"));
334
335     if (tmp == null) {
336       System.out.println ("JtINSERT: FAIL");
337     } else {
338       System.out.println ("JtINSERT: PASS");
339     }
340
341
342     main.setValue (msg, "msgId", "JtCLEAR");
343     main.sendMessage (member, msg);
344
345     main.setValue (member, "email", "user@freedom.com");
346     main.setValue (member, "key", "email");
347     main.setValue (member, "table", "members");
348
349     // Find object
350

351     tmp = (DAOMembers) main.sendMessage (member, new JtMessage ("JtFIND"));
352
353     if (tmp != null)
354       System.out.println ("JtFIND: PASS");
355     else
356       System.out.println ("JtFIND: FAIL");
357
358     main.sendMessage (member, new JtMessage ("JtPRINT"));
359
360     main.setValue (member, "status", "2006");
361     main.setValue (member, "email_flag", "101");
362
363     // Update object
364

365     tmp = (DAOMembers) main.sendMessage (member, new JtMessage ("JtUPDATE"));
366
367
368     if (tmp == null){
369       System.out.println ("JtUPDATE: FAIL");
370     } else {
371       System.out.println ("JtUPDATE: PASS");
372     }
373
374     main.setValue (msg, "msgId", "JtCLEAR");
375     main.sendMessage (member, msg);
376
377     main.setValue (member, "email", "user@freedom.com");
378     main.setValue (member, "key", "email");
379     main.setValue (member, "table", "members");
380
381     main.setValue (msg, "msgId", "JtFIND");
382     tmp = (DAOMembers) main.sendMessage (member, msg);
383
384     ex = (Exception JavaDoc) main.getValue (member, "objException");
385
386     if (ex != null) {
387         System.out.println ("JtFIND: FAIL");
388     } else
389       if (tmp == null)
390         System.out.println ("JtFIND: FAIL");
391
392
393     main.sendMessage (member, new JtMessage ("JtPRINT"));
394
395     main.setValue (member, "email", "user@freedom.com");
396     //main.setValue ("member", "status", "2005");
397
//main.setValue ("member", "email_flag", "100");
398

399     //main.sendMessage ("member", "message");
400

401     // Delete object
402

403     tmp = (DAOMembers) main.sendMessage (member, new JtMessage ("JtDELETE"));
404
405     if (tmp == null){
406       System.out.println ("JtDELETE: FAIL");
407     } else {
408       System.out.println ("JtDELETE: PASS");
409     }
410
411     main.sendMessage (member, new JtMessage ("JtREMOVE"));
412
413   }
414
415
416 }
Popular Tags