StdAir Logo  0.44.0
C++ Standard Airline IT Object Library
BomJSONExport.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <ostream>
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 #if BOOST_VERSION >= 103400
00013 // Boost ForEach
00014 #include <boost/foreach.hpp>
00015 #endif // BOOST_VERSION >= 103400
00016 // StdAir
00017 #include <stdair/basic/BasConst_BomDisplay.hpp>
00018 #include <stdair/bom/BomManager.hpp>
00019 #include <stdair/bom/BomRoot.hpp>
00020 #include <stdair/bom/Inventory.hpp>
00021 #include <stdair/bom/FlightDate.hpp>
00022 #include <stdair/bom/LegDate.hpp>
00023 #include <stdair/bom/SegmentDate.hpp>
00024 #include <stdair/bom/LegCabin.hpp>
00025 #include <stdair/bom/SegmentCabin.hpp>
00026 #include <stdair/bom/FareFamily.hpp>
00027 #include <stdair/bom/BookingClass.hpp>
00028 #include <stdair/bom/Bucket.hpp>
00029 #include <stdair/bom/BomJSONExport.hpp>
00030 
00031 #if BOOST_VERSION >= 104100
00032 namespace bpt = boost::property_tree;
00033 #else // BOOST_VERSION >= 104100
00034 namespace bpt {
00035   typedef char ptree;
00036 }
00037 #endif // BOOST_VERSION >= 104100
00038 
00039 namespace stdair {
00040 
00046   struct FlagSaver {
00047   public:
00049     FlagSaver (std::ostream& oStream)
00050       : _oStream (oStream), _streamFlags (oStream.flags()) {
00051     }
00052 
00054     ~FlagSaver() {
00055       // Reset formatting flags of the given output stream
00056       _oStream.flags (_streamFlags);
00057     }
00058     
00059   private:
00061     std::ostream& _oStream;
00063     std::ios::fmtflags _streamFlags;
00064   };
00065 
00066   // ////////////////////////////////////////////////////////////////////
00067   void jsonLegDateExport (bpt::ptree& ioPropertyTree,
00068                           const FlightDate& iFlightDate) {
00073     // Check whether there are LegDate objects
00074     if (BomManager::hasList<LegDate> (iFlightDate) == false) {
00075       return;
00076     }
00077     
00078     // Browse the leg-dates
00079     unsigned short idx = 0;
00080     const LegDateList_T& lLegDateList =
00081       BomManager::getList<LegDate> (iFlightDate);
00082     for (LegDateList_T::const_iterator itLD = lLegDateList.begin();
00083          itLD != lLegDateList.end(); ++itLD, ++idx) {
00084       const LegDate* lLD_ptr = *itLD;
00085       assert (lLD_ptr != NULL);
00086       
00087 #if BOOST_VERSION >= 104100
00088       //
00089       bpt::ptree lLegDateArray;
00090 
00091       // Put boarding point in property tree
00092       lLegDateArray.put ("board_point", lLD_ptr->getBoardingPoint());
00093       // Put off point in property tree
00094       lLegDateArray.put ("off_point", lLD_ptr->getOffPoint());
00095       // Put boarding date in property tree
00096       lLegDateArray.put ("board_date", lLD_ptr->getBoardingDate());
00097       // Put off date in property tree
00098       lLegDateArray.put ("off_date", lLD_ptr->getOffDate());
00099       // Put boarding time in property tree
00100       lLegDateArray.put ("board_time", lLD_ptr->getBoardingTime());
00101       // Put off time in property tree
00102       lLegDateArray.put ("off_time", lLD_ptr->getOffTime());
00103       // Put elapsed time in property tree
00104       lLegDateArray.put ("elapsed_time", lLD_ptr->getElapsedTime());
00105       // Put date offset in property tree
00106       lLegDateArray.put ("date_offset", lLD_ptr->getDateOffset());
00107       // Put time offset in property tree
00108       lLegDateArray.put ("time_offset", lLD_ptr->getTimeOffset());
00109       // Put distance in property tree
00110       lLegDateArray.put ("distance", lLD_ptr->getDistance());
00111       // Put capacity in property tree
00112       lLegDateArray.put ("capacity", lLD_ptr->getCapacity());
00113 
00114       //
00115       std::ostringstream oStream;
00116       oStream << "flight_date.legs";
00117       ioPropertyTree.put_child (oStream.str(), lLegDateArray);
00118 #endif // BOOST_VERSION >= 104100
00119     }
00120   }
00121     
00122   // ////////////////////////////////////////////////////////////////////
00123   void jsonSegmentDateExport (bpt::ptree& ioPropertyTree,
00124                               const FlightDate& iFlightDate) {
00128   }
00129 
00130   // ////////////////////////////////////////////////////////////////////
00131   void jsonLegCabinExport (bpt::ptree& ioPropertyTree,
00132                            const FlightDate& iFlightDate) {
00137     // Check whether there are LegDate objects
00138     if (BomManager::hasList<LegDate> (iFlightDate) == false) {
00139       return;
00140     }
00141     
00142     // Browse the leg-dates
00143     const LegDateList_T& lLegDateList =
00144       BomManager::getList<LegDate> (iFlightDate);
00145     for (LegDateList_T::const_iterator itLD = lLegDateList.begin();
00146          itLD != lLegDateList.end(); ++itLD) {
00147       const LegDate* lLD_ptr = *itLD;
00148       assert (lLD_ptr != NULL);
00149 
00150       // Browse the leg-cabins
00151       const LegCabinList_T& lLegCabinList =
00152         BomManager::getList<LegCabin> (*lLD_ptr);
00153       for (LegCabinList_T::const_iterator itLC = lLegCabinList.begin();
00154            itLC != lLegCabinList.end(); ++itLC) {
00155         const LegCabin* lLC_ptr = *itLC;
00156         assert (lLC_ptr != NULL);
00157       
00158 #if BOOST_VERSION >= 104100
00159         // Put boarding point in property tree
00160         ioPropertyTree.put ("flight_date.leg.cabin.code",
00161                             lLC_ptr->getCabinCode());
00162 
00163         /*
00164         oStream << lLC_ptr->getOfferedCapacity() << ", "
00165                 << lLC_ptr->getPhysicalCapacity() << ", "
00166                 << lLC_ptr->getRegradeAdjustment() << ", "
00167                 << lLC_ptr->getAuthorizationLevel() << ", "
00168                 << lLC_ptr->getUPR() << ", "
00169                 << lLC_ptr->getSoldSeat() << ", "
00170                 << lLC_ptr->getStaffNbOfSeats() << ", "
00171                 << lLC_ptr->getWLNbOfSeats() << ", "
00172                 << lLC_ptr->getGroupNbOfSeats() << ", "
00173                 << lLC_ptr->getCommittedSpace() << ", "
00174                 << lLC_ptr->getAvailabilityPool() << ", "
00175                 << lLC_ptr->getAvailability() << ", "
00176                 << lLC_ptr->getNetAvailability() << ", "
00177                 << lLC_ptr->getGrossAvailability() << ", "
00178                 << lLC_ptr->getAvgCancellationPercentage() << ", "
00179                 << lLC_ptr->getETB() << ", "
00180                 << lLC_ptr->getCurrentBidPrice() << ", "
00181                 << std::endl;
00182         */
00183 #endif // BOOST_VERSION >= 104100
00184       }
00185     }
00186   }
00187     
00188   // ////////////////////////////////////////////////////////////////////
00189   void jsonSegmentCabinExport (bpt::ptree& ioPropertyTree,
00190                                const FlightDate& iFlightDate) {
00194   }
00195 
00196   // ////////////////////////////////////////////////////////////////////
00197   void jsonFareFamilyExport (bpt::ptree& ioPropertyTree,
00198                              const FlightDate& iFlightDate) {
00203     // Check whether there are SegmentDate objects
00204     if (BomManager::hasList<SegmentDate> (iFlightDate) == false) {
00205       return;
00206     }
00207     
00208     // Browse the segment-dates
00209     const SegmentDateList_T& lSegmentDateList =
00210       BomManager::getList<SegmentDate> (iFlightDate);
00211     for (SegmentDateList_T::const_iterator itSD = lSegmentDateList.begin();
00212          itSD != lSegmentDateList.end(); ++itSD) {
00213       const SegmentDate* lSD_ptr = *itSD;
00214       assert (lSD_ptr != NULL);
00215       
00216       // Browse the segment-cabins
00217       const SegmentCabinList_T& lSegmentCabinList =
00218         BomManager::getList<SegmentCabin> (*lSD_ptr);
00219       for (SegmentCabinList_T::const_iterator itSC = lSegmentCabinList.begin();
00220            itSC != lSegmentCabinList.end(); ++itSC) {
00221         const SegmentCabin* lSC_ptr = *itSC;
00222         assert (lSC_ptr != NULL);
00223         
00224         // Check whether there are fare family objects
00225         if (BomManager::hasList<FareFamily> (*lSC_ptr) == false) {
00226           continue;
00227         }
00228     
00229         // Browse the fare families
00230         unsigned short ffIdx = 0;
00231         const FareFamilyList_T& lFareFamilyList =
00232           BomManager::getList<FareFamily> (*lSC_ptr);
00233         for (FareFamilyList_T::const_iterator itFF = lFareFamilyList.begin();
00234              itFF != lFareFamilyList.end(); ++itFF, ++ffIdx) {
00235           const FareFamily* lFF_ptr = *itFF;
00236           assert (lFF_ptr != NULL);
00237 
00238 #if BOOST_VERSION >= 104100
00239           //
00240           bpt::ptree lFFArray;
00241 
00242           // Put boarding point in property tree
00243           lFFArray.put ("code", lFF_ptr->getFamilyCode());
00244 
00245           /*
00246           oStream << lSC_ptr->getBookingCounter() << ", "
00247                   << lSC_ptr->getMIN() << ", "
00248                   << lSC_ptr->getUPR() << ", "
00249                   << lSC_ptr->getCommittedSpace() << ", "
00250                   << lSC_ptr->getAvailabilityPool() << ", "
00251                   << lSC_ptr->getCurrentBidPrice() << ", "
00252                   << std::endl;
00253           */
00254 
00255           //
00256           std::ostringstream oStream;
00257           oStream << "flight_date.segment.cabin.fare_families";
00258           ioPropertyTree.put_child (oStream.str(), lFFArray);
00259 #endif // BOOST_VERSION >= 104100
00260         }
00261       }
00262     }
00263   }
00264 
00265   // ////////////////////////////////////////////////////////////////////
00266   void jsonBucketExport (bpt::ptree& ioPropertyTree,
00267                          const FlightDate& iFlightDate) {
00272     // Check whether there are LegDate objects
00273     if (BomManager::hasList<LegDate> (iFlightDate) == false) {
00274       return;
00275     }
00276     
00277     // Browse the leg-dates
00278     const LegDateList_T& lLegDateList =
00279       BomManager::getList<LegDate> (iFlightDate);
00280     for (LegDateList_T::const_iterator itLD = lLegDateList.begin();
00281          itLD != lLegDateList.end(); ++itLD) {
00282       const LegDate* lLD_ptr = *itLD;
00283       assert (lLD_ptr != NULL);
00284 
00285       // Browse the leg-cabins
00286       const LegCabinList_T& lLegCabinList =
00287         BomManager::getList<LegCabin> (*lLD_ptr);
00288       for (LegCabinList_T::const_iterator itLC = lLegCabinList.begin();
00289            itLC != lLegCabinList.end(); ++itLC) {
00290         const LegCabin* lLC_ptr = *itLC;
00291         assert (lLC_ptr != NULL);
00292 
00293         // Check whether there are bucket objects
00294         if (BomManager::hasList<Bucket> (*lLC_ptr) == false) {
00295           continue;
00296         }
00297 
00298         // Browse the buckets
00299         const BucketList_T& lBucketList = BomManager::getList<Bucket> (*lLC_ptr);
00300         for (BucketList_T::const_iterator itBuck = lBucketList.begin();
00301              itBuck != lBucketList.end(); ++itBuck) {
00302           const Bucket* lBucket_ptr = *itBuck;
00303           assert (lBucket_ptr != NULL);
00304 
00305 #if BOOST_VERSION >= 104100
00306           // Put yield range upper value in property tree
00307           ioPropertyTree.put ("flight_date.leg.cabin.bucket.yield_range_upper",
00308                               lBucket_ptr->getYieldRangeUpperValue());
00309           /*
00310           oStream << lBucket_ptr->getSeatIndex() << ", "
00311                   << lBucket_ptr->getSoldSeats() << ", "
00312                   << lBucket_ptr->getAvailability() << ", ";
00313           */
00314 #endif // BOOST_VERSION >= 104100
00315         }
00316       }
00317     }
00318   }
00319     
00320   // ////////////////////////////////////////////////////////////////////
00321   void jsonBookingClassExport (bpt::ptree& ioPropertyTree,
00322                                const BookingClass& iBookingClass,
00323                                const std::string& iLeadingString) {
00330     std::ostringstream oStream;
00331     oStream << iBookingClass.getClassCode();
00332 
00333     if (iBookingClass.getSubclassCode() != 0) {
00334       oStream << iBookingClass.getSubclassCode();
00335     }
00336 
00337 #if BOOST_VERSION >= 104100
00338     // Put boarding point in property tree
00339     ioPropertyTree.put ("flight_date.segment.cabin.fare_family.booking_class.code",
00340                         oStream.str());
00341 
00342     /*
00343     oStream << iBookingClass.getAuthorizationLevel() << " ("
00344             << iBookingClass.getProtection() << "), "
00345             << iBookingClass.getNegotiatedSpace() << ", "
00346             << iBookingClass.getNoShowPercentage() << ", "
00347             << iBookingClass.getOverbookingPercentage() << ", "
00348             << iBookingClass.getNbOfBookings() << ", "
00349             << iBookingClass.getNbOfGroupBookings() << " ("
00350             << iBookingClass.getNbOfPendingGroupBookings() << "), "
00351             << iBookingClass.getNbOfStaffBookings() << ", "
00352             << iBookingClass.getNbOfWLBookings() << ", "
00353             << iBookingClass.getETB() << ", "
00354             << iBookingClass.getNetClassAvailability() << ", "
00355             << iBookingClass.getNetRevenueAvailability() << ", "
00356             << iBookingClass.getSegmentAvailability() << ", "
00357             << std::endl;
00358     */
00359 #endif // BOOST_VERSION >= 104100
00360   }
00361 
00362   // ////////////////////////////////////////////////////////////////////
00363   void jsonBookingClassExport (bpt::ptree& ioPropertyTree,
00364                                const FlightDate& iFlightDate) {
00365     // Check whether there are SegmentDate objects
00366     if (BomManager::hasList<SegmentDate> (iFlightDate) == false) {
00367       return;
00368     }
00369     
00370     // Browse the segment-dates
00371     const SegmentDateList_T& lSegmentDateList =
00372       BomManager::getList<SegmentDate> (iFlightDate);
00373     for (SegmentDateList_T::const_iterator itSD = lSegmentDateList.begin();
00374          itSD != lSegmentDateList.end(); ++itSD) {
00375       const SegmentDate* lSD_ptr = *itSD;
00376       assert (lSD_ptr != NULL);
00377       
00378       // Browse the segment-cabins
00379       const SegmentCabinList_T& lSegmentCabinList =
00380         BomManager::getList<SegmentCabin> (*lSD_ptr);
00381       for (SegmentCabinList_T::const_iterator itSC = lSegmentCabinList.begin();
00382            itSC != lSegmentCabinList.end(); ++itSC) {
00383         const SegmentCabin* lSC_ptr = *itSC;
00384         assert (lSC_ptr != NULL);
00385         
00386         // Build the leading string to be displayed
00387         std::ostringstream oLeadingStr;
00388 
00389         // Default Fare Family code, when there are no FF
00390         FamilyCode_T lFamilyCode ("NoFF");
00391 
00392         // Check whether there are FareFamily objects
00393         if (BomManager::hasList<FareFamily> (*lSC_ptr) == true) {
00394 
00395           // Browse the fare families
00396           const FareFamilyList_T& lFareFamilyList =
00397             BomManager::getList<FareFamily> (*lSC_ptr);
00398           for (FareFamilyList_T::const_iterator itFF = lFareFamilyList.begin();
00399                itFF != lFareFamilyList.end(); ++itFF) {
00400             const FareFamily* lFF_ptr = *itFF;
00401             assert (lFF_ptr != NULL);
00402 
00403             // Retrieve the key of the segment-cabin
00404             lFamilyCode = lFF_ptr->getFamilyCode();
00405 
00406             // Complete the leading string to be displayed
00407             oLeadingStr << lFamilyCode << ", ";
00408 
00409             // Browse the booking-classes
00410             const BookingClassList_T& lBookingClassList =
00411               BomManager::getList<BookingClass> (*lFF_ptr);
00412             for (BookingClassList_T::const_iterator itBC =
00413                    lBookingClassList.begin();
00414                  itBC != lBookingClassList.end(); ++itBC) {
00415               const BookingClass* lBC_ptr = *itBC;
00416               assert (lBC_ptr != NULL);
00417 
00418               //
00419               jsonBookingClassExport (ioPropertyTree, *lBC_ptr,
00420                                       oLeadingStr.str());
00421             }
00422           }
00423 
00424           return;
00425         }
00426         assert (BomManager::hasList<FareFamily> (*lSC_ptr) == false);
00427 
00428         // The fare family code is a fake one ('NoFF'), and therefore
00429         // does not vary
00430         oLeadingStr << lFamilyCode << ", ";
00431 
00432         // Browse the booking-classes, directly from the segment-cabin object
00433         const BookingClassList_T& lBookingClassList =
00434           BomManager::getList<BookingClass> (*lSC_ptr);
00435         for (BookingClassList_T::const_iterator itBC =
00436                lBookingClassList.begin();
00437              itBC != lBookingClassList.end(); ++itBC) {
00438           const BookingClass* lBC_ptr = *itBC;
00439           assert (lBC_ptr != NULL);
00440 
00441           //
00442           jsonBookingClassExport (ioPropertyTree, *lBC_ptr, oLeadingStr.str());
00443         }
00444       }
00445     }
00446   }
00447 
00448   // ////////////////////////////////////////////////////////////////////
00449   void BomJSONExport::jsonExport (std::ostream& oStream,
00450                                   const FlightDate& iFlightDate) {
00451     // Save the formatting flags for the given STL output stream
00452     FlagSaver flagSaver (oStream);
00453 
00454     // Create an empty property tree object
00455     bpt::ptree pt;
00456 
00457 #if BOOST_VERSION >= 104100
00458 
00461     const AirlineCode_T& lAirlineCode = iFlightDate.getAirlineCode();
00462     const FlightNumber_T& lFlightNumber = iFlightDate.getFlightNumber();
00463     const Date_T& lFlightDateDate = iFlightDate.getDepartureDate();
00464 
00465     // Put airline code in property tree
00466     pt.put ("flight_date.airline_code", lAirlineCode);
00467 
00468     // Put flight number level in property tree
00469     pt.put ("flight_date.flight_number", lFlightNumber);
00470 
00471     // Put the flight departure date in property tree
00472     const std::string& lDepartureDateStr =
00473       boost::gregorian::to_simple_string (lFlightDateDate);
00474     pt.put ("flight_date.departure_date", lDepartureDateStr);
00475 #endif // BOOST_VERSION >= 104100
00476 
00477     //
00478     jsonLegDateExport (pt, iFlightDate);
00479 
00480     //
00481     //jsonLegCabinExport (pt, iFlightDate);
00482 
00483     //
00484     //jsonBucketExport (pt, iFlightDate);
00485 
00486     //
00487     //jsonFareFamilyExport (pt, iFlightDate);
00488 
00489     //
00490     //jsonBookingClassExport (pt, iFlightDate);
00491 
00492 #if BOOST_VERSION >= 104100
00493     // Write the property tree into the JSON stream.
00494     write_json (oStream, pt);
00495 #endif // BOOST_VERSION >= 104100
00496   }
00497     
00498 }