1 package org.objectweb.rentacar.agency.services.reservation; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.util.ArrayList ; 6 import java.util.Date ; 7 import java.util.GregorianCalendar ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Set ; 12 13 import javax.jws.WebMethod; 14 import javax.jws.WebParam; 15 import javax.jws.WebService; 16 import javax.xml.datatype.DatatypeConfigurationException ; 17 import javax.xml.datatype.DatatypeFactory ; 18 import javax.xml.datatype.XMLGregorianCalendar ; 19 import javax.xml.namespace.QName ; 20 21 import org.apache.log4j.Logger; 22 import org.objectweb.rentacar.AgencyException; 23 import org.objectweb.rentacar.persistance.bo.Agency; 24 import org.objectweb.rentacar.persistance.bo.Car; 25 import org.objectweb.rentacar.persistance.bo.CarCriteria; 26 import org.objectweb.rentacar.persistance.bo.CarVO; 27 import org.objectweb.rentacar.persistance.bo.CentralOffice; 28 import org.objectweb.rentacar.persistance.bo.Contact; 29 import org.objectweb.rentacar.persistance.bo.Reservation; 30 import org.objectweb.rentacar.persistance.dao.AgencyDAO; 31 import org.objectweb.rentacar.persistance.dao.CarDAO; 32 import org.objectweb.rentacar.persistance.dao.CentralOfficeDAO; 33 import org.objectweb.rentacar.persistance.dao.ContactDAO; 34 import org.objectweb.rentacar.persistance.dao.DAOException; 35 import org.objectweb.rentacar.persistance.dao.ReservationDAO; 36 import org.objectweb.rentacar.services.client.AgencyCheckAvailabilityServiceImpl; 37 import org.objectweb.rentacar.services.client.AgencyCheckAvailabilityServiceImplService; 38 import org.objectweb.rentacar.services.client.AgencyCriteria; 39 import org.objectweb.rentacar.services.client.AgencyException_Exception; 40 import org.objectweb.rentacar.services.client.AgencyReservationServiceImplService; 41 import org.objectweb.rentacar.services.client.AgencyVO; 42 import org.objectweb.rentacar.services.client.CentralOfficeException; 43 import org.objectweb.rentacar.services.client.CentralOfficeException_Exception; 44 import org.objectweb.rentacar.services.client.ConsultationImpl; 45 import org.objectweb.rentacar.services.client.ConsultationImplService; 46 import org.objectweb.rentacar.services.client.UpdateCentralDBImpl; 47 import org.objectweb.rentacar.services.client.UpdateCentralDBImplService; 48 import org.objectweb.rentacar.util.PropertiesLoader; 49 import org.objectweb.rentacar.util.PropertyLoadingException; 50 51 import sun.management.resources.agent; 52 53 54 59 @WebService 60 public class AgencyReservationServiceImpl { 61 62 private Logger logger = Logger.getLogger(AgencyReservationServiceImpl.class); 63 64 77 @SuppressWarnings ("unchecked") 78 @WebMethod 79 public String createReservation(@WebParam(name="carId")String carId, 80 @WebParam(name="startDate")Date start, 81 @WebParam(name="endDate")Date end, 82 @WebParam(name="customerId")String customerId, 83 @WebParam(name="startAgencyId")String startAgencyId, 84 @WebParam(name="endAgencyId")String endAgencyId) throws AgencyException { 85 86 logger.info("Agency . createReservation : "+carId+", "+start.toString()+ 87 ", "+end.toString()+", "+customerId+ 88 ", "+startAgencyId+", "+endAgencyId); 89 90 ReservationDAO reservationDAO = ReservationDAO.getInstance(); 91 String result = null; 92 Set <Car> carsInitially = new HashSet <Car>(); 93 94 try { 95 CarCriteria criterium= new CarCriteria(); 96 criterium.setCarId(carId); 97 98 org.objectweb.rentacar.agency.services.availability.AgencyCheckAvailabilityServiceImpl check= new org.objectweb.rentacar.agency.services.availability.AgencyCheckAvailabilityServiceImpl(); 99 CarVO[] availableCars = check.consultAvailability(criterium, start, end); 100 101 if(availableCars.length >= 1){ 102 logger.debug("The car with Id "+ carId+"is disponible"); 103 carsInitially.add((Car) CarDAO.getInstance().retrieveById(Car.class, carId)); 104 105 106 Contact cont =(Contact) ContactDAO.getInstance().retrieveById(Contact.class, customerId); 107 Agency stAg = (Agency)AgencyDAO.getInstance().retrieveById(Agency.class, startAgencyId); 108 Agency enAg = (Agency)AgencyDAO.getInstance().retrieveById(Agency.class, endAgencyId); 109 110 Reservation res = new Reservation(cont, carsInitially, start, end, stAg, enAg); 111 112 113 result = reservationDAO.create(res); 114 String centralOfficePresent= PropertiesLoader.getProperty("agency.properties", "central_office"); 115 if(centralOfficePresent != null && centralOfficePresent.equals("1")){ 116 List <CentralOffice> centralOffices = new ArrayList <CentralOffice>(); 118 for (Object obj : CentralOfficeDAO.getInstance().retrieveAll(CentralOffice.class)) { 119 centralOffices.add((CentralOffice) obj); 120 } 121 CentralOffice centralOffice = centralOffices.get(0); 123 String centralOfficeHost = centralOffice.getHost(); 124 String centralOfficePort = centralOffice.getPort(); 125 126 URL updateWsdlLocation = new URL ("http://"+centralOfficeHost+":"+centralOfficePort+"/rentacar-central-office/updateDB?wsdl"); 127 QName updateName = new QName ("{http://update.services.centraloffice.rentacar.objectweb.org/}UpdateCentralDBImplPort"); 128 129 130 131 UpdateCentralDBImpl port = new UpdateCentralDBImplService(updateWsdlLocation,updateName).getUpdateCentralDBImplPort(); 132 133 135 XMLGregorianCalendar stDa = getDate(res.getStartingDate().getTime()); 136 137 XMLGregorianCalendar enDa = getDate(res.getEndingDate().getTime()); 138 139 port.addReservationInCentralDB(res.getCustomer().getContactId() 140 ,carId,stDa,enDa,res.getStartingAgency().getAgencyId(),res.getEndingAgency().getAgencyId()); 141 }else{ 142 logger.debug("This car is already renting"); 143 } 144 } 145 } catch (DAOException e) { 146 logger.error("Error creating Reservation", e); 147 throw new AgencyException("Error creating Reservation", e); 148 } catch (CentralOfficeException_Exception e) { 149 logger.error("Error while updating central DB",e); 150 throw new AgencyException("Error while updating central DB",e); 151 } catch (MalformedURLException e) { 152 logger.error("Error while proccessing WSDL URL",e); 153 throw new AgencyException("Error while proccessing WSDL URL",e); 154 } catch (PropertyLoadingException e) { 155 logger.error("Error while loading the properties",e); 156 throw new AgencyException("Error while loading the properties",e); 157 } 158 return result; 159 } 160 161 @SuppressWarnings ("unchecked") 162 @WebMethod 163 public String [] createReservationPartner(@WebParam(name="criterion")CarCriteria criterium, 164 @WebParam(name="quantity")int quantity, @WebParam(name="startDate")Date start, 165 @WebParam(name="endDate")Date end, 166 @WebParam(name="partnerId")String partnerId, 167 @WebParam(name="startAgencyId")String startAgencyId, 168 @WebParam(name="endAgencyId")String endAgencyId) throws DAOException { 169 String ids[] = {}; 170 ArrayList <String > idList= new ArrayList <String >(); 171 ArrayList <ElementsToReserve> carsToReserve = new ArrayList <ElementsToReserve>(); 172 173 174 try { 175 XMLGregorianCalendar startDate = getDate(start.getTime()); 176 177 XMLGregorianCalendar endDate = getDate(end.getTime()); 178 org.objectweb.rentacar.agency.services.availability.AgencyCheckAvailabilityServiceImpl check= new org.objectweb.rentacar.agency.services.availability.AgencyCheckAvailabilityServiceImpl(); 179 CarVO[] availableCars = check.consultAvailability(criterium, start, end); 180 181 if (availableCars.length >= quantity){ 183 for (CarVO carVO : availableCars) { 184 carsToReserve.add(new ElementsToReserve(null, carVO.getCarId())); 185 } 186 } 187 else{ 188 System.out.println(new String ("Search in close Agencies \n")); 190 for (CarVO carVO : availableCars) { 191 carsToReserve.add(new ElementsToReserve(null, carVO.getCarId())); 192 } 193 ArrayList <ElementsToReserve> availableAgencyBCars= availableCarsInCloseAgencies(criterium, quantity- availableCars.length,start,end); 194 carsToReserve.addAll(availableAgencyBCars); 195 System.out.println(new String ("cars to rent\n" + carsToReserve.size())); 196 } 197 198 if(carsToReserve.size()>= quantity){ 199 int i=0; 200 while (i < quantity){ 201 String id; 202 ElementsToReserve current=carsToReserve.get(i); 203 if (current.getResaImpl()==null){ 204 id=createReservation(current.getCarVOId(),start,end,partnerId,startAgencyId,endAgencyId); 206 }else{ 207 id= current.getResaImpl().createReservation(current.getCarVOId(),startDate,endDate,partnerId,startAgencyId,endAgencyId); 209 } 210 idList.add(id); 211 i++; 212 } 213 }else 214 idList.add("There aren't enough cars matching with the criteria"); 215 216 } 217 catch (AgencyException e) { 218 throw new DAOException("Error creating Reservation", e); 219 } 220 catch (AgencyException_Exception e) { 221 throw new DAOException("Error creating Reservation", e); 222 } 223 return idList.toArray(ids); 224 } 225 226 227 233 @WebMethod 234 public void closeReservation(@WebParam(name="reservationCode")String reservationCode) throws AgencyException { 235 236 logger.info("Agency . closeReservation : "+reservationCode); 237 238 ReservationDAO reservationDAO = ReservationDAO.getInstance(); 239 240 try { 241 Reservation res = (Reservation) reservationDAO.retrieveById(Reservation.class, reservationCode); 242 reservationDAO.delete(res); 243 244 } 245 catch (DAOException e) { 246 logger.error("Error closing Reservation", e); 247 throw new AgencyException("Error closing Reservation", e); 248 } 249 } 250 251 258 @WebMethod 259 public boolean cancelReservation(@WebParam(name="reservationId")String reservationId) throws AgencyException { 260 261 logger.info("Agency . cancelReservation : "+reservationId); 262 263 try { 264 Reservation resToCancel = (Reservation) ReservationDAO.getInstance().retrieveById(Reservation.class, reservationId); 265 ReservationDAO.getInstance().delete(resToCancel); 266 return true; 267 } 268 catch (DAOException e) { 269 logger.error("Error closing Reservation", e); 270 throw new AgencyException("Error closing Reservation", e); 271 } 272 273 } 274 275 280 private XMLGregorianCalendar getDate(long timeInMillis) { 281 try { 282 GregorianCalendar cal = new GregorianCalendar (); 283 cal.setTimeInMillis(timeInMillis); 284 return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); 285 } 286 catch (DatatypeConfigurationException e) { 287 throw new Error (e); 288 } 289 } 290 private ArrayList <ElementsToReserve> availableCarsInCloseAgencies(CarCriteria carCrit, int quantity, Date start, Date end) throws AgencyException{ 292 293 List <Object > centralOffices; 294 ArrayList <ElementsToReserve> availableCarsAgency = new ArrayList <ElementsToReserve>(); 295 AgencyCriteria agencyCriteria= new AgencyCriteria(); 296 try { 297 centralOffices = CentralOfficeDAO.getInstance().retrieveAll(CentralOffice.class); 298 CentralOffice centralOffice = (CentralOffice)centralOffices.get(0); 300 String centralOfficeHost = centralOffice.getHost(); 301 String centralOfficePort = centralOffice.getPort(); 302 303 ConsultationImpl consultPort; 304 AgencyCheckAvailabilityServiceImpl availabilityPort; 305 org.objectweb.rentacar.services.client.AgencyReservationServiceImpl reservationPort; 306 307 URL consultWsdlLocation; 308 309 consultWsdlLocation = new URL ("http://"+centralOfficeHost+":"+centralOfficePort+"/rentacar-central-office/consultation?wsdl"); 310 logger.debug("http://"+centralOfficeHost+":"+centralOfficePort+"/rentacar-central-office/consultation?wsdl"); 311 QName consultName = new QName ("{http://consultation.services.centraloffice.rentacar.objectweb.org/}ConsultationImplPor"); 312 313 consultPort= new ConsultationImplService(consultWsdlLocation, consultName).getConsultationImplPort(); 314 315 317 XMLGregorianCalendar stDa = getDate(start.getTime()); 318 319 XMLGregorianCalendar enDa = getDate(end.getTime()); 320 321 agencyCriteria.setAgencyId("2"); 322 323 List <org.objectweb.rentacar.services.client.AgencyVO> agencyClose = consultPort.retreiveAgencies(agencyCriteria); 325 326 for (org.objectweb.rentacar.services.client.AgencyVO agencyVO : agencyClose) { 327 logger.debug("agencyVO "+ agencyVO); 328 } 329 String closeAgencyHost; 330 String closeAgencyPort; 331 String closeAgencyWarName; 332 Iterator <AgencyVO> it= agencyClose.iterator(); 333 334 while(it.hasNext() && (availableCarsAgency.size()< quantity)){ 335 337 org.objectweb.rentacar.services.client.AgencyVO currentAgency= it.next(); 338 logger.debug("agency "+ currentAgency.getAgencyId()); 339 closeAgencyHost = currentAgency.getHost(); 340 closeAgencyPort = currentAgency.getPort(); 341 closeAgencyWarName = currentAgency.getWarName(); 342 343 URL consultCloseAgenceLocation = new URL ("http://"+closeAgencyHost+":"+closeAgencyPort+"/"+closeAgencyWarName+"/availability?wsdl"); 345 QName consultAgencyName = new QName ("{http://availability.services.agency.rentacar.objectweb.org/}AgencyCheckAvailabilityServiceImplPort"); 346 availabilityPort = new AgencyCheckAvailabilityServiceImplService(consultCloseAgenceLocation, consultAgencyName).getAgencyCheckAvailabilityServiceImplPort(); 347 URL resaWsdlLocation = new URL ("http://"+closeAgencyHost+":"+closeAgencyPort+"/"+closeAgencyWarName+"/reservation?wsdl"); 349 QName resaName = new QName ("{http://reservation.services.agency.rentacar.objectweb.org/}AgencyReservationServiceImplPort"); 350 reservationPort = (org.objectweb.rentacar.services.client.AgencyReservationServiceImpl) new AgencyReservationServiceImplService(resaWsdlLocation, resaName).getAgencyReservationServiceImplPort(); 351 353 org.objectweb.rentacar.services.client.CarCriteria carCritCl= new org.objectweb.rentacar.services.client.CarCriteria(); 354 carCritCl.setType(carCrit.getType()); 355 carCritCl.setModel(carCrit.getModel()); 356 carCritCl.setBrand(carCrit.getBrand()); 357 358 List <org.objectweb.rentacar.services.client.CarVO> result; 359 result = availabilityPort.consultAvailability(carCritCl, stDa, enDa); 360 361 362 if (result.size() < 0 ){ 363 System.out.println("No car available for your criteria"); 364 } 365 else{ 366 367 System.out.println(" There is "+ result.size() + " cars matching your criteria"); 368 369 for (org.objectweb.rentacar.services.client.CarVO carVO : result) { 370 availableCarsAgency.add(new ElementsToReserve(reservationPort, carVO.getCarId())); 373 } 374 } 375 } 376 return availableCarsAgency; 377 }catch (DAOException e1) { 378 throw new AgencyException("Error retriving all central offices ", e1); 379 } 380 catch (MalformedURLException e) { 381 throw new AgencyException("Error in service URL"+e); 382 } catch (CentralOfficeException_Exception e) { 383 throw new AgencyException("Error retriving all agencies matching to agency criteria "+ agencyCriteria, e); 384 } catch (AgencyException_Exception e) { 385 throw new AgencyException("Error consulting available cars"+e); 386 } 387 } 388 389 390 391 } 392 | Popular Tags |