session.removeAttribute(Util.ATTR_CHECKOUT);
session.removeAttribute(Util.ATTR_ORDERKEY);
}
// Store customer userid in HttpSession.
CustomerInfo customerInfo = login.getCustomerInfo(userid);
session.setAttribute(Util.ATTR_CUSTOMER, customerInfo);
Util.debug("updating=" + updating + "=");
// Was customer trying to edit account information.
if (updating.equals("true"))
{
req.setAttribute(Util.ATTR_EDITACCOUNTINFO, customerInfo);
requestDispatch( getServletConfig().getServletContext(),
req, resp, Util.PAGE_ACCOUNT );
}
else
{
// See if user was in the middle of checking out.
Boolean checkingOut = (Boolean) session.getAttribute(Util.ATTR_CHECKOUT);
Util.debug("checkingOut=" + checkingOut + "=");
if ((checkingOut != null) && (checkingOut.booleanValue()))
{
Util.debug("must be checking out");
requestDispatch( getServletConfig().getServletContext(),
req, resp, Util.PAGE_ORDERINFO);
}
else
{
Util.debug("must NOT be checking out");
String url;
String category = (String) session.getAttribute(Util.ATTR_CATEGORY);
// Default to plants
Util.debug("category : "+category);
if ((category == null) || (category.equals("null")))
{
url = Util.PAGE_PROMO;
}
else
{
url = Util.PAGE_SHOPPING;
req.setAttribute(Util.ATTR_INVITEMS,
catalog.getItemsByCategory(Integer.parseInt(category)));
}
requestDispatch( getServletConfig().getServletContext(),
req, resp, url);
}
}
}
}
catch (ServletException e)
{
e.printStackTrace();
req.setAttribute(Util.ATTR_RESULTS, "/nException occurred");
throw e;
}
catch (Exception e)
{
req.setAttribute(Util.ATTR_RESULTS, "/nException occurred");
e.printStackTrace();
throw new ServletException(e.getMessage());
}
}
else if (action.equals(ACTION_REGISTER))
{
// Register a new user.
// try
// {
String url;
HttpSession session = req.getSession(true);
String userid = req.getParameter("userid");
String password = req.getParameter("passwd");
String cpassword = req.getParameter("vpasswd");
String firstName = req.getParameter("fname");
String lastName = req.getParameter("lname");
String addr1 = req.getParameter("addr1");
String addr2 = req.getParameter("addr2");
String addrCity = req.getParameter("city");
String addrState = req.getParameter("state");
String addrZip = req.getParameter("zip");
String phone = req.getParameter("phone");
//validate all user input
//This could be done more eloquently using a framework such as Struts...
if (!Util.validateString(userid)){
req.setAttribute(Util.ATTR_RESULTS, "Email address contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(firstName)){
req.setAttribute(Util.ATTR_RESULTS, "First Name contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(lastName)){
req.setAttribute(Util.ATTR_RESULTS, "Last Name contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(addr1)){
req.setAttribute(Util.ATTR_RESULTS, "Address Line 1 contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(addr2)){
req.setAttribute(Util.ATTR_RESULTS, "Address Line 2 contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(addrCity)){
req.setAttribute(Util.ATTR_RESULTS, "City contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(addrState)){
req.setAttribute(Util.ATTR_RESULTS, "State contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(addrZip)){
req.setAttribute(Util.ATTR_RESULTS, "Zip contains invalid characters.");
url = Util.PAGE_REGISTER;
}
else if (!Util.validateString(phone)){
req.setAttribute(Util.ATTR_RESULTS, "Phone Number contains invalid characters.");
url = Util.PAGE_REGISTER;
}
// Make sure passwords match.
else if (!password.equals(cpassword))
{
req.setAttribute(Util.ATTR_RESULTS, "Passwords do not match.");
url = Util.PAGE_REGISTER;
}
else
{
// Create the new user.
CustomerInfo customerInfo =
login.createNewUser(userid, password, firstName,
lastName, addr1, addr2,
addrCity, addrState, addrZip, phone);
if (customerInfo != null)
{
// Store customer info in HttpSession.
session.setAttribute(Util.ATTR_CUSTOMER, customerInfo);
// See if user was in the middle of checking out.
Boolean checkingOut = (Boolean) session.getAttribute(Util.ATTR_CHECKOUT);
if ((checkingOut != null) && (checkingOut.booleanValue()))
{
url = Util.PAGE_ORDERINFO;
}
else
{
String category = (String) session.getAttribute(Util.ATTR_CATEGORY);
// Default to plants
if (category == null)
{
url = Util.PAGE_PROMO;
}
else
{
url = Util.PAGE_SHOPPING;
req.setAttribute(Util.ATTR_INVITEMS,
catalog.getItemsByCategory(Integer.parseInt(category)));
}
}
}
else
{
url = Util.PAGE_REGISTER;
req.setAttribute(Util.ATTR_RESULTS, "New user NOT created!");
}
}
requestDispatch( getServletConfig().getServletContext(),
req, resp, url);
// }
// catch (CreateException e) { }
}
else if (action.equals(ACTION_ACCOUNT))
{
String url;
HttpSession session = req.getSession(true);
CustomerInfo customerInfo = (CustomerInfo) session.getAttribute(Util.ATTR_CUSTOMER);
if (customerInfo == null)
{
url = Util.PAGE_LOGIN;
req.setAttribute(Util.ATTR_UPDATING, "true");
req.setAttribute(Util.ATTR_RESULTS, "\nYou must login first.");
}
else
{
url = Util.PAGE_ACCOUNT;
req.setAttribute(Util.ATTR_EDITACCOUNTINFO, customerInfo);
}
requestDispatch( getServletConfig().getServletContext(),
req, resp, url);
}
else if (action.equals(ACTION_ACCOUNTUPDATE))
{
// try
// {
String url;
HttpSession session = req.getSession(true);
CustomerInfo customerInfo = (CustomerInfo) session.getAttribute(Util.ATTR_CUSTOMER);
String userid = customerInfo.getCustomerID();
String firstName = req.getParameter("fname");
String lastName = req.getParameter("lname");
String addr1 = req.getParameter("addr1");
String addr2 = req.getParameter("addr2");
String addrCity = req.getParameter("city");