1 package org.objectweb.rentacar.centraloffice.services.reservation; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.util.Date ; 6 import java.util.HashSet ; 7 import java.util.Set ; 8 9 import javax.jws.WebMethod; 10 import javax.jws.WebParam; 11 import javax.jws.WebService; 12 import javax.xml.datatype.XMLGregorianCalendar ; 13 import javax.xml.namespace.QName ; 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 40 @WebService 41 public class ReservationServiceImpl { 42 43 private Logger logger = Logger.getLogger(ReservationServiceImpl.class); 44 45 59 @WebMethod 60 public String createReservationOnCentralOffice(@WebParam(name="car")String carId, 61 @WebParam(name="start")XMLGregorianCalendar start, 62 @WebParam(name="end")XMLGregorianCalendar end, 63 @WebParam(name="customer")String customerId, 64 @WebParam(name="startAgency")String startAgencyId, 65 @WebParam(name="endAgency")String endAgencyId) 66 throws CentralOfficeException { 67 68 URL wsdlLocation; 69 logger.info("Central Office . createReservationOnCentralOffice : "+carId+ 70 ", "+start.toString()+", "+end.toString()+ 71 ", "+customerId+", "+startAgencyId+", "+endAgencyId); 72 String 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 reservationId; 81 if(agenciesPresent != null && agenciesPresent.equals("1")){ 82 try { 83 ConsultationImpl consultationImpl = new ConsultationImpl(); 84 AgencyVO startAgency = consultationImpl.retreiveAgencies(new AgencyCriteria(startAgencyId, null, null))[0]; 86 wsdlLocation = new URL ("http://"+startAgency.getHost()+":"+startAgency.getPort()+"/"+startAgency.getWarName()+"/reservation?wsdl"); 87 } catch (MalformedURLException e) { 88 logger.debug("Error when creating wsdl URL", e); 89 throw new CentralOfficeException("Error when creating wsdl URL", e); 90 } 91 QName serviceQname = new QName ("{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 <Car> cars = new HashSet <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 |