$treeview $search $mathjax
StdAir Logo  1.00.2
$projectbrief
$projectbrief
$searchbox

stdair/bom/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/LegDateKey.hpp>
00019 #include <stdair/bom/ParsedKey.hpp>
00020 #include <stdair/service/Logger.hpp>
00021 
00022 namespace stdair {
00023 
00024   // ////////////// Tokenising support ///////////////
00028   typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
00029 
00033   const boost::char_separator<char> TokeniserDashSeparator ("-");
00034   
00038   const boost::char_separator<char> TokeniserTimeSeparator (":");
00039 
00040   // ////////////////////////////////////////////////////////////////////
00041   ParsedKey::ParsedKey() : _fullKey (""), _airlineCode (""), _flightNumber (""),
00042                             _departureDate (""), _boardingPoint (""),
00043                             _offPoint (""), _boardingTime ("") {
00044   }
00045 
00046   // ////////////////////////////////////////////////////////////////////
00047   ParsedKey::~ParsedKey() {
00048   }
00049   
00050   // ////////////////////////////////////////////////////////////////////
00051   InventoryKey ParsedKey::getInventoryKey() const {
00052     if (_airlineCode.size() < 2 || _airlineCode.size() > 3) {
00053       STDAIR_LOG_ERROR ("No airline code can be found in '" << _fullKey << "'");
00054       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00055       throw KeyNotFoundException ("No airline code can be found in '"
00056                                   + _fullKey + "'");
00057     }
00058     return _airlineCode;
00059   }
00060 
00061   // ////////////////////////////////////////////////////////////////////
00062   FlightDateKey ParsedKey::getFlightDateKey() const {
00063     // Check whether the departure date has been parsed correctly.
00064     Tokeniser_T lDateTokens (_departureDate, TokeniserDashSeparator);
00065 
00066     if (lDateTokens.begin() == lDateTokens.end()) {
00067       STDAIR_LOG_ERROR ("No date can be found in '" << _fullKey << "'");
00068       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00069       throw KeyNotFoundException ("No date can be found in '" + _fullKey + "'");
00070     }
00071 
00072     const FlightNumber_T lFlightNumber =
00073       boost::lexical_cast<FlightNumber_T> (_flightNumber);
00074 
00075     const Date_T lDepartureDate =
00076       boost::gregorian::from_simple_string (_departureDate);
00077 
00078     const FlightDateKey oFlightDateKey (lFlightNumber, lDepartureDate);
00079 
00080     return oFlightDateKey;
00081   } 
00082 
00083   // ////////////////////////////////////////////////////////////////////
00084   LegDateKey ParsedKey::getLegKey() const {
00085     if (_boardingPoint.size() != 3) {
00086       STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
00087       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00088       throw KeyNotFoundException ("No airport code can be found in '"
00089                                   + _fullKey + "'");
00090     }
00091 
00092     const LegDateKey oLegDateKey (_boardingPoint);
00093 
00094     return oLegDateKey;
00095   }
00096   
00097   // ////////////////////////////////////////////////////////////////////
00098   SegmentDateKey ParsedKey::getSegmentKey() const {
00099     if (_boardingPoint.size() != 3 || _offPoint.size() != 3) {
00100       STDAIR_LOG_ERROR ("No airport code can be found in '" << _fullKey << "'");
00101       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00102       throw KeyNotFoundException ("No airport code can be found in '"
00103                                   + _fullKey + "'");
00104     }
00105 
00106     const SegmentDateKey oSegmentDateKey (_boardingPoint, _offPoint);
00107 
00108     return oSegmentDateKey;
00109   }
00110 
00111   // ////////////////////////////////////////////////////////////////////
00112   const Duration_T ParsedKey::getBoardingTime() const {
00113     // Check whether the boarding time has been parsed correctly.
00114     Tokeniser_T lTimeTokens (_boardingTime, TokeniserTimeSeparator);
00115 
00116     if (lTimeTokens.begin() == lTimeTokens.end()) {
00117       STDAIR_LOG_ERROR ("No boarding time can be found in '" << _fullKey << "'");
00118       STDAIR_LOG_DEBUG ("Parsed key: " << toString());
00119       throw KeyNotFoundException ("No boarding time can be found in '"
00120                                   + _fullKey + "'");
00121     }
00122 
00123     const Duration_T oBoardingTime (boost::posix_time::
00124                                     duration_from_string (_boardingTime));
00125 
00126     return oBoardingTime;
00127   }
00128 
00129   // ////////////////////////////////////////////////////////////////////
00130   void ParsedKey::toStream (std::ostream& ioOut) const {
00131     ioOut << "ParsedKey: " << toString();
00132   }
00133 
00134   // ////////////////////////////////////////////////////////////////////
00135   void ParsedKey::fromStream (std::istream& ioIn) {
00136   }
00137 
00138   // ////////////////////////////////////////////////////////////////////
00139   const std::string ParsedKey::toString() const {
00140     std::ostringstream oStr;
00141     
00142     oStr << _airlineCode
00143          << DEFAULT_KEY_FLD_DELIMITER << " "
00144          << _flightNumber
00145          << DEFAULT_KEY_SUB_FLD_DELIMITER << " "
00146          << _departureDate
00147          << DEFAULT_KEY_FLD_DELIMITER << " "
00148          << _boardingPoint
00149          << DEFAULT_KEY_SUB_FLD_DELIMITER << " "
00150          << _offPoint
00151          << DEFAULT_KEY_FLD_DELIMITER << " "
00152          << _boardingTime;
00153     
00154     return oStr.str();
00155   }
00156 
00157 }