KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > centraloffice > services > consultation > ConsultationImpl


1 package org.objectweb.rentacar.centraloffice.services.consultation;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
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 /**
26  * This class expose services
27  * that allow to retrieve information in central office database.
28  * @author ofabre
29  *
30  */

31 @WebService
32 public class ConsultationImpl {
33     
34     private Logger logger = Logger.getLogger(ConsultationImpl.class);
35     
36     /**
37      * Retrieve all agencies that match criteria given in parameter
38      * @param agencyCriteria criteria of the searched agencies
39      * @return The list of all agencies that match criteria
40      * @throws CentralOfficeException
41      */

42     @WebMethod
43     public AgencyVO[] retreiveAgencies(
44                             @WebParam(name="agencyCriteria")AgencyCriteria agencyCriteria)
45                             throws CentralOfficeException{
46         
47         List JavaDoc<AgencyVO> agencies = new ArrayList JavaDoc<AgencyVO>();
48         List JavaDoc<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     /**
64      * Retrieve the list of cars that match the given criterium
65      * and that are available during the given period for all agencies
66      * @param criterium the car criteria for the research
67      * @param startDate the starting date
68      * @param endDate the ending date
69      * @return a CarVO table
70      * @throws CentralOfficeException
71      */

72     @WebMethod
73     public CarVO[] consult(@WebParam(name="criterium")CarCriteria criterium,
74                             @WebParam(name="startDate")Date JavaDoc startDate,
75                             @WebParam(name="endDate")Date JavaDoc endDate) throws CentralOfficeException {
76         Set JavaDoc<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     /**
83      * Retrieve the list of cars that match the given criterium
84      * and that are available during the given period
85      * @param criterium the car criteria for the research
86      * @param start the starting date
87      * @param end the ending date
88      * @return
89      * @throws CentralOfficeException
90      */

91     private Set JavaDoc<CarVO> availableCarsByCriterium(CarCriteria criterium,
92             Date JavaDoc start, Date JavaDoc end) throws CentralOfficeException {
93             Set JavaDoc<CarVO> availableCars= new HashSet JavaDoc<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 JavaDoc<Car> cars= getAvailableCars(start, end);
107                     Set JavaDoc<CarVO> carsVO= new HashSet JavaDoc<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     /**
130      * Returns all available cars in all agency for the given period
131      * @param start Starting date of the search
132      * @param end Ending date of the search
133      * @return A Set of avalaible cars in the agency for this period
134      * @throws DAOException
135      */

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

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

158     private Set JavaDoc<Car> getReservedCars(Date JavaDoc start, Date JavaDoc end) throws DAOException{
159         return (Set JavaDoc<Car>) CarDAO.getInstance().getReservedCarsByDates(start,end);
160     }
161
162 }
163
Popular Tags