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