KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > agency > services > availability > AgencyCheckAvailabilityServiceImpl


1 package org.objectweb.rentacar.agency.services.availability;
2
3 import java.util.Date JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Set JavaDoc;
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 /**
22  * This class expose web services that allow to check car availability in an agency.
23  * @author wjoseph
24  *
25  */

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     /**
34      * Retrieve the list of cars that match the given criterium
35      * and that are available during the given period
36      * @param criterium the car criteria for the research
37      * @param startDate the starting date
38      * @param endDate the ending date
39      * @return
40      * @throws AgencyException
41      */

42     @WebMethod
43     public CarVO[] consultAvailability(@WebParam(name = "criterium")CarCriteria criterium,
44                                 @WebParam(name = "startDate")Date JavaDoc startDate,
45                                 @WebParam(name = "endDate")Date JavaDoc endDate) throws AgencyException {
46         logger.info("Agency . consultAvailability : "+criterium.toString()+
47                 ", "+startDate.toString()+", "+endDate.toString());
48         Set JavaDoc<CarVO> availableCarInAgency;
49         availableCarInAgency = availableCarsByCriterium(criterium, startDate, endDate);
50         CarVO[] availableCar= {};
51         return availableCarInAgency.toArray(availableCar);
52     }
53     
54     /**
55      * It is a test method to validate polymorphic strategy with jaxws
56      */

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     /**
68      * It is a test method to validate polymorphic strategy with jaxws
69      */

70     @WebMethod
71     public void dummy(CarDeLuxeVO carDeLuxeVO) {
72         
73     }
74     
75     /**
76      * Retrieve the list of cars that match the given criterium
77      * and that are available during the given period
78      * @param criterium the car criteria for the research
79      * @param start the starting date
80      * @param end the ending date
81      * @return
82      * @throws AgencyException
83      */

84     private Set JavaDoc<CarVO> availableCarsByCriterium(CarCriteria criterium, Date JavaDoc start, Date JavaDoc end) throws AgencyException {
85             Set JavaDoc<CarVO> availableCars= new HashSet JavaDoc<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 JavaDoc<Car> cars= getAvailableCars(start, end);
102                     Set JavaDoc<CarVO> carsVO= new HashSet JavaDoc<CarVO>();
103                         
104                     for (Car car : cars) {
105                         carsVO.add(new CarVO(car));
106                     }
107                     
108                     for (CarVO carVO : carsVO) {
109                         //logger.debug(carVO);
110
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     /**
126      * Returns all available cars in this agency for the given period
127      * @param start Starting date of the search
128      * @param end Ending date of the search
129      * @return A Set of avalaible cars in the agency for this period
130      * @throws DAOException
131      */

132     private Set JavaDoc<Car> getAvailableCars(Date JavaDoc start, Date JavaDoc end) throws DAOException{
133         Set JavaDoc<Car> result = getAllAgencyCars();
134         result.removeAll(getReservedCars(start,end));
135         return result;
136     }
137     
138     /**
139      * Returns all cars of this agency
140      * @return A Set of cars belonging to this agency
141      * @throws DAOException
142      */

143     private Set JavaDoc<Car> getAllAgencyCars() throws DAOException{
144         return CarDAO.getInstance().getAllAgencyCars();
145     }
146     
147     /**
148      * Returns all reserved cars for the givien period
149      * @param start Starting date of the search
150      * @param end Ending date of the search
151      * @return A Set of reserved cars for this period
152      * @throws DAOException
153      */

154     private Set JavaDoc<Car> getReservedCars(Date JavaDoc start, Date JavaDoc end) throws DAOException{
155         return (Set JavaDoc<Car>) CarDAO.getInstance().getReservedCarsByDates(start,end);
156     }
157     
158 }
159
Popular Tags