KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > persistance > dao > ReservationDAO


1 package org.objectweb.rentacar.persistance.dao;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
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 /**
15  *
16  * @author ofabre
17  *
18  */

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     /**
36      * Returns the list of reservations for the given period of time
37      * @param start Starting date for the search
38      * @param end Ending date for the search
39      * @return List of found reservation matching the criteria
40      * @throws DAOException
41      */

42     public List JavaDoc<Reservation> getReservationByDates(Date JavaDoc start, Date JavaDoc end) throws DAOException {
43         Session session = null;
44         try{
45             session = HibernateUtil.getSession();
46         }catch (ExceptionInInitializerError JavaDoc e){
47             throw new DAOException("Error in Hibernate Session instanciation", e);
48         }catch (Throwable JavaDoc 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 JavaDoc<Reservation >result = new ArrayList JavaDoc<Reservation>();
56         for (Iterator JavaDoc iter = q.list().iterator();iter.hasNext();) {
57             result.add((Reservation) iter.next());
58         }
59         return result;
60     }
61     
62     /**
63      * Update the cars set of an reservation following the best practice given by Hibernate
64      * @param reservationId
65      * @param newCars
66      * @throws DAOExceptionAgency
67      */

68     public void updateReservationCars(String JavaDoc reservationId, Set JavaDoc<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