📄 shopownerbean.java
字号:
/*
* @author : Sujatha
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : ShopOwnerBean.java
* Creation/Modification History :
*
* Sujatha 30-Dec-2002 Created
*
*/
package oracle.otnsamples.vsm.services;
// Java Utility classes
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import oracle.otnsamples.util.KeyFactory;
import oracle.otnsamples.util.MailContent;
import oracle.otnsamples.util.Mailer;
import oracle.otnsamples.util.ServiceLocator;
import oracle.otnsamples.util.Utilities;
import oracle.otnsamples.vsm.Constants;
import oracle.otnsamples.vsm.entities.CustomerLocal;
import oracle.otnsamples.vsm.entities.CustomerLocalHome;
import oracle.otnsamples.vsm.entities.InventoryLocal;
import oracle.otnsamples.vsm.entities.InventoryLocalHome;
import oracle.otnsamples.vsm.entities.ItemAttributeLocal;
import oracle.otnsamples.vsm.entities.ItemAttributeLocalHome;
import oracle.otnsamples.vsm.entities.ItemDetailLocal;
import oracle.otnsamples.vsm.entities.ItemDetailLocalHome;
import oracle.otnsamples.vsm.entities.ItemLocal;
import oracle.otnsamples.vsm.entities.ItemLocalHome;
import oracle.otnsamples.vsm.entities.OrderItemLocal;
import oracle.otnsamples.vsm.entities.OrderItemLocalHome;
import oracle.otnsamples.vsm.entities.OrderItemPK;
import oracle.otnsamples.vsm.entities.ProductOrderLocal;
import oracle.otnsamples.vsm.entities.ProductOrderLocalHome;
import oracle.otnsamples.vsm.entities.ShopDetailLocal;
import oracle.otnsamples.vsm.entities.ShopDetailLocalHome;
import oracle.otnsamples.vsm.entities.ShopLocal;
import oracle.otnsamples.vsm.entities.ShopLocalHome;
import oracle.otnsamples.vsm.entities.SubCategoryLocal;
import oracle.otnsamples.vsm.entities.SubCategoryLocalHome;
import oracle.otnsamples.vsm.entities.SubCategoryPK;
import oracle.otnsamples.vsm.entities.ItemDetailPK;
import oracle.otnsamples.vsm.services.data.Item;
import oracle.otnsamples.vsm.services.data.OrderDetail;
import oracle.otnsamples.vsm.services.data.Shop;
import oracle.otnsamples.vsm.services.data.ShopDetail;
import oracle.otnsamples.vsm.services.data.SubCategory;
import oracle.otnsamples.vsm.entities.ShopDetailPK;
public class ShopOwnerBean implements SessionBean {
/**
* Initializes the bean
*/
public void ejbCreate() {
}
/**
* Container call back, called just after instance of this bean is activated
*/
public void ejbActivate() {
}
/**
* Container call back, called just before instance of this bean is passivated
*/
public void ejbPassivate() {
}
/**
* Container call back, called by container before it ends the life of the session object.
*/
public void ejbRemove() {
}
/**
* Set the associated session context.
* The container calls this method after the instance creation.
* @param <b>ctx</b> - A SessionContext interface for the instance.
*/
public void setSessionContext(SessionContext ctx) {
}
/**
* This business method adds a new item to a shop
* @param <b>item</b> - Information about the item to be added
* @throws <b>ItemException</b> if the item cannot be added
* @throws <b>RemoteException</b> container error
*/
public String addItem(Item item) throws ItemException {
// Check if item information has been specified
if( item == null ) {
throw new ItemException("Item data is null.","error.item.null");
}
try {
// Look up ItemsHome
ItemLocalHome itemHome = getItemLocalHome();
// Look up for items with the same name in the shop
Collection itemCollection = itemHome.findItemsInShop( item.getName(),
item.getShopID(),
item.getLangID() );
// If an item with the same name exists then thown an exception
if( itemCollection.size() > 0 ) {
throw new ItemException("Unable to add item as an item with the same " +
"name already exists in shop","error.item.duplicate");
}
// Item with same name doesnt exist in shop. Proceed with item creation
// Get a primary key for the item
String itemID = KeyFactory.getKey();
ItemDetailLocalHome itemDetailHome = getItemDetailLocalHome();
// Create a new item bean using the information in the value object
ItemLocal itemLocal = itemHome.create( itemID, item.getImage(),
item.getShopID(), item.getSubCatID() );
// Update item details
Collection itemCol = new ArrayList(1);
ItemDetailLocal itemDetail = itemDetailHome.create( itemID,
item.getLangID(),
item.getName(),
item.getUnitPrice(),
item.getDesc() );
itemCol.add(itemDetail);
itemLocal.setItemDetails(itemCol);
// Update inventory
InventoryLocalHome inventoryHome = getInventoryLocalHome();
InventoryLocal inventory = inventoryHome.create(itemID, item.getQuantity(),
item.getReorderLevel(),
item.getReorderQty());
itemLocal.setInventory(inventory);
return itemID; // Return the id of the newly added item
} catch( Exception ex ) {
if( ex instanceof ItemException )
throw new ItemException("Unable to add item as an item with the same " +
"name already exists in shop","error.item.duplicate");
else
throw new ItemException( "Unable to add item because " + ex.getMessage(),
"error.item.addition" );
}
}
/**
* This business method adds attributes to an item
* @param <b>item</b> - Information about the item and attributes to be added
* @throws <b>ItemException</b> if the item attributes cannot be added
* @throws <b>RemoteException</b> container error
*/
public void addItemAttributes(Item item) throws ItemException {
// Check if item information has been specified
if( item == null ) {
throw new ItemException("Item data is null.","error.item.null");
}
try {
// Look up the item
ItemLocalHome itemHome = getItemLocalHome();
ItemLocal itemLocal = itemHome.findByPrimaryKey(item.getID());
ItemAttributeLocalHome itemAttribHome = getItemAttributeLocalHome();
Map attributes = item.getAttributes();
List attribList = new ArrayList();
String label = null;
/* Loop through attributes in the value object and create a list of the
item attributes bean */
for( Iterator labels = attributes.keySet().iterator(); labels.hasNext(); ) {
label = (String)labels.next();
attribList.add(itemAttribHome.create(item.getID(), item.getLangID(),
label,(String)attributes.get(label)));
}
// Set the item attributes
itemLocal.setItemAttributes(attribList);
} catch( Exception ex ) {
throw new ItemException( "Unable to add item attributes because " +
ex.getMessage(), "error.item.attraddition" );
}
}
/**
* This business method deletes an item from a shop
* @param <b>itemID</b> - ID of the item to be removed
* @param <b>shopID</b> - ID of the shop from which the item has to be removed
* @throws <b>ItemException</b> if the item cannot be deleted
* @throws <b>RemoteException</b> container error
*/
public void deleteItem(String itemID, String shopID) throws ItemException {
// Check if item to be deleted has been specified
if( itemID == null || "".equals(itemID.trim()) ) {
throw new ItemException("Item data is null.","error.item.null");
}
// Check if shop from which the item has to be removed has been specified
if( shopID == null || "".equals(shopID.trim())) {
throw new ItemException("Item data is null.","error.shop.shopnull");
}
ItemLocalHome itemHome = null;
Iterator ordersIter = null;
try {
// Check if there are any pending orders for the shop
itemHome = getItemLocalHome();
ordersIter = getOrderLocalHome().findPendingOrdersForShop( shopID ).iterator();
} catch( FinderException ex ) {
// No orders for the shop. Continue with deleting.
}
try {
// Check if any orders pending on the item
List orderItems = null;
OrderDetail orderItem = null;
// Loop through the list of pending orders
while( ordersIter.hasNext() ) {
// Get the order items from the pending order
orderItems = (List)( (ProductOrderLocal)ordersIter.next() ).getOrderItems();
for( int i = orderItems.size() - 1; i >= 0; i-- ) {
orderItem = (OrderDetail)orderItems.get( i );
// Check if the item ID matches the ID of item to be deleted
if( orderItem.getItemID().equals(itemID) ) {
throw new ItemException("Unable to delete item as there are orders " +
"pending on it","error.item.pendingorders");
}
}
}
// Delete the item as there are no orders pending on it
ItemLocal item = (ItemLocal)itemHome.findByPrimaryKey( itemID );
item.remove();
} catch( Exception ex ) {
if(ex instanceof ItemException)
throw new ItemException("Unable to delete item as there are orders " +
"pending on it","error.item.pendingorders");
else
throw new ItemException( "Unable to delete item because " + ex.getMessage(),
"error.item.deletion" );
}
}
/**
* This business method searches a specified shop for items matching the keyword
* @param <b>keyword</b> - Search information given by user to look up an item
* @param <b>shopID</b> - ID of the shop from which the item has to be queried
* @param <b>langID</b> - Language in which the item information has to be retrieved
* @return <b>TreeMap</b> - Ordered Map of items matching the specified criterion
* @throws <b>ItemException</b> if the shop cannot be queried for items
* @throws <b>RemoteException</b> container error
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -