Package oss.ngocminh.lego.persistence

Source Code of oss.ngocminh.lego.persistence.InvoiceDAO

package oss.ngocminh.lego.persistence;

import java.sql.SQLException;
import java.util.List;

import oss.ngocminh.lego.data.Entity;
import oss.ngocminh.lego.data.EntityImpl;
import oss.ngocminh.lego.data.Invoice;

public class InvoiceDAO extends AbstractDAO {

  public void save(Invoice invoice) throws SQLException {
    invoice.put("user_id", invoice.getUser().getId());
    save("Invoice", invoice);
    for (Entity order : invoice.getOrders()) {
      EntityImpl invoiceOrder = new EntityImpl();
      invoiceOrder.put("invoice_id", invoice.getId());
      invoiceOrder.put("order_id", order.getId());
      save("InvoiceOrder", invoiceOrder);
    }
  }

  public List<Invoice> list() throws SQLException {
    return execute("select * from Invoice", Invoice.class);
  }

  public Invoice findById(int id) throws SQLException {
    return executeUnique("select * from Invoice where id=?",
        Invoice.class, id);
  }

  public void fetchOrders(List<Invoice> invoices) throws SQLException {
    for (Invoice invoice : invoices) {
      fetchOrders(invoice);
    }
  }

  public void fetchOrders(Invoice invoice) throws SQLException {
    List<Entity> orders = execute("select * from Order where id in (" +
        "  select order_id from InvoiceOrder where invoice_id=?)",
        invoice.getId());
    invoice.put("orders", orders);
  }

}
TOP

Related Classes of oss.ngocminh.lego.persistence.InvoiceDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.