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