// This is the session bean (POJO) for the student-oriented web pages // It is created at the studentWelcome page, and if the user happens // on another page first, the user is forwarded to that page. // This bean holds the room number for the user across the various // page visits, making it (roomNo) a session variable. package pizza.presentation.web; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; import java.util.Set; import java.util.HashSet; import java.util.List; import pizza.domain.PizzaOrder; import pizza.domain.PizzaSize; import pizza.domain.Topping; import pizza.service.StudentService; import pizza.config.PizzaSystemConfig; import pizza.service.ServiceException; public class StudentBean { private Log log = LogFactory.getLog(getClass()); private StudentService studentService; // Properties used from student-oriented JSP pages-- // roomNo is the only real session variable (keeps state between requests in // this session bean) Set by param gen'd on two different pages. private int roomNo; // size and toppings are set and used by the orderPizza page, based // on params generated by the previous orderForm page private int sizeId; private Set toppingIds = new HashSet(); public StudentBean() { studentService = PizzaSystemConfig.getStudentService(); } public int getMaxRoomNo() { return PizzaSystemConfig.NUM_OF_ROOMS; } public int getRoomNo() { return roomNo; } public void setRoomNo(int roomNo) { this.roomNo = roomNo; } // size id comes in a parameter from the order form page. public void setSizeId(int sizeId) { this.sizeId = sizeId; } // topping ids come in one by one from the query string processing // after submission of the order form. public void setToppingId(int toppingId) { toppingIds.add(toppingId); } // Size, toppings and roomNo are already set from params // coming in to pizzaOrder page, where also this is called-- public String makeOrder() { String result = null; try { int id = studentService.makeOrder(roomNo, sizeId, toppingIds); result = "success, order number is " + id; } catch (ServiceException e) { result = "failure: "+ PizzaSystemConfig.reportException(log, e); } toppingIds.clear(); return result; } // get status for the current room, i.e., list of orders with status public List getStatus() { List status = null; try { status = studentService.getOrderStatus(roomNo); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); } return status; } // proxy the calls to get all sizes and all toppings // (we could use studentService directly from the .jsp, but that // would involve letting exceptions get to the .jsp. // It's better to do exception handling in java public List getAllSizes() { try { return studentService.getPizzaSizes(); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); return null; } } public List getAllToppings() { try { return studentService.getToppings(); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); return null; } } }