From: <den...@us...> - 2010-02-07 19:43:36
|
Revision: 120 http://stdair.svn.sourceforge.net/stdair/?rev=120&view=rev Author: denis_arnaud Date: 2010-02-07 19:43:30 +0000 (Sun, 07 Feb 2010) Log Message: ----------- 1. Integrated more initialisation procedures within the STDAIR_Service object. 2. Added a sample stdair::DBManagerForAirlines class. Modified Paths: -------------- trunk/stdair/stdair/Makefile.am trunk/stdair/stdair/STDAIR_Service.hpp trunk/stdair/stdair/bom/BomRoot.cpp trunk/stdair/stdair/bom/BomRoot.hpp trunk/stdair/stdair/bom/BookingRequestStruct.cpp trunk/stdair/stdair/core/Makefile.am trunk/stdair/stdair/service/STDAIR_Service.cpp Added Paths: ----------- trunk/stdair/stdair/command/CmdAbstract.cpp trunk/stdair/stdair/command/CmdAbstract.hpp trunk/stdair/stdair/command/DBManagerForAirlines.cpp trunk/stdair/stdair/command/DBManagerForAirlines.hpp trunk/stdair/stdair/command/Makefile.am trunk/stdair/stdair/command/sources.mk Property Changed: ---------------- trunk/stdair/stdair/command/ Modified: trunk/stdair/stdair/Makefile.am =================================================================== --- trunk/stdair/stdair/Makefile.am 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/Makefile.am 2010-02-07 19:43:30 UTC (rev 120) @@ -6,7 +6,7 @@ MAINTAINERCLEANFILES = Makefile.in -SUBDIRS = basic bom dbadaptor factory config service core +SUBDIRS = basic bom dbadaptor factory command config service core #EXTRA_DIST = config_msvc.h EXTRA_DIST = Modified: trunk/stdair/stdair/STDAIR_Service.hpp =================================================================== --- trunk/stdair/stdair/STDAIR_Service.hpp 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/STDAIR_Service.hpp 2010-02-07 19:43:30 UTC (rev 120) @@ -14,7 +14,7 @@ // Forward declarations class BomRoot; - + class Inventory; /** Interface for the STDAIR Services. */ class STDAIR_Service { @@ -43,6 +43,12 @@ ~STDAIR_Service(); + /** Retrieve the Inventory corresponding to the given airline code + (Inventory key). + <br>If not existing, a ObjectNotFoundException exception is thrown. */ + Inventory& getInventory (const AirlineCode_T& iAirlineCode) const; + + // ///////////////// Getters /////////////////// /** Get a reference on the BomRoot object. <br>If the service context has not been initialised, that @@ -51,6 +57,17 @@ return _bomRoot; } + public: + // /////// Construction and Destruction helper methods /////// + /** Retrieve the Inventory corresponding to the given airline code + (Inventory key). + <br>If not existing, a ObjectNotFoundException exception is thrown. */ + Inventory& createInventory (const AirlineCode_T& iAirlineCode) const; + + /** Add the airline-specific AirlineFeature object to its AirlineFeatureSet + parent. */ + void addAirlineFeature (const AirlineCode_T& iAirlineCode) const; + private: // /////// Construction and Destruction helper methods /////// @@ -76,6 +93,10 @@ in order to secure the access to the corresponding resources. */ void init (); + /** Initialise the AirlineFeatureSet object, and attach it to the + BomRoot. */ + void initAirlineFeatureSet (); + /** Finalise. */ void finalise (); Modified: trunk/stdair/stdair/bom/BomRoot.cpp =================================================================== --- trunk/stdair/stdair/bom/BomRoot.cpp 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/bom/BomRoot.cpp 2010-02-07 19:43:30 UTC (rev 120) @@ -70,4 +70,10 @@ return oNetwork_ptr; } + // //////////////////////////////////////////////////////////////////// + AirlineFeatureSet& BomRoot::getAirlineFeatureSet() const { + assert (_airlineFeatureSet != NULL); + return *_airlineFeatureSet; + } + } Modified: trunk/stdair/stdair/bom/BomRoot.hpp =================================================================== --- trunk/stdair/stdair/bom/BomRoot.hpp 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/bom/BomRoot.hpp 2010-02-07 19:43:30 UTC (rev 120) @@ -117,10 +117,7 @@ NetworkMap_T getNetworkMap () const; /** Get the reference of the AirlineFeatureSet object. */ - const AirlineFeatureSet& getAirlineFeatureSet() const { - assert (_airlineFeatureSet != NULL); - return *_airlineFeatureSet; - } + AirlineFeatureSet& getAirlineFeatureSet() const; /** Retrieve, if existing, the Inventory corresponding to the given airline code (Inventory key). @@ -135,7 +132,7 @@ public: // //////////// Setters ////////////// /** Set the reference to the AirlineFeatureSet object. */ - void setAirlineFeatureSet (const AirlineFeatureSet* ioAirlineFeatureSet_ptr){ + void setAirlineFeatureSet (AirlineFeatureSet* ioAirlineFeatureSet_ptr) { _airlineFeatureSet = ioAirlineFeatureSet_ptr; } @@ -161,7 +158,7 @@ BomStructure_T& _bomRootStructure; /** Set of all AirlineFeatures.*/ - const AirlineFeatureSet* _airlineFeatureSet; + AirlineFeatureSet* _airlineFeatureSet; }; } Modified: trunk/stdair/stdair/bom/BookingRequestStruct.cpp =================================================================== --- trunk/stdair/stdair/bom/BookingRequestStruct.cpp 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/bom/BookingRequestStruct.cpp 2010-02-07 19:43:30 UTC (rev 120) @@ -33,7 +33,10 @@ // ////////////////////////////////////////////////////////////////////// const std::string BookingRequestStruct::describe() const { - return ""; + std::ostringstream oStr; + oStr << _origin << " - " << _destination << " " << _departureDate + << " " << _paxType << " " << _partySize; + return oStr.str(); } } Property changes on: trunk/stdair/stdair/command ___________________________________________________________________ Added: svn:ignore + .deps .libs Makefile.in Makefile Added: trunk/stdair/stdair/command/CmdAbstract.cpp =================================================================== --- trunk/stdair/stdair/command/CmdAbstract.cpp (rev 0) +++ trunk/stdair/stdair/command/CmdAbstract.cpp 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,9 @@ +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// StdAir +#include <stdair/command/CmdAbstract.hpp> + +namespace stdair { + +} Added: trunk/stdair/stdair/command/CmdAbstract.hpp =================================================================== --- trunk/stdair/stdair/command/CmdAbstract.hpp (rev 0) +++ trunk/stdair/stdair/command/CmdAbstract.hpp 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,17 @@ +#ifndef __STDAIR_CMD_CMDABSTRACT_HPP +#define __STDAIR_CMD_CMDABSTRACT_HPP + +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// + +namespace stdair { + + /** Base class for the Command layer. */ + class CmdAbstract { + public: + + }; + +} +#endif // __STDAIR_CMD_CMDABSTRACT_HPP Added: trunk/stdair/stdair/command/DBManagerForAirlines.cpp =================================================================== --- trunk/stdair/stdair/command/DBManagerForAirlines.cpp (rev 0) +++ trunk/stdair/stdair/command/DBManagerForAirlines.cpp 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,167 @@ +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// STL +#include <cassert> +// SOCI +#include <soci/core/soci.h> +#include <soci/backends/mysql/soci-mysql.h> +// StdAir +#include <stdair/bom/AirlineStruct.hpp> +#include <stdair/dbadaptor/DbaAirline.hpp> +#include <stdair/service/Logger.hpp> +// Stdair +#include <stdair/command/DBManagerForAirlines.hpp> + +namespace stdair { + + // ////////////////////////////////////////////////////////////////////// + void DBManagerForAirlines:: + prepareSelectStatement (DBSession_T& ioSociSession, + DBRequestStatement_T& ioSelectStatement, + AirlineStruct& ioAirline) { + + try { + + // Instanciate a SQL statement (no request is performed at that stage) + /** + select code, name + from airlines; + */ + + ioSelectStatement = (ioSociSession.prepare + << "select iata_code, name " + << "from airlines ", soci::into (ioAirline)); + + // Execute the SQL query + ioSelectStatement.execute(); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error: " << lException.what()); + throw SQLDatabaseException(); + } + } + + // ////////////////////////////////////////////////////////////////////// + void DBManagerForAirlines:: + prepareSelectOnAirlineCodeStatement (DBSession_T& ioSociSession, + DBRequestStatement_T& ioSelectStatement, + const AirlineCode_T& iAirlineCode, + AirlineStruct& ioAirline) { + + try { + + // Instanciate a SQL statement (no request is performed at that stage) + /** + select code, name + from airlines + where code = iAirlineCode; + */ + + ioSelectStatement = (ioSociSession.prepare + << "select iata_code, name " + << "from airlines " + << "where iata_code = :airline_code ", + soci::into (ioAirline), soci::use (iAirlineCode)); + + // Execute the SQL query + ioSelectStatement.execute(); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error: " << lException.what()); + throw SQLDatabaseException(); + } + } + + // ////////////////////////////////////////////////////////////////////// + bool DBManagerForAirlines:: + iterateOnStatement (DBRequestStatement_T& ioStatement, + AirlineStruct& ioAirline) { + bool hasStillData = false; + + try { + + // Retrieve the next row of Airline object + hasStillData = ioStatement.fetch(); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error: " << lException.what()); + throw SQLDatabaseException(); + } + + return hasStillData; + } + + // ////////////////////////////////////////////////////////////////////// + void DBManagerForAirlines::updateAirlineInDB (DBSession_T& ioSociSession, + const AirlineStruct& iAirline) { + + try { + + // Begin a transaction on the database + ioSociSession.begin(); + + // Retrieve the airline code + const std::string& lAirlineCode = iAirline.getAirlineCode(); + + // Retrieve the airline name + const std::string& lAirlineName = iAirline.getAirlineName(); + + // Instanciate a SQL statement (no request is performed at that stage) + DBRequestStatement_T lUpdateStatement = + (ioSociSession.prepare + << "update airlines " + << "set name = :name " + << "where iata_code = :iata_code", + soci::use (lAirlineName), soci::use (lAirlineCode)); + + // Execute the SQL query + lUpdateStatement.execute (true); + + // Commit the transaction on the database + ioSociSession.commit(); + + // Debug + // STDAIR_LOG_DEBUG ("[" << lAirlineCode << "] " << iAirline); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error: " << lException.what()); + throw SQLDatabaseException(); + } + } + + // ////////////////////////////////////////////////////////////////////// + bool DBManagerForAirlines::retrieveAirline (DBSession_T& ioSociSession, + const AirlineCode_T& iAirlineCode, + AirlineStruct& ioAirline) { + bool oHasRetrievedAirline = false; + + try { + + // Prepare the SQL request corresponding to the select statement + DBRequestStatement_T lSelectStatement (ioSociSession); + prepareSelectOnAirlineCodeStatement (ioSociSession, lSelectStatement, + iAirlineCode, ioAirline); + + const bool shouldDoReset = true; + bool hasStillData = iterateOnStatement (lSelectStatement, ioAirline); + if (hasStillData == true) { + oHasRetrievedAirline = true; + } + + // Sanity check + const bool shouldNotDoReset = false; + hasStillData = iterateOnStatement (lSelectStatement, ioAirline); + + // Debug + // STDAIR_LOG_DEBUG ("[" << iDocID << "] " << ioAirline); + + } catch (std::exception const& lException) { + STDAIR_LOG_ERROR ("Error: " << lException.what()); + throw SQLDatabaseException(); + } + + return oHasRetrievedAirline; + } + +} Added: trunk/stdair/stdair/command/DBManagerForAirlines.hpp =================================================================== --- trunk/stdair/stdair/command/DBManagerForAirlines.hpp (rev 0) +++ trunk/stdair/stdair/command/DBManagerForAirlines.hpp 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,74 @@ +#ifndef __DSIM_CMD_DBMANAGERFORAIRLINES_HPP +#define __DSIM_CMD_DBMANAGERFORAIRLINES_HPP + +// ////////////////////////////////////////////////////////////////////// +// Import section +// ////////////////////////////////////////////////////////////////////// +// StdAir +#include <stdair/STDAIR_Types.hpp> +#include <stdair/command/CmdAbstract.hpp> + +namespace stdair { + + // Forward declarations + struct AirlineStruct; + + /** Class building the Business Object Model (BOM) from data retrieved + from the database. */ + class DBManagerForAirlines : public CmdAbstract { + public: + /** Update the fields of the database row + corresponding to the given BOM object. + @parameter DBSession_T& + @parameter AirlineStruct& . */ + static void updateAirlineInDB (DBSession_T&, const AirlineStruct&); + + /** Retrieve, from the (MySQL) database, the row corresponding to + the given BOM code, and fill the given BOM object with that retrieved + data. + @parameter DBSession_T& + @parameter const AirlineCode_T& + @parameter AirlineStruct& . */ + static bool retrieveAirline (DBSession_T&, const AirlineCode_T&, + AirlineStruct&); + + + public: + /** Prepare (parse and put in cache) the SQL statement. + @parameter DBSession_T& + @parameter DBRequestStatement_T& + @parameter AirlineStruct& . */ + static void prepareSelectStatement (DBSession_T&, DBRequestStatement_T&, + AirlineStruct&); + + /** Iterate on the SQL statement. + <br>The SQL has to be already prepared. + @parameter DBRequestStatement_T& + @parameter AirlineStruct& . */ + static bool iterateOnStatement (DBRequestStatement_T&, AirlineStruct&); + + + private: + /** Prepare (parse and put in cache) the SQL statement. + @parameter DBSession_T& + @parameter DBRequestStatement_T& + @parameter const AirlineCode_T& + @parameter AirlineStruct& */ + static void prepareSelectOnAirlineCodeStatement (DBSession_T&, + DBRequestStatement_T&, + const AirlineCode_T&, + AirlineStruct&); + + + private: + // /////////////////// Constructors and Destructors ////////////// + /** Default constructors. As all the methods are static, there is no + need to use those constructors. */ + DBManagerForAirlines () {} + DBManagerForAirlines (const DBManagerForAirlines&) {} + /** Destructor. */ + ~DBManagerForAirlines () {} + }; + +} +#endif // __DSIM_CMD_DBMANAGERFORAIRLINES_HPP Added: trunk/stdair/stdair/command/Makefile.am =================================================================== --- trunk/stdair/stdair/command/Makefile.am (rev 0) +++ trunk/stdair/stdair/command/Makefile.am 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,16 @@ +## command sub-directory +include $(top_srcdir)/Makefile.common +include $(srcdir)/sources.mk + +# +noinst_LTLIBRARIES = libcmd.la + +libcmd_la_SOURCES = $(cmd_h_sources) $(cmd_cc_sources) +libcmd_la_CXXFLAGS = $(BOOST_CFLAGS) $(SOCI_CFLAGS) +libcmd_la_LIBADD = +libcmd_la_LDFLAGS = $(BOOST_LIBS) $(SOCI_LIBS) + + +# Header files +pkgincludedir = $(includedir)/stdair/command +pkginclude_HEADERS = $(cmd_h_sources) Added: trunk/stdair/stdair/command/sources.mk =================================================================== --- trunk/stdair/stdair/command/sources.mk (rev 0) +++ trunk/stdair/stdair/command/sources.mk 2010-02-07 19:43:30 UTC (rev 120) @@ -0,0 +1,6 @@ +cmd_h_sources = \ + $(top_srcdir)/stdair/command/CmdAbstract.hpp \ + $(top_srcdir)/stdair/command/DBManagerForAirlines.hpp +cmd_cc_sources = \ + $(top_srcdir)/stdair/command/CmdAbstract.cpp \ + $(top_srcdir)/stdair/command/DBManagerForAirlines.cpp Modified: trunk/stdair/stdair/core/Makefile.am =================================================================== --- trunk/stdair/stdair/core/Makefile.am 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/core/Makefile.am 2010-02-07 19:43:30 UTC (rev 120) @@ -18,6 +18,7 @@ $(top_builddir)/stdair/bom/libbom.la \ $(top_builddir)/stdair/dbadaptor/libdba.la \ $(top_builddir)/stdair/factory/libfac.la \ + $(top_builddir)/stdair/command/libcmd.la \ $(top_builddir)/stdair/service/libsvc.la libstdair_la_LDFLAGS = \ $(BOOST_DATE_TIME_LIB) $(BOOST_PROGRAM_OPTIONS_LIB) \ Modified: trunk/stdair/stdair/service/STDAIR_Service.cpp =================================================================== --- trunk/stdair/stdair/service/STDAIR_Service.cpp 2010-02-07 02:48:33 UTC (rev 119) +++ trunk/stdair/stdair/service/STDAIR_Service.cpp 2010-02-07 19:43:30 UTC (rev 120) @@ -7,8 +7,13 @@ #include <stdair/basic/BasChronometer.hpp> #include <stdair/bom/BomManager.hpp> // for display() #include <stdair/bom/BomRoot.hpp> -#include <stdair/bom/Inventory.hpp> // Child of BomRoot, needed for creation of BomRoot -#include <stdair/bom/Network.hpp> // Child of BomRoot, needed for creation of BomRoot +#include <stdair/bom/AirlineFeatureSet.hpp> +#include <stdair/bom/AirlineFeature.hpp> +// Inventory: child of BomRoot, needed for creation of BomRoot +#include <stdair/bom/Inventory.hpp> +// Network: child of BomRoot, needed for creation of BomRoot +#include <stdair/bom/Network.hpp> +#include <stdair/bom/FlightDate.hpp> #include <stdair/factory/FacSupervisor.hpp> #include <stdair/factory/FacBomContent.hpp> #include <stdair/service/Logger.hpp> @@ -35,6 +40,9 @@ // The root of the BOM tree, on which all of the other BOM objects // will be attached, is being created with the STDAIR_Service constructor. + // Initialise the AirlineFeatureSet object, and attach it to the BomRoot + initAirlineFeatureSet (); + // Set the log file logInit (iLogParams); @@ -49,6 +57,9 @@ // The root of the BOM tree, on which all of the other BOM objects // will be attached, is being created with the STDAIR_Service constructor. + // Initialise the AirlineFeatureSet object, and attach it to the BomRoot + initAirlineFeatureSet (); + // Set the log file logInit (iLogParams); @@ -78,12 +89,92 @@ // ////////////////////////////////////////////////////////////////////// void STDAIR_Service::init () { } + + // ////////////////////////////////////////////////////////////////////// + void STDAIR_Service::initAirlineFeatureSet () { + // Initialise the set of required airline features + stdair::AirlineFeatureSet& lAirlineFeatureSet = + stdair::FacBomContent::instance().create<stdair::AirlineFeatureSet>(); + + // Set the AirlineFeatureSet for the BomRoot. + _bomRoot.setAirlineFeatureSet (&lAirlineFeatureSet); + } // ////////////////////////////////////////////////////////////////////// + void STDAIR_Service:: + addAirlineFeature (const AirlineCode_T& iAirlineCode) const { + + // Initialise an AirlineFeature object + stdair::AirlineFeatureKey_T lAirlineFeatureKey (iAirlineCode); + stdair::AirlineFeature& lAirlineFeature = stdair::FacBomContent:: + instance().create<stdair::AirlineFeature> (lAirlineFeatureKey); + + // Retrieve the AirlineFeatureSet object + stdair::AirlineFeatureSet& lAirlineFeatureSet = + _bomRoot.getAirlineFeatureSet(); + + // Add the AirlineFeature object to its AirlineFeatureSet parent + stdair::FacBomContent:: + linkWithParent<stdair::AirlineFeature> (lAirlineFeature, + lAirlineFeatureSet); + } + + // ////////////////////////////////////////////////////////////////////// void STDAIR_Service::finalise () { - // std::cout << "In STDAIR_Service destructor, before cleaning" << std::endl; + //std::cout << "In STDAIR_Service destructor, before cleaning" << std::endl; FacSupervisor::cleanAll(); - // std::cout << "In STDAIR_Service destructor, after cleaning" << std::endl; + //std::cout << "In STDAIR_Service destructor, after cleaning" << std::endl; } + // ////////////////////////////////////////////////////////////////////// + Inventory& STDAIR_Service:: + getInventory (const AirlineCode_T& iAirlineCode) const { + + Inventory* lInventory_ptr = _bomRoot.getInventory (iAirlineCode); + if (lInventory_ptr == NULL) { + throw ObjectNotFoundException(); + } + assert (lInventory_ptr != NULL); + + return *lInventory_ptr; + } + + // ////////////////////////////////////////////////////////////////////// + Inventory& STDAIR_Service:: + createInventory (const AirlineCode_T& iAirlineCode) const { + Inventory* lInventory_ptr = _bomRoot.getInventory (iAirlineCode); + + // If there is no Inventory object for that airline already, create one + if (lInventory_ptr == NULL) { + const stdair::InventoryKey_T lInventoryKey (iAirlineCode); + + // Instantiate an Inventory object with the given airline code + lInventory_ptr = + &stdair::FacBomContent::instance(). + create<stdair::Inventory> (lInventoryKey); + assert (lInventory_ptr != NULL); + + // Link the created inventory with the bom root. + stdair::FacBomContent::linkWithParent<stdair::Inventory> (*lInventory_ptr, + _bomRoot); + + // Set the AirlineFeature for the inventory. + const stdair::AirlineFeatureSet& lAirlineFeatureSet = + _bomRoot.getAirlineFeatureSet (); + const stdair::AirlineFeature* lAirlineFeature_ptr = + lAirlineFeatureSet.getAirlineFeature (iAirlineCode); + + // TODO: throw an exception? + if (lAirlineFeature_ptr == NULL) { + STDAIR_LOG_ERROR (lAirlineFeatureSet.display() + << "Needed airline code: " << iAirlineCode); + } + + lInventory_ptr->setAirlineFeature (lAirlineFeature_ptr); + } + assert (lInventory_ptr != NULL); + + return *lInventory_ptr; + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |