$treeview $search $mathjax
SimFQT Logo  1.00.0
$projectbrief
$projectbrief
$searchbox

SIMFQT_Service.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 // Boost
00007 #include <boost/make_shared.hpp>
00008 // StdAir
00009 #include <stdair/basic/BasChronometer.hpp>
00010 #include <stdair/bom/BomDisplay.hpp>
00011 #include <stdair/bom/TravelSolutionStruct.hpp>
00012 #include <stdair/bom/BookingRequestStruct.hpp>
00013 #include <stdair/service/Logger.hpp>
00014 #include <stdair/STDAIR_Service.hpp>
00015 // Simfqt
00016 #include <simfqt/basic/BasConst_SIMFQT_Service.hpp>
00017 #include <simfqt/factory/FacSimfqtServiceContext.hpp>
00018 #include <simfqt/command/FareParser.hpp>
00019 #include <simfqt/command/FareQuoter.hpp>
00020 #include <simfqt/service/SIMFQT_ServiceContext.hpp>
00021 #include <simfqt/SIMFQT_Service.hpp>
00022 
00023 namespace SIMFQT {
00024 
00025   // //////////////////////////////////////////////////////////////////////
00026   SIMFQT_Service::SIMFQT_Service() : _simfqtServiceContext (NULL) {
00027     assert (false);
00028   }
00029 
00030   // //////////////////////////////////////////////////////////////////////
00031   SIMFQT_Service::SIMFQT_Service (const SIMFQT_Service& iService) {
00032     assert (false);
00033   }
00034 
00035   // ////////////////////////////////////////////////////////////////////
00036   SIMFQT_Service::SIMFQT_Service (const stdair::BasLogParams& iLogParams)
00037     : _simfqtServiceContext (NULL) {
00038     
00039     // Initialise the STDAIR service handler
00040     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00041       initStdAirService (iLogParams);
00042     
00043     // Initialise the service context
00044     initServiceContext();
00045 
00046     // Add the StdAir service context to the SIMFQT service context
00047     // \note SIMFQT owns the STDAIR service resources here.
00048     const bool ownStdairService = true;
00049     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00050     
00051     // Initialise the (remaining of the) context
00052     initSimfqtService();
00053   }
00054 
00055   // ////////////////////////////////////////////////////////////////////
00056   SIMFQT_Service::SIMFQT_Service (const stdair::BasLogParams& iLogParams,
00057                                   const stdair::BasDBParams& iDBParams)
00058     : _simfqtServiceContext (NULL) {
00059     
00060     // Initialise the STDAIR service handler
00061     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr =
00062       initStdAirService (iLogParams, iDBParams);
00063     
00064     // Initialise the service context
00065     initServiceContext();
00066 
00067     // Add the StdAir service context to the SIMFQT service context
00068     // \note SIMFQT owns the STDAIR service resources here.
00069     const bool ownStdairService = true;
00070     addStdAirService (lSTDAIR_Service_ptr, ownStdairService);
00071     
00072     // Initialise the (remaining of the) context
00073     initSimfqtService();
00074   }
00075 
00076   // ////////////////////////////////////////////////////////////////////
00077   SIMFQT_Service::
00078   SIMFQT_Service (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr)
00079     : _simfqtServiceContext (NULL) {
00080 
00081     // Initialise the service context
00082     initServiceContext();
00083 
00084     // Store the STDAIR service object within the (SIMFQT) service context
00085     // \note Simfqt does not own the STDAIR service resources here.
00086     const bool doesNotOwnStdairService = false;
00087     addStdAirService (ioSTDAIR_Service_ptr, doesNotOwnStdairService);
00088     
00089     // Initialise the context
00090     initSimfqtService();
00091   }
00092 
00093   // //////////////////////////////////////////////////////////////////////
00094   SIMFQT_Service::~SIMFQT_Service() {
00095     // Delete/Clean all the objects from memory
00096     finalise();
00097   }
00098 
00099   // //////////////////////////////////////////////////////////////////////
00100   void SIMFQT_Service::finalise() {
00101     assert (_simfqtServiceContext != NULL);
00102     // Reset the (Boost.)Smart pointer pointing on the STDAIR_Service object.
00103     _simfqtServiceContext->reset();
00104   }
00105 
00106   // //////////////////////////////////////////////////////////////////////
00107   void SIMFQT_Service::initServiceContext() {
00108     // Initialise the service context
00109     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = 
00110       FacSimfqtServiceContext::instance().create();
00111     _simfqtServiceContext = &lSIMFQT_ServiceContext;
00112   }
00113 
00114   // ////////////////////////////////////////////////////////////////////
00115   void SIMFQT_Service::
00116   addStdAirService (stdair::STDAIR_ServicePtr_T ioSTDAIR_Service_ptr,
00117                     const bool iOwnStdairService) {
00118 
00119     // Retrieve the SimFQT service context
00120     assert (_simfqtServiceContext != NULL);
00121     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00122 
00123     // Store the STDAIR service object within the (SimFQT) service context
00124     lSIMFQT_ServiceContext.setSTDAIR_Service (ioSTDAIR_Service_ptr,
00125                                               iOwnStdairService);
00126   }
00127   
00128   // //////////////////////////////////////////////////////////////////////
00129   stdair::STDAIR_ServicePtr_T SIMFQT_Service::
00130   initStdAirService (const stdair::BasLogParams& iLogParams,
00131                      const stdair::BasDBParams& iDBParams) {
00132 
00139     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00140       boost::make_shared<stdair::STDAIR_Service> (iLogParams, iDBParams);
00141     assert (lSTDAIR_Service_ptr != NULL);
00142     
00143     return lSTDAIR_Service_ptr;
00144   }
00145   
00146   // //////////////////////////////////////////////////////////////////////
00147   stdair::STDAIR_ServicePtr_T SIMFQT_Service::
00148   initStdAirService (const stdair::BasLogParams& iLogParams) {
00149 
00156     stdair::STDAIR_ServicePtr_T lSTDAIR_Service_ptr = 
00157       boost::make_shared<stdair::STDAIR_Service> (iLogParams);
00158     assert (lSTDAIR_Service_ptr != NULL);
00159 
00160     return lSTDAIR_Service_ptr;
00161   }
00162   
00163   // ////////////////////////////////////////////////////////////////////
00164   void SIMFQT_Service::initSimfqtService() {
00165     // Do nothing at this stage. A sample BOM tree may be built by
00166     // calling the buildSampleBom() method
00167   }
00168 
00169   // ////////////////////////////////////////////////////////////////////
00170   void SIMFQT_Service::
00171   parseAndLoad (const FareFilePath& iFareFilename) { 
00172 
00173     // Retrieve the SimFQT service context
00174     if (_simfqtServiceContext == NULL) {
00175       throw stdair::NonInitialisedServiceException ("The SimFQT service "
00176                                                     "has not been initialised");
00177     }
00178     assert (_simfqtServiceContext != NULL);
00179 
00180     // Retrieve the SimFQT service context and whether it owns the Stdair
00181     // service
00182     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00183     const bool doesOwnStdairService =
00184       lSIMFQT_ServiceContext.getOwnStdairServiceFlag();
00185 
00186     // Retrieve the StdAir service object from the (SimFQT) service context
00187     stdair::STDAIR_Service& lSTDAIR_Service =
00188       lSIMFQT_ServiceContext.getSTDAIR_Service();
00189 
00190     // Retrieve the persistent BOM root object.
00191     stdair::BomRoot& lPersistentBomRoot = 
00192       lSTDAIR_Service.getPersistentBomRoot();
00193     
00197     FareParser::fareRuleGeneration (iFareFilename, lPersistentBomRoot);    
00198 
00210     buildComplementaryLinks (lPersistentBomRoot);
00211 
00216     if (doesOwnStdairService == true) {
00217       //
00218       clonePersistentBom ();
00219     }
00220   }
00221  
00222   // ////////////////////////////////////////////////////////////////////
00223   void SIMFQT_Service::buildSampleBom() {
00224 
00225     // Retrieve the SimFQT service context
00226     if (_simfqtServiceContext == NULL) {
00227       throw stdair::NonInitialisedServiceException ("The SimFQT service "
00228                                                     "has not been initialised");
00229     }
00230     assert (_simfqtServiceContext != NULL);
00231 
00232     // Retrieve the SimFQT service context and whether it owns the Stdair
00233     // service
00234     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00235     const bool doesOwnStdairService =
00236       lSIMFQT_ServiceContext.getOwnStdairServiceFlag();
00237 
00238     // Retrieve the StdAir service object from the (SimFQT) service context
00239     stdair::STDAIR_Service& lSTDAIR_Service =
00240       lSIMFQT_ServiceContext.getSTDAIR_Service(); 
00241 
00242     // Retrieve the persistent BOM root object.
00243     stdair::BomRoot& lPersistentBomRoot = 
00244       lSTDAIR_Service.getPersistentBomRoot();
00245     
00250     if (doesOwnStdairService == true) {
00251       //
00252       lSTDAIR_Service.buildSampleBom();
00253     } 
00254 
00266     buildComplementaryLinks (lPersistentBomRoot);
00267 
00272     if (doesOwnStdairService == true) {
00273       //
00274       clonePersistentBom ();
00275     }
00276   } 
00277 
00278   // ////////////////////////////////////////////////////////////////////
00279   void SIMFQT_Service::clonePersistentBom () {
00280 
00281     // Retrieve the SimFQT service context
00282     if (_simfqtServiceContext == NULL) {
00283       throw stdair::NonInitialisedServiceException ("The SimFQT service "
00284                                                     "has not been initialised");
00285     }
00286     assert (_simfqtServiceContext != NULL);
00287 
00288     // Retrieve the SimFQT service context and whether it owns the Stdair
00289     // service
00290     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00291     const bool doesOwnStdairService =
00292       lSIMFQT_ServiceContext.getOwnStdairServiceFlag();
00293 
00294     // Retrieve the StdAir service object from the (SimFQT) service context
00295     stdair::STDAIR_Service& lSTDAIR_Service =
00296       lSIMFQT_ServiceContext.getSTDAIR_Service();
00297     
00302     if (doesOwnStdairService == true) {
00303       //
00304       lSTDAIR_Service.clonePersistentBom (); 
00305     }  
00306 
00310     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();   
00311     buildComplementaryLinks (lBomRoot);
00312   } 
00313 
00314   // ////////////////////////////////////////////////////////////////////
00315   void SIMFQT_Service::buildComplementaryLinks (stdair::BomRoot& ioBomRoot) {
00316     // Currently, no more things to do by SimFQT at that stage.
00317   }
00318 
00319   // //////////////////////////////////////////////////////////////////////
00320   stdair::BookingRequestStruct SIMFQT_Service::buildBookingRequest(const bool isForCRS) {
00321 
00322     // Retrieve the SIMFQT service context
00323     if (_simfqtServiceContext == NULL) {
00324       throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
00325                                                     "been initialised");
00326     }
00327     assert (_simfqtServiceContext != NULL);
00328 
00329     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00330   
00331     // Retrieve the STDAIR service object from the (Simfqt) service context
00332     stdair::STDAIR_Service& lSTDAIR_Service =
00333       lSIMFQT_ServiceContext.getSTDAIR_Service();
00334 
00335     // Delegate the BOM building to the dedicated service
00336     stdair::BookingRequestStruct oBookingRequest =
00337       lSTDAIR_Service.buildSampleBookingRequest (isForCRS);
00338 
00339     return oBookingRequest;
00340   }
00341 
00342   // //////////////////////////////////////////////////////////////////////
00343   void SIMFQT_Service::
00344   buildSampleTravelSolutions(stdair::TravelSolutionList_T& ioTravelSolutionList){
00345 
00346     // Retrieve the SIMFQT service context
00347     if (_simfqtServiceContext == NULL) {
00348       throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
00349                                                     "been initialised");
00350     }
00351     assert (_simfqtServiceContext != NULL);
00352 
00353     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00354     
00355     // Retrieve the STDAIR service object from the (Simfqt) service context
00356     stdair::STDAIR_Service& lSTDAIR_Service =
00357       lSIMFQT_ServiceContext.getSTDAIR_Service();
00358 
00359     // Delegate the BOM building to the dedicated service
00360     lSTDAIR_Service.buildSampleTravelSolutionForPricing (ioTravelSolutionList);
00361   }
00362 
00363 
00364   // ////////////////////////////////////////////////////////////////////
00365   std::string SIMFQT_Service::csvDisplay() const {
00366 
00367     // Retrieve the SIMFQT service context
00368     if (_simfqtServiceContext == NULL) {
00369       throw stdair::NonInitialisedServiceException ("The SimFQT service "
00370                                                     "has not been initialised");
00371     }
00372     assert (_simfqtServiceContext != NULL);
00373 
00374     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00375   
00376     // Retrieve the STDAIR service object from the (SimFQT) service context
00377     stdair::STDAIR_Service& lSTDAIR_Service =
00378       lSIMFQT_ServiceContext.getSTDAIR_Service();
00379 
00380     // Get the root of the BOM tree, on which all of the other BOM objects
00381     // are attached
00382     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00383 
00384     // Delegate the BOM display to the dedicated service
00385     std::ostringstream oCSVStr;
00386     stdair::BomDisplay::csvSimFQTAirRACDisplay (oCSVStr, lBomRoot);
00387     return oCSVStr.str(); 
00388   }
00389 
00390   // //////////////////////////////////////////////////////////////////////
00391   std::string SIMFQT_Service::
00392   csvDisplay (const stdair::TravelSolutionList_T& ioTravelSolutionList) const {
00393 
00394     // Retrieve the Simfqt service context
00395     if (_simfqtServiceContext == NULL) {
00396       throw stdair::NonInitialisedServiceException ("The Simfqt service has not "
00397                                                     "been initialised");
00398     }
00399     assert (_simfqtServiceContext != NULL);
00400 
00401     // Retrieve the Simfqt service context
00402     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00403   
00404     // Retrieve the STDAIR service object from the (Simfqt) service context
00405     stdair::STDAIR_Service& lSTDAIR_Service =
00406       lSIMFQT_ServiceContext.getSTDAIR_Service();
00407 
00408     // Delegate the BOM building to the dedicated service
00409     return lSTDAIR_Service.csvDisplay (ioTravelSolutionList);
00410   }
00411 
00412   // //////////////////////////////////////////////////////////////////////
00413   std::string SIMFQT_Service::
00414   csvDisplay (const stdair::AirportCode_T& iOrigin,
00415               const stdair::AirportCode_T& iDestination,
00416               const stdair::Date_T& iDepartureDate) const {
00417 
00418     // Retrieve the SIMFQT service context
00419     if (_simfqtServiceContext == NULL) {
00420       throw stdair::NonInitialisedServiceException ("The Simfqt service "
00421                                                     "has not been initialised");
00422     }
00423     assert (_simfqtServiceContext != NULL);
00424 
00425     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00426   
00427     // Retrieve the STDAIR service object from the (SIMFQT) service context
00428     stdair::STDAIR_Service& lSTDAIR_Service =
00429       lSIMFQT_ServiceContext.getSTDAIR_Service();
00430 
00431     // Delegate the BOM display to the dedicated service
00432     return lSTDAIR_Service.csvDisplay (iOrigin, iDestination,
00433                                        iDepartureDate);
00434   }
00435 
00436   // //////////////////////////////////////////////////////////////////////
00437   std::string SIMFQT_Service::list() const {
00438 
00439     // Retrieve the SIMFQT service context
00440     if (_simfqtServiceContext == NULL) {
00441       throw stdair::NonInitialisedServiceException ("The Simfqt service "
00442                                                     "has not been initialised");
00443     }
00444     assert (_simfqtServiceContext != NULL);
00445 
00446     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00447   
00448     // Retrieve the STDAIR service object from the (SIMFQT) service context
00449     stdair::STDAIR_Service& lSTDAIR_Service =
00450       lSIMFQT_ServiceContext.getSTDAIR_Service();
00451 
00452     // Delegate the BOM display to the dedicated service
00453     return lSTDAIR_Service.listAirportPairDateRange ();
00454   }
00455 
00456   // ////////////////////////////////////////////////////////////////////
00457   bool SIMFQT_Service::
00458   check (const stdair::AirportCode_T& iOrigin,
00459          const stdair::AirportCode_T& iDestination,
00460          const stdair::Date_T& iDepartureDate) const {
00461     std::ostringstream oFlightListStr;
00462 
00463     if (_simfqtServiceContext == NULL) {
00464       throw stdair::NonInitialisedServiceException ("The Simfqt service "
00465                                                     "has not been initialised");
00466     }
00467     assert (_simfqtServiceContext != NULL);
00468     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00469 
00470     // Retrieve the STDAIR service object from the (SIMFQT) service context
00471     stdair::STDAIR_Service& lSTDAIR_Service =
00472       lSIMFQT_ServiceContext.getSTDAIR_Service();
00473 
00474     // Delegate the BOM display to the dedicated service
00475     return lSTDAIR_Service.check (iOrigin, iDestination, iDepartureDate);
00476   }
00477 
00478   // ////////////////////////////////////////////////////////////////////
00479   void SIMFQT_Service::
00480   quotePrices (const stdair::BookingRequestStruct& iBookingRequest,
00481                stdair::TravelSolutionList_T& ioTravelSolutionList) {
00482     
00483     // Retrieve the Simfqt service context
00484     if (_simfqtServiceContext == NULL) {
00485       throw stdair::NonInitialisedServiceException ("The SimFQT service "
00486                                                     "has not been initialised");
00487     }
00488     assert (_simfqtServiceContext != NULL);
00489 
00490     SIMFQT_ServiceContext& lSIMFQT_ServiceContext = *_simfqtServiceContext;
00491 
00492     // Retrieve the StdAir service context
00493     stdair::STDAIR_Service& lSTDAIR_Service =
00494       lSIMFQT_ServiceContext.getSTDAIR_Service();
00495     
00496     // Get the root of the BOM tree, on which all of the other BOM objects
00497     // will be attached
00498     stdair::BomRoot& lBomRoot = lSTDAIR_Service.getBomRoot();
00499 
00500     // Delegate the action to the dedicated command
00501     stdair::BasChronometer lFareQuoteRetrievalChronometer;
00502     lFareQuoteRetrievalChronometer.start();
00503     FareQuoter::priceQuote (iBookingRequest, ioTravelSolutionList, lBomRoot);
00504 
00505     // DEBUG
00506     const double lFareQuoteRetrievalMeasure =
00507       lFareQuoteRetrievalChronometer.elapsed();
00508     STDAIR_LOG_DEBUG ("Fare Quote retrieving: " << lFareQuoteRetrievalMeasure
00509                       << " - " << lSIMFQT_ServiceContext.display());
00510   }
00511   
00512 }