/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao_impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import dao.BillDAO;
import dao.DeliverstatusDAO;
import pojo.Bill;
import pojo.Billdetail;
import pojo.Deliverstatus;
import pojo.MyInteger;
import util.HibernateUtil;
/**
*
* @author phuc
*/
public class BillDAOImpl implements BillDAO {
private static final Logger logger = Logger.getLogger(BillDAOImpl.class);
//get all bill from database
public List<Bill> getListBill() {
logger.debug(" getListBill start");
List<Bill> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Bill";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug(" getListBill success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error(" getListBill error : " + ex);
} finally {
session.close();
}
logger.debug(" getListBill end");
return ds;
}
//get list bill by page, block, index
public List<Bill> getListBill(int page, int block, MyInteger soTrang) {
logger.debug("getListBill by page, block, index start");
List<Bill> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Bill";
Query query = session.createQuery(hql);
int size = query.list().size();
int nPage = size / block;
if (nPage == 0 || (size % block != 0))
nPage++;
soTrang.setValue(nPage);
query.setFirstResult((page - 1) * block);
query.setMaxResults(block);
ds = query.list();
logger.debug("getListBill by page, block, index success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error(" getListBill by page, block, index error : " + ex);
} finally {
session.close();
}
logger.debug(" getListBill by page, block, index end");
return ds;
}
//get list bill by idKhachhang
public List<Bill> getListBill(String idKhachHang) {
logger.debug("getListBill by idKhachHang start");
List<Bill> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Bill hd WHERE hd.iduser=:idKhachHang";
Query query = session.createQuery(hql);
query.setString("idKhachHang", idKhachHang);
ds = query.list();
logger.debug("getListBill by idKhachHang success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error(" getListBill by idKhachHang error : " + ex);
} finally {
session.close();
}
logger.debug("getListBill by idKhachHang end");
return ds;
}
//get bill info by idBill
public Bill getBillInfo(int idBill) {
logger.debug("getBillInfo by idBill start");
Bill bill = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
bill = (Bill) session.get(Bill.class, idBill);
logger.debug("getBillInfo by idBill success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getBillInfo by idBill : " + ex);
} finally {
session.close();
}
logger.debug("getBillInfo by idBill end");
return bill;
}
//add bill by Bill
public boolean addBill(Bill bill) {
logger.debug("addBill by Bill start");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(bill);
transaction.commit();
logger.debug("addBill by Bill success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addBill by Bill error: " + ex);
} finally {
session.close();
}
logger.debug("addBill by Bill end");
return true;
}
//add bill by Billdetail
public boolean addBill(Billdetail bill) {
logger.debug("addBill by Billdetail start");
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.saveOrUpdate(bill);
transaction.commit();
logger.debug("addBill by Billdetail success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addBill by Billdetail error: " + ex);
} finally {
session.close();
}
logger.debug("addBill by Billdetail end");
return true;
}
//delete Bill
public boolean delBill(int idBill) {
logger.debug("delBill start");
Session session = HibernateUtil.getSessionFactory().openSession();
Bill bill = (Bill) session.get(Bill.class, idBill);
if (bill == null) {
return false;
}
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.delete(bill);
transaction.commit();
logger.debug("delBill success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("delBill error: " + ex);
} finally {
session.close();
}
logger.debug("delBill end");
return true;
}
//update Bill
public boolean updateBill(Integer idBill, Integer idstatus) {
logger.debug("updateBill start");
Session session = HibernateUtil.getSessionFactory().openSession();
Bill bill = (Bill) session.get(Bill.class, idBill);
session.save(bill);
session.close();
if (bill == null) {
return false;
}
session = HibernateUtil.getSessionFactory().openSession();
bill.getDeliverstatus().setIdstatus(idstatus);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.saveOrUpdate(bill);
transaction.commit();
logger.debug("updateBill success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("updateBill error: " + ex);
} finally {
session.close();
}
logger.debug("updateBill end");
return true;
}
}