KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.rentacar.persistance.dao;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.hibernate.Query;
10 import org.hibernate.Session;
11 import org.objectweb.rentacar.persistance.bo.Agency;
12 import org.objectweb.rentacar.persistance.bo.AgencyCriteria;
13 import org.objectweb.rentacar.persistance.bo.Car;
14 import org.objectweb.rentacar.persistance.bo.Contact;
15 import org.objectweb.rentacar.persistance.util.HibernateUtil;
16
17 /**
18  * @author ofabre
19  */

20 public class AgencyDAO extends GenericDAO {
21
22     Logger logger = Logger.getLogger(AgencyDAO.class);
23
24     private static AgencyDAO theInstance;
25
26     private AgencyDAO() {
27
28     }
29
30     public static synchronized AgencyDAO getInstance() {
31         if (null == theInstance) {
32             theInstance = new AgencyDAO();
33         }
34         return theInstance;
35     }
36
37     public Agency getAgencyByContact(Contact contact) throws DAOException {
38         Session session = null;
39         Agency agency = null;
40         try {
41             session = HibernateUtil.getSession();
42         }
43         catch (ExceptionInInitializerError JavaDoc e) {
44             throw new DAOException("Error in Hibernate Session instanciation", e);
45         }
46         catch (Exception JavaDoc e) {
47             throw new DAOException("Error in Hibernate Session instanciation", e);
48         }
49
50         Query q = session.createQuery("from Agency a where a.contact >= :contact");
51         q.setParameter("contact", contact);
52         agency = (Agency) q.uniqueResult();
53
54         return agency;
55     }
56
57     /**
58      * Retrieve all agencies that match criteria given in parameter. If all criteria are
59      * null, it returns all agencies.
60      * @param AgencyCriteria criteria of the searched agencies
61      * @return The list of all agencies that match criteria
62      * @throws DAOException
63      */

64     public List JavaDoc<Agency> retreiveAgencies(AgencyCriteria agencyCriteria) throws DAOException {
65         Session session = null;
66         try {
67             session = HibernateUtil.getSession();
68         }
69         catch (ExceptionInInitializerError JavaDoc e) {
70             throw new DAOException("Error in Hibernate Session instanciation", e);
71         }
72         catch (Exception JavaDoc e) {
73             throw new DAOException("Error in Hibernate Session instanciation", e);
74         }
75
76         // Create the query for all the selected criteria
77
String JavaDoc query = "";
78         if (agencyCriteria.getZipPattern() == null && agencyCriteria.getLastName() == null && agencyCriteria.getAgencyId() == null) {
79             query = "from Agency";
80         }
81         else {
82             query = "from Agency a where ";
83             if (agencyCriteria.getAgencyId()!=null){
84                 query+="a.agencyId = '"+agencyCriteria.getAgencyId()+"' and ";
85             }
86             if (agencyCriteria.getZipPattern() != null) {
87                 query += "a.contact.address.zip like '" + agencyCriteria.getZipPattern() + "' and ";
88             }
89             if (agencyCriteria.getLastName() != null) {
90                 query += "a.contact.lastName = '" + agencyCriteria.getLastName() + "' and ";
91             }
92             query = query.substring(0, query.length() - 5);
93         }
94
95         List JavaDoc<Agency> agencies = new ArrayList JavaDoc<Agency>();
96         logger.debug(query);
97         Query q = session.createQuery(query);
98         for (Iterator JavaDoc iter = q.list().iterator(); iter.hasNext();) {
99             agencies.add((Agency) iter.next());
100         }
101         return agencies;
102     }
103
104     /**
105      * Update the cars set of an agency following the best practice given by Hibernate
106      * @param agencyId
107      * @param newCars
108      * @throws DAOException
109      */

110     public void updateAgencyCars(String JavaDoc agencyId, Set JavaDoc<Car> newCars) throws DAOException {
111
112         Agency agency = null;
113
114         agency = (Agency) retrieveById(Agency.class, agencyId);
115
116         agency.getCars().clear();
117         agency.getCars().addAll(newCars);
118
119         update(agency);
120     }
121 }
122
Popular Tags