⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ordermanagementbean.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**
   * This business method cancels a specified order
   *
   * @param <b>orderID</b> - id of the order that has to be cancelled
   * @param <b>itemID</b> - id of the item that has to be cancelled
   *
   * @throws <b>OrderException</b> if the orders could not be cancelled
   * @throws <b>RemoteException</b> container error
   */
  public void cancelOrderItem(String orderID, String itemID)
                       throws OrderException {

    // Check if order ID has been specified                                        
    if(orderID == null || "".equals(orderID.trim())) {
      throw new OrderException("Order not specified", "error.order.null");
    }

    // Check if item ID has been specified  
    if(itemID == null || "".equals(itemID.trim())) {
      throw new OrderException(
                               "Order items not specified",
                               "error.order.nullitemid");
    }

    try {

      // Look up product order entity
      ProductOrderLocalHome home   = getOrderLocalHome();
      ProductOrderLocal orderLocal = home.findByPrimaryKey(orderID);

      Collection orderItemsList = orderLocal.getOrderItems();

      OrderItemLocal orderItemLocal = null;

      // Loop through the order items to track the order item specified
      for(Iterator ordersIter = orderItemsList.iterator();
            ordersIter.hasNext();) {
        orderItemLocal = (OrderItemLocal) ordersIter.next();

        if(orderItemLocal.getItemID().equals(itemID)) {

          // Check if the order item has already been shipped
          if(orderItemLocal.getStatus().equals("Shipped")) {
            throw new OrderException(
                                     "Item already shipped. Cannot cancel item order",
                                     "error.order.shipped");
          }
          // if not, remove the order item from the relationship
          ordersIter.remove();
          // and then from persistence
          orderItemLocal.remove();
          // no more checks needed, break out of the loop
          break;
        }
      }

      if(orderItemsList.size() == 0) {

        // No more order items - remove order
        orderLocal.remove();
      } 
    } catch(Exception ex) {

      if(ex instanceof OrderException) {
        throw new OrderException(
                                 "Item already shipped. Cannot cancel item order",
                                 "error.order.shipped");
      } else {
        throw new OrderException(
                                 "Unable to cancel order item because " +
                                 ex.getMessage(), "error.order.cancellation");
      }
    }
  }
  /**
   * This business method retrieves information about a specified order
   *
   * @param <b>orderID</b> - id of the order which has to be queried
   *
   * @return <b>Order</b> - order value object having order information
   *
   * @throws <b>OrderException</b> if the orders could not be fetched
   * @throws <b>RemoteException</b> container error
   */
  public Order getOrder(String orderID) throws OrderException {

    // Check if the order ID has been specified
    if(orderID == null || "".equals(orderID.trim())) {
      throw new OrderException("Order not specified", "error.order.null");
    }

    Order order = null;

    try {

      // Look up product order entity 
      ProductOrderLocalHome home   = getOrderLocalHome();
      ProductOrderLocal orderLocal = home.findByPrimaryKey(orderID);

      List orderItems               = new ArrayList();
      OrderItemLocal orderItemLocal = null;

      // Loop through the order items for this order and add them to a list
      for(
          Iterator orderItemsIter = orderLocal.getOrderItems().iterator();
            orderItemsIter.hasNext();) {
        orderItemLocal = (OrderItemLocal) orderItemsIter.next();
        orderItems.add(new OrderDetail(
                                       orderID, orderItemLocal.getItemID(),
                                       orderItemLocal.getShopID(),
                                       orderItemLocal.getQuantity().intValue(),
                                       orderItemLocal.getUnitPrice()
                                                     .doubleValue(),
                                       orderItemLocal.getStatus()));
      }

      // Construct a new order value object holding the order information
      order =
        new Order(
                  orderLocal.getId(), orderLocal.getOrderDate(),
                  orderLocal.getUserName(), orderLocal.getShipToAddress(),
                  orderLocal.getCity(), orderLocal.getState(),
                  orderLocal.getCountry(), orderLocal.getZip(),
                  orderLocal.getPhone(), orderItems);

    } catch(Exception ex) {
      throw new OrderException(
                               "Unable to retrieve order information because " +
                               ex.getMessage(), "error.order.retrieval");
    }

    return order;
  }
  /**
   * This business method retrieves information for a given item
   *
   * @param <b>itemID</b> - ID of the item that has to be queried
   * @param <b>langID</b> - language in which the item information is required
   *
   * @return <b>String</b> - the item name
   *
   * @throws <b>OrderException</b> if the item could not be queried
   * @throws <b>RemoteException</b> container error
   */
  public String getItemName(String itemID, String langID)
                     throws OrderException {

    // Check if the language in which item information is required has been specified
    if(langID == null || "".equals(langID.trim())) {
      throw new OrderException(
                               "Language not specified", "error.order.nulllang");
    }

    // Check if item ID has been specified  
    if(itemID == null || "".equals(itemID.trim())) {
      throw new OrderException(
                               "Order item not specified",
                               "error.order.nullitemid");
    }

    String itemName = null;

    try {

      // Look up item entity
      ItemDetailLocalHome home = getItemDetailLocalHome();
      ItemDetailLocal item     =
        home.findByPrimaryKey(new ItemDetailPK(itemID, langID));
      itemName                 = item.getName();
    } catch(Exception ex) {
      throw new OrderException(
                               "Unable to retrieve item details because " +
                               ex.getMessage(), "error.order.itemretrieval");
    }

    return itemName;
  }
  /**
   * This business method changes the shipping address for a given order
   *
   * @param <b>address</b> - the new shipping address
   * @param <b>orderID</b> - id of the order for which the shipping address has
   *        to be modified
   *
   * @throws <b>OrderException</b> if the shipping address could not be changed
   * @throws <b>RemoteException</b> container error
   */
  public void changeShippingAddress(Address address, String orderID)
                             throws OrderException {

    // Check if a new address has been specified                                            
    if(address == null) {
      throw new OrderException(
                               "Address not specified",
                               "error.order.nulladdress");
    }

    // Check if order ID has been specified  
    if(orderID == null || "".equals(orderID.trim())) {
      throw new OrderException("Order not specified", "error.order.null");
    }

    try {

      // Look up order entity
      ProductOrderLocalHome home   = getOrderLocalHome();
      ProductOrderLocal orderLocal = home.findByPrimaryKey(orderID);

      OrderItemLocal item = null;

      /* Check if any of the items are already shipped. If shipped, the shipping
         address cannot be changed */
      for(
          Iterator ordItemsIter = orderLocal.getOrderItems().iterator();
            ordItemsIter.hasNext();) {
        item = (OrderItemLocal) ordItemsIter.next();

        if(!(Constants.PENDING.equalsIgnoreCase(item.getStatus()))) {
          throw new OrderException(
                                   "Item already shipped. Cannot change Shipping Address",
                                   "error.order.changeaddress");
        }
      }

      // Set the new shipping address
      orderLocal.setShipToAddress(address.getAddress());
      orderLocal.setCity(address.getCity());
      orderLocal.setState(address.getState());
      orderLocal.setZip(new Integer(address.getZip()));
      orderLocal.setCountry(address.getCountry());
      orderLocal.setPhone(address.getPhone());

    } catch(FinderException ex) {
      throw new OrderException(
                               "Unable to change shipping address because " +
                               ex.getMessage(), "error.order.addresschange");
    } catch(Exception ex) {
      throw new OrderException(
                               "Unable to change shipping address because " +
                               ex.getMessage(), "error.order.addresschange");
    }
  }
  /**
   * This business method retrieves the shipping address for a given order
   *
   * @param <b>orderID</b> - id of the order for which the shipping address has
   *        to be modified
   *
   * @return <b>address</b> - the shipping address
   *
   * @throws <b>OrderException</b> if the shipping address could not be
   *         retrieved
   * @throws <b>RemoteException</b> container error
   */
  public Address getShippingAddress(String orderID)
                             throws OrderException {

    // Check if the order ID has been specified
    if(orderID == null || "".equals(orderID.trim())) {
      throw new OrderException("Order not specified", "error.order.null");
    }

    Address address = null;

    try {

      // Look up order entity
      ProductOrderLocalHome home   = getOrderLocalHome();
      ProductOrderLocal orderLocal = home.findByPrimaryKey(orderID);

      /* Construct a new value object holding the shipping address for the
         specified order */
      address =
        new Address(
                    orderLocal.getShipToAddress(), orderLocal.getCity(),
                    orderLocal.getState(), orderLocal.getCountry(),
                    orderLocal.getZip().intValue(), orderLocal.getPhone());

    } catch(FinderException ex) {
      throw new OrderException(
                               "Unable to retrieve shipping address because " +
                               ex.getMessage(), "error.order.addressretrieval");
    } catch(Exception ex) {
      throw new OrderException(
                               "Unable to retrieve shipping address because " +
                               ex.getMessage(), "error.order.addressretrieval");
    }

    return address;
  }

  /*
   * Returns the local home for Order entity
   */
  private ProductOrderLocalHome getOrderLocalHome() {

    return (ProductOrderLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/OrderLocal");
  }

  /*
   * Returns the local home for OrderItem entity
   */
  private OrderItemLocalHome getOrderItemLocalHome() {

    return (OrderItemLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/OrderItemLocal");
  }

  /*
   * Returns the local home for Inventory entity
   */
  private ItemDetailLocalHome getItemDetailLocalHome() {

    return (ItemDetailLocalHome) ServiceLocator.getLocator().getService("java:comp/env/ejb/ItemDetailLocal");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -