KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > centraloffice > services > reservation > ReservationServiceImpl


1 package org.objectweb.rentacar.centraloffice.services.reservation;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import javax.jws.WebMethod;
10 import javax.jws.WebParam;
11 import javax.jws.WebService;
12 import javax.xml.datatype.XMLGregorianCalendar JavaDoc;
13 import javax.xml.namespace.QName JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.objectweb.rentacar.centraloffice.services.CentralOfficeException;
17 import org.objectweb.rentacar.centraloffice.services.consultation.ConsultationImpl;
18 import org.objectweb.rentacar.persistance.bo.Agency;
19 import org.objectweb.rentacar.persistance.bo.AgencyCriteria;
20 import org.objectweb.rentacar.persistance.bo.AgencyVO;
21 import org.objectweb.rentacar.persistance.bo.Car;
22 import org.objectweb.rentacar.persistance.bo.Contact;
23 import org.objectweb.rentacar.persistance.bo.Reservation;
24 import org.objectweb.rentacar.persistance.dao.AgencyDAO;
25 import org.objectweb.rentacar.persistance.dao.CarDAO;
26 import org.objectweb.rentacar.persistance.dao.ContactDAO;
27 import org.objectweb.rentacar.persistance.dao.DAOException;
28 import org.objectweb.rentacar.persistance.dao.ReservationDAO;
29 import org.objectweb.rentacar.services.client.AgencyException_Exception;
30 import org.objectweb.rentacar.services.client.AgencyReservationServiceImpl;
31 import org.objectweb.rentacar.services.client.AgencyReservationServiceImplService;
32 import org.objectweb.rentacar.util.PropertiesLoader;
33 import org.objectweb.rentacar.util.PropertyLoadingException;
34
35 /**
36  *
37  * @author ofabre
38  *
39  */

40 @WebService
41 public class ReservationServiceImpl {
42     
43     private Logger logger = Logger.getLogger(ReservationServiceImpl.class);
44     
45     /**
46      * Create a new reservation by invoking
47      * the starting Agency create reservation web service.
48      * An update of the central office database is made by
49      * the agency web service.
50      * @param carId the reserved car Id
51      * @param start the starting date
52      * @param end the ending date
53      * @param customerId the customer Id
54      * @param startAgencyId the starting agency Id
55      * @param endAgencyId the ending agency Id
56      * @return the new reservation Id
57      * @throws CentralOfficeException
58      */

59     @WebMethod
60     public String JavaDoc createReservationOnCentralOffice(@WebParam(name="car")String JavaDoc carId,
61             @WebParam(name="start")XMLGregorianCalendar JavaDoc start,
62             @WebParam(name="end")XMLGregorianCalendar JavaDoc end,
63             @WebParam(name="customer")String JavaDoc customerId,
64             @WebParam(name="startAgency")String JavaDoc startAgencyId,
65             @WebParam(name="endAgency")String JavaDoc endAgencyId)
66     throws CentralOfficeException {
67         
68         URL JavaDoc wsdlLocation;
69         logger.info("Central Office . createReservationOnCentralOffice : "+carId+
70                 ", "+start.toString()+", "+end.toString()+
71                 ", "+customerId+", "+startAgencyId+", "+endAgencyId);
72         String JavaDoc agenciesPresent = null;
73         try{
74             agenciesPresent= PropertiesLoader.getProperty("central_office.properties", "agencies");
75         }
76         catch (PropertyLoadingException e){
77             logger.error("Error while loading properties",e);
78             throw new CentralOfficeException("Error while loading properties",e);
79         }
80         String JavaDoc reservationId;
81         if(agenciesPresent != null && agenciesPresent.equals("1")){
82             try {
83                 ConsultationImpl consultationImpl = new ConsultationImpl();
84                 // TODO : check nullity
85
AgencyVO startAgency = consultationImpl.retreiveAgencies(new AgencyCriteria(startAgencyId, null, null))[0];
86                 wsdlLocation = new URL JavaDoc("http://"+startAgency.getHost()+":"+startAgency.getPort()+"/"+startAgency.getWarName()+"/reservation?wsdl");
87             } catch (MalformedURLException JavaDoc e) {
88                 logger.debug("Error when creating wsdl URL", e);
89                 throw new CentralOfficeException("Error when creating wsdl URL", e);
90             }
91             QName JavaDoc serviceQname = new QName JavaDoc("{http://reservation.services.agency.rentacar.objectweb.org/}AgencyReservationServiceImplPort");
92             
93             AgencyReservationServiceImpl port;
94             port = new AgencyReservationServiceImplService(wsdlLocation, serviceQname).getAgencyReservationServiceImplPort();
95             
96             
97             try {
98                 reservationId = port.createReservation(carId, start, end, customerId, startAgencyId, endAgencyId);
99             } catch (AgencyException_Exception e) {
100                 logger.error("Error when creating reservation for distant agency", e);
101                 throw new CentralOfficeException("Error when creating reservation for distant agency", e);
102             }
103         }
104         else{
105             logger.info("Central Office . createReservationInCentralDB : "+ customerId+", "+
106                     carId+", "+start.toString()+", "+end.toString()+", "+
107                     startAgencyId+", "+endAgencyId);
108             AgencyDAO agencyDAO = AgencyDAO.getInstance();
109             ContactDAO contactDAO = ContactDAO.getInstance();
110             CarDAO carDAO = CarDAO.getInstance();
111             
112             Car car = null;
113             Contact customer = null;
114             Agency startingAgency = null;
115             Agency endingAgency = null;
116             try {
117                 car = (Car) carDAO.retrieveById(Car.class, carId);
118                 customer = (Contact) contactDAO.retrieveById(Contact.class, customerId);
119                 startingAgency = (Agency) agencyDAO.retrieveById(Agency.class, startAgencyId);
120                 endingAgency = (Agency) agencyDAO.retrieveById(Agency.class, endAgencyId);
121             } catch (DAOException e) {
122                 logger.error("Error when retrieving object in database", e);
123                 throw new CentralOfficeException("Error when retrieving object in database", e);
124             }
125             Set JavaDoc<Car> cars = new HashSet JavaDoc<Car>();
126             cars.add(car);
127             Reservation reservationBO = new Reservation(customer, cars, start.toGregorianCalendar().getTime(), end.toGregorianCalendar().getTime(), startingAgency, endingAgency);
128             try {
129                 reservationId = ReservationDAO.getInstance().create(reservationBO);
130             } catch (DAOException e1) {
131                 logger.error("Error when creating a reservation in central office DB", e1);
132                 throw new CentralOfficeException("Error when creating a reservation in central office DB", e1);
133             }
134         }
135         
136         return reservationId;
137         
138     }
139     
140 }
141
Popular Tags