package dao;
import bo.Admin;
import daoI.IAdminDao;
import org.hibernate.Query;
import org.hibernate.Session;
import bo.HibernateUtil;
import java.security.MessageDigest;
import java.util.List;
public class AdminDao implements IAdminDao {
Session session = null;
public Admin adminExistent(String username, String parola) {
List<Admin> langList = null;
// incriptare parola
byte[] parolaB = parola.getBytes();
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(parolaB);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
parola = hexString + "";
} catch (Exception e) {
}
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Admin as admin where admin.username='" + username + "'and admin.parola='" + parola + "'");
langList = (List<Admin>) q.list();
tx.commit();
if (langList.size() > 0) {
return ((Admin) langList.get(0));
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Admin getAdminByID(int idAdmin) {
Admin admin = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
admin = (Admin) session.get(Admin.class, idAdmin);
} catch (Exception e) {
e.printStackTrace();
}
return admin;
}
public void saveOrUpdate(Admin admin) {
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
if (admin.getIdAdmin() == null) {
session.save(admin);
} else {
session.merge(admin);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}