📄 shopownerbean.java
字号:
*/
public TreeMap findItems(String keyword, String shopID, String langID) throws ItemException {
// Check if shop to be searched has been specified
if( shopID == null || "".equals(shopID.trim()) ) {
throw new ItemException("Insufficient information","error.shop.shopnull");
}
/* Check if language in which the item information needs to be queried has
been specified */
if( langID == null || "".equals(langID.trim()) ) {
throw new ItemException("Insufficient information","error.shop.langnull");
}
// TreeMap to hold item ID and item names
TreeMap itemList = new TreeMap();
try {
// Search for similar items by looking up the items bean
ItemLocalHome itemHome = getItemLocalHome();
Iterator itemsIter = itemHome.findItemsInShop( "%" + keyword + "%", shopID, langID ).iterator();
ItemLocal item = null;
String itemName = null;
// Loop through the items and add item id and name to treemap
ItemDetailLocal itemDetail = null;
ItemDetailLocalHome detailHome = getItemDetailLocalHome();
while( itemsIter.hasNext() ) {
item = (ItemLocal)itemsIter.next();
itemName= detailHome.findByPrimaryKey(new ItemDetailPK(item.getId(),langID)).getName();
//itemName = ((ItemDetailLocal)item.getItemDetails().iterator().next()).getName();
itemList.put(item.getId(), itemName );
}
} catch( Exception ex ) {
ex.printStackTrace();
throw new ItemException( "Unable to search for items because " +
ex.getMessage(), "error.item.search" );
}
return itemList;
}
/**
* This business method retrieves item information
* @param <b>itemID</b> - ID of the item that needs to be queried
* @param <b>langID</b> - Language in which information has to be queried
* @return <b>Item</b> - Details of the item
* @throws <b>ShopException</b> if the item cannot be queried
* @throws <b>RemoteException</b> container error
*/
public Item getItemDetails(String itemID, String langID) throws ItemException {
// Check if item ID has been specified
if( itemID == null || "".equals(itemID.trim()) ) {
throw new ItemException("Item data is null.","error.item.idnull");
}
// Check if language in which item details is required has been specified
if( langID == null || "".equals(langID.trim())) {
throw new ItemException("Language id is null.","error.shop.langnull");
}
Item itemInfo = null; // Value object to hold item information
try {
// Look up the item bean
ItemLocalHome itemHome = getItemLocalHome();
ItemLocal item = null;
ItemDetailLocal itemDetails = null;
ItemAttributeLocal itemAttribute = null;
HashMap attributes = new HashMap();
// Loop through the item records found
ItemDetailLocalHome detailHome = getItemDetailLocalHome();
for( Iterator itemsIter = itemHome.findByLanguage( itemID, langID ).iterator();
itemsIter.hasNext(); ) {
item = (ItemLocal)itemsIter.next();
// Get the item details
itemDetails = detailHome.findByPrimaryKey(new ItemDetailPK(item.getId(),langID));
// Loop through the item attributes and add them to a HashMap
for( Iterator itemAttributesIter = item.getItemAttributes().iterator();
itemAttributesIter.hasNext(); ) {
itemAttribute = (ItemAttributeLocal)itemAttributesIter.next();
//Check if the langid's are same. the check is needed because
// the CMR returns all detail entities for the selected master entity
if(langID.equals(itemAttribute.getLangID())){
attributes.put(itemAttribute.getLabel(), itemAttribute.getDescription());
}
}
// Construct the value object holding the item information
itemInfo = new Item( item.getId(), itemDetails.getName(),
itemDetails.getDescription(),
item.getSubCatID(), item.getShopID(),
itemDetails.getUnitPrice().doubleValue(),
((InventoryLocal)item.getInventory()).getQuantity().intValue(),
((InventoryLocal)item.getInventory()).getReorderLevel().intValue(),
((InventoryLocal)item.getInventory()).getReorderQty().intValue(),
item.getImage(), itemDetails.getLangID(), attributes);
}
} catch( Exception ex ) {
ex.printStackTrace();
throw new ItemException( "Unable to search for items because " +
ex.getMessage(), "error.item.retrival" );
}
return itemInfo;
}
/**
* The business method updates information about a shop
* @param <b>shop</b> - Information about the shop
* @throws <b>ShopException</b> if the shop cannot be updated
* @throws <b>RemoteException</b> container error
*/
public void updateShopDetails(Shop shop) throws ShopException {
// Check if shop information has been specified
if( shop == null) {
throw new ShopException("Shop information is null", "error.shop.null");
}
try {
// Look up the shop bean
ShopLocalHome shopHome = getShopLocalHome();
Collection shopCollection = shopHome.findShopByName( shop.getShopDetail(0).getShopName(),
shop.getShopDetail(0).getLangId());
ShopLocal shopLocal = null;
for( Iterator shopsIter = shopCollection.iterator();shopsIter.hasNext(); ) {
shopLocal = (ShopLocal)shopsIter.next();
// If a shop with the same name exists then return without editing the shop
if( !shopLocal.getId().equals(shop.getId())) {
throw new ShopException("Unable to update shop as a shop with the "+
"same name already exists in mall","error.shop.duplicate");
}
}
shopLocal = shopHome.findByPrimaryKey( shop.getId() );
Collection shopsList = shopLocal.getShopDetails(); // Fetch shop details
ShopDetailLocal shopDetail = null;
ShopDetailLocalHome shopDetailHome = getShopDetailLocalHome();
shopDetail = shopDetailHome.findByPrimaryKey(new ShopDetailPK(shop.getId()
,shop.getShopDetail(0).getLangId()));
shopDetail.remove();
// Create a new shopdetail bean with the new data
shopsList = new ArrayList(1);
shopsList.add(shopDetailHome.create(shop.getId(),
shop.getShopDetail(0).getLangId(),
shop.getShopDetail(0).getShopName(),
shop.getShopDetail(0).getDescription()));
// Add shopdetail to the shop
shopLocal.setShopDetails(shopsList);
} catch( ObjectNotFoundException ex ) {
ex.printStackTrace();
throw new ShopException( "Shop not found" , "error.shop.invalidshop" );
} catch( Exception ex ) {
ex.printStackTrace();
throw new ShopException( "Unable to update shop because " + ex.getMessage(),
"error.shop.updation" );
}
}
/**
* This business method updates an item
* @param <b>Item</b> - Item information to be updated
* @throws <b>ItemException</b> if the item cannot be updated
* @throws <b>RemoteException</b> container error
*/
public void updateItem(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 all items with a item name in the same shop and same langugage
ItemLocalHome itemHome = getItemLocalHome();
Collection itemCollection = itemHome.findItemsInShop( item.getName(),
item.getShopID(),
item.getLangID());
ItemLocal itemLocal = null;
ItemDetailLocal itemDetail = null;
ItemAttributeLocal itemAttribute = null;
for( Iterator itemsIter = itemCollection.iterator();itemsIter.hasNext(); ) {
itemLocal = (ItemLocal)itemsIter.next();
// If an item with the same name exists then return without editing the item
if( !itemLocal.getId().equals(item.getID())) {
throw new ItemException("Unable to update item as an item with the "+
"same name already exists in shop","error.item.duplicate");
}
}
// Proceed with item updation
itemLocal = (ItemLocal)itemHome.findByPrimaryKey( item.getID() );
itemLocal.setImage(item.getImage());
itemLocal.setShopID(item.getShopID());
itemLocal.setSubCatID(item.getSubCatID());
// Remove all item detail records associated with this item
ItemDetailLocalHome itemDetailHome = getItemDetailLocalHome();
itemDetail = itemDetailHome.findByPrimaryKey(new ItemDetailPK(
itemLocal.getId(),item.getLangID()));
itemDetail.remove();
// Add item detail information
ArrayList itemsList = new ArrayList(1);
itemsList.add(itemDetailHome.create( item.getID(), item.getLangID(),
item.getName(), item.getUnitPrice(),
item.getDesc()));
itemLocal.setItemDetails(itemsList);
// Update inventory
InventoryLocal inventory = itemLocal.getInventory();
inventory.setQuantity(new Integer(item.getQuantity()));
inventory.setReorderLevel(new Integer(item.getReorderLevel()));
inventory.setReorderQty(new Integer(item.getReorderQty()));
itemLocal.setInventory(inventory);
// Remove all item attributes associated with this item
for( Iterator itemDetailIter = itemLocal.getItemAttributes().iterator();
itemDetailIter.hasNext(); ) {
itemAttribute = (ItemAttributeLocal)itemDetailIter.next();
itemDetailIter.remove();
itemAttribute.remove();
}
// Add item attributes
ItemAttributeLocalHome itemAttributeHome = getItemAttributeLocalHome();
itemsList = new ArrayList();
Map attributes = item.getAttributes();
String label = null;
for( Iterator labels = attributes.keySet().iterator();labels.hasNext(); ) {
label = (String)labels.next();
itemsList.add(itemAttributeHome.create(item.getID(), item.getLangID(),
label,(String)attributes.get(label)));
}
itemLocal.setItemAttributes(itemsList);
} catch( Exception ex ) {
ex.printStackTrace();
if( ex instanceof ItemException )
throw new ItemException("Unable to update item as an item with the same "+
"name already exists in shop","error.item.duplicate");
else
throw new ItemException( "Unable to update item because " +
ex.getMessage(), "error.item.updation" );
}
}
/**
* This business method adds a subcategory
* @param <b>subCat</b> - Subcategory information to be added
* @throws <b>CategoryException</b> if the subcategory cannot be added
* @throws <b>RemoteException</b> container error
*/
public void addSubcategory(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();
// Check if a subcategory with the same name already exists
Collection subCatCollection = home.findByName( subCat.getName(),
subCat.getShopID(),
subCat.getLangID());
// If a subcategory with the same name exists then throw an exception
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -