1 package org.objectweb.rentacar.persistance.dao; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Date ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Set ; 10 11 import org.hibernate.Session; 12 import org.objectweb.rentacar.persistance.bo.Car; 13 import org.objectweb.rentacar.persistance.util.HibernateUtil; 14 15 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 40 public Set <Car> getReservedCarsByDates(Date start, Date end) throws DAOException { 41 Session session = null; 42 try { 43 session = HibernateUtil.getSession(); 44 } 45 catch (ExceptionInInitializerError e) { 46 throw new DAOException("Error in Hibernate Session instanciation", e); 47 } 48 catch (Throwable 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 <Car> cars = new ArrayList <Car>(); 55 for (Iterator iter = q.list().iterator(); iter.hasNext();) { 56 cars.add((Car) iter.next()); 57 } 58 Set <Car> result = new HashSet <Car>(cars); 59 return result; 60 } 61 62 69 public Set <Car> getAllAgencyCars() throws DAOException { 70 77 List <Car> cars = Arrays.asList(retrieveAll(Car.class).toArray(new Car[10])); 78 Set <Car> result = new HashSet <Car>(cars); 79 return result; 80 } 81 } 82 | Popular Tags |