KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > DAOExample


1 package Jt.examples;
2
3 import Jt.*;
4 import Jt.xml.*;
5
6
7 import java.util.*;
8 import java.text.*;
9 import java.io.*;
10
11
12 /**
13   * Data Access Object (DAO) example. This subclass of JtDAO contains the
14   * attributes to be mapped to database columns.
15   */

16
17 public class DAOExample extends JtDAO {
18 private String JavaDoc email;
19 private String JavaDoc name;
20 private int status;
21 private Date tstamp;
22 private boolean booleanv;
23
24   public DAOExample () {
25   }
26
27   public int getStatus () {
28     return (status);
29   }
30
31
32   public void setStatus (int newStatus) {
33     status = newStatus;
34   }
35
36   public boolean getBooleanv () {
37     return (booleanv);
38   }
39
40   public void setBooleanv (boolean booleanv) {
41     this.booleanv = booleanv;
42   }
43
44
45   public String JavaDoc getEmail() {
46     return email;
47   }
48
49   public void setEmail (String JavaDoc newEmail) {
50     email = newEmail;
51   }
52
53   public void setName (String JavaDoc name) {
54     this.name = name;
55   }
56
57   public String JavaDoc getName() {
58     return name;
59   }
60
61
62   public void setTstamp (Date tstamp) {
63     this.tstamp = tstamp;
64   }
65
66   public Date getTstamp () {
67     return tstamp;
68   }
69
70
71   // ConfigFileExist - Verify if the configuration
72
// file exists
73

74   private boolean configFileExists (String JavaDoc configFile) {
75
76     String JavaDoc fname;
77     File file;
78  
79     //fname = (String) getValue (this, configFile);
80
fname = configFile;
81
82     if (fname == null)
83       return false;
84
85     file = new File (fname);
86     return (file.exists ());
87   }
88
89   public void realize () {
90
91     JtMessage msg;
92
93     msg = new JtMessage ();
94
95     // Set attribute/column mappings
96

97     if (getValue (this, "configFile") != null)
98       return;
99
100     // Use the following fallback mappings (attribute/column)
101
// if the config file is not present
102

103     handleWarning ("DAOExample: config file (DAOExample.xml) not found.");
104     handleWarning ("DAOExample: fallback mappings will be used.");
105     setValue (msg, "msgContent", "status");
106     setValue (msg, "msgData", "status");
107     setValue (msg, "msgId", "JtMAP_ATTRIBUTE");
108     sendMessage (this, msg);
109
110     setValue (msg, "msgContent", "email");
111     setValue (msg, "msgData", "email");
112     sendMessage (this, msg);
113
114     setValue (msg, "msgContent", "name");
115     setValue (msg, "msgData", "name");
116     sendMessage (this, msg);
117
118     setValue (msg, "msgContent", "tstamp");
119     setValue (msg, "msgData", "tstamp");
120     sendMessage (this, msg);
121
122     setValue (msg, "msgContent", "booleanv");
123     setValue (msg, "msgData", "booleanv");
124     sendMessage (this, msg);
125
126   }
127
128   // processMessageEvent: process messages
129

130   public Object JavaDoc processMessage (Object JavaDoc event) {
131   String JavaDoc content;
132   String JavaDoc query;
133   JtMessage e = (JtMessage) event;
134
135
136     if (e == null || (e.getMsgId() == null)) {
137       handleError ("processMessage:invalid message:" +
138         e);
139       return (null);
140     }
141     // Let the superclass handle all the messages
142

143     if (e.getMsgId().equals("JtREALIZE")) {
144         super.processMessage (new JtMessage ("JtREALIZE"));
145     realize ();
146         return (null);
147     }
148
149     return (super.processMessage (event));
150
151   }
152
153   // Test program
154

155   public static void main(String JavaDoc[] args) {
156
157     JtObject main = new JtObject ();
158     JtMessage msg = new JtMessage ();
159     Date date = new Date ();
160     DAOExample tmp, member;
161     Exception JavaDoc ex;
162     Object JavaDoc tmp1;
163     File file;
164
165     // Create DAO object
166

167     member = (DAOExample) main.createObject ("Jt.examples.DAOExample", "member");
168
169
170     // Set database key and table
171

172     main.setValue (member, "key", "email");
173     main.setValue (member, "table", "members");
174
175     file = new File ("DAOExample.xml");
176
177     if (file.exists ())
178       main.setValue (member, "configFile", "DAOExample.xml");
179
180
181     // Realize object
182

183     //main.setValue (msg, "msgId", "JtREALIZE");
184
main.sendMessage (member, new JtMessage ("JtREALIZE"));
185
186     // Set JDBC driver and url
187

188     member.setValue ("db", "driver", "com.mysql.jdbc.Driver");
189     member.setValue ("db", "url", "jdbc:mysql://localhost/test");
190     member.setValue ("db", "user", "root");
191     member.setValue ("db", "password", "123456");
192
193     // Set Object attributes
194

195     main.setValue (member, "email", "user@freedom.com");
196     main.setValue (member, "name", "John Dow");
197     main.setValue (member, "tstamp", new Date ());
198     main.setValue (member, "status", "2005");
199     main.setValue (member, "booleanv", "true");
200
201     // Insert object (object is added to the database)
202

203     tmp = (DAOExample) main.sendMessage (member, new JtMessage ("JtINSERT"));
204
205     if (tmp == null) {
206       System.out.println ("JtINSERT: FAIL");
207     } else {
208       System.out.println ("JtINSERT: PASS");
209     }
210
211
212     main.setValue (member, "email", "user@freedom.com");
213     main.setValue (member, "key", "email");
214     main.setValue (member, "table", "members");
215
216     // Find object
217

218     tmp = (DAOExample) main.sendMessage (member, new JtMessage ("JtFIND"));
219
220     if (tmp != null)
221       System.out.println ("JtFIND: PASS");
222     else
223       System.out.println ("JtFIND: FAIL");
224
225     main.sendMessage (member, new JtMessage ("JtPRINT"));
226
227     main.setValue (member, "status", "2006");
228     main.setValue (member, "name", "Jane Dow");
229     main.setValue (member, "booleanv", "false");
230
231     // Update object
232

233     tmp = (DAOExample) main.sendMessage (member, new JtMessage ("JtUPDATE"));
234
235
236     if (tmp == null){
237       System.out.println ("JtUPDATE: FAIL");
238     } else {
239       System.out.println ("JtUPDATE: PASS");
240     }
241
242     main.setValue (member, "email", "user@freedom.com");
243
244     main.setValue (msg, "msgId", "JtFIND");
245     tmp = (DAOExample) main.sendMessage (member, msg);
246
247     ex = (Exception JavaDoc) main.getValue (member, "objException");
248
249     if (ex != null) {
250         System.out.println ("JtFIND: FAIL");
251     } else
252       if (tmp == null)
253         System.out.println ("JtFIND: FAIL");
254
255
256     main.sendMessage (member, new JtMessage ("JtPRINT"));
257
258     main.setValue (member, "email", "user@freedom.com");
259
260     // Delete object
261

262     tmp = (DAOExample) main.sendMessage (member, new JtMessage ("JtDELETE"));
263
264     if (tmp == null){
265       System.out.println ("JtDELETE: FAIL");
266     } else {
267       System.out.println ("JtDELETE: PASS");
268     }
269
270     // Remove the DAO. This closes the database connection
271

272     main.sendMessage (member, new JtMessage ("JtREMOVE"));
273
274   }
275
276
277 }
Popular Tags