1 32 33 34 42 43 package stockonline.ejb.entity.cmp.account; 44 45 import java.sql.*; 46 import java.util.*; 47 import java.rmi.*; 48 49 import javax.ejb.*; 50 import javax.naming.*; 51 import javax.sql.*; 52 import javax.rmi.*; 53 54 import stockonline.ejb.sql.SeqGenerator; 55 import stockonline.ejb.entity.interf.*; 56 57 61 public class AccountBean implements EntityBean 62 { 63 final static boolean verbose = false; 64 final static String BEAN_NAME = "cmpAccount"; 65 final static String RESOURCE_NAME = "java:comp/env/jdbc/StockDB"; 66 67 public int sub_accno; 68 public String sub_name; 69 public String sub_address; 70 public int sub_credit; 71 72 protected EntityContext ctx; 73 private transient boolean isDirty; 75 public AccountBean() 76 { 77 if (verbose) System.out.println(BEAN_NAME + " instantiated"); 78 } 79 80 84 87 public void update(int amount) 88 { 89 if (verbose) 90 System.out.println(BEAN_NAME + ".update(" + amount + ") called."); 91 92 sub_credit = amount; 93 setModified(true); 94 } 95 96 99 public void deposit(int amount) 100 { 101 if (verbose) 102 System.out.println(BEAN_NAME + ".deposit(" + amount + ") called."); 103 104 sub_credit += amount; 105 setModified(true); 106 } 107 108 112 public int withdraw(int amount) throws Exception 113 { 114 if (verbose) 115 System.out.println(BEAN_NAME + ".withdraw(" + amount + ") called."); 116 117 if (amount > sub_credit) { 118 throw new Exception ("Your credit is " + sub_credit + "! You cannot withdraw " + amount + "!"); 119 } 120 121 sub_credit -= amount; 122 setModified(true); 123 124 return sub_credit; 125 } 126 127 129 public int getCredit() 130 { 131 if (verbose) 132 System.out.println(BEAN_NAME + ".getCredit() called."); 133 134 return sub_credit; 135 } 136 137 141 146 150 public void setEntityContext(EntityContext ctx) 151 { 152 if (verbose) 153 System.out.println(BEAN_NAME + ".setEntityContext called"); 154 155 this.ctx = ctx; 156 } 157 158 162 public void unsetEntityContext() 163 { 164 if (verbose) 165 System.out.println(BEAN_NAME + ".unsetEntityContext called"); 166 167 this.ctx = null; 168 } 169 170 173 public void ejbActivate() 174 { 175 if (verbose) 176 System.out.println(BEAN_NAME + ".ejbActivate() called."); 177 178 setModified(true); 179 } 180 181 184 public void ejbPassivate() 185 { 186 if (verbose) 187 System.out.println(BEAN_NAME + ".ejbPassivate () called."); 188 } 189 190 196 public AccountPK ejbCreate(String sub_name, String sub_address, int sub_credit) 197 throws CreateException 198 { 199 if (verbose) 200 System.out.println(BEAN_NAME + ".ejbCreate() called."); 201 202 try 203 { 204 this.sub_accno = SeqGenerator.getNextAccountID(RESOURCE_NAME); 205 this.sub_name = sub_name; 206 this.sub_address = sub_address; 207 this.sub_credit = sub_credit; 208 209 return null; 210 } 211 catch(Exception ex) 212 { 213 String msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString(); 214 215 System.err.println(msg); 216 throw new CreateException(msg); 217 } 218 } 219 220 224 public void ejbPostCreate(String sub_name, String sub_address, int sub_credit) 225 { 226 if (verbose) 227 System.out.println(BEAN_NAME + ".ejbPostCreate() called."); 228 229 setModified(false); 230 } 231 232 238 public AccountPK ejbCreate(int sub_accno, String sub_name, String sub_address, int sub_credit) 239 throws CreateException 240 { 241 if (verbose) 242 System.out.println(BEAN_NAME + ".ejbCreate() called."); 243 244 try 245 { 246 this.sub_accno = sub_accno; 247 this.sub_name = sub_name; 248 this.sub_address = sub_address; 249 this.sub_credit = sub_credit; 250 251 return null; 252 } 253 catch(Exception ex) 254 { 255 String msg = BEAN_NAME + ".ejbCreate(): Failed to get a sequence number, due to: " + ex.toString(); 256 257 System.err.println(msg); 258 throw new CreateException(msg); 259 } 260 } 261 262 266 public void ejbPostCreate(int sub_accno, String sub_name, String sub_address, int sub_credit) 267 { 268 if (verbose) 269 System.out.println(BEAN_NAME + ".ejbPostCreate() called."); 270 271 setModified(false); 272 } 273 274 275 279 public void ejbRemove() throws RemoteException 280 { 281 if (verbose) 282 System.out.println(BEAN_NAME + ".ejbRemove() called."); 283 } 284 285 289 public void ejbLoad() throws RemoteException 290 { 291 if (verbose) 292 System.out.println(BEAN_NAME + ".ejbLoad() called."); 293 294 if (!isModified()) return; 295 setModified(false); 296 } 297 298 302 public void ejbStore() throws RemoteException 303 { 304 if (verbose) 305 System.out.println(BEAN_NAME + ".ejbStore() called."); 306 if (!isModified()) { 307 return; 308 } 309 310 setModified(false); 311 } 312 313 318 public boolean isModified() 319 { 320 if (verbose) 321 System.out.println (BEAN_NAME + ".isModified(): isDirty = " + (isDirty ? "true" : "false")); 322 323 return isDirty; 324 } 325 326 331 public void setModified(boolean flag) 332 { 333 isDirty = flag; 334 } 335 336 } 340 | Popular Tags |