Programming Assignment 2 Due October 25, 2016

Programming Assignment 2 Due October 25, 2016 Send your NetBeans project snapshot (with Subject: A2-EC FirstnameLastname) to [email protected]...
Author: Dustin McCarthy
0 downloads 2 Views 568KB Size
Programming Assignment 2 Due October 25, 2016 Send your NetBeans project snapshot (with Subject: A2-EC FirstnameLastname) to [email protected] Note: Your project snapshot includes the following project components only.

See the files view (Note: Project name is your project name) Note: Your NetBeans project name must be your first name initial followed by last name and A2 (for example gJungA2) References: This assignment is based on Java Persistence API (JPA) and stateless session Enterprise Java Beans (EJB), please refer to the lecture notes on JPA/EJB and JSTL/EL. You need to refer to the chapters of the reference R1 (AffableBean tutorial chapters 2, 4, 5, 6, 7, 8, 9, 10). Chapters 5 and 6 of the R1 explains how to design JSP View pages and shows how to use advanced JSTL/EL for developing applications based on MVC architecture. Web Application: In this programming assignment, you will design and implement Lehman College Underground Bistro web application based on MVC architecture (JSP pages as View, Servlets as Controller, Entity class/session EJBs as Model). Database access for your application is transparently performed by JPA and JTA. JSP pages will be developed based on EL/JSTL only.

