package pizza.presentation.web; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; import pizza.service.AdminService; import pizza.service.StudentService; import pizza.config.PizzaSystemConfig; import pizza.service.ServiceException; import java.util.List; import pizza.domain.PizzaOrder; import pizza.domain.PizzaSize; import pizza.domain.Topping; public class AdminBean { // log for this class (part of app log) private static Log log = LogFactory.getLog(AdminBean.class); private AdminService adminService; private StudentService studentService; // Note that we have no real session variables here // Properties used from JSP--only used for in-request communication // i.e., not real session variables holding state between requests private String command; // add or delete private String item; // topping or size ID // get service object ref at start of session for current database public AdminBean() { adminService = PizzaSystemConfig.getAdminService(); studentService = PizzaSystemConfig.getStudentService(); } //Mutators, called from scriptlets, to avoid using getters as mutators public String initDB() { String info = null; try { adminService.initializeDb(); log.info("initDB OK"); info = "OK"; } catch (ServiceException e) { info = "failed: " + PizzaSystemConfig.reportException(log, e); } return info; } public String makeNextOrderReady() { String info = null; try { adminService.markNextOrderReady(); info = "success"; } catch (ServiceException e) { info = "failed: " + PizzaSystemConfig.reportException(log, e); } return info; } public String advanceDay() { String info = null; try { adminService.advanceDay(); info = "success"; } catch (ServiceException e) { info = "failed: " + PizzaSystemConfig.reportException(log, e); } return info; } public String changeSize() { String info = ""; if (item != null) { // null command means textbox entry with submitted by browser if (command == null || command.equalsIgnoreCase("add")) { info = addPizzaSize(item); // item is name } else if (command.equalsIgnoreCase("remove")) { info = removeSize(item); // item is id } } reset(); return info; } public String changeTopping() { String info = ""; if (item != null) { // null command means textbox entry with submitted by browser if (command == null || command.equalsIgnoreCase("add")) { info = addTopping(item); // item is name } else if (command.equalsIgnoreCase("remove")) { info = removeTopping(item); // item is id } } reset(); return info; } private String addPizzaSize(String sizeName) { String result; try { adminService.addPizzaSize(sizeName); result = "Success"; } catch (ServiceException e) { result = "failed: " + PizzaSystemConfig.reportException(log, e); } return result; } private String addTopping(String topping) { String result; try { adminService.addTopping(topping); result = "Success"; } catch (ServiceException e) { result = "failed: " + PizzaSystemConfig.reportException(log, e); } return result; } private String removeSize(String sizeId) { String result; try { int id = Integer.parseInt(sizeId); adminService.deletePizzaSize(id); result = "Success"; } catch (ServiceException e) { result = "failed: " + PizzaSystemConfig.reportException(log, e); } return result; } private String removeTopping(String toppingId) { String result; try { int id = Integer.parseInt(toppingId); adminService.deleteTopping(id); result = "Success"; } catch (ServiceException e) { result = "failed: " + PizzaSystemConfig.reportException(log, e); } return result; } public void setItem(String item) { this.item = item; } public void setCommand(String command) { this.command = command; } // The next two methods are duplicated in StudentBean public List getAllSizes() { List allSizes = null; try { allSizes = studentService.getPizzaSizes(); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); } return allSizes; } public List getAllToppings() { List allToppings = null; try { allToppings = studentService.getToppings(); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); } return allToppings; } public List getDailyReport() { try { return adminService.getDailyReport(); } catch (ServiceException e) { PizzaSystemConfig.reportException(log, e); return null; } } public List getAdminReport() { try { return adminService.getAdminReport(); } catch (ServiceException e) { return null; } } private void reset() { command = null; item = null; } }