KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > ejb > examples > EJBClient


1 package Jt.ejb.examples;
2
3 import Jt.*;
4 import Jt.examples.*;
5 import java.io.*;
6 import Jt.ejb.*;
7
8
9 // This class demonstrates the use of most J2EE design patterns.
10
// It also uses an Entity bean and the Jt DAO pattern.
11
// A Jt remote business object is used to interface with the entity bean (local
12
// home interfaces). The application itself manipulates a set of customer records.
13
// The usual operations are demonstrated: find, create, modify, delete, etc.
14

15 public class EJBClient extends JtObject {
16
17
18   JtBusinessDelegate businessDelegate;
19   JtKeyboard keyboard;
20
21   private Object JavaDoc findMember (String JavaDoc email) {
22
23     JtMessage msg = new JtMessage ("JtFIND");
24
25
26     if (businessDelegate == null) {
27       handleError ("findMember: invalid parameters");
28       return (null);
29     }
30
31     msg.setMsgContent (email);
32
33
34     return (businessDelegate.sendMessage ("remoteBusinessObject", msg));
35        
36      
37   }
38
39
40   public Object JavaDoc processMessage (Object JavaDoc message) {
41
42    String JavaDoc msgid = null;
43    JtMessage e = (JtMessage) message;
44    Object JavaDoc content;
45    Object JavaDoc data;
46
47
48      if (e == null)
49     return null;
50
51      msgid = (String JavaDoc) e.getMsgId ();
52
53      if (msgid == null)
54     return null;
55
56      content = e.getMsgContent();
57      data = e.getMsgData ();
58
59      // Remove this object
60
if (msgid.equals ("JtREMOVE")) {
61        return (null);
62      }
63
64      if (msgid.equals ("JtACTIVATE")) {
65        test ();
66        return (null);
67      }
68              
69      handleError ("JtHashTable.processMessage: invalid message id:" + msgid);
70      return (null);
71
72   }
73
74
75
76   private void modifyMember (String JavaDoc email, JtValueObject valueObject) {
77     JtIterator jit;
78     String JavaDoc str;
79     JtValueObject outValueObject = null;
80     Object JavaDoc value;
81     String JavaDoc input;
82     String JavaDoc svalue = null;
83     JtMessage msg;
84
85     if (email == null || valueObject == null)
86       return;
87
88     jit = (JtIterator) sendMessage (valueObject, new JtMessage ("JtGET_KEYS"));
89
90     if (jit == null)
91       return;
92
93     System.out.println
94      ("Please enter a new value or press <enter> to leave the current value ...");
95
96     for (;;) {
97       str = (String JavaDoc) jit.processMessage (new JtMessage ("JtNEXT"));
98       if (str == null)
99         break;
100
101       msg = new JtMessage ("JtGET");
102       msg.setMsgData (str);
103       value = valueObject.processMessage (msg);
104       //value = getValue (valueObject, str);
105

106       //if (value instanceof java.sql.Date)
107
// continue;
108

109       if (str.equals ("email") || str.equals ("mdate") || str.equals ("tstamp"))
110         continue;
111     
112       System.out.print (str + "(" + value + "):");
113       
114       input = (String JavaDoc) sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
115
116       if (input == null)
117         continue;
118
119       input = input.trim ();
120       if (input.equals (""))
121         continue;
122       if (value == null)
123         svalue = null;
124       else
125         svalue = value.toString ();
126
127
128       if (input.equals (svalue))
129         continue;
130
131       if (outValueObject == null)
132         outValueObject = new JtValueObject ();
133
134       msg = new JtMessage ("JtPUT");
135       msg.setMsgData (str);
136       msg.setMsgContent (input);
137       outValueObject.processMessage (msg);
138       
139     }
140
141
142     if (outValueObject == null)
143       return;
144     
145     msg = new JtMessage ("MODIFY");
146     msg.setMsgContent (email);
147     msg.setMsgData (outValueObject);
148
149
150     businessDelegate.sendMessage ("remoteBusinessObject", msg);
151   
152   }
153
154
155   // Test program
156

157   public static void main(String JavaDoc[] args) {
158     JtFactory factory = new JtFactory (); // Jt Factory
159

160     EJBClient main;
161
162  
163     main = (EJBClient) factory.createObject ("Jt.ejb.examples.EJBClient",
164      "EJBClient");
165
166     factory.sendMessage (main, new JtMessage ("JtACTIVATE"));
167
168   }
169
170
171   private void test () {
172
173     JtFactory factory = new JtFactory (); // Jt Factory
174
String JavaDoc reply;
175     JtMessage msg;
176     JtValueObject member;
177     String JavaDoc input, email;
178     JtValueObject valueObject = new JtValueObject ();
179
180     
181     // Create an instance of JtBusinessDelegate (Jt implementation of
182
// the J2EE business delegate pattern)
183

184     businessDelegate = (JtBusinessDelegate) factory.createObject ("Jt.ejb.JtBusinessDelegate",
185      "businessDelegate");
186
187     keyboard = (JtKeyboard) factory.createObject ("Jt.JtKeyboard", "keyboard");
188
189     // Create a remote business object
190

191     businessDelegate.createObject ("Jt.ejb.examples.RemoteBusinessObject", "remoteBusinessObject");
192
193
194     for (;;) {
195
196       System.out.println ("Please select an operation ...");
197       System.out.println ("1. Find ...");
198       System.out.println ("2. Create ...");
199       System.out.println ("3. Modify ...");
200       System.out.println ("4. Delete ...");
201       System.out.println ("5. Disable ...");
202       System.out.println ("6. Exit ...");
203
204
205
206       input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
207
208
209       if (input == null)
210         continue;
211
212       input = input.trim ();
213
214       if (input.equals ("1")) {
215
216         System.out.println ("Please enter an email address ...");
217
218         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
219         msg = new JtMessage ("JtFIND");
220
221         if (input == null)
222           continue;
223
224         input = input.trim ();
225
226         if (input.equals (""))
227           continue;
228
229         msg.setMsgContent (input);
230         member = (JtValueObject) businessDelegate.sendMessage ("remoteBusinessObject", msg);
231
232         if (member == null) {
233           System.out.println ("Record not found:" + input);
234           continue;
235         }
236
237        
238         //msg = new JtMessage ("JtENCODE_OBJECT");
239
//msg.setMsgContent (member);
240

241         // Print the content of the ValueObject
242

243         System.out.println (member);
244
245       }
246
247       if (input.equals ("2")) {
248
249         System.out.println ("Please enter an email address ...");
250
251         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
252
253         if (input == null)
254           continue;
255
256         input = input.trim ();
257
258         if (input.equals (""))
259           continue;
260
261         msg = new JtMessage ("CREATE");
262         msg.setMsgContent (input);
263
264
265         System.out.print ("Name:");
266         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
267
268         if (input != null)
269           input = input.trim ();
270
271
272
273         businessDelegate.setValue ("remoteBusinessObject", "firstname", input);
274
275
276         System.out.print ("Last Name:");
277         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
278
279         if (input != null)
280           input = input.trim ();
281
282         businessDelegate.setValue ("remoteBusinessObject", "lastname", input);
283         reply = (String JavaDoc) businessDelegate.sendMessage ("remoteBusinessObject", msg);
284
285         if (reply == null)
286           System.out.println ("Error: unable to create record.");
287
288       }
289
290       if (input.equals ("4")) {
291
292         System.out.println ("Please enter an email address ...");
293
294         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
295
296         if (input == null)
297           continue;
298
299         input = input.trim ();
300
301         if (input.equals (""))
302           continue;
303
304         email = input;
305         valueObject = (JtValueObject) findMember (input);
306
307         if (valueObject == null) {
308           System.out.println ("Record doesn't exist:" + input);
309           continue;
310         }
311
312         System.out.print ("Do you want to delete this record(Y/N) ?");
313
314         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
315
316         if (input != null)
317           input = input.trim ();
318
319         if (!("y".equals(input) || "Y".equals (input)))
320           continue;
321
322         msg = new JtMessage ("DELETE");
323
324         msg.setMsgContent (email);
325         reply = (String JavaDoc) businessDelegate.sendMessage ("remoteBusinessObject", msg);
326
327         if (reply == null)
328           System.out.println ("Unable to delete record.");
329       }
330
331       if (input.equals ("3")) {
332
333         System.out.println ("Please enter an email address ...");
334
335         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
336
337         if (input == null)
338           continue;
339
340         input = input.trim ();
341
342         if (input.equals (""))
343           continue;
344
345
346         valueObject = (JtValueObject) findMember (input);
347
348         if (valueObject == null) {
349           System.out.println ("Record doesn't exist:" + input);
350           continue;
351         }
352
353         modifyMember (input, valueObject);
354
355         //if (reply == null)
356
//System.out.println ("Unable to modify record.");
357

358       }
359
360       if (input.equals ("5")) {
361
362         System.out.println ("Please enter an email address ...");
363
364         input = (String JavaDoc) factory.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
365
366         if (input == null)
367           continue;
368
369         input = input.trim ();
370
371         if (input.equals (""))
372           continue;
373
374         msg = new JtMessage ("DISABLE");
375         msg.setMsgContent (input);
376
377         businessDelegate.sendMessage ("remoteBusinessObject", msg);
378
379       }
380
381       if (input.equals ("6")) {
382         break;
383       }
384
385     }
386
387     // Remove objects
388

389     businessDelegate.removeObject ("remoteBusinessObject");
390
391     factory.removeObject ("businessDelegate");
392         
393   }
394
395 }
396
397
398
399
Popular Tags