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

📄 shopownerbean.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      if( subCatCollection.size() > 0 ) { 
        throw new CategoryException("Unable to add subcategory as a subcategory " +
          "with the same name already exists in shop","error.subcat.duplicate");
      }
      
      // Get a new primary key
      String subCatID = KeyFactory.getKey();
      // Create new sub category
      home.create( subCatID, subCat.getCatID(), subCat.getLangID(), 
                   subCat.getShopID(), subCat.getName());
      
    } catch (Exception ex) {
      if( ex instanceof CategoryException )
        throw new CategoryException("Unable to add subcategory as a subcategory " +
          "with the same name already exists in shop","error.subcat.duplicate");
      else
      throw new CategoryException("Unable to add sub category because " + 
                                   ex.getMessage(), "error.subcat.addition");
    }
  }

  /**
   * This business method updates a subcategory
   * @param <b>subCat</b> - Subcategory information to be updated
   * @throws <b>CategoryException</b> if the subcategory cannot be updated
   * @throws <b>RemoteException</b> container error
   */
  public void updateSubCategory(SubCategory subCat) throws CategoryException {

    // Check if subcategory information has been specified
    if( subCat == null ) {
      throw new CategoryException("SubCategory data is null", "error.subcat.null");
    }  
    try {
      // Find Entity home
      SubCategoryLocalHome home = getSubCategoryLocalHome();
      SubCategoryLocal subCategory = null; 
      
      // Look up for subcategories with the same name in the shop
      Collection subCatCollection = home.findByName( subCat.getName(), 
                                                     subCat.getShopID(),
                                                     subCat.getLangID());                               
      
      for( Iterator subCatIter = subCatCollection.iterator(); 
           subCatIter.hasNext(); ) {       
        subCategory = (SubCategoryLocal)subCatIter.next();
        
        // If a subcategory with the same name exists then throw an exception
        if( !subCategory.getSubCatID().equals(subCat.getId())) {
        throw new CategoryException("Unable to update subcategory as a " +
                     "subcategory with the same name already exists in shop",
                     "error.subcat.duplicate");
        }  
      }

      // Locate the subcategory bean to be updated
      subCategory = (SubCategoryLocal)home.findByPrimaryKey( 
                          new SubCategoryPK(subCat.getId(), subCat.getLangID()));      
                          
      // Update Sub Category name using the information in the value object
      subCategory.setSubCatName(subCat.getName());
    } catch (Exception ex) {
      if( ex instanceof CategoryException )
        throw new CategoryException("Unable to update subcategory as a " +
                     "subcategory with the same name already exists in shop",
                     "error.subcat.duplicate");
      else
        throw new CategoryException("Unable to update sub category because " + 
                                     ex.getMessage(), "error.subcat.updation");
    }  
  }

  /**
   * This business method deletes an subcategory from a shop
   * @param <b>subCatID</b> - ID of the subcategory to be removed
   * @param <b>shopID</b> - ID of the shop from which the subcategory has to be removed
   * @throws <b>CategoryException</b> if the subcategory cannot be deleted
   * @throws <b>RemoteException</b> container error
   */
  public void deleteSubCategory(String subCatID, String shopID ) 
              throws CategoryException {

    // Check if the subcategory to be deleted has been specified
    if( subCatID == null || "".equals(subCatID.trim()) ) {
      throw new CategoryException("Insufficient information", "error.subcat.null");          
    }

    /* Check if the shop from which the subcategory has to be deleted has been
       specified */
    if( shopID == null || "".equals(shopID.trim()) ) {
      throw new CategoryException("Insufficient information", "error.shop.shopnull");          
    }
    try {
      // Find all the orders pending for the specified shop
      Iterator ordersIter = 
            getOrderLocalHome().findPendingOrdersForShop( shopID ).iterator();
      Collection          orderItems = null;
      OrderItemLocal   orderItem  = null;
      ItemLocal     item       = null;      
      ItemLocalHome itemHome   = getItemLocalHome();           
      
      while( ordersIter.hasNext() ) {
        // Get the order items from the pending order
        orderItems = (Collection)( (ProductOrderLocal)ordersIter.next() ).getOrderItems();
        // Check if any order item has this subCatID 
        for( Iterator i = orderItems.iterator(); i.hasNext();) {
          orderItem = (OrderItemLocal)i.next();//orderItems.get( i );
          item = itemHome.findByPrimaryKey(orderItem.getItemID());
          // Check the subcategory of each order-item is the same as the
          //  subcategory specified 
          if( item.getSubCatID().equals(subCatID) ) {
            throw new CategoryException("Unable to delete sub category as there " +
                     "are orders pending on it","error.subcat.pendingorders");
          }
        }
      }
      // No pending orders with this subcategory - delete it
      SubCategoryLocalHome home  = getSubCategoryLocalHome();
      Collection subCategoryList = home.findByID( subCatID);
      for( Iterator subCat = subCategoryList.iterator(); subCat.hasNext(); ) {
        ((SubCategoryLocal)subCat.next()).remove();
      }  
      
    } catch( Exception ex ) {
      if( ex instanceof CategoryException )
            throw new CategoryException("Unable to delete sub category as there " +
                     "are orders pending on it","error.subcat.pendingorders");
      else    
        throw new CategoryException( "Unable to delete sub category because " + 
                                      ex.getMessage(),"error.subcat.delete" );
    }  
  }

  /**
   * This business method looks up all the sub-categories for a shop
   * @param <b>shopID</b> - ID of the shop whose subcategories are to be queried
   * @param <b>langID</b> - Language in which the subcategories have to be queried
   * @return <b>SubCategory[]</b>  - Array of sub-category value objects
   * @throws <b>CategoryException</b> if the subcategories cannot be looked up
   * @throws <b>RemoteException</b> container error
   */
  public SubCategory[] getSubCategories( String shopID, String langID ) 
                                         throws CategoryException {

    // Check if the shop whose subcategories are to be queried has been specified
    if( shopID == null || "".equals(shopID.trim()) ) {
      throw new CategoryException("Insufficient information", "error.shop.shopnull");                
    }

    // Check if the language in which the information is required has been specified
    if( langID == null || "".equals(langID.trim())) {
      throw new CategoryException("Insufficient information", "error.shop.langnull");          
    }
  
    List subCatList = new ArrayList(); // List to hold subcategory value objects
    try {

      // Find the list of subcategory available in the shop
      SubCategoryLocalHome home = getSubCategoryLocalHome();
      Iterator subCatIter = home.findByShopAndLanguage( shopID, langID ).iterator();
      
      SubCategoryLocal subCat = null;          
      
      /* Loop through the list of subcategories. Construct a value object for
         each of those and add them to a list */
      while(subCatIter.hasNext()) {
        subCat = (SubCategoryLocal)subCatIter.next();
        subCatList.add( new SubCategory(subCat.getSubCatID(), subCat.getShopID(),
                        subCat.getSubCatName(), subCat.getCatID(),
                        subCat.getLangID()));
      }
    } catch( Exception ex ) {
      throw new CategoryException( "Unable to get subcategory information because " 
                                   + ex.getMessage(), "error.subcat.retrieval" );
    }  
    return (SubCategory[]) subCatList.toArray( new SubCategory[subCatList.size()]);      
  }


  /**
   * This business method retrieves subcategory information
   * @param <b>subCatID</b> - ID of the subcategory that needs to be queried
   * @param <b>langID</b> - Language in which information has to be queried
   * @return <b>SubCategory</b>  - Details of the subcategory
   * @throws <b>ShopException</b> if the subcategory cannot be queried
   * @throws <b>RemoteException</b> container error
   */
  public SubCategory getSubCategory( String subCatID, String langID ) 
                                     throws CategoryException {

    // Check if the subcategory to be queried has been specified                                     
    if( subCatID == null || "".equals(subCatID.trim()) ) {
      throw new CategoryException("Insufficient information", "error.subcat.null");          
    }

   // Check if the language in which subcategory information is required has 
   // been specified 
    if( langID == null || "".equals(langID.trim())) {
      throw new CategoryException("Insufficient information", "error.shop.langnull");          
    }
  
    SubCategory subCategory = null; // Value object to hold subcategory information
    try {
      // Find the subcategory bean specified
      SubCategoryLocalHome home = getSubCategoryLocalHome();
      SubCategoryLocal subCat =  home.findByPrimaryKey(new SubCategoryPK(subCatID, langID));

      // Construct a value object having the subcategory information
      subCategory = new SubCategory(subCat.getSubCatID(), subCat.getShopID(),
                        subCat.getSubCatName(), subCat.getCatID(),
                        subCat.getLangID());
    } catch( Exception ex ) {
      throw new CategoryException( "Unable to get subcategory information because " 
                                   + ex.getMessage(), "error.subcat.retrieval" );
    }  
    return subCategory;      
  }

  /**
   * This business method ship the pending orders
   * @param <b>orderIDs</b> - List of order IDs that need to be shipped
   * @param <b>orderItemIDs</b> - List of order items that need to be shipped
   * @throws <b>OrderException</b> if the order item status cannot be modified
   * @throws <b>RemoteException</b> container error
   */
  public void manageOrders( List orderDetails ) 
                            throws OrderException {

    // Check if the list of orders and order items to be shipped have been specified
    if( orderDetails == null || orderDetails.size() == 0 ) {
      throw new OrderException("No orders specified for shipment", "error.order.null");          
    }
    try {

      // Look up the orders bean
      OrderItemLocalHome home = getOrderItemLocalHome();
      OrderItemLocal orderItem = null;
      InventoryLocalHome inventoryHome = getInventoryLocalHome();
      InventoryLocal inventory = null;

      OrderDetail orderDetail = null;
      // Set the status of each order item specified to shipped
      for( int i=0; i< orderDetails.size(); i++) {

        orderDetail = (OrderDetail)orderDetails.get(i);
        // find inventory for the item
        inventory = inventoryHome.findByPrimaryKey( orderDetail.getItemID() );

        orderItem = 
            home.findByPrimaryKey( new OrderItemPK(orderDetail.getItemID(), 
                                                 orderDetail.getOrderID()));
        // If order needs to be shipped,try to update the inventory
        if(Constants.SHIPPED.equals(orderDetail.getStatus())){
          // if the inventory is less than requested quantity raise error
          try{
            inventory.purchase( orderDetail.getQuantity());
            orderItem.setStatus(Constants.SHIPPED);
          } catch( InventoryException ex ) {
            orderItem.setStatus(Constants.IN_PROGRESS);                
          }        
        }else{
          // else, change the order status to the selected status
          orderItem.setStatus(orderDetail.getStatus());
        }
      }

    } catch( Exception ex ) {
      throw new OrderException( "Unable to alter order status because " + 
                                 ex.getMessage(), "error.order.manage" );
    }  
  }

  /**
   * This business method look up the details for a given shop

⌨️ 快捷键说明

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