1 5 package com.nightlabs.ipanema.trade; 6 7 import java.io.Serializable ; 8 import java.util.Collection ; 9 import java.util.Date ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 import javax.jdo.JDOHelper; 15 import javax.jdo.PersistenceManager; 16 17 import com.nightlabs.ModuleException; 18 import com.nightlabs.ipanema.accounting.Accounting; 19 import com.nightlabs.ipanema.accounting.Currency; 20 import com.nightlabs.ipanema.accounting.ProductInfo; 21 import com.nightlabs.ipanema.security.User; 22 import com.nightlabs.ipanema.store.Product; 23 24 36 public class Offer 37 implements Serializable 38 { 39 43 private String organisationID; 44 45 48 private long offerID; 49 50 53 private Order order; 54 55 58 private Currency currency; 59 60 63 private String primaryKey; 64 65 81 private Map invoices = new HashMap (); 82 83 90 private boolean stable; 91 92 98 private boolean valid = false; 99 100 103 private boolean confirmed = false; 104 105 126 private Map itemsToSell = new HashMap (); 127 128 149 private Map itemsToRefund = new HashMap (); 150 151 156 private OfferItemPrice price; 157 158 161 private boolean containsPricesDependentOnOffer = false; 162 163 protected Offer() { } 164 165 public Offer(Order order, long offerID) 166 { 167 if (order == null) 168 throw new NullPointerException ("order must not be null!"); 169 170 PersistenceManager pm = JDOHelper.getPersistenceManager(order); 171 if (pm == null) 172 throw new IllegalStateException ("order is not persistent! Could not get a PersistenceManager from it!"); 173 174 Accounting accounting = Accounting.getAccounting(pm); 175 176 this.order = order; 177 this.organisationID = order.getOrganisationID(); 178 this.offerID = offerID; 179 this.currency = order.getCurrency(); 180 this.primaryKey = getPrimaryKey(organisationID, offerID); 181 } 183 184 187 public String getOrganisationID() 188 { 189 return organisationID; 190 } 191 192 195 public long getOfferID() 196 { 197 return offerID; 198 } 199 public static String getPrimaryKey(String organisationID, long offerID) 200 { 201 return organisationID + '/' + Long.toHexString(offerID); 202 } 203 204 public String getPrimaryKey() 205 { 206 return primaryKey; 207 } 208 209 public synchronized OfferItem createOfferItemToRefund(User user, OfferItem offerItemToRefund) 210 throws ModuleException 211 { 212 if (isConfirmed()) 213 throw new IllegalStateException ("This offer is already confirmed! Cannot create a new OfferItem!"); 214 215 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 216 if (pm == null) 217 throw new IllegalStateException ("This instance of Offer is not persistent! Cannot create an OfferItem!"); 218 219 this.valid = false; 221 222 Product product = offerItemToRefund.getProduct(); 223 String productPK = product.getPrimaryKey(); 224 227 Trader trader = Trader.getTrader(pm); 228 229 OfferItem offerItem = (OfferItem)itemsToSell.get(productPK); 230 if (offerItem == null) { 231 232 if (trader.getOrganisationID().equals(this.getOrganisationID())) { 233 offerItem = new OfferItem(this, offerItemToRefund); 234 } 235 else { 236 return null; } 254 255 itemsToRefund.put(productPK, offerItem); 256 } 258 return offerItem; 259 } 260 261 public synchronized OfferItem createOfferItemToSell(User user, Product product, Date autoReleaseDT) throws ModuleException 262 { 263 if (isConfirmed()) 264 throw new IllegalStateException ("This offer is already confirmed! Cannot create a new OfferItem!"); 265 266 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 267 if (pm == null) 268 throw new IllegalStateException ("This instance of Offer is not persistent! Cannot create an OfferItem!"); 269 270 this.valid = false; 272 273 String productPK = product.getPrimaryKey(); 274 277 OfferItem offerItem = (OfferItem)itemsToSell.get(productPK); 278 if (offerItem == null) { 279 Trader trader = Trader.getTrader(pm); 280 if (trader.getOrganisationID().equals(this.getOrganisationID())) { 281 offerItem = new OfferItem(this, product, currency); 282 } else { 284 return null; } 303 304 itemsToSell.put(productPK, offerItem); 305 } 307 offerItem.allocate(user, autoReleaseDT); 312 313 return offerItem; 314 } 315 316 321 public synchronized void calculatePrice() 322 { 323 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 325 if (pm == null) 326 throw new IllegalStateException ("This instance of Offer is not persistent! Cannot calculate the price!"); 327 328 Trader trader = Trader.getTrader(pm); 329 if (trader.getOrganisationID().equals(this.getOrganisationID())) { 330 long amountBeforeRecalc = price.getAmount(); 331 calculateOfferItemPrices(false); 332 long firstNewAmount = price.getAmount(); 333 if (containsPricesDependentOnOffer) { 334 calculateOfferItemPrices(false); 336 long secondNewAmount = price.getAmount(); 337 this.stable = secondNewAmount == firstNewAmount; 338 } 339 else 340 this.stable = true; 341 } else { 343 346 throw new UnsupportedOperationException ("NYI"); 348 } 349 } 350 351 359 private void calculateOfferItemPrices(boolean all) 360 { 361 Accounting accounting = null; 362 price.clearFragments(); 363 price.setAmount(0); 364 containsPricesDependentOnOffer = false; 365 366 for (int i = 0; i<2; i++) { 367 Collection items = i == 0 ? itemsToSell.values() : itemsToRefund.values(); 368 for (Iterator it = items.iterator(); it.hasNext(); ) { 369 OfferItem offerItem = (OfferItem)it.next(); 370 if (all || offerItem.isPriceDependentOnOffer()) { 371 if (offerItem.isPriceDependentOnOffer()) 372 containsPricesDependentOnOffer = true; 373 374 if (accounting == null) { 375 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 376 if (pm == null) 377 throw new IllegalStateException ("This instance of Offer is currently not persistent! Cannot obtain a PersistenceManager"); 378 379 accounting = Accounting.getAccounting(pm); 380 } ProductInfo productInfo = accounting.getProductInfo(offerItem.getProduct(), true); 382 offerItem.setPriceDependentOnOffer(productInfo.getPriceConfig().isDependentOnOffer()); 383 offerItem.getPrice().assign(productInfo.getPriceConfig().getPrice(offerItem)); 384 } price.sumPrice(offerItem.getPrice()); 386 } 387 } 388 } 389 390 393 public Order getOrder() 394 { 395 return order; 396 } 397 398 401 public boolean isConfirmed() 402 { 403 return confirmed; 404 } 405 408 protected void confirm() 409 { 410 if (!isValid()) 411 throw new IllegalStateException ("This Offer cannot be confirmed, because it is not valid! Call 'validate()' first and ensure the Offer is stable!"); 412 413 confirmed = true; 414 } 415 416 419 protected Currency getCurrency() 420 { 421 return currency; 422 } 423 426 public Collection getItemsToSell() 427 { 428 return itemsToSell.values(); 429 } 430 431 434 public Collection getItemsToRefund() 435 { 436 return itemsToRefund.values(); 437 } 438 439 442 protected Map getItemsToSellMap() 443 { 444 return itemsToSell; 445 } 446 447 450 protected Map getItemsToRefundMap() 451 { 452 return itemsToRefund; 453 } 454 455 465 public boolean validate() 466 { 467 valid = false; 468 469 472 valid = true; 474 475 calculatePrice(); 476 if (!isStable()) 477 valid = false; 478 479 return isValid(); 480 } 481 482 488 public boolean isStable() 489 { 490 return stable; 491 } 492 493 496 public boolean isValid() 497 { 498 if (valid && !stable) { 499 return false; 501 } 502 return valid; 503 } 504 505 508 protected void setValid(boolean valid) 509 { 510 this.valid = valid; 511 } 512 513 } 514 | Popular Tags |