1 package org.objectweb.rentacar.persistance.dao; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Set ; 8 9 import org.hibernate.Session; 10 import org.objectweb.rentacar.persistance.bo.Car; 11 import org.objectweb.rentacar.persistance.bo.Reservation; 12 import org.objectweb.rentacar.persistance.util.HibernateUtil; 13 14 19 public class ReservationDAO extends GenericDAO { 20 21 private static ReservationDAO theInstance; 22 23 private ReservationDAO(){ 24 25 } 26 27 public static synchronized ReservationDAO getInstance() { 28 if (null == theInstance) { 29 theInstance = new ReservationDAO(); 30 } 31 return theInstance; 32 } 33 34 35 42 public List <Reservation> getReservationByDates(Date start, Date end) throws DAOException { 43 Session session = null; 44 try{ 45 session = HibernateUtil.getSession(); 46 }catch (ExceptionInInitializerError e){ 47 throw new DAOException("Error in Hibernate Session instanciation", e); 48 }catch (Throwable e){ 49 throw new DAOException("Error in Hibernate Session instanciation", e); 50 } 51 org.hibernate.Query q = session.createQuery("select r from Reservation r where (" + 52 "(r.endingDate <= :end AND r.endingDate >= :start) OR (r.startingDate <= :end AND r.startingDate >= :start))"); 53 q.setDate("start",start); 54 q.setDate("end",end); 55 List <Reservation >result = new ArrayList <Reservation>(); 56 for (Iterator iter = q.list().iterator();iter.hasNext();) { 57 result.add((Reservation) iter.next()); 58 } 59 return result; 60 } 61 62 68 public void updateReservationCars(String reservationId, Set <Car> newCars) throws DAOException{ 69 70 Reservation reservation = null; 71 72 reservation = (Reservation)retrieveById(Reservation.class, reservationId); 73 74 reservation.getCars().clear(); 75 reservation.getCars().addAll(newCars); 76 77 update(reservation); 78 } 79 } 80 | Popular Tags |