StdAir Logo  0.44.0
C++ Standard Airline IT Object Library
ParsedKey.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // Boost
00008 #include <boost/tokenizer.hpp>
00009 #include <boost/lexical_cast.hpp>
00010 #include <boost/date_time/gregorian/parsers.hpp>
00011 // StdAir
00012 #include <stdair/stdair_exceptions.hpp>
00013 #include <stdair/basic/BasConst_Inventory.hpp>
00014 #include <stdair/basic/BasConst_BomDisplay.hpp>
00015 #include <stdair/bom/InventoryKey.hpp>
00016 #include <stdair/bom/FlightDateKey.hpp>
00017 #include <stdair/bom/SegmentDateKey.hpp>
00018 #include <stdair/bom/ParsedKey.hpp>
00019 #include <stdair/service/Logger.hpp>
00020 
00021 namespace stdair {
00022 
00023   // ////////////// Tokenising support ///////////////
00027   typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
00028 
00032   const boost::char_separator<char> TokeniserDashSeparator ("-");
00033   
00037   const boost::char_separator<char> TokeniserTimeSeparator (":");
00038 
00039   // ////////////////////////////////////////////////////////////////////
00040   ParsedKey::ParsedKey() : _fullKey (""), _airlineCode (""), _flightNumber (""),
00041                             _departureDate (""), _boardingPoint (""),
00042                             _offPoint (""), _boardingTime ("") {
00043   }
00044 
00045   // ////////////////////////////////////////////////////////////////////
00046   ParsedKey::~ParsedKey() {
00047   }
00048   
00049   // ////////////////////////////////////////////////////////////////////
00050   InventoryKey ParsedKey::getInventoryKey() const {
00051     if (_airlineCode.size() < 2 || _airlineCode.size() > 3) {
00052       STDAIR_LOG_ERROR ("No airline code can be found in '" << _fullKey << "'");
00053       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00054       throw KeyNotFoundException ("No airline code can be found in '"
00055                                   + _fullKey + "'");
00056     }
00057     return _airlineCode;
00058   }
00059 
00060   // ////////////////////////////////////////////////////////////////////
00061   FlightDateKey ParsedKey::getFlightDateKey() const {
00062     // Check whether the departure date has been parsed correctly.
00063     Tokeniser_T lDateTokens (_departureDate, TokeniserDashSeparator);
00064 
00065     if (lDateTokens.begin() == lDateTokens.end()) {
00066       STDAIR_LOG_ERROR ("No date can be found in '" << _fullKey << "'");
00067       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00068       throw KeyNotFoundException ("No date can be found in '" + _fullKey + "'");
00069     }
00070 
00071     const FlightNumber_T lFlightNumber =
00072       boost::lexical_cast<FlightNumber_T> (_flightNumber);
00073 
00074     const Date_T lDepartureDate =
00075       boost::gregorian::from_simple_string (_departureDate);
00076 
00077     const FlightDateKey oFlightDateKey (lFlightNumber, lDepartureDate);
00078 
00079     return oFlightDateKey;
00080   }
00081   
00082   // ////////////////////////////////////////////////////////////////////
00083   SegmentDateKey ParsedKey::getSegmentKey() const {
00084     if (_boardingPoint.size() != 3 || _offPoint.size() != 3) {
00085       STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
00086       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00087       throw KeyNotFoundException ("No airport code can be found in '"
00088                                   + _fullKey + "'");
00089     }
00090 
00091     const SegmentDateKey oSegmentDateKey (_boardingPoint, _offPoint);
00092 
00093     return oSegmentDateKey;
00094   }
00095 
00096   // ////////////////////////////////////////////////////////////////////
00097   const Duration_T ParsedKey::getBoardingTime() const {
00098     // Check whether the boarding time has been parsed correctly.
00099     Tokeniser_T lTimeTokens (_boardingTime, TokeniserTimeSeparator);
00100 
00101     if (lTimeTokens.begin() == lTimeTokens.end()) {
00102       STDAIR_LOG_ERROR ("No boarding time can be found in '" << _fullKey << "'");
00103       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00104       throw KeyNotFoundException ("No boarding time can be found in '"
00105                                   + _fullKey + "'");
00106     }
00107 
00108     const Duration_T oBoardingTime (boost::posix_time::
00109                                     duration_from_string (_boardingTime));
00110 
00111     return oBoardingTime;
00112   }
00113 
00114   // ////////////////////////////////////////////////////////////////////
00115   void ParsedKey::toStream (std::ostream& ioOut) const {
00116     ioOut << "ParsedKey: " << toString();
00117   }
00118 
00119   // ////////////////////////////////////////////////////////////////////
00120   void ParsedKey::fromStream (std::istream& ioIn) {
00121   }
00122 
00123   // ////////////////////////////////////////////////////////////////////
00124   const std::string ParsedKey::toString() const {
00125     std::ostringstream oStr;
00126     
00127     oStr << _airlineCode
00128          << DEFAULT_KEY_FLD_DELIMITER << " "
00129          << _flightNumber
00130          << DEFAULT_KEY_SUB_FLD_DELIMITER << " "
00131          << _departureDate
00132          << DEFAULT_KEY_FLD_DELIMITER << " "
00133          << _boardingPoint
00134          << DEFAULT_KEY_SUB_FLD_DELIMITER << " "
00135          << _offPoint
00136          << DEFAULT_KEY_FLD_DELIMITER << " "
00137          << _boardingTime;
00138     
00139     return oStr.str();
00140   }
00141 
00142 }