package dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import bo.Geolog;
import bo.HibernateUtil;
import daoI.IGeologDao;
import java.security.MessageDigest;
public class GeologDao implements IGeologDao {
Session session = null;
public Geolog geologExistent(String user, String parola) {
List<Geolog> langList = null;
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 Geolog as geolog where geolog.username='" + user + "'and geolog.parola='" + parola + "'");
langList = (List<Geolog>) q.list();
tx.commit();
if (langList != null && langList.size() > 0) {
return ((Geolog) langList.get(0));
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<Geolog> geologiPublici() {
List<Geolog> langList = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Geolog as geolog where geolog.vizibilitate='da' and geolog.confirmare='acceptat'");
langList = (List<Geolog>) q.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
return langList;
}
public List getAll() {
List<Geolog> geologi = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Geolog as geolog where geolog.confirmare='" + "acceptat" + "'");
geologi = (List<Geolog>) q.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
return geologi;
}
public List<Geolog> getGeologiPtStergere() {
List<Geolog> geologi = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Geolog");
geologi = (List<Geolog>) q.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
return geologi;
}
public void saveOrUpdate(Geolog geolog) {
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
if (geolog.getIdGeolog() == null) {
session.save(geolog);
} else {
session.merge(geolog);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public List getAllNVGeo() {
List<Geolog> geologi = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Geolog as geolog where geolog.confirmare = 'in asteptare'");
geologi = (List<Geolog>) q.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
return geologi;
}
public Geolog getGeologByID(int idGeolog) {
Geolog geolog = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
geolog = (Geolog) session.get(Geolog.class, idGeolog);
} catch (Exception e) {
e.printStackTrace();
}
return geolog;
}
public void delete(int idGeolog) {
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Geolog geolog = (Geolog) session.get(Geolog.class, idGeolog);
session.delete(geolog);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}