Step 1: Download Assignment2Resources.zip from the web site Step 2: Database creation  Download createLehmanBistro.sql SQL script from the course web site and save it in a directory (e.g., /usr/home/gjung/MyDBs / or C:\MyDBs\). Change directory to MyDBs, and log in to mysql server. After you get the mysql prompt, type Mysql > source createLehmanBistro.sql; Note: fully qualified target SQL script file name can be used by the MySQL source command, in such a case you do not need to change directory to MyDBs (e.g., Mysql > source /usr/home/gjung/MyDBs/createLehmanBistro.sql; #UNIX Mysql > source C:/MyDBs/createLehmanBistro.sql; #Windows) 1

Step 3: Design the JSP view pages, Controller Servlet, and Admin Servlet (refer to the snapshot 10 of the AffableBean tutorial) Lehman Bistro web application home page (index.jsp)

URL (http://localhost:8080/LehmanBistro/category?3)

2

URL: (http://localhost:8080/LehmanBistro/viewCart)

3

LehmanBistro project WebPages layout

4

LehmanBistro img/categories and img/products layout

5

Note: The lehmanbistro.css is almost same as affablebean.css. You may revise the lehmanbistro.css as you design your view pages. The js folder has recent version of the jQuery javascript libraries including jQuery Validate zip folder (https://jquery.com/). The jQuery javascript libraries are available on the course web site (Assignment 3 section).

6

LehmanBistro Source Packages layout

Other folders of the LehmanBistro project are shown below (Note: glassfish-resources.xml is automatically created for accessing lehmanbistro database but it is not needed to build/deploy the web application)

7

Step 4: Create entity classes from the database and create session Façade EJBs from entities. AffableBean project uses OrderManager.java as an EJB for transaction management. For our LehmanBistro project, we will add additional methods for transaction management in session EJBs. Before we add additional business logic to the session EJBs, make sure your persistence.xml design view is like the one shown below. Note the PU name is your own project PU name (e.g., gJungA3ECPU). You must check Use Java Transaction APIs option. Because the current version of EclipseLink JPA provider API library is still unstable, it may generate unnecessary runtime errors (severity level Severe). To avoid unnecessary Bean validation runtime errors, check None option for Validation Strategy as shown below.

Add the following method in CustomerFacade //add addCustomer business method 8

public Customer addCustomer(String name, String email, String phone, String address, String ccNumber) { Customer customer = new Customer(); customer.setName(name); customer.setEmail(email); customer.setPhone(phone); customer.setAddress(address); // customer.setCityRegion(cityRegion); customer.setCcNumber(ccNumber); em.persist(customer); return customer; } Note: We do not have cityRegion property in our customer database table. JSP view pages of AffableBean project will be reused for your project, make sure view pages do not access cityRegion property.

Add the following methods in CustomerOrderFacade // overridden - refresh method called to retrieve order id from database public CustomerOrder find(Object id) { CustomerOrder order = em.find(CustomerOrder.class, id); em.refresh(order); return order; } // in this implementation, there is only one order per customer // the data model however allows for multiple orders per customer public CustomerOrder findByCustomer(Object customer) { return (CustomerOrder) em.createNamedQuery("CustomerOrder.findByCustomer").setParameter("customer", customer).getSingleResult(); } //add CustomerOrder addOrder business method public CustomerOrder addOrder(Customer customer, ShoppingCart cart) { // set up customer order CustomerOrder order = new CustomerOrder(); order.setCustomerId(customer); order.setAmount(BigDecimal.valueOf(cart.getTotal())); // create confirmation number Random random = new Random(); int i = random.nextInt(999999999); 9

order.setConfirmationNumber(i); em.persist(order); return order; } Add the following business methods in OrderedProductFacade //added findByOrderId business method public List findByOrderId(Object id) { return em.createNamedQuery("OrderedProduct.findByCustomerOrderId").setParameter("customerOrderId", id).getResultList(); } //added addOrderedItems business method public void addOrderedItems(CustomerOrder order, ShoppingCart cart) { em.flush(); List items = cart.getItems(); // iterate through shopping cart and create OrderedProducts for (ShoppingCartItem scItem : items) { int productId = scItem.getProduct().getId(); // set up primary key object OrderedProductPK orderedProductPK = new OrderedProductPK(); orderedProductPK.setCustomerOrderId(order.getId()); orderedProductPK.setProductId(productId); // create ordered item using PK object OrderedProduct orderedItem = new OrderedProduct(orderedProductPK); // set quantity orderedItem.setQuantity(scItem.getQuantity()); em.persist(orderedItem); } }

Step 5: Complete ShoppingCart.java, ShoppingCartitem.java, SessionTimeoutFilter.java, resources/messages.properties, and resources/messages_en.properties. You can reuse AffableBean project source code for completing ShoppingCart.java, ShoppingCartitem.java, and SessionTimeoutFilter.java. The resources/messages.properties and resources/messages_en.properties files are available on the course web site. 10

Step 6: In ControllerServlet, inject EJBs listed below. @EJB private CategoryFacade categoryFacade; @EJB private ProductFacade productFacade; @EJB private CustomerFacade customerFacade; @EJB private CustomerOrderFacade customerOrderFacade; @EJB private OrderedProductFacade orderedProductFacade;

Add the following two methods to the ControllerServlet as shown below. //added helper method private Map getOrderDetails(int orderId) { Map orderMap = new HashMap(); // get order CustomerOrder order = customerOrderFacade.find(orderId); // get customer Customer customer = order.getCustomerId(); // get all ordered products List orderedProducts = orderedProductFacade.findByOrderId(orderId); // get product details for ordered items List products = new ArrayList(); for (OrderedProduct op : orderedProducts) { Product p = (Product) productFacade.find(op.getOrderedProductPK().getProductId()); products.add(p); } // add each item to orderMap orderMap.put("orderRecord", order); orderMap.put("customer", customer); orderMap.put("orderedProducts", orderedProducts); orderMap.put("products", products); return orderMap; } //added helper method private int placeOrder(String name, String email, String phone, String address, String ccNumber, ShoppingCart cart) { 11

try { Customer customer = customerFacade.addCustomer(name, email, phone, address, ccNumber); CustomerOrder order = customerOrderFacade.addOrder(customer, cart); orderedProductFacade.addOrderedItems(order, cart); return order.getId(); } catch (Exception e) { //context.setRollbackOnly(); e.printStackTrace(); return 0; } }

Step 7: In AdminServlet, inject EJBs listed below. @EJB private CustomerFacade customerFacade; @EJB private CustomerOrderFacade customerOrderFacade; @EJB private OrderedProductFacade orderedProductFacade; @EJB private ProductFacade productFacade;

Add the following helper method private Map getOrderDetails(int orderId) { Map orderMap = new HashMap(); // get order CustomerOrder order = customerOrderFacade.find(orderId); // get customer Customer customer = order.getCustomerId(); // get all ordered products List orderedProducts = orderedProductFacade.findByOrderId(orderId); // get product details for ordered items List products = new ArrayList(); for (OrderedProduct op : orderedProducts) { Product p = (Product) productFacade.find(op.getOrderedProductPK().getProductId()); products.add(p); } 12

// add each item to orderMap orderMap.put("orderRecord", order); orderMap.put("customer", customer); orderMap.put("orderedProducts", orderedProducts); orderMap.put("products", products); return orderMap; } Step 8: Complete Web Pages folder. Refer to the JSP view pages source code in the AffableBean project snapshot 10. You need to slightly modify view pages including header.jspf and footer.jspf files. Images and lehman.css are available on our web site (in the Assignment2Resources.zip)

13