1 package org.objectweb.rentacar.centraloffice.services.consultation; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 import java.util.HashSet ; 6 import java.util.List ; 7 import java.util.Set ; 8 9 import javax.jws.WebMethod; 10 import javax.jws.WebParam; 11 import javax.jws.WebService; 12 13 import org.apache.log4j.Logger; 14 import org.objectweb.rentacar.centraloffice.services.CentralOfficeException; 15 import org.objectweb.rentacar.persistance.bo.Agency; 16 import org.objectweb.rentacar.persistance.bo.AgencyCriteria; 17 import org.objectweb.rentacar.persistance.bo.AgencyVO; 18 import org.objectweb.rentacar.persistance.bo.Car; 19 import org.objectweb.rentacar.persistance.bo.CarCriteria; 20 import org.objectweb.rentacar.persistance.bo.CarVO; 21 import org.objectweb.rentacar.persistance.dao.AgencyDAO; 22 import org.objectweb.rentacar.persistance.dao.CarDAO; 23 import org.objectweb.rentacar.persistance.dao.DAOException; 24 25 31 @WebService 32 public class ConsultationImpl { 33 34 private Logger logger = Logger.getLogger(ConsultationImpl.class); 35 36 42 @WebMethod 43 public AgencyVO[] retreiveAgencies( 44 @WebParam(name="agencyCriteria")AgencyCriteria agencyCriteria) 45 throws CentralOfficeException{ 46 47 List <AgencyVO> agencies = new ArrayList <AgencyVO>(); 48 List <Agency> agenciesBO = null; 49 try { 50 logger.info("Central Office . retreiveAgencies : "+agencyCriteria.toString()); 51 agenciesBO = AgencyDAO.getInstance().retreiveAgencies(agencyCriteria); 52 } catch (DAOException e) { 53 logger.error("Error when retrieving agencies matching criteria :"+agencyCriteria.toString(), e); 54 throw new CentralOfficeException("Error when retrieving agencies matching criteria :"+agencyCriteria.toString(), e); 55 } 56 for (Agency agency : agenciesBO) { 57 agencies.add(new AgencyVO(agency)); 58 } 59 60 return agencies.toArray(new AgencyVO[0]); 61 } 62 63 72 @WebMethod 73 public CarVO[] consult(@WebParam(name="criterium")CarCriteria criterium, 74 @WebParam(name="startDate")Date startDate, 75 @WebParam(name="endDate")Date endDate) throws CentralOfficeException { 76 Set <CarVO> availableCarInAgency; 77 logger.info("Central Office . consult : "+criterium.toString()+", "+startDate.toString()+", "+endDate.toString()); 78 availableCarInAgency = availableCarsByCriterium(criterium, startDate, endDate); 79 return availableCarInAgency.toArray(new CarVO[0]); 80 } 81 82 91 private Set <CarVO> availableCarsByCriterium(CarCriteria criterium, 92 Date start, Date end) throws CentralOfficeException { 93 Set <CarVO> availableCars= new HashSet <CarVO>(); 94 boolean type=false, model= false, brand=false; 95 96 if( criterium.getType()== null) 97 type=true; 98 99 if(criterium.getModel()== null ) 100 model=true; 101 102 if(criterium.getBrand()==null) 103 brand=true; 104 105 try { 106 Set <Car> cars= getAvailableCars(start, end); 107 Set <CarVO> carsVO= new HashSet <CarVO>(); 108 109 for (Car car : cars) { 110 carsVO.add(new CarVO(car)); 111 } 112 113 for (CarVO carVO : carsVO) { 114 if(type || carVO.getType().equals(criterium.getType())) 115 if(model || carVO.getModel().equals(criterium.getModel())) 116 if(brand || carVO.getBrand().equals(criterium.getBrand())) 117 availableCars.add(carVO); 118 119 } 120 121 } catch (DAOException e) { 122 logger.error("Error while retrieving available cars", e); 123 throw new CentralOfficeException("Error while retrieving available cars", e); 124 } 125 126 return availableCars; 127 } 128 129 136 private Set <Car> getAvailableCars(Date start, Date end) throws DAOException{ 137 Set <Car> result = getAllAgencyCars(); 138 result.removeAll(getReservedCars(start,end)); 139 return result; 140 } 141 142 147 private Set <Car> getAllAgencyCars() throws DAOException{ 148 return CarDAO.getInstance().getAllAgencyCars(); 149 } 150 151 158 private Set <Car> getReservedCars(Date start, Date end) throws DAOException{ 159 return (Set <Car>) CarDAO.getInstance().getReservedCarsByDates(start,end); 160 } 161 162 } 163 | Popular Tags |