1 package edu.rice.rubis.beans; 2 3 import java.rmi.*; 4 import javax.ejb.*; 5 import javax.naming.Context ; 6 import javax.naming.InitialContext ; 7 import javax.rmi.PortableRemoteObject ; 8 9 29 30 public class BidBean implements EntityBean 31 { 32 private EntityContext entityContext; 33 private transient boolean isDirty; 35 36 37 public Integer id; 38 public Integer userId; 39 public Integer itemId; 40 public int qty; 41 public float bid; 42 public float maxBid; 43 public String date; 44 45 46 52 public Integer getId() throws RemoteException 53 { 54 return id; 55 } 56 57 63 public Integer getUserId() throws RemoteException 64 { 65 return userId; 66 } 67 68 74 public Integer getItemId() throws RemoteException 75 { 76 return itemId; 77 } 78 79 85 public int getQuantity() throws RemoteException 86 { 87 return qty; 88 } 89 90 96 public float getBid() throws RemoteException 97 { 98 return bid; 99 } 100 101 107 public float getMaxBid() throws RemoteException 108 { 109 return maxBid; 110 } 111 112 118 public String getDate() throws RemoteException 119 { 120 return date; 121 } 122 123 129 public String getBidderNickName() throws RemoteException 130 { 131 Context initialContext = null; 132 try 133 { 134 initialContext = new InitialContext (); 135 } 136 catch (Exception e) 137 { 138 System.err.print("Cannot get initial context for JNDI: " + e); 139 return null; 140 } 141 142 UserHome uHome; 144 try 145 { 146 uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("UserHome"), 147 UserHome.class); 148 } 149 catch (Exception e) 150 { 151 System.err.print("Cannot lookup User: " +e); 152 return null; 153 } 154 try 155 { 156 User u = uHome.findByPrimaryKey(new UserPK(userId)); 157 return u.getNickName(); 158 } 159 catch (Exception e) 160 { 161 System.err.print("This user does not exist (got exception: " +e+")<br>"); 162 return null; 163 } 164 } 165 166 167 174 public void setUserId(Integer id) throws RemoteException 175 { 176 userId = id; 177 isDirty = true; } 179 180 187 public void setItemId(Integer id) throws RemoteException 188 { 189 itemId = id; 190 isDirty = true; } 192 193 199 public void setQuantity(int Qty) throws RemoteException 200 { 201 qty = Qty; 202 isDirty = true; } 204 205 214 public void setBid(float newBid) throws RemoteException 215 { 216 bid = newBid; 217 isDirty = true; } 219 220 226 public void setMaxBid(float newBid) throws RemoteException 227 { 228 maxBid = newBid; 229 isDirty = true; } 231 232 238 public void setDate(String newDate) throws RemoteException 239 { 240 date = newDate; 241 isDirty = true; } 243 244 245 260 public BidPK ejbCreate(Integer bidUserId, Integer bidItemId, float userBid, float userMaxBid, int quantity) throws CreateException, RemoteException, RemoveException 261 { 262 Item item; 263 InitialContext initialContext = null; 264 try 266 { 267 initialContext = new InitialContext (); 268 ItemHome iHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("ItemHome"), ItemHome.class); 269 item = iHome.findByPrimaryKey(new ItemPK(bidItemId)); 270 } 271 catch (Exception e) 272 { 273 throw new CreateException("Error while getting item id "+bidItemId+" in BidBean: " + e+"<br>"); 274 } 275 item.setMaxBid(userBid); 276 item.addOneBid(); 277 278 IDManagerHome home = null; 280 IDManager idManager = null; 281 282 try 283 { 284 home = (IDManagerHome)PortableRemoteObject.narrow(initialContext.lookup( 285 "java:comp/env/ejb/IDManager"), IDManagerHome.class); 286 } 287 catch (Exception e) 288 { 289 throw new EJBException("Cannot lookup IDManager: " +e); 290 } 291 try 292 { 293 IDManagerPK idPK = new IDManagerPK(); 294 idManager = home.findByPrimaryKey(idPK); 295 id = idManager.getNextBidID(); 296 userId = bidUserId; 297 itemId = bidItemId; 298 bid = userBid; 299 maxBid = userMaxBid; 300 qty = quantity; 301 date = TimeManagement.currentDateToString(); 302 } 303 catch (Exception e) 304 { 305 throw new EJBException("Cannot create bid: " +e); 306 } 307 return null; 308 } 309 310 311 313 public void ejbPostCreate(Integer bidUserId, Integer bidItemId, float userBid, float userMaxBid, int quantity) 314 { 315 isDirty = true; } 317 318 320 public void ejbLoad() throws RemoteException 321 { 322 isDirty = false; 323 } 324 325 327 public void ejbStore() throws RemoteException 328 { 329 isDirty = false; 330 } 331 332 333 public void ejbActivate() throws RemoteException {} 334 335 public void ejbPassivate() throws RemoteException {} 336 337 public void ejbRemove() throws RemoteException, RemoveException {} 338 339 356 public void setEntityContext(EntityContext context) throws RemoteException 357 { 358 entityContext = context; 359 } 360 361 378 public void unsetEntityContext() throws RemoteException 379 { 380 entityContext = null; 381 } 382 383 384 391 public boolean isModified() 392 { 393 return isDirty; 394 } 395 396 397 404 public String printBidHistory() throws RemoteException 405 { 406 return "<TR><TD><a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.ViewUserInfo?userId="+userId+ 407 "\">"+getBidderNickName()+"<TD>"+bid+"<TD>"+date+"\n"; 408 } 409 } 410 | Popular Tags |