示例#1
0
  // extract from inventory
  public int checkInventory(String sku, String color, String size, int quantity) {

    InventoryPK pk = new InventoryPK(sku, color, size);

    Inventory inventory = em.find(Inventory.class, pk, LockModeType.OPTIMISTIC);
    int amount = inventory.getInventory();
    if (amount > 0) {
      if (amount >= quantity) {
        amount = amount - quantity;
        inventory.setInventory(amount);
        try {
          em.merge(inventory);
        } catch (OptimisticLockException e) {
          return -9999;
        }

        return quantity;
      } else {
        inventory.setInventory(0);
        try {
          em.merge(inventory);
        } catch (OptimisticLockException e) {
          return -9999;
        }

        return amount;
      }
    } else {
      return amount;
    }
  }
示例#2
0
  // restore inventory when a product was removed from shopping cart
  public int refreshInventory(String sku, String color, String size, int quantity) {

    InventoryPK pk = new InventoryPK(sku, color, size);

    Inventory inventory = em.find(Inventory.class, pk, LockModeType.OPTIMISTIC);
    int amount = inventory.getInventory();

    amount = amount + quantity;

    inventory.setInventory(amount);

    try {
      em.merge(inventory);
    } catch (OptimisticLockException e) {
      return -9999;
    }

    return quantity;
  }