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 16 17 public class DAOExample extends JtDAO { 18 private String email; 19 private String 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 getEmail() { 46 return email; 47 } 48 49 public void setEmail (String newEmail) { 50 email = newEmail; 51 } 52 53 public void setName (String name) { 54 this.name = name; 55 } 56 57 public String 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 74 private boolean configFileExists (String configFile) { 75 76 String fname; 77 File file; 78 79 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 97 if (getValue (this, "configFile") != null) 98 return; 99 100 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 130 public Object processMessage (Object event) { 131 String content; 132 String 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 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 155 public static void main(String [] args) { 156 157 JtObject main = new JtObject (); 158 JtMessage msg = new JtMessage (); 159 Date date = new Date (); 160 DAOExample tmp, member; 161 Exception ex; 162 Object tmp1; 163 File file; 164 165 167 member = (DAOExample) main.createObject ("Jt.examples.DAOExample", "member"); 168 169 170 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 183 main.sendMessage (member, new JtMessage ("JtREALIZE")); 185 186 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 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 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 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 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 ) 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 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 272 main.sendMessage (member, new JtMessage ("JtREMOVE")); 273 274 } 275 276 277 } | Popular Tags |