Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package per.mnn.controller; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.Date; import java.util.Random; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Session; import org.hibernate.Transaction; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import per.mnn.cart.ShoppingCart; import per.mnn.cart.ShoppingCartItem; import per.mnn.hibernate.Customer; import per.mnn.hibernate.CustomerOrder; import per.mnn.hibernate.HibernateUtil; import per.mnn.hibernate.OrderedProduct; import per.mnn.hibernate.OrderedProductId; /** * * @author mnn */ @Controller public class CheckoutController { @RequestMapping(value = "/checkout") public ModelAndView checkout() { return new ModelAndView("checkout"); } @RequestMapping(value = "/confirm", method = RequestMethod.POST) public ModelAndView confirm(HttpServletRequest request, HttpServletResponse respond) throws UnsupportedEncodingException { request.setCharacterEncoding("UTF-8"); ModelAndView retval = new ModelAndView("confirmation"); String name = request.getParameter("name"); String email = request.getParameter("email"); String phone = request.getParameter("phone"); String address = request.getParameter("address"); String cityRegion = request.getParameter("cityRegion"); String creditcard = request.getParameter("creditcard"); if (name == null || name.isEmpty()) { retval.addObject("nameError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (email == null || email.isEmpty()) { retval.addObject("emailError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (phone == null || phone.isEmpty()) { retval.addObject("phoneError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (address == null || address.isEmpty()) { retval.addObject("addressError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (cityRegion == null || cityRegion.isEmpty()) { retval.addObject("cityRegionError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (creditcard == null || creditcard.isEmpty()) { retval.addObject("creditcardError", "true"); retval.addObject("validationErrorFlag", "true"); retval.setViewName("checkout"); } if (retval.getViewName() == "confirmation") { ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute("cart"); Session sess = HibernateUtil.getSessionFactory().openSession(); Transaction tran = sess.beginTransaction(); Customer customer = new Customer(name, email, phone, address, cityRegion, creditcard); sess.save(customer); CustomerOrder order = new CustomerOrder(); order.setCustomer(customer); order.setAmount(BigDecimal.valueOf(cart.getTotal())); order.setDateCreated(new Date()); order.setConfirmationNumber(new Random().nextInt(999999999)); sess.save(order); for (ShoppingCartItem item : cart.getItems()) { OrderedProductId id = new OrderedProductId(order.getId(), item.getProduct().getId()); OrderedProduct orderedProduct = new OrderedProduct(); orderedProduct.setId(id); orderedProduct.setCustomerOrder(order); orderedProduct.setProduct(item.getProduct()); orderedProduct.setQuantity((short) item.getQuantity()); sess.save(orderedProduct); order.getOrderedProducts().add(orderedProduct); } tran.commit(); sess.close(); cart.clear(); request.getSession().invalidate(); retval.addObject("customer", customer); retval.addObject("orderRecord", order); retval.addObject("orderedProducts", order.getOrderedProducts()); } return retval; } }