CS636 Pizza1's DAO API, plus sample methods

DAO methods:

topcat$ grep public *DAO.java

      
AdminDAO.java:public class AdminDAO {
AdminDAO.java:  public AdminDAO(DbDAO db) throws SQLException {
AdminDAO.java:  public void advanceDay() throws SQLException
AdminDAO.java:  public int findCurrentDay() throws SQLException
DbDAO.java:public class DbDAO {
DbDAO.java:     public DbDAO(String dbUrl, String usr, String passwd) throws SQLException {
DbDAO.java:     public void close() throws SQLException {
DbDAO.java:     public void initializeDb() throws SQLException {
MenuDAO.java:public class MenuDAO {
MenuDAO.java:   public MenuDAO(DbDAO db) throws SQLException {
MenuDAO.java:   public MenuSize findMenuSize(String name) throws SQLException
MenuDAO.java:   public Set<MenuSize> findMenuSizes() throws SQLException
MenuDAO.java:   public MenuTopping findMenuTopping(String name) throws SQLException
MenuDAO.java:   public Set<MenuTopping> findMenuToppings() throws SQLException {
MenuDAO.java:   public void createMenuTopping(String toppingName) throws SQLException {
MenuDAO.java:   public void createMenuSize(String sizeName) throws SQLException {
MenuDAO.java:   public void deleteMenuTopping(String topping) throws SQLException
MenuDAO.java:   public void deleteMenuSize(String size) throws SQLException
PizzaOrderDAO.java:public class PizzaOrderDAO {
PizzaOrderDAO.java:     public PizzaOrderDAO(DbDAO db) throws SQLException {
PizzaOrderDAO.java:     public void insertOrder(PizzaOrder order) throws SQLException {
PizzaOrderDAO.java:     public List<PizzaOrder> findOrdersByRoom(int roomNumber, int day) throws SQLException
PizzaOrderDAO.java:     public int findFirstOrder(int status) throws SQLException
PizzaOrderDAO.java:     public void updateOrderStatus(int ordNo, int newStatus) throws SQLException
PizzaOrderDAO.java:     public List<PizzaOrder> findOrdersByDays(int day1, int day2) throws SQLException {

      S

These throw SQLException on errors, but they first carefully close the statement in a finally clause.
They use a private connection object set up at the program start for reuse--typical client-server use of longer-lived connection.

   // find all active toppings
   public Set<MenuTopping> findMenuToppings() throws SQLException {
      Set<MenuTopping> toppings = new TreeSet<MenuTopping>();

      Statement stmt = connection.createStatement();
      try {
            ResultSet set = stmt.executeQuery("select * from " + MENU_TOPPING_TABLE);
            while (set.next())
              toppings.add(new MenuTopping(set.getInt("id"), set.getString("topping_name")));
      } finally {
            stmt.close();
      }
      return toppings;
   }

Another DAO method, from AdminDAO.java:

   
   public void advanceDay() throws SQLException
   {
       Statement stmt = connection.createStatement();
       try {
           stmt.executeUpdate("update " + SYS_TABLE
                               + " set current_day = current_day + 1 ");
       } finally {
           stmt.close();
       }
   } 

Sample Service methods

Here, adminDAO is an object of type AdminDAO. This method is in AdminService.java.
    public void addTopping(String name) throws ServiceException {
      try {
         menuDAO.createMenuTopping(name);
      } catch (SQLException e) {
          throw new ServiceException("Topping was not added successfully: ",  e);
      }
   } 

Note no transactions yet, and this one pretty much passes the buck to the DAO.
Here's a service-layer method that actually does "business logic":  the DAO does the basic steps, following the direction of this code.

   public void markNextOrderReady() throws ServiceException {
     try {
        int ordNo = pizzaOrderDAO.findFirstOrder(PizzaOrder.PREPARING);
        pizzaOrderDAO.updateOrderStatus(ordNo, PizzaOrder.BAKED);
     } catch (SQLException e) {
        throw new ServiceException("Error in marking the next order ready", e);
     }
   }  

Sample Presentation code: from TakeOrder.java

Here, adminService is an object of type AdminService, and studentService is of type StudentService, as you might have guessed.

               
   System.out.println("Available pizza toppings to choose from:");
   Set<String> allToppings = studentService.getToppingNames(); 
...
   studentService.makeOrder(roomNum, chosenSizeName, chosenToppings);

from ShopAdmin.java:

        adminService.addTopping(PresentationUtils.readEntry(in,
                                        "Enter the topping Name"));

You can see that the presentation layer code needs references to the studentService and adminService objects to do its work, while the service layer similarly needs DAO object refs. The DAO code uses JDBC, so it needs the Connection object.