StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
BomRetriever.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // StdAir
00008 #include <stdair/basic/BasConst_Inventory.hpp>
00009 #include <stdair/bom/BomKeyManager.hpp>
00010 #include <stdair/bom/BomManager.hpp>
00011 #include <stdair/bom/BomRoot.hpp>
00012 #include <stdair/bom/Inventory.hpp>
00013 #include <stdair/bom/FlightDate.hpp>
00014 #include <stdair/bom/LegDate.hpp>
00015 #include <stdair/bom/SegmentDate.hpp>
00016 #include <stdair/bom/LegCabin.hpp>
00017 #include <stdair/bom/SegmentCabin.hpp>
00018 #include <stdair/bom/FareFamily.hpp>
00019 #include <stdair/bom/BookingClass.hpp>
00020 #include <stdair/bom/BomRetriever.hpp>
00021 #include <stdair/bom/ParsedKey.hpp>
00022 #include <stdair/bom/AirportPair.hpp>
00023 #include <stdair/service/Logger.hpp>
00024 
00025 namespace stdair {
00026 
00027   // ////////////////////////////////////////////////////////////////////
00028   Inventory* BomRetriever::
00029   retrieveInventoryFromLongKey (const BomRoot& iBomRoot,
00030                                 const std::string& iFullKeyStr) {
00031     Inventory* oInventory_ptr = NULL;
00032 
00033     // Extract the inventory key (i.e., airline code)
00034     const InventoryKey& lInventoryKey =
00035       BomKeyManager::extractInventoryKey (iFullKeyStr);
00036 
00037     oInventory_ptr = iBomRoot.getInventory (lInventoryKey);
00038 
00039     return oInventory_ptr;
00040   }
00041 
00042   // ////////////////////////////////////////////////////////////////////
00043   Inventory* BomRetriever::retrieveInventoryFromKey (const BomRoot& iBomRoot,
00044                                                      const InventoryKey& iKey) {
00045     Inventory* oInventory_ptr = NULL;
00046 
00047     //
00048     oInventory_ptr = iBomRoot.getInventory (iKey);
00049 
00050     return oInventory_ptr;
00051   }
00052 
00053   // ////////////////////////////////////////////////////////////////////
00054   Inventory* BomRetriever::
00055   retrieveInventoryFromKey (const BomRoot& iBomRoot,
00056                             const AirlineCode_T& iAirlineCode) {
00057     Inventory* oInventory_ptr = NULL;
00058 
00059     //
00060     const InventoryKey lKey (iAirlineCode);
00061     oInventory_ptr = iBomRoot.getInventory (lKey);
00062 
00063     return oInventory_ptr;
00064   }
00065 
00066   // ////////////////////////////////////////////////////////////////////
00067   FlightDate* BomRetriever::
00068   retrieveFlightDateFromLongKey (const BomRoot& iBomRoot,
00069                                  const std::string& iFullKeyStr) {
00070     FlightDate* oFlightDate_ptr = NULL;
00071 
00072     // Retrieve the inventory
00073     Inventory* oInventory_ptr =
00074       BomRetriever::retrieveInventoryFromLongKey (iBomRoot, iFullKeyStr);
00075     if (oInventory_ptr == NULL) {
00076       return oFlightDate_ptr;
00077     }
00078     assert (oInventory_ptr != NULL);
00079 
00080     // Extract the flight-date key (i.e., flight number and date)
00081     const FlightDateKey& lFlightDateKey =
00082       BomKeyManager::extractFlightDateKey (iFullKeyStr);
00083 
00084     oFlightDate_ptr = oInventory_ptr->getFlightDate (lFlightDateKey);
00085 
00086     return oFlightDate_ptr;
00087   }
00088 
00089   // ////////////////////////////////////////////////////////////////////
00090   FlightDate* BomRetriever::
00091   retrieveFlightDateFromKeySet (const BomRoot& iBomRoot,
00092                                 const AirlineCode_T& iAirlineCode,
00093                                 const FlightNumber_T& iFlightNumber,
00094                                 const Date_T& iFlightDateDate) {
00095     FlightDate* oFlightDate_ptr = NULL;
00096 
00097     // Retrieve the inventory
00098     Inventory* oInventory_ptr =
00099       BomRetriever::retrieveInventoryFromKey (iBomRoot, iAirlineCode);
00100     if (oInventory_ptr == NULL) {
00101       return oFlightDate_ptr;
00102     }
00103     assert (oInventory_ptr != NULL);
00104 
00105     //
00106     oFlightDate_ptr = retrieveFlightDateFromKey (*oInventory_ptr,
00107                                                  iFlightNumber, iFlightDateDate);
00108 
00109     return oFlightDate_ptr;
00110   }
00111 
00112   // ////////////////////////////////////////////////////////////////////
00113   FlightDate* BomRetriever::
00114   retrieveFlightDateFromLongKey (const Inventory& iInventory,
00115                                  const std::string& iFullKeyStr) {
00116     FlightDate* oFlightDate_ptr = NULL;
00117 
00118     // Extract the flight-date key (i.e., flight number and date)
00119     const FlightDateKey& lFlightDateKey =
00120       BomKeyManager::extractFlightDateKey (iFullKeyStr);
00121 
00122     oFlightDate_ptr = iInventory.getFlightDate (lFlightDateKey);
00123 
00124     return oFlightDate_ptr;
00125   }
00126 
00127   // ////////////////////////////////////////////////////////////////////
00128   FlightDate* BomRetriever::
00129   retrieveFlightDateFromKey (const Inventory& iInventory,
00130                              const FlightDateKey& iKey) {
00131     FlightDate* oFlightDate_ptr = NULL;
00132 
00133     //
00134     oFlightDate_ptr = iInventory.getFlightDate (iKey);
00135 
00136     return oFlightDate_ptr;
00137   }
00138 
00139   // ////////////////////////////////////////////////////////////////////
00140   FlightDate* BomRetriever::
00141   retrieveFlightDateFromKey (const Inventory& iInventory,
00142                              const FlightNumber_T& iFlightNumber,
00143                              const Date_T& iFlightDateDate) {
00144     FlightDate* oFlightDate_ptr = NULL;
00145 
00146     //
00147     const FlightDateKey lKey (iFlightNumber, iFlightDateDate);
00148     oFlightDate_ptr = iInventory.getFlightDate (lKey);
00149 
00150     return oFlightDate_ptr;
00151   }
00152 
00153   // ////////////////////////////////////////////////////////////////////
00154   SegmentDate* BomRetriever::
00155   retrieveSegmentDateFromLongKey (const BomRoot& iBomRoot,
00156                                   const std::string& iFullKeyStr) {
00157     SegmentDate* oSegmentDate_ptr = NULL;
00158 
00159     // Retrieve the flight-date
00160     FlightDate* oFlightDate_ptr =
00161       BomRetriever::retrieveFlightDateFromLongKey (iBomRoot, iFullKeyStr);
00162     if (oFlightDate_ptr == NULL) {
00163       return oSegmentDate_ptr;
00164     }
00165     assert (oFlightDate_ptr != NULL);
00166 
00167     // Extract the segment-date key (i.e., origin and destination)
00168     const SegmentDateKey& lSegmentDateKey =
00169       BomKeyManager::extractSegmentDateKey (iFullKeyStr);
00170 
00171     oSegmentDate_ptr = oFlightDate_ptr->getSegmentDate (lSegmentDateKey);
00172 
00173     return oSegmentDate_ptr;
00174   }
00175 
00176   // ////////////////////////////////////////////////////////////////////
00177   SegmentDate* BomRetriever::
00178   retrieveSegmentDateFromLongKey (const Inventory& iInventory,
00179                                   const std::string& iFullKeyStr) {
00180     SegmentDate* oSegmentDate_ptr = NULL;
00181 
00182     ParsedKey lParsedKey = BomKeyManager::extractKeys (iFullKeyStr);
00183 
00184     if (iInventory.getAirlineCode() != lParsedKey._airlineCode) {
00185       STDAIR_LOG_DEBUG ("Airline code: " << lParsedKey._airlineCode);
00186       return oSegmentDate_ptr;
00187     }
00188 
00189     FlightDate* lFlightDate_ptr =
00190       retrieveFlightDateFromKey (iInventory, lParsedKey.getFlightDateKey());
00191     if (lFlightDate_ptr == NULL) {
00192       STDAIR_LOG_DEBUG ("Flight-date key: "
00193                         << lParsedKey.getFlightDateKey().toString());
00194       return oSegmentDate_ptr;
00195     }
00196 
00197     oSegmentDate_ptr =
00198       retrieveSegmentDateFromKey (*lFlightDate_ptr, lParsedKey.getSegmentKey());
00199     if (oSegmentDate_ptr == NULL) {
00200       STDAIR_LOG_DEBUG ("Segment-date key: "
00201                         << lParsedKey.getSegmentKey().toString());
00202       return oSegmentDate_ptr;
00203     }
00204     
00205     return oSegmentDate_ptr;
00206   }
00207 
00208   // ////////////////////////////////////////////////////////////////////
00209   SegmentDate* BomRetriever::
00210   retrieveSegmentDateFromLongKey (const FlightDate& iFlightDate,
00211                                   const std::string& iFullKeyStr) {
00212     SegmentDate* oSegmentDate_ptr = NULL;
00213 
00214     // Extract the segment-date key (i.e., origin and destination)
00215     const SegmentDateKey& lSegmentDateKey =
00216       BomKeyManager::extractSegmentDateKey (iFullKeyStr);
00217 
00218     oSegmentDate_ptr = iFlightDate.getSegmentDate (lSegmentDateKey);
00219 
00220     return oSegmentDate_ptr;
00221   }
00222 
00223   // ////////////////////////////////////////////////////////////////////
00224   SegmentDate* BomRetriever::
00225   retrieveSegmentDateFromKey (const FlightDate& iFlightDate,
00226                               const SegmentDateKey& iKey) {
00227     SegmentDate* oSegmentDate_ptr = NULL;
00228 
00229     //
00230     oSegmentDate_ptr = iFlightDate.getSegmentDate (iKey);
00231 
00232     return oSegmentDate_ptr;
00233   }
00234 
00235   // ////////////////////////////////////////////////////////////////////
00236   SegmentDate* BomRetriever::
00237   retrieveSegmentDateFromKey (const FlightDate& iFlightDate,
00238                               const AirportCode_T& iOrigin,
00239                               const AirportCode_T& iDestination) {
00240     SegmentDate* oSegmentDate_ptr = NULL;
00241 
00242     //
00243     const SegmentDateKey lKey (iOrigin, iDestination);
00244     oSegmentDate_ptr = iFlightDate.getSegmentDate (lKey);
00245 
00246     return oSegmentDate_ptr;
00247   }
00248 
00249   // ////////////////////////////////////////////////////////////////////
00250   BookingClass* BomRetriever::
00251   retrieveBookingClassFromLongKey (const Inventory& iInventory,
00252                                    const std::string& iFullKeyStr,
00253                                    const ClassCode_T& iClassCode) {
00254     BookingClass* oBookingClass_ptr = NULL;
00255 
00256     SegmentDate* lSegmentDate_ptr = retrieveSegmentDateFromLongKey (iInventory,
00257                                                                     iFullKeyStr);
00258 
00259     if (lSegmentDate_ptr == NULL) {
00260       return oBookingClass_ptr;
00261     }
00262     assert (lSegmentDate_ptr != NULL);
00263 
00264     // 
00265     oBookingClass_ptr =
00266       BomManager::getObjectPtr<BookingClass> (*lSegmentDate_ptr, iClassCode);
00267 
00268     return oBookingClass_ptr;
00269   }
00270 
00271   // ////////////////////////////////////////////////////////////////////
00272   AirportPair* BomRetriever::
00273   retrieveAirportPairFromKeySet (const BomRoot& iBomRoot,
00274                                  const stdair::AirportCode_T& iOrigin,
00275                                  const stdair::AirportCode_T& iDestination) {
00276 
00277     // Get the Airport pair stream of the segment path.
00278     const AirportPairKey lAirportPairKey (iOrigin, iDestination);
00279     
00280     // Search for the fare rules having the same origin and
00281     // destination airport as the travel solution
00282     AirportPair* oAirportPair_ptr = BomManager::
00283       getObjectPtr<AirportPair> (iBomRoot, lAirportPairKey.toString());  
00284 
00285     return oAirportPair_ptr;
00286    
00287   }
00288 
00289   // ////////////////////////////////////////////////////////////////////
00290   void BomRetriever::
00291   retrieveDatePeriodListFromKey (const AirportPair& iAirportPair,
00292                                  const stdair::Date_T& iDepartureDate,
00293                                  stdair::DatePeriodList_T& ioDatePeriodList) {
00294 
00295     // Get the list of date-period
00296     const DatePeriodList_T& lFareDatePeriodList =
00297       BomManager::getList<DatePeriod> (iAirportPair);
00298 
00299     // Browse the date-period list
00300     for (DatePeriodList_T::const_iterator itDateRange =
00301            lFareDatePeriodList.begin();
00302          itDateRange != lFareDatePeriodList.end(); ++itDateRange) {
00303 
00304       DatePeriod* lCurrentFareDatePeriod_ptr = *itDateRange ;
00305       assert (lCurrentFareDatePeriod_ptr != NULL);
00306 
00307       // Select the date-period objects having a corresponding date range
00308       const bool isDepartureDateValid =
00309         lCurrentFareDatePeriod_ptr->isDepartureDateValid (iDepartureDate);
00310       
00311       // Add the date-period objects having a corresponding date range
00312       // to the list to display
00313       if (isDepartureDateValid == true) {
00314         ioDatePeriodList.push_back(lCurrentFareDatePeriod_ptr);
00315       }
00316     }
00317 
00318   }
00319 
00320   // ////////////////////////////////////////////////////////////////////
00321   void BomRetriever::
00322   retrieveDatePeriodListFromKeySet (const BomRoot& iBomRoot,
00323                                     const stdair::AirportCode_T& iOrigin,
00324                                     const stdair::AirportCode_T& iDestination,
00325                                     const stdair::Date_T& iDepartureDate,
00326                                     stdair::DatePeriodList_T& ioDatePeriodList) {
00327 
00328     // Retrieve the airport-pair
00329     AirportPair* oAirportPair_ptr =
00330       BomRetriever::retrieveAirportPairFromKeySet(iBomRoot, iOrigin,
00331                                                  iDestination);
00332     if (oAirportPair_ptr == NULL) {
00333       return;
00334     }
00335     assert (oAirportPair_ptr != NULL);
00336 
00337     // Retrieve the flight date
00338     BomRetriever::retrieveDatePeriodListFromKey (*oAirportPair_ptr, iDepartureDate,
00339                                                  ioDatePeriodList);
00340    
00341   }
00342   
00343   // ////////////////////////////////////////////////////////////////////
00344   LegCabin& BomRetriever::
00345   retrieveDummyLegCabin (stdair::BomRoot& iBomRoot) {
00346 
00347     LegCabin* oLegCabin_ptr = NULL;
00348 
00349     // Retrieve the Inventory
00350     const Inventory* lInventory_ptr = BomRetriever::
00351       retrieveInventoryFromKey (iBomRoot, DEFAULT_AIRLINE_CODE);
00352 
00353     if (lInventory_ptr == NULL) {
00354       std::ostringstream oStr;
00355       oStr << "The inventory corresponding to the '"
00356            << DEFAULT_AIRLINE_CODE << "' airline can not be found";
00357       throw ObjectNotFoundException (oStr.str());
00358     }
00359     
00360     // Retrieve the FlightDate
00361     const FlightDate* lFlightDate_ptr = BomRetriever::
00362       retrieveFlightDateFromKey (*lInventory_ptr, DEFAULT_FLIGHT_NUMBER,
00363                                  DEFAULT_DEPARTURE_DATE);
00364     
00365     if (lFlightDate_ptr == NULL) {
00366       std::ostringstream oStr;
00367       oStr << "The flight-date corresponding to ("
00368            << DEFAULT_FLIGHT_NUMBER << ", "
00369            << DEFAULT_DEPARTURE_DATE << ") can not be found";
00370       throw ObjectNotFoundException (oStr.str());
00371     }
00372 
00373     // Retrieve the LegDate
00374     const LegDateKey lLegDateKey (DEFAULT_ORIGIN);
00375     const LegDate* lLegDate_ptr =
00376       lFlightDate_ptr->getLegDate (lLegDateKey);
00377 
00378     if (lLegDate_ptr == NULL) {
00379       std::ostringstream oStr;
00380       oStr << "The leg-date corresponding to the '"
00381            << DEFAULT_ORIGIN << "' origin can not be found";
00382       throw ObjectNotFoundException (oStr.str());
00383     }
00384     
00385     // Retrieve the LegCabin
00386     const LegCabinKey lLegCabinKey (DEFAULT_CABIN_CODE);
00387     oLegCabin_ptr = lLegDate_ptr->getLegCabin (lLegCabinKey);
00388 
00389     if (oLegCabin_ptr == NULL) {
00390       std::ostringstream oStr;
00391       oStr << "The leg-cabin corresponding to the '"
00392            << DEFAULT_CABIN_CODE << "' cabin code can not be found";
00393       throw ObjectNotFoundException (oStr.str());
00394     }
00395     
00396     assert (oLegCabin_ptr != NULL);
00397     return *oLegCabin_ptr;
00398 
00399   }
00400 
00401   // ////////////////////////////////////////////////////////////////////
00402   SegmentCabin& BomRetriever::
00403   retrieveDummySegmentCabin (stdair::BomRoot& iBomRoot) {
00404 
00405     SegmentCabin* oSegmentCabin_ptr = NULL;
00406 
00407     // Retrieve the Inventory
00408     const Inventory* lInventory_ptr = BomRetriever::
00409       retrieveInventoryFromKey (iBomRoot, DEFAULT_AIRLINE_CODE);
00410 
00411     if (lInventory_ptr == NULL) {
00412       std::ostringstream oStr;
00413       oStr << "The inventory corresponding to the '"
00414            << DEFAULT_AIRLINE_CODE << "' airline can not be found";
00415       throw ObjectNotFoundException (oStr.str());
00416     }
00417     
00418     // Retrieve the FlightDate
00419     const FlightDate* lFlightDate_ptr = BomRetriever::
00420       retrieveFlightDateFromKey (*lInventory_ptr, DEFAULT_FLIGHT_NUMBER,
00421                                  DEFAULT_DEPARTURE_DATE);
00422     
00423     if (lFlightDate_ptr == NULL) {
00424       std::ostringstream oStr;
00425       oStr << "The flight-date corresponding to ("
00426            << DEFAULT_FLIGHT_NUMBER << ", "
00427            << DEFAULT_DEPARTURE_DATE << ") can not be found";
00428       throw ObjectNotFoundException (oStr.str());
00429     }
00430     
00431     // Retrieve the SegmentDate
00432     const SegmentDateKey lSegmentDateKey (DEFAULT_ORIGIN, DEFAULT_DESTINATION);
00433     const SegmentDate* lSegmentDate_ptr =
00434       lFlightDate_ptr->getSegmentDate (lSegmentDateKey);
00435 
00436     if (lSegmentDate_ptr == NULL) {
00437       std::ostringstream oStr;
00438       oStr << "The segment-date corresponding to the '"
00439            << DEFAULT_ORIGIN << "' origin and '"
00440            << DEFAULT_DESTINATION << "' destination can not be found";
00441       throw ObjectNotFoundException (oStr.str());
00442     }
00443     
00444     // Retrieve the SegmentCabin
00445     const SegmentCabinKey lSegmentCabinKey (DEFAULT_CABIN_CODE);
00446     oSegmentCabin_ptr =
00447       BomManager::getObjectPtr<SegmentCabin> (*lSegmentDate_ptr, lSegmentCabinKey.toString());
00448 
00449     if (oSegmentCabin_ptr == NULL) {
00450       std::ostringstream oStr;
00451       oStr << "The segment-cabin corresponding to the '"
00452            << DEFAULT_CABIN_CODE << "' cabin code can not be found";
00453       throw ObjectNotFoundException (oStr.str());
00454     }
00455 
00456     assert (oSegmentCabin_ptr != NULL);
00457     return *oSegmentCabin_ptr;
00458   }
00459 
00460 }