1 4 package com.nightlabs.ipanema.trade; 5 6 import java.util.Date ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import javax.jdo.JDOHelper; 12 import javax.jdo.JDOObjectNotFoundException; 13 import javax.jdo.PersistenceManager; 14 15 import com.nightlabs.ModuleException; 16 import com.nightlabs.ipanema.accounting.Accounting; 17 import com.nightlabs.ipanema.accounting.Currency; 18 import com.nightlabs.ipanema.organisation.LocalOrganisation; 19 import com.nightlabs.ipanema.organisation.Organisation; 20 import com.nightlabs.ipanema.organisation.id.OrganisationID; 21 import com.nightlabs.ipanema.security.User; 22 import com.nightlabs.ipanema.store.Product; 23 import com.nightlabs.ipanema.store.ProductStatusTracker; 24 import com.nightlabs.ipanema.store.Store; 25 import com.nightlabs.ipanema.trade.id.OrderID; 26 27 40 public class Trader 41 { 42 49 public static Trader getTrader(PersistenceManager pm) 50 { 51 Iterator it = pm.getExtent(Trader.class).iterator(); 52 if (it.hasNext()) 53 return (Trader)it.next(); 54 55 Trader trader = new Trader(); 56 57 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 58 trader.organisationID = localOrganisation.getOrganisation().getOrganisationID(); 59 trader.mandator = new OrganisationLegalEntity(localOrganisation.getOrganisation()); 60 trader.addVendor(trader.mandator); 61 trader.addCustomer(trader.mandator); 62 trader.store = Store.getStore(pm); 63 trader.accounting = Accounting.getAccounting(pm); 64 65 pm.makePersistent(trader); 66 return trader; 67 } 68 69 73 private String organisationID; 74 75 private Accounting accounting; 76 77 private Store store; 78 79 82 private OrganisationLegalEntity mandator; 83 84 101 protected Map legalEntities = new HashMap (); 102 103 121 protected Map customers = new HashMap (); 122 123 141 protected Map vendors = new HashMap (); 142 143 157 protected Map orderRequirements = new HashMap (); 158 159 173 protected Map offerRequirements = new HashMap (); 174 175 178 public String getOrganisationID() 179 { 180 return organisationID; 181 } 182 183 189 protected OrganisationLegalEntity getMandator() 190 { 191 return mandator; 192 } 193 public void addVendor(LegalEntity vendor) 194 { 195 vendors.put(vendor.getPrimaryKey(), vendor); 196 } 197 public void addCustomer(LegalEntity customer) 198 { 199 customers.put(customer.getPrimaryKey(), customer); 200 } 201 202 public LegalEntity getVendor(String organisationID, String anchorID) 203 { 204 String vendorPK = LegalEntity.getPrimaryKey(organisationID, anchorID); 205 return (LegalEntity) vendors.get(vendorPK); 206 } 207 208 public LegalEntity getCustomer(String organisationID, String anchorID) 209 { 210 String customerPK = LegalEntity.getPrimaryKey(organisationID, anchorID); 211 return (LegalEntity) vendors.get(customerPK); 212 } 213 214 222 public OrganisationLegalEntity createCustomerOrganisationLegalEntity(String organisationID) 223 { 224 String anchorPK = OrganisationLegalEntity.getPrimaryKey(organisationID); 225 OrganisationLegalEntity res = (OrganisationLegalEntity)customers.get(anchorPK); 226 if (res != null) 227 return res; 228 229 res = createOrganisationLegalEntity(organisationID); 230 customers.put(anchorPK, res); 231 return res; 232 } 233 234 242 public OrganisationLegalEntity createVendorOrganisationLegalEntity(String organisationID) 243 { 244 String anchorPK = OrganisationLegalEntity.getPrimaryKey(organisationID); 245 OrganisationLegalEntity res = (OrganisationLegalEntity)vendors.get(anchorPK); 246 if (res != null) 247 return res; 248 249 res = createOrganisationLegalEntity(organisationID); 250 vendors.put(anchorPK, res); 251 return res; 252 } 253 254 private OrganisationLegalEntity createOrganisationLegalEntity(String organisationID) 255 { 256 String anchorPK = OrganisationLegalEntity.getPrimaryKey(organisationID); 257 OrganisationLegalEntity res = (OrganisationLegalEntity)legalEntities.get(anchorPK); 258 if (res != null) 259 return res; 260 261 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 262 if (pm == null) 263 throw new IllegalStateException ("This instance of Trader is currently not persistent!"); 264 265 pm.getExtent(Organisation.class); 266 Organisation organisation; 267 try { 268 organisation = (Organisation) pm.getObjectById(OrganisationID.create(organisationID), true); 269 } catch (JDOObjectNotFoundException x) { 270 throw new JDOObjectNotFoundException("There is no organisation existing for organisationID=\""+organisationID+"\"! Cannot create an OrganisationLegalEntity for a non-existent Organisation!"); 271 } 272 res = new OrganisationLegalEntity(organisation); 273 274 legalEntities.put(anchorPK, res); 275 return res; 276 } 277 278 281 protected long nextOrderID = 0; 282 283 284 291 public synchronized Order createLocalOrder(LegalEntity customer, Currency currency) { 292 Order order = new Order(getMandator(), customer, nextOrderID++, currency); 293 String pk = order.getPrimaryKey(); 294 getPersistenceManager().makePersistent(order); 295 return order; 296 } 297 298 public Order getOrder(OrderID orderID) throws OrderNotFoundException { 299 Order order = null; 300 try { 301 order = (Order) getPersistenceManager().getObjectById(orderID); 302 } catch (JDOObjectNotFoundException e) { 303 throw new OrderNotFoundException("Could not find order with OrderID: "+orderID); 304 } 305 return order; 306 } 307 308 public synchronized Order createOrder(OrganisationLegalEntity vendor, LegalEntity customer, Currency currency) 309 throws ModuleException 310 { 311 if (customer == null) 312 throw new NullPointerException ("customer"); 313 314 if (currency == null) 315 throw new NullPointerException ("currency"); 316 317 PersistenceManager pm = JDOHelper.getPersistenceManager(this); 318 if (pm == null) 319 throw new IllegalStateException ("This OrganisationLegalEntity is currently not persistent! Can invoke this method only on a persistent object!"); 320 321 LocalOrganisation localOrganisation = LocalOrganisation.getLocalOrganisation(pm); 322 323 Order order = null; 324 325 if (mandator.getPrimaryKey().equals(vendor.getPrimaryKey())) { 326 return createLocalOrder(customer, currency); 328 } 329 else { 330 } 349 return order; 350 } 351 352 public OrderRequirement createOrderRequirement(Order order) 353 { 354 if (!order.getOrganisationID().equals(this.getOrganisationID())) 355 throw new IllegalArgumentException ("Cannot create an instance of OrderRequirement for a foreign Organisation!"); 356 357 OrderRequirement orderRequirement = (OrderRequirement)orderRequirements.get(order.getPrimaryKey()); 358 if (orderRequirement == null) { 359 orderRequirement = new OrderRequirement(this, order); 360 orderRequirements.put(order.getPrimaryKey(), orderRequirement); 361 } 362 return orderRequirement; 363 } 364 365 public OfferRequirement createOfferRequirement(Offer offer) 366 { 367 if (!offer.getOrganisationID().equals(this.getOrganisationID())) 368 throw new IllegalArgumentException ("Cannot create an instance of OfferRequirement for a foreign Organisation!"); 369 370 OfferRequirement offerRequirement = (OfferRequirement)offerRequirements.get(offer.getPrimaryKey()); 371 if (offerRequirement == null) { 372 offerRequirement = new OfferRequirement(this, offer); 373 offerRequirements.put(offer.getPrimaryKey(), offerRequirement); 374 } 375 return offerRequirement; 376 } 377 378 386 public Offer createOfferRequirementOffer(OfferRequirement offerRequirement, OrganisationLegalEntity vendor) 387 throws ModuleException 388 { 389 Offer offer = offerRequirement.getOfferByVendor(vendor); 390 if (offer == null) { 391 OrderRequirement orderRequirement = createOrderRequirement(offerRequirement.getOffer().getOrder()); 395 396 Order order = createOrderRequirenmentOrder(orderRequirement,vendor); 399 400 offerRequirement.addOffer(offer); 402 } 403 return offer; 404 } 405 406 407 415 public Order createOrderRequirenmentOrder(OrderRequirement orderRequirement, OrganisationLegalEntity vendor) 416 throws ModuleException 417 { 418 Order order = orderRequirement.getOrder(vendor); 419 if (order == null) { 420 order = createOrder(vendor, getMandator(), order.getCurrency()); 421 orderRequirement.addOrder(order); 422 } 423 return order; 424 } 425 426 427 protected void allocateProduct(User user, OfferItem offerItem, Product product, Date autoReleaseDT) 429 throws ModuleException 430 { 431 ProductStatusTracker psr = store.getProductStatusTracker(product); 432 433 if (this.organisationID.equals(product.getOrganisationID())) { 434 synchronized (product) { 436 for (Iterator it = product.getPackagedProducts().iterator(); it.hasNext(); ) { 437 Product packagedProduct = (Product)it.next(); 438 allocateProduct(user, offerItem, packagedProduct, autoReleaseDT); 439 } 440 } 441 } 442 else { 443 if (psr.getCurrentStatus().isHollow()) { 447 OfferRequirement offerRequirement = createOfferRequirement(offerItem.getOffer()); 448 OrganisationLegalEntity vendor = createVendorOrganisationLegalEntity(product.getOrganisationID()); 449 Offer backhandOffer = createOfferRequirementOffer(offerRequirement, vendor); 450 451 backhandOffer.createOfferItemToSell(user, product, autoReleaseDT); 452 } } 454 455 psr.allocate(user, offerItem.getOffer().getPrimaryKey()); 458 } 459 460 463 protected long nextOfferID = 0; 464 465 protected synchronized long createOfferID() 466 { 467 return nextOfferID++; 468 } 469 470 public synchronized Offer createLocalOffer(Order order) { 471 Offer offer = new Offer(order, createOfferID()); 472 String pk = offer.getPrimaryKey(); 473 getPersistenceManager().makePersistent(offer); 474 return offer; 476 } 477 478 public synchronized Offer createOffer(Order order) 479 throws ModuleException 480 { 481 if (mandator.getPrimaryKey().equals(order.getVendor().getPrimaryKey())) { 482 return createLocalOffer(order); 483 } 484 return null; 500 } 501 502 public void allocateOfferItem(User user, OfferItem offerItem, Date autoReleaseDT) 503 throws ModuleException 504 { 505 Product product = offerItem.getProduct(); 506 allocateProduct(user, offerItem, product, autoReleaseDT); 507 } 508 509 protected void releaseProduct(User user, OfferItem offerItem, Product product) 510 throws ModuleException 511 { 512 ProductStatusTracker psr = store.getProductStatusTracker(product); 513 514 if (this.organisationID.equals(product.getOrganisationID())) { 515 synchronized (product) { 517 for (Iterator it = product.getPackagedProducts().iterator(); it.hasNext(); ) { 518 Product packagedProduct = (Product)it.next(); 519 releaseProduct(user, offerItem, packagedProduct); 520 } 521 } 522 } 523 else { 524 if (psr.getCurrentStatus().isHollow()) { 528 OfferRequirement offerRequirement = createOfferRequirement(offerItem.getOffer()); 529 OrganisationLegalEntity vendor = createVendorOrganisationLegalEntity(product.getOrganisationID()); 530 Offer backhandOffer = createOfferRequirementOffer(offerRequirement, vendor); 531 532 deleteOfferItemToSell(backhandOffer, user, product); 533 } } 535 536 psr.release(user); 539 } 540 541 public void releaseOfferItem(User user, OfferItem offerItem) 542 throws ModuleException 543 { 544 Product product = offerItem.getProduct(); 545 releaseProduct(user, offerItem, product); 546 } 547 548 549 public void deleteOfferItemToSell(Offer offer, User user, Product product) 550 throws ModuleException 551 { 552 deleteOfferItemToSell(offer, user, product.getOrganisationID(), product.getProductID()); 553 } 554 555 public void deleteOfferItemToSell(Offer offer, User user, String productOrganisationID, String productID) 556 throws ModuleException 557 { 558 if (offer.isConfirmed()) 559 throw new IllegalStateException ("The given offer is already confirmed! Cannot create a new OfferItem!"); 560 561 PersistenceManager pm = JDOHelper.getPersistenceManager(offer); 562 if (pm == null) 563 throw new IllegalStateException ("This instance of Offer is not persistent! Cannot delete an OfferItem!"); 564 565 offer.setValid(false); 567 568 String productPK = Product.getPrimaryKey(productOrganisationID, productID); 569 570 OfferItem offerItem = (OfferItem) offer.getItemsToSellMap().get(productPK); 571 if (offerItem == null) return; 573 574 offerItem.release(user); 575 offer.getItemsToSellMap().remove(productPK); 576 } 577 578 581 protected transient PersistenceManager traderPM = null; 582 583 protected PersistenceManager getPersistenceManager() { 584 if (traderPM == null) { 585 traderPM = JDOHelper.getPersistenceManager(this); 586 if (traderPM == null) 587 throw new IllegalStateException ("This instance of Trader is not persistent, can not get a PersistenceManager!"); 588 } 589 return traderPM; 590 } 591 } 592 | Popular Tags |