1 package org.objectweb.rentacar.persistance.dao; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Set ; 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 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 e) { 44 throw new DAOException("Error in Hibernate Session instanciation", e); 45 } 46 catch (Exception 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 64 public List <Agency> retreiveAgencies(AgencyCriteria agencyCriteria) throws DAOException { 65 Session session = null; 66 try { 67 session = HibernateUtil.getSession(); 68 } 69 catch (ExceptionInInitializerError e) { 70 throw new DAOException("Error in Hibernate Session instanciation", e); 71 } 72 catch (Exception e) { 73 throw new DAOException("Error in Hibernate Session instanciation", e); 74 } 75 76 String 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 <Agency> agencies = new ArrayList <Agency>(); 96 logger.debug(query); 97 Query q = session.createQuery(query); 98 for (Iterator iter = q.list().iterator(); iter.hasNext();) { 99 agencies.add((Agency) iter.next()); 100 } 101 return agencies; 102 } 103 104 110 public void updateAgencyCars(String agencyId, Set <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 |