StdAir Logo  0.45.0
C++ Standard Airline IT Object Library
STDAIR_Service.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 #if BOOST_VERSION >= 104100
00008 // Boost Property Tree
00009 #include <boost/property_tree/ptree.hpp>
00010 #include <boost/property_tree/json_parser.hpp>
00011 #endif // BOOST_VERSION >= 104100
00012 // StdAir
00013 #include <stdair/stdair_types.hpp>
00014 #include <stdair/basic/BasChronometer.hpp>
00015 #include <stdair/bom/BomManager.hpp>
00016 #include <stdair/bom/BomRetriever.hpp>
00017 #include <stdair/bom/BomJSONExport.hpp>
00018 #include <stdair/bom/BomDisplay.hpp>
00019 #include <stdair/bom/BomRoot.hpp>
00020 #include <stdair/bom/EventQueue.hpp>
00021 #include <stdair/bom/EventStruct.hpp>
00022 #include <stdair/bom/BookingRequestStruct.hpp>
00023 #include <stdair/bom/DatePeriod.hpp>
00024 #include <stdair/command/CmdBomManager.hpp>
00025 #include <stdair/service/FacSupervisor.hpp>
00026 #include <stdair/service/FacSTDAIRServiceContext.hpp>
00027 #include <stdair/service/STDAIR_ServiceContext.hpp>
00028 #include <stdair/service/Logger.hpp>
00029 #include <stdair/service/DBSessionManager.hpp>
00030 #include <stdair/STDAIR_Service.hpp>
00031 
00032 #if BOOST_VERSION >= 104100
00033 namespace bpt = boost::property_tree;
00034 #else // BOOST_VERSION >= 104100
00035 namespace bpt {
00036   typedef char ptree;
00037 }
00038 #endif // BOOST_VERSION >= 104100
00039 
00040 namespace stdair {
00041 
00042   // //////////////////////////////////////////////////////////////////////
00043   STDAIR_Service::STDAIR_Service() : _stdairServiceContext (NULL) {
00044 
00045     // Initialise the service context
00046     initServiceContext();
00047     
00048     // Initialise the (remaining of the) context
00049     init();
00050   }
00051 
00052   // //////////////////////////////////////////////////////////////////////
00053   STDAIR_Service::STDAIR_Service (const STDAIR_Service& iService) 
00054     : _stdairServiceContext (NULL) {
00055     assert (false);
00056   }
00057 
00058   // //////////////////////////////////////////////////////////////////////
00059   STDAIR_Service::STDAIR_Service (const BasLogParams& iLogParams)  
00060     : _stdairServiceContext (NULL) {
00061 
00062     // Initialise the service context
00063     initServiceContext();
00064     
00065     // Set the log file
00066     logInit (iLogParams);
00067 
00068     // Initialise the (remaining of the) context
00069     init();
00070   }
00071 
00072   // //////////////////////////////////////////////////////////////////////
00073   STDAIR_Service::STDAIR_Service (const BasLogParams& iLogParams,
00074                                   const BasDBParams& iDBParams) 
00075     : _stdairServiceContext (NULL) { 
00076 
00077     // Initialise the service context
00078     initServiceContext();
00079 
00080     // Set the log file
00081     logInit (iLogParams);
00082 
00083     // Create a database session
00084     dbInit (iDBParams);
00085 
00086     // Initialise the (remaining of the) context
00087     init();
00088   }
00089 
00090   // //////////////////////////////////////////////////////////////////////
00091   STDAIR_Service::~STDAIR_Service() {
00092     // Delete/Clean all the objects from memory
00093     finalise();
00094   }
00095 
00096   // //////////////////////////////////////////////////////////////////////
00097   void STDAIR_Service::initServiceContext() {
00098     // Initialise the service context
00099     STDAIR_ServiceContext& lSTDAIR_ServiceContext = 
00100       FacSTDAIRServiceContext::instance().create();
00101 
00102     // Store the stdair service context
00103     _stdairServiceContext = &lSTDAIR_ServiceContext;
00104   }
00105 
00106   // //////////////////////////////////////////////////////////////////////
00107   void STDAIR_Service::logInit (const BasLogParams& iLogParams) {
00108     Logger::init (iLogParams);
00109   }
00110 
00111   // //////////////////////////////////////////////////////////////////////
00112   void STDAIR_Service::dbInit (const BasDBParams& iDBParams) {
00113     DBSessionManager::init (iDBParams);
00114 
00115     // Store the database parameters into the StdAir service context
00116     assert (_stdairServiceContext != NULL);
00117     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00118     lSTDAIR_ServiceContext.setDBParams (iDBParams);
00119   }
00120 
00121   // //////////////////////////////////////////////////////////////////////
00122   void STDAIR_Service::init() {
00123   }
00124   
00125   // //////////////////////////////////////////////////////////////////////
00126   BomRoot& STDAIR_Service::getBomRoot() const {
00127     // Retrieve the StdAir service context
00128     assert (_stdairServiceContext != NULL);
00129     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00130     return lSTDAIR_ServiceContext.getBomRoot();
00131   }
00132 
00133   // //////////////////////////////////////////////////////////////////////
00134   EventQueue& STDAIR_Service::getEventQueue() const {
00135     // Retrieve the StdAir service context
00136     assert (_stdairServiceContext != NULL);
00137     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00138     return lSTDAIR_ServiceContext.getEventQueue();
00139   }
00140 
00141   // //////////////////////////////////////////////////////////////////////
00142   BasLogParams STDAIR_Service::getLogParams() const {
00143     return Logger::getLogParams();
00144   }
00145 
00146   // //////////////////////////////////////////////////////////////////////
00147   const BasDBParams& STDAIR_Service::getDBParams() const {
00148     // Retrieve the StdAir service context
00149     assert (_stdairServiceContext != NULL);
00150     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00151     return lSTDAIR_ServiceContext.getDBParams();
00152   }
00153   
00154   // //////////////////////////////////////////////////////////////////////
00155   const ServiceInitialisationType& STDAIR_Service::
00156   getServiceInitialisationType() const {
00157     // Retrieve the StdAir service context
00158     assert (_stdairServiceContext != NULL);
00159     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00160     return lSTDAIR_ServiceContext.getServiceInitialisationType();
00161   }
00162 
00163   // //////////////////////////////////////////////////////////////////////
00164   void STDAIR_Service::buildSampleBom() {
00165     // Retrieve the StdAir service context
00166     assert (_stdairServiceContext != NULL);
00167     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00168 
00169     // Retrieve the BOM tree root
00170     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00171     
00172     // Delegate the building process to the dedicated command
00173     CmdBomManager::buildSampleBom (lBomRoot);
00174   }
00175 
00176   // //////////////////////////////////////////////////////////////////////
00177   void STDAIR_Service::
00178   buildDummyInventory (const CabinCapacity_T& iCabinCapacity) {
00179     // Retrieve the StdAir service context
00180     assert (_stdairServiceContext != NULL);
00181     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00182 
00183     // Retrieve the BOM tree root
00184     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00185     
00186     // Delegate the building process to the dedicated command
00187     CmdBomManager::buildDummyInventory (lBomRoot, iCabinCapacity);
00188   }
00189 
00190   // //////////////////////////////////////////////////////////////////////
00191   void STDAIR_Service::
00192   buildSampleTravelSolutionForPricing (TravelSolutionList_T& ioTravelSolutionList) {
00193     // Build a sample list of travel solution structures
00194     CmdBomManager::buildSampleTravelSolutionForPricing (ioTravelSolutionList);
00195   }
00196 
00197   // //////////////////////////////////////////////////////////////////////
00198   void STDAIR_Service::
00199   buildSampleTravelSolutions (TravelSolutionList_T& ioTravelSolutionList) {
00200     // Build a sample list of travel solution structures
00201     CmdBomManager::buildSampleTravelSolutions (ioTravelSolutionList);
00202   }
00203 
00204   // //////////////////////////////////////////////////////////////////////
00205   BookingRequestStruct STDAIR_Service::
00206   buildSampleBookingRequest (const bool isForCRS) {
00207 
00208     // Build a sample booking request structure
00209     if (isForCRS == true) {
00210       return CmdBomManager::buildSampleBookingRequestForCRS();
00211     }
00212 
00213     return CmdBomManager::buildSampleBookingRequest();
00214   }
00215 
00216   // //////////////////////////////////////////////////////////////////////
00217   std::string STDAIR_Service::
00218   jsonExport (const stdair::AirlineCode_T& iAirlineCode,
00219               const stdair::FlightNumber_T& iFlightNumber,
00220               const stdair::Date_T& iDepartureDate) const {
00221     std::ostringstream oStr;
00222 
00223     // Retrieve the StdAir service context
00224     assert (_stdairServiceContext != NULL);
00225     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00226 
00227     // Retrieve the BOM tree root
00228     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00229 
00230     // Retrieve the flight-date object corresponding to the key
00231     FlightDate* lFlightDate_ptr = 
00232       BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
00233                                                   iFlightNumber, iDepartureDate);
00234 
00235     // Dump the content of the whole BOM tree into the string
00236     if (lFlightDate_ptr != NULL) {
00237       BomJSONExport::jsonExport (oStr, *lFlightDate_ptr);
00238       
00239     } else {
00240 #if BOOST_VERSION >= 104100
00241       //
00242       bpt::ptree lPropertyTree;
00243       
00244       // Build the appropriate message, so that the client may know that
00245       // no flight-date can be found for that given key.
00246       std::ostringstream oNoFlightDateStream;
00247       oNoFlightDateStream << "No flight-date found for the given key: '"
00248                           << iAirlineCode << iFlightNumber
00249                           << " - " << iDepartureDate << "'";
00250       const std::string oNoFlightDateString (oNoFlightDateStream.str());
00251 
00252       // Put in the property tree the fact that no flight-date has been found.
00253       // \note That is not (necessary) an error.
00254       lPropertyTree.put ("error", oNoFlightDateString.c_str());
00255 
00256       // Write the property tree into the JSON stream.
00257       write_json (oStr, lPropertyTree);
00258 #endif // BOOST_VERSION >= 104100
00259     }
00260     
00261     return oStr.str();
00262   }
00263 
00264   // //////////////////////////////////////////////////////////////////////
00265   std::string STDAIR_Service::list (const AirlineCode_T& iAirlineCode,
00266                                     const FlightNumber_T& iFlightNumber) const {
00267     std::ostringstream oStr;
00268 
00269     // Retrieve the StdAir service context
00270     assert (_stdairServiceContext != NULL);
00271     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00272 
00273     // Retrieve the BOM tree root
00274     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00275     
00276     // Dump the content of the whole BOM tree into the string
00277     BomDisplay::list (oStr, lBomRoot, iAirlineCode, iFlightNumber);
00278     
00279     return oStr.str();
00280   }
00281 
00282   // //////////////////////////////////////////////////////////////////////
00283   std::string STDAIR_Service::listAirportPairDateRange () const {
00284     std::ostringstream oStr;
00285 
00286     // Retrieve the StdAir service context
00287     assert (_stdairServiceContext != NULL);
00288     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00289 
00290     // Retrieve the BOM tree root
00291     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00292     
00293     // Dump the content of the whole BOM tree into the string
00294     BomDisplay::listAirportPairDateRange (oStr, lBomRoot);
00295     
00296     return oStr.str();
00297   }
00298 
00299   // //////////////////////////////////////////////////////////////////////
00300   bool STDAIR_Service::check (const AirlineCode_T& iAirlineCode,
00301                               const FlightNumber_T& iFlightNumber,
00302                               const stdair::Date_T& iDepartureDate) const {
00303     std::ostringstream oStr;
00304 
00305     // Retrieve the StdAir service context
00306     assert (_stdairServiceContext != NULL);
00307     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00308 
00309     // Retrieve the BOM tree root
00310     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00311     
00312     // Dump the content of the whole BOM tree into the string
00313     const FlightDate* lFlightDate_ptr =
00314       BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
00315                                                   iFlightNumber,
00316                                                   iDepartureDate);    
00317     
00318     return (lFlightDate_ptr != NULL);
00319   }
00320 
00321   // //////////////////////////////////////////////////////////////////////
00322   bool STDAIR_Service::check (const stdair::AirportCode_T& ioOrigin,
00323                               const stdair::AirportCode_T& ioDestination,
00324                               const stdair::Date_T& ioDepartureDate) const {
00325     std::ostringstream oStr;
00326 
00327     // Retrieve the StdAir service context
00328     assert (_stdairServiceContext != NULL);
00329     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00330 
00331     // Retrieve the BOM tree root
00332     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00333     
00334     // Dump the content of the whole BOM tree into the string
00335     stdair::DatePeriodList_T lDatePeriodList;
00336     BomRetriever::retrieveDatePeriodListFromKeySet  (lBomRoot, ioOrigin,
00337                                                      ioDestination,
00338                                                      ioDepartureDate,
00339                                                      lDatePeriodList);    
00340     
00341     return (lDatePeriodList.size() != 0);
00342   }
00343 
00344   // //////////////////////////////////////////////////////////////////////
00345   std::string STDAIR_Service::csvDisplay() const {
00346     std::ostringstream oStr;
00347 
00348     // Retrieve the StdAir service context
00349     assert (_stdairServiceContext != NULL);
00350     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00351 
00352     // Retrieve the BOM tree root
00353     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00354     
00355     // Dump the content of the whole BOM tree into the string
00356     BomDisplay::csvDisplay (oStr, lBomRoot);
00357     
00358     return oStr.str();
00359   }
00360 
00361   // //////////////////////////////////////////////////////////////////////
00362   std::string STDAIR_Service::
00363   csvDisplay (const stdair::AirlineCode_T& iAirlineCode,
00364               const stdair::FlightNumber_T& iFlightNumber,
00365               const stdair::Date_T& iDepartureDate) const {
00366     std::ostringstream oStr;
00367 
00368     // Retrieve the StdAir service context
00369     assert (_stdairServiceContext != NULL);
00370     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00371 
00372     // Retrieve the BOM tree root
00373     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00374 
00375     // Retrieve the flight-date object corresponding to the key
00376     FlightDate* lFlightDate_ptr = 
00377       BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
00378                                                   iFlightNumber, iDepartureDate);
00379 
00380     // Dump the content of the whole BOM tree into the string
00381     if (lFlightDate_ptr != NULL) {
00382       BomDisplay::csvDisplay (oStr, *lFlightDate_ptr);
00383       
00384     } else {
00385       oStr << "   No flight-date found for the given key: '"
00386            << iAirlineCode << iFlightNumber << " - " << iDepartureDate << "'";
00387     }
00388     
00389     return oStr.str();
00390   }
00391 
00392   // //////////////////////////////////////////////////////////////////////
00393   std::string STDAIR_Service::
00394   csvDisplay (const TravelSolutionList_T& iTravelSolutionList) const {
00395 
00396     // Dump the content of the whole list of travel solutions into the string
00397     std::ostringstream oStr;
00398     BomDisplay::csvDisplay (oStr, iTravelSolutionList);
00399     
00400     return oStr.str();
00401   }
00402 
00403   // //////////////////////////////////////////////////////////////////////
00404   std::string STDAIR_Service::
00405   csvDisplay (const stdair::AirportCode_T& iOrigin,
00406               const stdair::AirportCode_T& iDestination,
00407               const stdair::Date_T& iDepartureDate) const {
00408     std::ostringstream oStr;
00409 
00410     // Retrieve the StdAir service context
00411     assert (_stdairServiceContext != NULL);
00412     const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00413 
00414     // Retrieve the BOM tree root
00415     BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
00416 
00417     // Retrieve the flight-date object corresponding to the key
00418     DatePeriodList_T lDatePeriodList;
00419     BomRetriever::retrieveDatePeriodListFromKeySet (lBomRoot, iOrigin,
00420                                                     iDestination, iDepartureDate,
00421                                                     lDatePeriodList);
00422 
00423     // Dump the content of the whole BOM tree into the string
00424     if (lDatePeriodList.empty()) {
00425       oStr << "   No fare-rule found for the given key: '"
00426             << iOrigin << "-" << iDestination << " - " << iDepartureDate << "'";
00427     } else {
00428       BomDisplay::csvDisplay (oStr, lDatePeriodList);
00429     }
00430     
00431     return oStr.str();
00432   }
00433 
00434   // //////////////////////////////////////////////////////////////////////
00435   void STDAIR_Service::finalise() {
00436     // Clean all the objects
00437     FacSupervisor::cleanAll();
00438   }
00439 
00440   // ////////////////////////////////////////////////////////////////////
00441   const Count_T& STDAIR_Service::
00442   getExpectedTotalNumberOfEventsToBeGenerated() const {
00443     
00444     // Retrieve the StdAir service context
00445     assert (_stdairServiceContext != NULL);
00446     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00447 
00448     // Retrieve the event queue object instance
00449     const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00450     
00451     // Delegate the call to the dedicated command
00452     const Count_T& oExpectedTotalNumberOfEventsToBeGenerated =
00453       lQueue.getExpectedTotalNbOfEvents();
00454 
00455     //
00456     return oExpectedTotalNumberOfEventsToBeGenerated;
00457   }
00458 
00459   // ////////////////////////////////////////////////////////////////////
00460   const Count_T& STDAIR_Service::
00461   getExpectedTotalNumberOfEventsToBeGenerated (const EventType::EN_EventType& iType) const {
00462     
00463     // Retrieve the StdAir service context
00464     assert (_stdairServiceContext != NULL);
00465     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00466 
00467     // Retrieve the event queue object instance
00468     const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00469     
00470     // Delegate the call to the dedicated command
00471     const Count_T& oExpectedTotalNumberOfEventsToBeGenerated =
00472       lQueue.getExpectedTotalNbOfEvents (iType);
00473 
00474     //
00475     return oExpectedTotalNumberOfEventsToBeGenerated;
00476   }
00477 
00478   // ////////////////////////////////////////////////////////////////////
00479   const Count_T& STDAIR_Service::
00480   getActualTotalNumberOfEventsToBeGenerated() const {
00481     
00482     // Retrieve the StdAir service context
00483     assert (_stdairServiceContext != NULL);
00484     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00485 
00486     // Retrieve the event queue object instance
00487     const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00488     
00489     // Delegate the call to the dedicated command
00490     const Count_T& oActualTotalNumberOfEventsToBeGenerated =
00491       lQueue.getActualTotalNbOfEvents();
00492 
00493     //
00494     return oActualTotalNumberOfEventsToBeGenerated;
00495   }
00496 
00497   // ////////////////////////////////////////////////////////////////////
00498   const Count_T& STDAIR_Service::
00499   getActualTotalNumberOfEventsToBeGenerated (const EventType::EN_EventType& iType) const {
00500     
00501     // Retrieve the StdAir service context
00502     assert (_stdairServiceContext != NULL);
00503     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00504 
00505     // Retrieve the event queue object instance
00506     const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00507     
00508     // Delegate the call to the dedicated command
00509     const Count_T& oActualTotalNumberOfEventsToBeGenerated =
00510       lQueue.getActualTotalNbOfEvents (iType);
00511 
00512     //
00513     return oActualTotalNumberOfEventsToBeGenerated;
00514   }
00515 
00516   // ////////////////////////////////////////////////////////////////////
00517   ProgressStatusSet STDAIR_Service::popEvent (EventStruct& ioEventStruct) const {
00518 
00519     // Retrieve the StdAir service context
00520     assert (_stdairServiceContext != NULL);
00521     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00522 
00523     // Retrieve the event queue object instance
00524     EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00525     
00526     // Extract the next event from the queue
00527     return lQueue.popEvent (ioEventStruct);
00528   }
00529 
00530   // ////////////////////////////////////////////////////////////////////
00531   bool STDAIR_Service::isQueueDone() const {
00532 
00533     // Retrieve the StdAir service context
00534     assert (_stdairServiceContext != NULL);
00535     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00536 
00537     // Retrieve the event queue object instance
00538     const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00539     
00540     // Calculates whether the event queue has been fully emptied
00541     const bool isQueueDone = lQueue.isQueueDone();
00542 
00543     //
00544     return isQueueDone;
00545   }
00546 
00547   // ////////////////////////////////////////////////////////////////////
00548   void STDAIR_Service::reset() const {
00549 
00550     // Retrieve the StdAir service context
00551     assert (_stdairServiceContext != NULL);
00552     STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
00553 
00554     // Retrieve the event queue object instance
00555     EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
00556     
00557     // Delegate the call to the event queue object
00558     lQueue.reset();
00559   }
00560 
00561 }