KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.rentacar.persistance.dao;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.hibernate.Session;
12 import org.objectweb.rentacar.persistance.bo.Car;
13 import org.objectweb.rentacar.persistance.util.HibernateUtil;
14
15 /**
16  * @author ofabre
17  */

18 public class CarDAO extends GenericDAO {
19
20     private static CarDAO theInstance;
21
22     private CarDAO() {
23
24     }
25
26     public static synchronized CarDAO getInstance() {
27         if (null == theInstance) {
28             theInstance = new CarDAO();
29         }
30         return theInstance;
31     }
32
33     /**
34      * Returns the list of reservations for the given period of time
35      * @param start Starting date for the search
36      * @param end Ending date for the search
37      * @return List of found reservation matching the criteria
38      * @throws DAOException
39      */

40     public Set JavaDoc<Car> getReservedCarsByDates(Date JavaDoc start, Date JavaDoc end) throws DAOException {
41         Session session = null;
42         try {
43             session = HibernateUtil.getSession();
44         }
45         catch (ExceptionInInitializerError JavaDoc e) {
46             throw new DAOException("Error in Hibernate Session instanciation", e);
47         }
48         catch (Throwable JavaDoc e) {
49             throw new DAOException("Error in Hibernate Session instanciation", e);
50         }
51         org.hibernate.Query q = session.createQuery("select cars from Reservation r where (" + "(r.endingDate <= :end AND r.endingDate >= :start) OR (r.startingDate <= :end AND r.startingDate >= :start)" + "OR (r.startingDate <= :start AND r.endingDate >= :end))");
52         q.setDate("start", start);
53         q.setDate("end", end);
54         List JavaDoc<Car> cars = new ArrayList JavaDoc<Car>();
55         for (Iterator JavaDoc iter = q.list().iterator(); iter.hasNext();) {
56             cars.add((Car) iter.next());
57         }
58         Set JavaDoc<Car> result = new HashSet JavaDoc<Car>(cars);
59         return result;
60     }
61
62     /**
63      * Returns the list of reservations for the given period of time
64      * @param start Starting date for the search
65      * @param end Ending date for the search
66      * @return Set of found reservation matching the criteria
67      * @throws DAOException
68      */

69     public Set JavaDoc<Car> getAllAgencyCars() throws DAOException {
70         /*
71          * Session session = null; try{ session = HibernateUtil.getSession(); }catch
72          * (ExceptionInInitializerError e){ throw new DAOException("Error in Hibernate
73          * Session instanciation", e); }catch (Throwable e){ throw new DAOException("Error
74          * in Hibernate Session instanciation", e); } org.hibernate.Query q =
75          * session.createQuery("select c from Car c "); List<Car> cars = q.list();
76          */

77         List JavaDoc<Car> cars = Arrays.asList(retrieveAll(Car.class).toArray(new Car[10]));
78         Set JavaDoc<Car> result = new HashSet JavaDoc<Car>(cars);
79         return result;
80     }
81 }
82
Popular Tags