package dao;
import bo.Amplasament;
import bo.HibernateUtil;
import daoI.IAmplasamentDao;
import java.util.Collections;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class AmplasamentDao implements IAmplasamentDao {
Session session = null;
public Amplasament getAmplasamentByID(int amplasamentID) {
Amplasament amplasament = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
amplasament = (Amplasament) session.get(Amplasament.class, amplasamentID);
} catch (Exception e) {
e.printStackTrace();
}
return amplasament;
}
public List getAll() {
List<Amplasament> amplasamente = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament");
amplasamente = (List<Amplasament>) q.list();
Collections.reverse(amplasamente);
} catch (Exception e) {
e.printStackTrace();
}
return amplasamente;
}
public List getAmplasamenteByBeneficiarId(int beneficiarID) {
List<Amplasament> amplasamente = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament as amplasament where amplasament.beneficiar.idBeneficiar = '" + beneficiarID + "'");
amplasamente = (List<Amplasament>) q.list();
Collections.reverse(amplasamente);
} catch (Exception e) {
e.printStackTrace();
}
return amplasamente;
}
public void saveOrUpdate(Amplasament amplasament) {
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
if (amplasament.getIdAmplasament() == null) {
session.save(amplasament);
} else {
session.merge(amplasament);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(int idAmplasament) {
List<Amplasament> list = null;
Amplasament a = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament as amplasament where amplasament.idAmplasament='" + idAmplasament + "' ");
list = (List<Amplasament>) q.list();
a = ((Amplasament) list.get(0));
session.delete(a);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Amplasament> getAmplasamenteByProiectId(int proiectID) {
List<Amplasament> amplasamente = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament as amplasament where amplasament.idProiect = '" + proiectID + "'");
amplasamente = (List<Amplasament>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return amplasamente;
}
public Amplasament getLastAmplasament() {
List<Amplasament> amplasamente = null;
Amplasament amplasament = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament ");
amplasamente = (List<Amplasament>) q.list();
Collections.reverse(amplasamente);
amplasament = amplasamente.get(0);
} catch (Exception e) {
e.printStackTrace();
}
return amplasament;
}
public List getAmplasamenteByBeneficiarIdAndGeologId(int beneficiarID, int geologID) {
List<Amplasament> amplasamente = null;
try {
this.session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
session = (Session) HibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Amplasament as amplasament where amplasament.beneficiar.idBeneficiar = '" + beneficiarID + "'and amplasament.idGeolog='" + geologID + "'");
amplasamente = (List<Amplasament>) q.list();
Collections.reverse(amplasamente);
} catch (Exception e) {
e.printStackTrace();
}
return amplasamente;
}
}