📄 printermanager.java
字号:
package oracle.otnsamples.AQ.Manager;
/**
* @author Anupama
* @version 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the Application : PrinterManager.java
* Creation/Modification History :
*
* Anupama 25-Jan-2002 Created
*
*/
// Servlet imports
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// Util imports
import java.util.Hashtable;
import java.util.Enumeration;
// Other files
import oracle.otnsamples.AQ.Helper.Product;
import oracle.otnsamples.AQ.Helper.ManufactureHelper;
/**
* This class is called for any operation performed by the user who
* logged in as Printer Manufacturer. Each method corresponds to one request
* from the manufacturer. Each method retrieves the data required by the
* Helper class either from the request/session and sends it to the Helper.
* The data retrieved from the Helper is stored in request or session as per
* the requirement.
*/
public class PrinterManager implements Manager {
/**
* SessionName to stores the session variable.
*/
HttpSession m_session;
/**
* Stores the user type who has logged in.
*/
String m_userType="";
/**
* Empty Constructor.
*/
public PrinterManager(){
}
/**
* This method is called after the manufacturer logged in successfully.
* The basic functionality of this method is to get the parameters from
* session, call the corresponding method in Helper to retrieve the
* products the manufacturer
* has from the database and stores them in session.
*
* @param p_request Holds the request object
* @exception Exception Incase of some error or unreported Exception
* @see ManufactureHelper.java
*/
public void displayProducts(HttpServletRequest p_request) throws Exception {
// Retrieves the parameters present in request
setParameters(p_request);
// Retrieve the details of the products the manufacture has in the form of
// a hash table and store it in session.
m_session.setAttribute("ProductsList",
ManufactureHelper.displayProducts(m_userType));
}
/**
* Empty method which is called when the manufacturer clicks on the Add button.
* Nothing is done here and the manufacturer is redirected to the page
* where he is asked to enter the details of the product to be added.
*
* @param p_request Holds the request object
* @exception Exception Incase of some error or unreported Exception
*/
public void addRequest(HttpServletRequest p_request) throws Exception {
}
/**
* This method is called if the manufacturer wants to update a product
* selected in the JSP page. the basic functionality of this method is to
* get the parameter(s) from session, retrieve the details of the product
* to be changed using the id of the product sent through request and send
* the details of the product thus retrieved back through request.
*
* @param p_request Holds the request object
* @exception Exception Incase of some error or unreported Exception
* @see ManufactureHelper.java
*/
public void updateRequest(HttpServletRequest p_request) throws Exception {
// Retrieves the parameters present in request
setParameters(p_request);
// Retrieve the ids of all the products the manufacture selected for update.
// Only one product id will be sent as only one product can be updated at
// a time.(Javascript present in the page checks to see that only one
// product is selected for update)
String[] productIds = p_request.getParameterValues("ProductsToBeChanged");
Product productObj = new Product();
// Get the details of all the products stored in session
Hashtable productsList = (Hashtable)m_session.getAttribute("ProductsList");
// Retrieve the details of the product selected using its id.
Enumeration enum = productsList.keys();
while(enum.hasMoreElements()){
Object key = enum.nextElement();
if(key.equals(productIds[0])){
productObj = (Product)productsList.get(key);
break;
}
}
// Store the details of the product to be updated in request.
p_request.setAttribute("UpdatePrdInfo",productObj);
}
/**
* This method is called if the manufacturer wants to add a product.
* The basic functionality of this method is to get the parameter(s)
* from session, store the details of the product
* entered in JSP page in database using the helper class and store the
* hash table returned containing the list of all products present in
* the database in session.
*
* @param p_request Holds the request object
* @exception Exception Incase of some error or unreported Exception
*/
public void addProduct(HttpServletRequest p_request) throws Exception {
// Retrieves the parameters present in request
setParameters(p_request);
// Retrieves the details of the product from the JSP page in the form of
// Product object.
Product product = getProductDetails(p_request);
// Calls the addProduct method present in Helper to add the product details
// in the database. If the product is added successfully
m_session.setAttribute("ProductsList",ManufactureHelper.addProduct(
m_userType,(Hashtable)m_session.getAttribute("ProductsList"),product));
infoSentToJSP("Add",product.getProductID(),p_request);
}
/**
* This method is called if the manufacturer wants to update a product.
* The basic functionality of this method is to get the parameter(s)
* from session and the product details from
* the JSP page and
* stores them in Product object. It then sends the object
* to the helper and stores the product list returned containing the
* details of all the products including the updated product in session.
*
* @exception Exception Incase of some error or unreported Exception
* @param p_request Holds the request object
* @see ManufactureHelper.java
* @see Product.java
*/
public void updateProduct(HttpServletRequest p_request) throws Exception {
// Retrieves the parameters present in request
setParameters(p_request);
// Gets the details of the product from JSP page using the method and stores
// it in Product object.
Product product = getProductDetails(p_request);
// Send the details of all the products present along with the details of
// the product to be updated to the Helper which updates it in the database
// as well as in the hash table and returns the hash table back. Store
// hash table in session.
m_session.setAttribute("ProductsList",ManufactureHelper.updateProduct(
m_userType,(Hashtable)m_session.getAttribute("ProductsList"),product));
infoSentToJSP("Update",product.getProductID(),p_request);
}
/**
* This method is called if the manufacturer wants to delete a product.
* The basic functionality of this method is to gets the parameter(s)
* from session. It then gets the details of all the products
* selected for deletion from their product ids using the hash table in session
* and sends them to Helper. At last it stores the product list returned
* from the helper after deleting the products in database inside session.
*
* @exception Exception Incase of some error or unreported Exception
* @param p_request Holds the request object
* @see Product.java
* @see ManufactureHelper.java
*/
public void deleteProducts(HttpServletRequest p_request) throws Exception {
// Retrieves the parameters present in request
setParameters(p_request);
// Retrieve the id of all products which are to be deleted.
String[] productIds = p_request.getParameterValues("ProductsToBeChanged");
int noOfPrds = productIds.length;
Product[] productsToBeDeleted = new Product[noOfPrds];
// Retrieve the details of all those products using their product ids
// from the product list present in session.
for(int i=0;i<noOfPrds;i++){
productsToBeDeleted[i] = getProductDetailsFromHash(productIds[i]);
}
// Send the details of the product to be deleted to the helper and store
// the returned updated product list in session.
m_session.setAttribute("ProductsList",ManufactureHelper.deleteProducts(
m_userType,(Hashtable)m_session.getAttribute("ProductsList"),
productsToBeDeleted));
infoSentToJSP("Delete",productIds,p_request);
}
/**
* Gets the details of the product entered in the JSP page using the
* request object and stores it in Product object.
*
* @param p_request Holds the request object
* @exception Exception Incase of some error or unreported Exception
* @return Complete product details got from request object
*/
private Product getProductDetails(HttpServletRequest p_request)
throws Exception{
Product product = new Product();
// If the id of the product is present then retrieve it. This condition
// is required because when a product is added its product id can't be
// retrieved from the JSP page.
if(p_request.getParameter("ProductId") != null){
product.setProductID(p_request.getParameter("ProductId"));
}
product.setProductName(p_request.getParameter("ProductName"));
// Get the manufacturerId from the Hash table present in session.
// If no products are present in session then null is returned.
product.setManufactureID(getManufacturerIDFromHash());
product.setProductDesc(p_request.getParameter("ProductDesc"));
product.setCategoryDesc(p_request.getParameter("CategoryDesc").trim());
product.setProductPrice(p_request.getParameter("Price"));
product.setQuantityOnHand(p_request.getParameter("Quantity"));
return product;
}
/**
* Retrieves the manufacturer id of the products. In this sample as the
* database contains the products sold by only one manufacturer the
* manufacturer id of the first product is retrieved and sent. If there are no
* no products in hash table then null is returned.
*
* @exception Exception Incase of some error or unreported Exception
* @return Manufacturer id of the products
*/
private String getManufacturerIDFromHash() throws Exception{
// Retrieve the details of all the products from session
Hashtable productsList = null;
// If no products are present in the hash table then return null
if(m_session.getAttribute("ProductsList")==null){
return null;
}else{
productsList = (Hashtable)m_session.getAttribute("ProductsList");
}
// If there are no products in hash table
if(productsList.size()==0){
return null;
}else{
Enumeration enum = productsList.keys();
Object key = enum.nextElement();
// Get the details of first product from the Hash
Product product = (Product)productsList.get(key);
// Retrieve the manufacturer id of the first product. As the database
// contains the products which only on manufacturer holds, the manufacturer
// id can be got using manufacturer id of any product.
return (product.getManufactureID());
}
}
/**
* Retrieves the session object and using that retrieves the
* user type of the person logged in.
*
* @param p_request Holds the request object
*/
private void setParameters(HttpServletRequest p_request){
// Get the already existing session object.
m_session = p_request.getSession(false);
// Retrieve the user type from session.
m_userType = (String)m_session.getAttribute("User");
}
/**
* Gets the details of the products from the product list stored in session
* using the product id sent.
*
* @param p_productId Id of the product whose details are required.
* @exception Exception Incase of some error or unreported Exception
* @return Details of the product whose id is sent.
*/
private Product getProductDetailsFromHash(String p_productId) throws Exception{
// Get the details of all the products from the session.
Hashtable productsList = (Hashtable)m_session.getAttribute("ProductsList");
Enumeration enum = productsList.keys();
Product product = new Product();
while(enum.hasMoreElements()){
Object key = enum.nextElement();
// If the product id sent is equal to the product id present in the
// list of products then store the product details.
if(key.equals(p_productId)){
product = (Product)productsList.get(key);
break;
}
}
return product;
}
/**
* This method helps to send the data required by the JSP
* out of the information sent to it. It stores the product
* ID sent to it in an String array and forwards it to
* the overloaded method.
*
* @exception Exception Incase of some error or unreported Exception
* @param p_action Tells what action was performed
* @param p_productId Id of the product whose details are changed.
* @param p_request Holds the request object
*/
private void infoSentToJSP(String p_action,String p_productId,
HttpServletRequest p_request) throws Exception{
infoSentToJSP(p_action,new String[]{p_productId},p_request);
}
/**
* This is an overloaded method. This method helps to send
* the data required by the JSP out of the information sent to it.
*
* @exception Exception Incase of some error or unreported Exception
* @param p_action Tells what action was performed
* @param p_productIdsArr Id of all the products whose details are
* deleted.
* @param p_request Holds the request object
*/
private void infoSentToJSP(String p_action,String[] p_productIdsArr,
HttpServletRequest p_request) throws Exception{
p_request.setAttribute("ProductIdArr",p_productIdsArr);
p_request.setAttribute("Action",p_action);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -