Advertisement
Guest User

Ozy

a guest
Aug 23rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package dao.H2Factory;
  2.  
  3. import customerproductorder.models.Customer;
  4. import customerproductorder.models.Order;
  5. import customerproductorder.models.Product;
  6. import dao.CustomerDaoInterface;
  7. import dao.DaoException;
  8. import dao.OrderDaoInterface;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17.  
  18. public class H2OrderDao implements OrderDaoInterface {
  19.  
  20.     private final ConnectionProvider connectionProvider;
  21.     private final CustomerDaoInterface customerDao;
  22.     private static final Logger LOG = LoggerFactory.getLogger(H2OrderDao.class);
  23.  
  24.     protected H2OrderDao(ConnectionProvider connectionProvider,
  25.             CustomerDaoInterface customerDao) {
  26.         this.connectionProvider = connectionProvider;
  27.         this.customerDao = customerDao;
  28.     }
  29.  
  30.     public void create(Order newOrder) throws DaoException {
  31.         try {
  32.             PreparedStatement st = connectionProvider.getConnection()
  33.                     .prepareStatement("insert into customerapplication."
  34.                             + "ordertable(customerid,orderdate) values(?,?)");
  35.             st.setInt(1, newOrder.getCustomer().getCardNumber());
  36.             st.setDate(2, new java.sql.Date(new Date().getDate()));
  37.             st.executeUpdate();
  38.             ResultSet pk = st.getGeneratedKeys();
  39.             long currentId = 0;
  40.             if (pk.next()) {
  41.                 currentId = pk.getLong(1);
  42.             }
  43.             for (Product product : newOrder.getProducts()) {
  44.                 st = connectionProvider.getConnection().prepareStatement(
  45.                         "set @idvar = ?; insert into customerapplication"
  46.                         + ".product_order(orderid,"
  47.                         + "productid) values(@idvar,?)");
  48.                 st.setLong(1, currentId);
  49.                 st.setInt(2, product.getProductId());
  50.                 st.executeUpdate();
  51.                 connectionProvider.destroy();
  52.             }
  53.         } catch (SQLException ex) {
  54.             LOG.error(ex.getSQLState());
  55.             throw new DaoException("Inserting data error", ex);
  56.         }
  57.     }
  58.  
  59.     public List<Order> getAll() throws DaoException {
  60.         List<Order> list = new ArrayList<Order>();
  61.         try {
  62.             PreparedStatement st = connectionProvider.getConnection()
  63.                     .prepareStatement("select * from customerapplication"
  64.                             + ".ordertable");
  65.             ResultSet rs = st.executeQuery();
  66.             list = this.convertToOrder(rs);
  67.             connectionProvider.destroy();
  68.         } catch (SQLException ex) {
  69.             LOG.error(ex.getSQLState());
  70.             throw new DaoException("Getting data error", ex);
  71.         }
  72.         return list;
  73.     }
  74.  
  75.     public void delete(int id) throws DaoException {
  76.         try {
  77.             PreparedStatement st = connectionProvider.getConnection()
  78.                     .prepareStatement("delete from customerapplication."
  79.                             + "ordertable where id=?;"
  80.                             + "delete from customerapplication."
  81.                             + "order_product where orderId=?");
  82.             st.setInt(1, id);
  83.             st.setInt(2, id);
  84.             st.executeUpdate();
  85.             connectionProvider.destroy();
  86.         } catch (SQLException ex) {
  87.             LOG.error(ex.getSQLState());
  88.             throw new DaoException("Deleting data error", ex);
  89.         }
  90.     }
  91.  
  92.     public void save(Order changedOrder) throws DaoException {
  93.         try {
  94.             //TODO implementation of inserting new products in order
  95.             PreparedStatement st = connectionProvider.getConnection()
  96.                     .prepareStatement("update customerapplication.ordertable"
  97.                             + "set orderdate=?,customerid=? where orderid=?");
  98.             st.setInt(1, changedOrder.getCustomer().getCardNumber());
  99.             st.setDate(2, new java.sql.Date(changedOrder.getOrderDate()
  100.                     .getDate()));
  101.             st.setInt(3, changedOrder.getOrderId());
  102.             st.executeUpdate();
  103.             connectionProvider.destroy();
  104.         } catch (SQLException ex) {
  105.             LOG.error(ex.getSQLState());
  106.             throw new DaoException("Saving error", ex);
  107.         }
  108.     }
  109.  
  110.     public List<Order> get(Customer customer) throws DaoException {
  111.         List<Order> list = new ArrayList<Order>();
  112.         try {
  113.             PreparedStatement st = connectionProvider.getConnection()
  114.                     .prepareStatement("select * "
  115.                             + "from customerapplication.ordertable where id=?");
  116.             ResultSet rs = st.executeQuery();
  117.             list = this.convertToOrder(rs);
  118.             connectionProvider.destroy();
  119.         } catch (SQLException ex) {
  120.             LOG.error(ex.getSQLState());
  121.             throw new DaoException("Getting data error", ex);
  122.         }
  123.         return list;
  124.     }
  125.  
  126.     public Order get(int id) throws DaoException {
  127.         //TODO Optional
  128.         Order gotOrder = null;
  129.         try {
  130.             PreparedStatement st = connectionProvider.getConnection()
  131.                     .prepareStatement("select * "
  132.                             + "from customerapplication.ordertable where id=?");
  133.             ResultSet rs = st.executeQuery();
  134.             gotOrder = this.convertToOrder(rs).get(0);
  135.             connectionProvider.destroy();
  136.         } catch (SQLException ex) {
  137.             LOG.error(ex.getSQLState());
  138.             throw new DaoException("Getting data error", ex);
  139.         }
  140.         return gotOrder;
  141.     }
  142.  
  143.     private List<Product> getOrderProducts(int orderId) throws SQLException, DaoException {
  144.         List<Product> list = new ArrayList<Product>();
  145.         PreparedStatement st = connectionProvider.getConnection()
  146.                 .prepareStatement("select "
  147.                         + "customerapplication.product.* from "
  148.                         + "customerapplication.product as product"
  149.                         + "inner join customerapplication.order_product"
  150.                         + "as order_product"
  151.                         + "on product.id = order_product.productid"
  152.                         + "where order_product.productid=?");
  153.         st.setInt(1, orderId);
  154.         ResultSet rs = st.executeQuery();
  155.         connectionProvider.destroy();
  156.         while (rs.next()) {
  157.             int tempId = rs.getInt("id");
  158.             String tempName = rs.getString("productname");
  159.             int tempCost = rs.getInt("productcost");
  160.             list.add(new Product(tempName, tempCost, tempId));
  161.         }
  162.         return list;
  163.     }
  164.  
  165.     private List<Order> convertToOrder(ResultSet rs) throws SQLException, DaoException {
  166.         List<Order> list = new ArrayList<Order>();
  167.         while (rs.next()) {
  168.             int tempId = rs.getInt("id");
  169.             Date tempDate = rs.getDate("orderdate");
  170.             Customer tempCustomer = customerDao
  171.                     .get(rs.getInt("customerid"));
  172.             list.add(new Order(tempCustomer, this
  173.                     .getOrderProducts(tempId), tempId, tempDate));
  174.         }
  175.         return list;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement