1 4 package org.objectweb.jonas.stests.appli; 5 6 import java.rmi.RemoteException ; 7 import java.sql.Connection ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 import java.util.Vector ; 12 13 import javax.ejb.CreateException ; 14 import javax.ejb.EJBException ; 15 import javax.ejb.SessionBean ; 16 import javax.ejb.SessionContext ; 17 import javax.naming.Context ; 18 import javax.naming.InitialContext ; 19 import javax.rmi.PortableRemoteObject ; 20 import javax.sql.DataSource ; 21 22 import org.objectweb.jonas.common.Log; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.Logger; 25 26 27 30 public class OrderEntryClerkSFR implements SessionBean { 31 32 static private Logger logger = null; 33 SessionContext ejbContext; 34 private transient Connection dbConn = null; 35 private static String whid = "0001"; 36 private static String did = "1"; 37 38 private static String customersTable = null; 40 private static String stocksTable = null; 41 private static String itemsTable = null; 42 private static String ordersTable = null; 43 private static String orderlinesTable = null; 44 45 public Integer custID; 47 public Vector items; 48 49 protected static DataSource dataSource = null; 51 52 56 57 public void setSessionContext(SessionContext ctx) { 58 if (logger == null) { 59 logger = Log.getLogger("org.objectweb.jonas_tests"); 60 } 61 logger.log(BasicLevel.DEBUG, ""); 62 ejbContext = ctx; 63 if ( customersTable == null) { 64 try { 65 InitialContext initialContext = new InitialContext (); 66 customersTable = (String ) initialContext.lookup("java:comp/env/CustomerTableName"); 67 stocksTable = (String ) initialContext.lookup("java:comp/env/StockTableName"); 68 itemsTable = (String ) initialContext.lookup("java:comp/env/ItemTableName"); 69 ordersTable = (String ) initialContext.lookup("java:comp/env/OrderTableName"); 70 orderlinesTable = (String ) initialContext.lookup("java:comp/env/OrderlineTableName"); 71 } catch (Exception e) { 72 logger.log(BasicLevel.ERROR, "cannot lookup env-entry "+e); 73 System.exit(2); } 75 } 76 } 77 78 79 public void ejbRemove() { 80 logger.log(BasicLevel.DEBUG, ""); 81 } 82 83 84 public void ejbCreate() throws CreateException { 85 logger.log(BasicLevel.DEBUG, ""); 86 items = new Vector (); 87 custID = null; 88 } 89 90 public void ejbPassivate() { 91 logger.log(BasicLevel.DEBUG, ""); 92 } 93 94 public void ejbActivate() { 95 logger.log(BasicLevel.DEBUG, ""); 96 } 97 98 102 public void setCustomer(Integer cid) throws RemoteException { 104 logger.log(BasicLevel.DEBUG, "customer = "+cid); 105 if ( verifyCustomer(cid) ) { 106 custID = cid; 107 } else { 108 throw new CpwejbException("Customer id " + cid + " not valid"); 109 } 110 } 111 112 public Vector findAllCustomers() throws RemoteException { 114 logger.log(BasicLevel.DEBUG, ""); 115 Vector customerVector = new Vector (); 116 117 Customer customer = null; 118 try { 119 Connection con = getConnection(); 120 Statement stmt = con.createStatement(); 121 ResultSet rs = stmt.executeQuery("select cid, cfirst, clast, cinit, caddr1, caddr2, ccity, cstate, czip from "+ customersTable); 122 while ( rs != null && rs.next() ) { 123 String customerString[] = new String [9]; 124 customerString[0] = new Integer (rs.getInt("cid")).toString(); 125 customerString[1] = rs.getString("clast"); 126 customerString[2] = rs.getString("cfirst"); 127 customerString[3] = rs.getString("cinit"); 128 customerString[4] = rs.getString("caddr1"); 129 customerString[5] = rs.getString("caddr2"); 130 customerString[6] = rs.getString("ccity"); 131 customerString[7] = rs.getString("cstate"); 132 customerString[8] = rs.getString("czip"); 133 customerVector.addElement(customerString); 134 } 135 stmt.close(); 136 releaseConnection(con); 137 } catch (Exception e) { 138 throw new RemoteException (e.getMessage()); 139 } 140 return customerVector; 141 142 } 143 144 public void addOrderLine(Integer iid, int quantity) throws RemoteException { 146 logger.log(BasicLevel.DEBUG, "item = "+iid+" qty = "+quantity); 147 if ( custID == null ) { 148 throw new CpwejbException("OrderEntryClerkBean: Customer ID must be set first"); 149 } 150 151 if ( items.size() == 25 ) { 152 throw new CpwejbException("OrderEntryClerkBean: Maximum of 25 lines per order reached"); 153 } 154 Item item = null; 155 try { 156 InitialContext initCtx = new InitialContext (); 157 ItemHome home = 158 (ItemHome) PortableRemoteObject.narrow(initCtx.lookup("ItemHome"), ItemHome.class); 159 item = home.findByPrimaryKey(iid); 160 } catch (Exception e) { 161 throw new RemoteException (e.getMessage()); 162 } 163 OrderDetail ol = new OrderDetail(iid, item.getItemPrice()*quantity, quantity); 164 items.addElement(ol); 165 } 166 167 168 public void undoAll() throws RemoteException { 170 logger.log(BasicLevel.DEBUG, ""); 171 items = new Vector (); 173 custID = null; 174 } 175 public boolean verifyCustomer(Integer cid) throws RemoteException { 177 logger.log(BasicLevel.DEBUG, "cid = "+cid); 178 Customer customer = null; 179 boolean retval = false; 180 try { 181 Connection con = getConnection(); 182 Statement stmt = con.createStatement(); 183 ResultSet rs = stmt.executeQuery("select cid from "+ customersTable +" where cid = '" + cid + "'"); 184 if ( rs != null && rs.next()) 185 retval = true; 186 stmt.close(); 187 releaseConnection(con); 188 } catch (Exception e) { 189 throw new RemoteException (e.getMessage()); 190 } 191 return retval; 192 } 193 194 public String placeOrder() throws RemoteException { 196 logger.log(BasicLevel.DEBUG, ""); 197 String orderNumber = null; 198 if ( custID == null ) { 199 throw new CpwejbException("OrderEntryClerkBean: Customer ID must be set before placing an order"); 200 } 201 if ( items.size() == 0 ) { 202 throw new CpwejbException("OrderEntryClerkBean: No order lines. Cannot place order."); 203 } 204 205 try { 206 InitialContext initCtx = new InitialContext (); 207 OrderPlacementHome home = (OrderPlacementHome) PortableRemoteObject.narrow(initCtx.lookup("OrderPlacementHome"), OrderPlacementHome.class); 208 OrderPlacement placement = home.create(); 209 float number = placement.placeOrder(whid, did, custID, items); 210 orderNumber = Float.toString(number); 211 orderNumber = orderNumber.substring(0,orderNumber.length()-2); 212 items = new Vector (); 214 custID = null; 215 } catch (Exception e) { 216 throw new RemoteException (e.getMessage()); 217 } 218 return orderNumber; 219 } 220 221 public Vector findRangeOfItems(Integer lowID, Integer highID) throws RemoteException { 223 logger.log(BasicLevel.DEBUG, "low = "+lowID+" high = "+highID); 224 Vector itemVector = new Vector (); 225 226 Item item = null; 227 try { 228 Connection con = getConnection(); 229 Statement stmt = con.createStatement(); 230 ResultSet rs = stmt.executeQuery("select iid, iname, iprice, idata, stqty from "+itemsTable+" , " + stocksTable +" where iid = stiid and iid >= '" + lowID + 231 "' and iid <= '" + highID + "'"); 232 while ( rs != null && rs.next() ) { 233 String itemString[] = new String [5]; 234 itemString[0] = rs.getString("iid"); 235 itemString[1] = rs.getString("iname"); 236 itemString[2] = "$" + Float.toString(rs.getFloat("iprice")); 237 itemString[3] = Integer.toString(rs.getInt("stqty")); 238 itemString[4] = rs.getString("idata"); 239 itemVector.addElement(itemString); 240 } 241 stmt.close(); 242 releaseConnection(con); 243 } catch (Exception e) { 244 throw new RemoteException (e.getMessage()); 245 } 246 return itemVector; 247 } 248 249 public Vector findAllItems() throws RemoteException { 250 logger.log(BasicLevel.DEBUG, ""); 251 Vector itemVector = new Vector (); 252 Item item = null; 253 try { 254 Connection con = getConnection(); 255 Statement stmt = con.createStatement(); 256 257 ResultSet rs = stmt.executeQuery("select ProdId, Name, Price, Descrip, stqty from "+ customersTable + " , " + stocksTable + " where ProdId = stiid"); 258 259 logger.log(BasicLevel.DEBUG,"select ProdId, Name, Price, Descrip, stqty from itemtable,stocktable where ProdId = stiid"); 260 while ( rs != null && rs.next() ) { 261 String itemString[] = new String [5]; 262 itemString[0] = rs.getString("ProdId"); 263 itemString[1] = rs.getString("Name"); 264 String price = "$" + Float.toString(rs.getFloat("Price")); 265 if ( price.endsWith(".0") ) { 267 price += "0"; 268 } 269 itemString[2] = price; 270 itemString[3] = Integer.toString(rs.getInt("stqty")); 271 itemString[4] = rs.getString("Descrip"); 272 itemVector.addElement(itemString); 273 } 274 stmt.close(); 275 releaseConnection(con); 276 } catch (Exception e) { 277 throw new RemoteException (e.getMessage()); 278 } 279 return itemVector; 280 } 281 public String [] getCustomerForOrder(String wid, int did, int oid) 282 throws RemoteException , CpwejbException { 283 logger.log(BasicLevel.DEBUG, "oid = "+oid); 284 Integer customerID = null; 285 String custInfo[] = new String [4]; 286 try { 287 Connection con = getConnection(); 288 Statement stmt = con.createStatement(); 289 String stmtString = "select ocid from " + ordersTable + " where owid = '" + wid + "' and odid = " + did + 290 " and oid = " + oid; 291 ResultSet rs = stmt.executeQuery(stmtString); 292 if ( rs != null && rs.next() ) { 293 customerID = new Integer (rs.getInt("ocid")); 294 custInfo[0] = custID.toString(); 295 if ( rs.next() ) { 296 throw new RemoteException ("Duplicate Key Error: " + wid + "/" + did + "/" + oid); 298 } 299 InitialContext initCtx = new InitialContext (); 300 CustomerHome cHome = (CustomerHome) PortableRemoteObject.narrow(initCtx.lookup("CustomerHome"), CustomerHome.class); 301 Customer customer = (Customer)cHome.findByPrimaryKey(customerID); 302 custInfo[1] = customer.getFirstName(); 303 custInfo[2] = customer.getMiddleInitials(); 304 custInfo[3] = customer.getLastName(); 305 } else { 306 throw new CpwejbException("Order does not exists for warehouse " + wid + 308 " district " + did + " order " + oid); 309 } 310 stmt.close(); 311 releaseConnection(con); 312 } catch (Exception e) { 313 throw new RemoteException (e.getMessage()); 314 } 315 return custInfo; 316 } 317 318 319 public Vector getItemsForOrder(String wid, int did, int oid) 320 throws RemoteException , CpwejbException { 321 logger.log(BasicLevel.DEBUG, "oid = "+oid); 322 Vector itemVector = new Vector (); 323 try { 324 Connection con = getConnection(); 325 Statement stmt = con.createStatement(); 326 String stmtString = "select oliid, olqty, olamnt from " + orderlinesTable + " where olwid = '" + wid + "' and oldid = " + did + 327 " and oloid = " + oid; 328 ResultSet rs = stmt.executeQuery(stmtString); 329 InitialContext initCtx = new InitialContext (); 330 ItemHome iHome = (ItemHome) PortableRemoteObject.narrow(initCtx.lookup("ItemHome"), ItemHome.class); 331 while ( rs != null && rs.next() ) { 332 String itemInfo[] = new String [4]; 333 Integer ItemID = new Integer (rs.getInt("oliid")); 334 itemInfo[0] = ItemID.toString(); 335 itemInfo[2] = Integer.toString(rs.getInt("olqty")); 336 itemInfo[3] = Float.toString(rs.getFloat("olamnt")); 337 Item item = (Item)iHome.findByPrimaryKey(ItemID); 338 itemInfo[1] = item.getItemName(); 339 itemVector.addElement(itemInfo); 340 } 341 stmt.close(); 342 releaseConnection(con); 343 } catch (Exception e) { 344 throw new RemoteException (e.getMessage()); 345 } 346 return itemVector; 347 } 348 349 public void createAllTables() { 350 logger.log(BasicLevel.DEBUG, ""); 351 createCustomersTable(); 352 createItemsTable(); 353 createStocksTable(); 354 createDistrictsTable(); 355 createOtherTables(); 356 357 } 358 359 public void createOtherTables() { 360 createOrdersTable(); 361 createOrderlinesTable(); 362 } 363 364 public void createOrdersTable() { 365 logger.log(BasicLevel.DEBUG, ""); 366 Connection conn = null; 368 Statement stmt = null; 369 try { 370 conn = getConnection(); 371 stmt = conn.createStatement(); 372 stmt.execute("drop table jt2_appli_order"); 373 } catch (Exception e) { 374 logger.log(BasicLevel.INFO, "Exception in dropTable : " + e); 376 } 377 378 try { 380 stmt.execute("create table jt2_appli_order(owid varchar(4),odid integer,ocid integer,ooid float,olines integer,primary key (owid,odid,ooid))"); 381 stmt.close(); 382 conn.close(); 383 logger.log(BasicLevel.DEBUG, "jt2_appli_order created"); 384 } catch (SQLException e) { 385 logger.log(BasicLevel.ERROR, "Exception in createOrderTable : " + e); 386 throw new EJBException ("Exception in createOrderTable"); 387 } 388 } 389 390 391 public void createOrderlinesTable() { 392 logger.log(BasicLevel.DEBUG, ""); 393 Connection conn = null; 395 Statement stmt = null; 396 try { 397 conn = getConnection(); 398 stmt = conn.createStatement(); 399 stmt.execute("drop table jt2_appli_orderline"); 400 } catch (Exception e) { 401 logger.log(BasicLevel.INFO, "Exception in dropTable : " + e); 403 } 404 405 try { 407 stmt.execute("create table jt2_appli_orderline (OLOID float not null, OLDID integer not null, OLWID varchar(4) not null, OLNBR integer not null,OLIID integer, OLQTY integer, OLAMNT float,primary key (OLWID,OLDID,OLOID,OLNBR))"); 408 stmt.close(); 409 conn.close(); 410 logger.log(BasicLevel.DEBUG, "jt2_appli_orderline created"); 411 } catch (SQLException e) { 412 logger.log(BasicLevel.ERROR, "Exception in createOrderlinesTable : " + e); 413 throw new EJBException ("Exception in createOrderlinesTable"); 414 } 415 } 416 417 private void createDistrictsTable() { 418 logger.log(BasicLevel.DEBUG, ""); 419 420 try { 421 InitialContext initCtx = new InitialContext (); 422 DistrictHome home = 423 (DistrictHome) PortableRemoteObject.narrow(initCtx.lookup("DistrictHome"), 424 DistrictHome.class); 425 home.create(1,"0001","Belgique",0); 426 home.create(2,"0001","Espagne",0); 427 home.create(3,"0001","Finlande",0); 428 home.create(4,"0001","Irlande",0); 429 logger.log(BasicLevel.DEBUG, "jt2_appli_district created"); 430 } catch (Exception e) { 431 logger.log(BasicLevel.ERROR, "Exception in createTable : " + e); 432 throw new EJBException ("Exception in createTable"); 433 } 434 } 435 436 private void createCustomersTable() { 437 logger.log(BasicLevel.DEBUG, ""); 438 439 try { 440 InitialContext initCtx = new InitialContext (); 441 CustomerHome home = 442 (CustomerHome) PortableRemoteObject.narrow(initCtx.lookup("CustomerHome"), CustomerHome.class); 443 home.create( new Integer (1),"Paul","Desgranges","PD","1 rue du bourg","", 444 "Nice","06","","04050607",1000); 445 home.create( new Integer (2),"François","Exertier","FE","16 avenue J. jaures","", 446 "Grenoble","38","","0476276115",1000); 447 home.create( new Integer (3),"Florent","Benoit","FB","20 place de la gare","", 448 "Lyon","69","","04286745",1000); 449 home.create( new Integer (4),"Gérard","Vandome","GV","13 rue A. Einstein","", 450 "Marseille","13","","04122436",1000); 451 home.create( new Integer (5),"Philippe","Coq","PC","3 rue du Village","", 452 "Nice","06","","0405060713",1000); 453 home.create( new Integer (6),"Philippe","Durieux","PD","6 avenue J. Valles","", 454 "Grenoble","38","","0476276115",1000); 455 home.create( new Integer (7),"Hélène","Joanin","HJ","30 place du Tramway","", 456 "Lyon","69","","0428674513",1000); 457 home.create( new Integer (8),"Adriana","Danes","AD","23 rue Galilé","", 458 "Marseille","13","","0412243613",1000); 459 home.create( new Integer (9),"Jacques","Cayuela","JC","29 rue de la Clef","", 460 "Nice","06","","0405060717",1000); 461 462 home.create( new Integer (10),"Eric","Hardesty","EC","41 rue de la gare","", 463 "Nancy","54","","0302060627",1000); 464 home.create( new Integer (11),"Goulven","Lejeune","GL","37 rue du Hameau","", 465 "Nice","06","","0405060717",1000); 466 home.create( new Integer (12),"Christophe","Loridan","CL","26 avenue Gambetta","", 467 "Grenoble","38","","0476276117",1000); 468 home.create( new Integer (13),"Jerome","Camilleri","JC","30 place des VFD","", 469 "Lyon","69","","0428674519",1000); 470 home.create( new Integer (14),"Guillaume","Sauthier","GS","37 rue B.Giordano","", 471 "Marseille","13","","0412243196",1000); 472 logger.log(BasicLevel.DEBUG, "jt2_appli_customer created"); 473 } catch (Exception e) { 474 logger.log(BasicLevel.ERROR, "Exception in createTable : " + e); 475 throw new EJBException ("Exception in createTable"); 476 } 477 } 478 private void createItemsTable() { 479 logger.log(BasicLevel.DEBUG, ""); 480 481 try { 482 InitialContext initCtx = new InitialContext (); 483 ItemHome home = 484 (ItemHome) PortableRemoteObject.narrow(initCtx.lookup("ItemHome"), ItemHome.class); 485 home.create(new Integer (1),"Beaujolais 95",19,"Grumage - Domaine des Averlys"); 486 home.create(new Integer (2),"Beaujolais 95",20,"Domaine des Averlys"); 487 home.create(new Integer (3),"Beaujolais 96",18,"Cuvée Antique - Domaine de Rochecorbière"); 488 home.create(new Integer (4),"Beaujolais 96",19,"Vin de garde - Domaine des Averlys"); 489 home.create(new Integer (5),"Beaujolais 96",22,"Domaine des Fortières"); 490 home.create(new Integer (6),"Beaujolais 97",28,"Médaille Or - Paul Champier Producteur"); 491 home.create(new Integer (7),"Beaujolais 97",21,"Domaine du Berret"); 492 home.create(new Integer (8),"Beaujolais 98",21,"Domaine des Fortières"); 493 home.create(new Integer (9),"Beauj Rosé 96",19,"Domaine de Rochecorbière"); 494 home.create(new Integer (10),"Beauj Rosé 96",19,"Domaine de la Croix Ange"); 495 home.create(new Integer (11),"Beauj Rosé 97",21,"Domaine de la Chambarde"); 496 home.create(new Integer (12),"Beauj Rosé 97",18,"Domaine des Averlys"); 497 home.create(new Integer (13),"Beauj Blanc 96",24,"Domaine de Rochcorbière"); 498 home.create(new Integer (14),"Beauj Blanc 96",21,"Domaine du Berret"); 499 home.create(new Integer (15),"Beauj Nouveau",20,"Domaine du Berret"); 500 home.create(new Integer (16),"Beauj Nouveau",21,"Domaine des Fortières"); 501 home.create(new Integer (17),"Beauj Nouveau",19,"Domaine de la Chambarde"); 502 home.create(new Integer (18),"Beauj Nouveau",18,"Domaine de la Croix Ange"); 503 home.create(new Integer (19),"Beauj Villages",25,"Agrément Terra Vitis"); 504 home.create(new Integer (20),"Brouilly",22,"Paul Champier Producteur"); 505 home.create(new Integer (21),"Brouilly 1994",35,"Domaine dit Barron"); 506 home.create(new Integer (22),"Brouilly 1996",28,"Médaille de Bronze - Paul Champier Producteur"); 507 home.create(new Integer (23),"Brouilly 1997",71,"Médaille Or - Paul Champier Producteur"); 508 home.create(new Integer (24),"Côte de Br 94",30,"Paul Champier Producteur"); 509 home.create(new Integer (25),"Côte de Br 97",32,"Les Muses - Château de Briante"); 510 home.create(new Integer (26),"Morgon 1997",29,"Domaine dit Barron"); 511 home.create(new Integer (27),"Régnié",26,"Cercle des Bulliats"); 512 home.create(new Integer (28),"Régnié 1998",26,"Cercle des Bulliats"); 513 logger.log(BasicLevel.DEBUG, "jt2_appli_item created"); 514 } catch (Exception e) { 515 logger.log(BasicLevel.ERROR, "Exception in createTable : " + e); 516 throw new EJBException ("Exception in createTable"); 517 } 518 } 519 520 private void createStocksTable() { 521 logger.log(BasicLevel.DEBUG, ""); 522 523 try { 524 InitialContext initCtx = new InitialContext (); 525 StockHome home = 526 (StockHome) PortableRemoteObject.narrow(initCtx.lookup("StockHome"), StockHome.class); 527 home.create("0001", new Integer (1),1000); 528 home.create("0001", new Integer (2),1000); 529 home.create("0001", new Integer (3),1000); 530 home.create("0001", new Integer (4),1000); 531 home.create("0001", new Integer (5),1000); 532 home.create("0001", new Integer (6),1000); 533 home.create("0001", new Integer (7),1000); 534 home.create("0001", new Integer (8),1000); 535 home.create("0001", new Integer (9),1000); 536 home.create("0001", new Integer (10),1000); 537 home.create("0001", new Integer (11),1000); 538 home.create("0001", new Integer (12),1000); 539 home.create("0001", new Integer (13),1000); 540 home.create("0001", new Integer (14),1000); 541 home.create("0001", new Integer (15),1000); 542 home.create("0001", new Integer (16),1000); 543 home.create("0001", new Integer (17),1000); 544 home.create("0001", new Integer (18),1000); 545 home.create("0001", new Integer (19),1000); 546 home.create("0001", new Integer (20),1000); 547 home.create("0001", new Integer (21),1000); 548 home.create("0001", new Integer (22),1000); 549 home.create("0001", new Integer (23),1000); 550 home.create("0001", new Integer (24),1000); 551 home.create("0001", new Integer (25),1000); 552 home.create("0001", new Integer (26),1000); 553 home.create("0001", new Integer (27),1000); 554 home.create("0001", new Integer (28),1000); 555 logger.log(BasicLevel.DEBUG, "jt2_appli_stock created"); 556 } catch (Exception e) { 557 logger.log(BasicLevel.ERROR, "Exception in createTable : " + e); 558 throw new EJBException ("Exception in createTable"); 559 } 560 } 561 565 private Connection getConnection() throws SQLException ,RemoteException { 566 if (dataSource == null) { 567 568 Context initialContext = null; 569 try { 570 initialContext = new InitialContext (); 571 } catch (Exception e) { 572 throw new RemoteException ("Cannot get InitialContext ", e); 573 } 574 String dataSourceName = "java:comp/env/jdbc/OrderEntryClerk"; 575 576 try { 577 dataSource = (DataSource )initialContext.lookup(dataSourceName); 578 } catch (Exception e) { 579 throw new RemoteException ("cannot lookup " + dataSourceName, e); 580 } 581 } 582 return dataSource.getConnection(); 583 } 584 585 private void releaseConnection(Connection con) { 587 try{ 588 con.close(); 589 } catch (Exception ignore) {} 590 } 591 592 593 594 595 596 } 597 598 | Popular Tags |