📄 customerhelper.java
字号:
package oracle.otnsamples.AQ.Helper;
/**
* @author Rajat Gupta
* @version 1.0
*
* Name of the Application : CustomerHelper.java
* Development Environment : Oracle 9i JDeveloper
* Creation/Modification History :
*
* Rajat Gupta 15-Jan-2001 Created
*
*/
// Util Imports
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Date;
// Text Imports
import java.text.SimpleDateFormat;
// Other files
import oracle.otnsamples.AQ.DataBase.DBConnect;
/**
* This class is called for all the operations done by a Customer. Whether
* the customer wants to add, delete, update products from his shopping
* cart or order the products, all the requests go through this file.
* Every method in this class corresponds to a request from the customer.
* This class generates the query required for any of the operations to
* be done on the database. For IDAP calls, the SOAP message is also
* created in this class.
*/
public class CustomerHelper{
/**
* Empty Constructor
*/
public CustomerHelper(){
}
/**
* This method is called once when the customer successfully logs
* on to the Retail Shop. The basic functionality of this method
* is to display all the products that are available in the Retail
* Shop. This method returns a Hashtable which contains information
* about the products. The key in the Hashtable is a combination of
* the product ID and manufacturer ID. The corresponding value is
* a Product (Class) Object which contains various parameters about
* the product. The application can use the get() methods provided
* by the Product Class to get information about the product.
*
* @param p_user Type of User (Customer)
* @exception Exception In case of some unreported Exception
* @return Hashtable that contains all the products available for ordering
* by the customer
* @see Product.java
* @see DBConnect.java
*/
public static Hashtable displayProducts(String p_user) throws Exception{
StringBuffer query = new StringBuffer("select product_id, initcap(product_name), product_desc, ");
query.append("initcap(manufacturer_name), pm.manufacturer_id, price, category_desc, " );
query.append("quantity_on_hand from product_master pm, manufacturer_master mm " );
query.append("where pm.manufacturer_id = mm.manufacturer_id order by lower(product_name)");
// Get Instance and execute query
DBConnect dbInstance = DBConnect.getInstance();
Vector list = dbInstance.executeQuery(query.toString(), p_user);
// Add the products to the Hashtable
Hashtable productsList = new Hashtable();
String[] productArr;
int size = list.size();
for (int i=0; i < size; i++){
productArr = (String[])list.elementAt(i);
// Create a Product Class Object to add to the Hashtable
Product productObj =
new Product(productArr[0],productArr[1],productArr[2],productArr[3],
productArr[4],productArr[5],productArr[6], productArr[7]);
// Create the key
String key = productArr[0] + productArr[4];
productsList.put(key, productObj);
}
return productsList;
}
/**
* This method is called when the customer wants to update the quantity of
* some product in his shopping cart. This method assigns the new value of
* quantity to the product and calculates out the new price which is then
* set for the product.
*
* @exception Exception In case of some unreported Exception
* @param p_updatedItems String Array which holds the quantity entered for
* each product
* @param p_shoppingCart Shopping Cart of the Customer
* @return Updated Shopping Cart
* @see ShoppingCartProduct.java
*/
public static Hashtable updateShoppingCart(String[] p_updatedItems, Hashtable p_shoppingCart)
throws Exception{
int index = 0;
// Get all keys of the Hashtable in an enumeration
Enumeration enum = p_shoppingCart.keys();
while (enum.hasMoreElements()){
String quantity = p_updatedItems[index];
Object key = enum.nextElement();
// Get the object of ShoppingCartProduct
ShoppingCartProduct scProduct = (ShoppingCartProduct)p_shoppingCart.get(key);
// Set the new Quantity
scProduct.setQuantityOrdered(quantity);
// Calculate the new total price
int totalPrice = Integer.parseInt(scProduct.getProductPrice()) * Integer.parseInt(quantity);
// Set the new total price
scProduct.setTotalPrice(String.valueOf(totalPrice));
// Add to the shopping cart
p_shoppingCart.put(key,scProduct);
index++;
}
return p_shoppingCart;
}
/**
* This method is called if the customer wants to add single/multiple
* products to his shopping cart. This method creates an object of
* ShoppingCartProduct Class and stores it in the Hashtable i.e.
* the shopping cart.
*
* @param p_purchasedItems Products to add to the shopping cart
* @param p_availableProducts Available Products which a customer can
* order
* @param p_shoppingCart Shopping Cart of the Customer
* @exception Exception In case of some unreported Exception
* @return Updated Shopping Cart
* @see Product.java
* @see ShoppingCartProduct.java
*/
public static Hashtable addToShoppingCart(String[] p_purchasedItems, Hashtable p_availableProducts,
Hashtable p_shoppingCart) throws Exception{
int length = p_purchasedItems.length;
for (int i=0; i<length; i++){
// Get ID of the product to be added to the Shopping Cart
String id = p_purchasedItems[i];
// Create an object of ShoppingCartProduct and add it to the Shopping Cart
Product product = (Product)p_availableProducts.get(id);
ShoppingCartProduct scProduct = new ShoppingCartProduct(product, "1", product.getProductPrice());
String key = product.getProductID() + product.getManufactureID();
p_shoppingCart.put(key, scProduct);
}
return p_shoppingCart;
}
/**
* This method is called when a customer wants to delete single/multiple
* products from his shopping cart. It gets the ID of the product which
* has to be deleted and removes it from the shopping cart.
*
* @exception Exception In case of some unreported Exception
* @param p_deletedItems Products to be deleted from the shopping cart
* @param p_shoppingCart Customers Shopping Cart
* @return Customer's Shopping Cart
*/
public static Hashtable deleteFromShoppingCart(String[] p_deletedItems, Hashtable p_shoppingCart)
throws Exception{
int length = p_deletedItems.length;
for (int i=0; i<length; i++){
// Retrieve the ID to be deleted and remove it from the Shopping Cart
String id = p_deletedItems[i];
p_shoppingCart.remove(id);
}
return p_shoppingCart;
}
/**
* This method is called when the customer wants to order his/her shopping
* cart. This requirement is fulfilled using a feature Advanced Queuing
* called IDAP. The input to IDAP is a SOAP message which is generated here.
* In the SOAP message, we specify various parameters. The important ones
* are as follows :
* Destination - Name of the Queue where this information has to be stored
* Agent_Name - Name of the http agent who connects to the DataBase
* Message_Payload - Information to be stored
*
* @param p_shoppingCart Customer's Shopping Cart
* @param p_loginID Customers Login ID
* @exception Exception In case of some unreported Exception
* @return SOAP Message
*/
public static String createSOAPMessage(Hashtable p_shoppingCart, String p_loginID) throws Exception{
StringBuffer soapBuffer = new StringBuffer("<?xml version=\"1.0\"?>\n");
StringBuffer tempString = new StringBuffer();
int orderTotalPrice = 0;
soapBuffer.append(" <Envelope xmlns=\"http://ns.oracle.com/AQ/schemas/envelope\">\n");
soapBuffer.append(" <Body>\n <AQXmlSend xmlns = \"http://ns.oracle.com/AQ/schemas/access\">\n");
soapBuffer.append("<producer_options>\n");
soapBuffer.append(" <destination>AQCUSTOMER.CUSTOMERORDERQUEUE</destination>\n");
soapBuffer.append(" </producer_options>\n");
soapBuffer.append(" <message_set>\n <message_count>1</message_count>\n <message>");
soapBuffer.append("\n <message_number>1</message_number>\n");
soapBuffer.append(" <message_header>\n <correlation>1</correlation>\n ");
soapBuffer.append("<sender_id>\n <agent_name>admin</agent_name>\n");
soapBuffer.append(" </sender_id>\n </message_header>\n <message_payload>");
soapBuffer.append("\n <CUSTOMERORDERS>\n <CUSTOMER_ID>");
soapBuffer.append(p_loginID);
soapBuffer.append("</CUSTOMER_ID>\n <ORDER_DATE>");
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss");
String currDateTime = formatter.format(new Date());
soapBuffer.append(currDateTime + "</ORDER_DATE>\n <ORDER_TOTAL>");
Enumeration enum = p_shoppingCart.keys();
while (enum.hasMoreElements()){
Object key = enum.nextElement();
ShoppingCartProduct scProduct = (ShoppingCartProduct)p_shoppingCart.get(key);
tempString.append(" <ORDERS_ARRAY_ITEM_ITEM>\n <PRODUCT_ID>");
tempString.append(scProduct.getProductID());
tempString.append("</PRODUCT_ID>\n <MANUFACTURER_ID>");
tempString.append(scProduct.getManufactureID());
tempString.append("</MANUFACTURER_ID>\n <QUANTITY_ORDERED>");
tempString.append(scProduct.getQuantityOrdered());
tempString.append("</QUANTITY_ORDERED>\n </ORDERS_ARRAY_ITEM_ITEM>\n");
orderTotalPrice = orderTotalPrice + Integer.parseInt(scProduct.getTotalPrice());
}
soapBuffer.append(orderTotalPrice);
soapBuffer.append("</ORDER_TOTAL>\n <ORDERS_ARRAY>\n ");
soapBuffer.append("<ORDERS_ARRAY_ITEM>\n");
soapBuffer.append(tempString);
soapBuffer.append(" </ORDERS_ARRAY_ITEM>\n </ORDERS_ARRAY>");
soapBuffer.append("\n </CUSTOMERORDERS>\n </message_payload>\n");
soapBuffer.append(" </message>\n </message_set>\n <AQXmlCommit>");
soapBuffer.append("\n </AQXmlCommit>\n </AQXmlSend>\n </Body>\n </Envelope>");
return soapBuffer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -