/*
* 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.UserDAO;
import pojo.Bill;
import pojo.MyInteger;
import pojo.User;
import util.HibernateUtil;
/**
*
* @author phuc
*/
public class UserDAOImpl implements UserDAO {
private static final Logger logger = Logger.getLogger(UserDAOImpl.class);
// get all user from database
public List<User> getListUser() {
List<User> ds = null;
logger.debug("getListUser start");
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from User";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug("getListUser success");
} catch (HibernateException ex) {
logger.error("getListUser error : " + ex);
System.err.println(ex);
} finally {
session.close();
}
logger.debug("getListUser end");
return ds;
}
// get list user in page, block;
public List<User> getListUser(int trang, int block, MyInteger soTrang) {
List<User> ds = null;
logger.debug("getListUser start");
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from User";
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((trang - 1) * block);
query.setMaxResults(block);
ds = query.list();
logger.debug("getListUser success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getListUser error : " + ex);
} finally {
session.close();
}
return ds;
}
// get info of a user
public User getUserInfo(String idUser) {
User sp = null;
logger.debug("getUserInfo start");
Session session = HibernateUtil.getSessionFactory().openSession();
try {
sp = (User) session.get(User.class, idUser);
logger.debug("getUserInfo success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getUserInfo error : " + ex);
} finally {
session.close();
}
logger.debug("getUserInfo end");
return sp;
}
// add a user to database
public boolean addUser(User u) {
logger.debug("addUser start");
Session session = HibernateUtil.getSessionFactory().openSession();
UserDAO khDAO = new UserDAOImpl();
if (khDAO.getUserInfo(u.getIduser()) != null) {
return false;
}
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(u);
transaction.commit();
logger.debug("addUser success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addUser error : " + ex);
} finally {
session.close();
}
logger.debug("addUser end");
return true;
}
// delete a user from database
public boolean delUser(String idUser) {
logger.debug("delUser start");
Session session = HibernateUtil.getSessionFactory().openSession();
User u = (User) session.get(User.class, idUser);
if (u == null) {
return false;
}
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.delete(u);
transaction.commit();
logger.debug("delUser success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("delUser error : " + ex);
} finally {
session.close();
}
logger.debug("delUser end");
return true;
}
// change state of a user, from not active to active
public boolean changeState(String id) {
logger.debug("changeState start");
Session session = HibernateUtil.getSessionFactory().openSession();
User user = (User) session.get(User.class, id);
session.close();
if (user == null) {
return false;
}
session = HibernateUtil.getSessionFactory().openSession();
if (user.getActivated() == 1) {
user.setActivated(0);
} else {
user.setActivated(1);
}
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.saveOrUpdate(user);
transaction.commit();
logger.debug("changeState success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("changeState error : " + ex);
} finally {
session.close();
}
logger.debug("changeState end");
return true;
}
// change role of a user, from user to admin or revert
public boolean changeRole(String id, String role) {
logger.debug("changeRole start");
Session session = HibernateUtil.getSessionFactory().openSession();
User user = (User) session.get(User.class, id);
session.close();
if (user == null) {
return false;
}
session = HibernateUtil.getSessionFactory().openSession();
user.setRole(role);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.saveOrUpdate(user);
transaction.commit();
logger.debug("changeRole success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("changeRole error : " + ex);
} finally {
session.close();
}
logger.debug("changeRole end");
return true;
}
}