📄 itemactions.java
字号:
/*
* @author : Sujatha
* @Version : 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the File : ItemActions.java
* Creation/Modification History :
*
* Sujatha 16-Jan-2003 Created
*
*/
package oracle.otnsamples.vsm.actions.owner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.otnsamples.util.MessageCache;
import oracle.otnsamples.util.ServiceLocator;
import oracle.otnsamples.vsm.actions.forms.ItemForm;
import oracle.otnsamples.vsm.services.Administration;
import oracle.otnsamples.vsm.services.AdministrationHome;
import oracle.otnsamples.vsm.services.CategoryException;
import oracle.otnsamples.vsm.services.ItemException;
import oracle.otnsamples.vsm.services.ShopOwner;
import oracle.otnsamples.vsm.services.ShopOwnerHome;
import oracle.otnsamples.vsm.services.data.Category;
import oracle.otnsamples.vsm.services.data.Item;
import oracle.otnsamples.vsm.services.data.Shop;
import oracle.otnsamples.vsm.services.data.SubCategory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
/**
* This action class is used to manage - add/ update/ delete item information.
*
* @author Sujatha
* @version 1.0
*/
public class ItemActions extends DispatchAction {
/**
* This is the action called from the Struts framework to get the list of
* subcategories associated with the given shop. These subcategories will be
* listed in the add item form
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward addItemForm(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ActionForward forward = mapping.findForward("addItemForm");
ItemForm item = (ItemForm) form;
try {
String langID = request.getLocale().getLanguage();
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
String shopID = (String) request.getSession().getAttribute("shopID");
if(shopID != null) {
SubCategory[] subCategories = shopBean.getSubCategories(shopID, langID);
request.setAttribute("subcategories", subCategories);
}
} catch(CategoryException ex) {
servlet.log("AddItemFormError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("AddItemFormError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
/**
* This is the action called from the Struts framework to add an item to a
* shop
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward addItem(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
AdministrationHome adminHome = null;
Administration adminBean = null;
Category category = null;
Shop shop = null;
ItemForm item = (ItemForm) form;
ActionForward forward = mapping.findForward("displayItemAttr");
String langID = request.getLocale().getLanguage();
String shopID =
(String) request.getSession().getAttribute("shopID");
try {
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
// Upload the image for the item, if specified
FormFile imageFile = item.getImage();
if(imageFile.getFileName() != null &&
!"".equals(imageFile.getFileName())) {
String uploadDir =
servlet.getServletConfig().getInitParameter("UploadDir");
java.net.URL url =
servlet.getServletContext().getResource(uploadDir);
FileOutputStream out =
new FileOutputStream(url.getFile() + File.separator +
imageFile.getFileName());
out.write(imageFile.getFileData());
}
// Construct an item value object and call item service bean for item addition
String itemID =
shopBean.addItem(new Item(
null, item.getName(), item.getDesc(),
item.getSubCatID(), shopID,
Double.parseDouble(item.getUnitPrice()),
Integer.parseInt(item.getQuantity()),
Integer.parseInt(item.getReorderLevel()),
Integer.parseInt(item.getReorderQty()),
imageFile.getFileName(), langID, null));
/* Get the list of attributes associated with the item category to be
displayed in the item attributes addition form */
shop = shopBean.getShopDetails(shopID, langID);
adminHome =
(AdministrationHome) ServiceLocator.getLocator().getService("Administration");
adminBean = adminHome.create();
category = adminBean.findCategory(shop.getCategoryId(), langID);
request.setAttribute("categoryAttr", category.getAttributes());
// Add the newly added item's ID to the request scope
request.setAttribute("itemID", itemID);
} catch(ItemException ex) {
servlet.log("AddItemError", ex);
request.setAttribute(
"message",
MessageCache.getMessage(
ex.getMessageCode(),
getLocale(request)));
forward = mapping.findForward("addItemForm");
try {
SubCategory[] subCategories = shopBean.getSubCategories(shopID, langID);
request.setAttribute("subcategories", subCategories);
} catch(CategoryException cex) {
servlet.log("AddItemError", ex);
forward = mapping.findForward("error");
}
} catch(Exception ex) {
servlet.log("AddItemError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
}
/**
* This is the action called from the Struts framework to add item attributes
*
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward addItemAttr(
ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
ShopOwnerHome shopHome = null;
ShopOwner shopBean = null;
ActionForward forward = mapping.findForward("success");
try {
String langID = request.getLocale().getLanguage();
// Get all user parameters from the request
Enumeration names = request.getParameterNames();
String paramName = null;
Map attributes = new HashMap();
/* Retrieve attribute labels and attribute values from the request object
and add them to a Map */
while(names.hasMoreElements()) {
paramName = (String) names.nextElement();
if(
!paramName.equalsIgnoreCase("itemID") &&
!paramName.equalsIgnoreCase("command")) {
attributes.put(paramName, request.getParameter(paramName));
}
}
// Construct an item value object with the attributes information
Item item =
new Item(
request.getParameter("itemID"), null, null, null, null, 0.0, 0,
0, 0, null, langID, attributes);
shopHome =
(ShopOwnerHome) ServiceLocator.getLocator().getService("ShopOwner");
shopBean = shopHome.create();
shopBean.addItemAttributes(item);
request.setAttribute(
"message",
MessageCache.getMessage(
"message.additem.success",
getLocale(request)));
} catch(ItemException ex) {
servlet.log("AddItemAttributeError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} catch(Exception ex) {
servlet.log("AddItemAttributeError", ex);
ActionErrors errors = new ActionErrors();
errors.add("ItemError", new ActionError("common.error", ex.getMessage()));
saveErrors(request, errors);
return mapping.findForward("error");
} finally {
try {
if(shopBean != null) {
shopBean.remove();
}
} catch(Exception ex) {
}
}
return forward;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -