/*
* 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.ProviderDAO;
import dao.UserDAO;
import pojo.Product;
import pojo.Provider;
import pojo.User;
import util.HibernateUtil;
/**
*
* @author phuc
*/
public class ProviderDAOImpl implements ProviderDAO {
private static final Logger logger = Logger
.getLogger(ProviderDAOImpl.class);
// get all provider from database
public List<Provider> getListProvider() {
logger.debug("getListProvider start");
List<Provider> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Provider";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug("getListProvider success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getListProvider error : " + ex);
} finally {
session.close();
}
logger.debug("getListProvider end");
return ds;
}
// get top provider
public List<Provider> getTopProvider(int n) {
logger.debug("getTopProvider start");
List<Provider> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Provider";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug("getTopProvider success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getTopProvider error : " + ex);
} finally {
session.close();
}
logger.debug("getTopProvider end");
return ds;
}
// get infor of a provider by id
public Provider getInfoProvider(int id) {
logger.debug("getInfoProvider start");
Provider sp = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
sp = (Provider) session.get(Provider.class, id);
logger.debug("getInfoProvider success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getInfoProvider error : " + ex);
} finally {
session.close();
}
logger.debug("getInfoProvider end");
return sp;
}
// add a provider to databse
public boolean addProvider(Provider sp) {
logger.debug("addProvider start");
Session session = HibernateUtil.getSessionFactory().openSession();
ProviderDAO khDAO = new ProviderDAOImpl();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(sp);
transaction.commit();
logger.debug("addProvider success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addProvider error : " + ex);
} finally {
session.close();
}
logger.debug("addProvider end");
return true;
}
// delete ad provider by id
public boolean delProvider(int id) {
logger.debug("delProvider start");
Session session = HibernateUtil.getSessionFactory().openSession();
Provider u = (Provider) session.get(Provider.class, id);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.delete(u);
transaction.commit();
logger.debug("delProvider success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("delProvider error : " + ex);
} finally {
session.close();
}
logger.debug("delProvider end");
return true;
}
}