1 package org.objectweb.rentacar.agency.services.availability; 2 3 import java.util.Date ; 4 import java.util.HashSet ; 5 import java.util.Set ; 6 7 import javax.jws.WebMethod; 8 import javax.jws.WebParam; 9 import javax.jws.WebService; 10 import javax.jws.soap.SOAPBinding; 11 12 import org.apache.log4j.Logger; 13 import org.objectweb.rentacar.AgencyException; 14 import org.objectweb.rentacar.persistance.bo.Car; 15 import org.objectweb.rentacar.persistance.bo.CarCriteria; 16 import org.objectweb.rentacar.persistance.bo.CarDeLuxeVO; 17 import org.objectweb.rentacar.persistance.bo.CarVO; 18 import org.objectweb.rentacar.persistance.dao.CarDAO; 19 import org.objectweb.rentacar.persistance.dao.DAOException; 20 21 26 @WebService 27 @SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL) 28 public class AgencyCheckAvailabilityServiceImpl{ 29 30 31 private static Logger logger = Logger.getLogger( AgencyCheckAvailabilityServiceImpl.class); 32 33 42 @WebMethod 43 public CarVO[] consultAvailability(@WebParam(name = "criterium")CarCriteria criterium, 44 @WebParam(name = "startDate")Date startDate, 45 @WebParam(name = "endDate")Date endDate) throws AgencyException { 46 logger.info("Agency . consultAvailability : "+criterium.toString()+ 47 ", "+startDate.toString()+", "+endDate.toString()); 48 Set <CarVO> availableCarInAgency; 49 availableCarInAgency = availableCarsByCriterium(criterium, startDate, endDate); 50 CarVO[] availableCar= {}; 51 return availableCarInAgency.toArray(availableCar); 52 } 53 54 57 @WebMethod 58 public CarVO[] getAListOfPolymorphicCars() throws AgencyException { 59 CarDeLuxeVO carDeLuxeVO = new CarDeLuxeVO("G", "mercedes-benz", "TwinportV12", "f45", "sw45", "smb45", "tv45"); 60 CarVO carVO = new CarVO("G", "peugeot", "206"); 61 CarVO[] cars = new CarVO[2]; 62 cars[0] = carDeLuxeVO; 63 cars[1] = carVO; 64 return cars; 65 } 66 67 70 @WebMethod 71 public void dummy(CarDeLuxeVO carDeLuxeVO) { 72 73 } 74 75 84 private Set <CarVO> availableCarsByCriterium(CarCriteria criterium, Date start, Date end) throws AgencyException { 85 Set <CarVO> availableCars= new HashSet <CarVO>(); 86 boolean type=false, model= false, brand=false, carId=false; 87 88 if( criterium.getType()== null) 89 type=true; 90 91 if(criterium.getModel()== null ) 92 model=true; 93 94 if(criterium.getBrand()==null) 95 brand=true; 96 97 if(criterium.getCarId()==null) 98 carId=true; 99 100 try { 101 Set <Car> cars= getAvailableCars(start, end); 102 Set <CarVO> carsVO= new HashSet <CarVO>(); 103 104 for (Car car : cars) { 105 carsVO.add(new CarVO(car)); 106 } 107 108 for (CarVO carVO : carsVO) { 109 if(carId || carVO.getCarId().equals(criterium.getCarId())) 111 if(type || carVO.getType().equals(criterium.getType())) 112 if(model || carVO.getModel().equals(criterium.getModel())) 113 if(brand || carVO.getBrand().equals(criterium.getBrand())) 114 availableCars.add(carVO); 115 } 116 117 } catch (DAOException e) { 118 logger.error("Error when retrieving available cars", e); 119 throw new AgencyException("Error when retrieving available cars", e); 120 } 121 122 return availableCars; 123 } 124 125 132 private Set <Car> getAvailableCars(Date start, Date end) throws DAOException{ 133 Set <Car> result = getAllAgencyCars(); 134 result.removeAll(getReservedCars(start,end)); 135 return result; 136 } 137 138 143 private Set <Car> getAllAgencyCars() throws DAOException{ 144 return CarDAO.getInstance().getAllAgencyCars(); 145 } 146 147 154 private Set <Car> getReservedCars(Date start, Date end) throws DAOException{ 155 return (Set <Car>) CarDAO.getInstance().getReservedCarsByDates(start,end); 156 } 157 158 } 159 | Popular Tags |