StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
STDAIR_Service.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 #if BOOST_VERSION >= 104100
8 // Boost Property Tree
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/property_tree/json_parser.hpp>
11 #endif // BOOST_VERSION >= 104100
12 // StdAir
13 #include <stdair/stdair_types.hpp>
19 #include <stdair/bom/BomRoot.hpp>
31 
32 #if BOOST_VERSION >= 104100
33 namespace bpt = boost::property_tree;
34 #else // BOOST_VERSION >= 104100
35 namespace bpt {
36  typedef char ptree;
37 }
38 #endif // BOOST_VERSION >= 104100
39 
40 namespace stdair {
41 
42  // //////////////////////////////////////////////////////////////////////
43  STDAIR_Service::STDAIR_Service() : _stdairServiceContext (NULL) {
44 
45  // Initialise the service context
46  initServiceContext();
47 
48  // Initialise the (remaining of the) context
49  init();
50  }
51 
52  // //////////////////////////////////////////////////////////////////////
54  : _stdairServiceContext (NULL) {
55  assert (false);
56  }
57 
58  // //////////////////////////////////////////////////////////////////////
60  : _stdairServiceContext (NULL) {
61 
62  // Initialise the service context
63  initServiceContext();
64 
65  // Set the log file
66  logInit (iLogParams);
67 
68  // Initialise the (remaining of the) context
69  init();
70  }
71 
72  // //////////////////////////////////////////////////////////////////////
74  const BasDBParams& iDBParams)
75  : _stdairServiceContext (NULL) {
76 
77  // Initialise the service context
78  initServiceContext();
79 
80  // Set the log file
81  logInit (iLogParams);
82 
83  // Create a database session
84  dbInit (iDBParams);
85 
86  // Initialise the (remaining of the) context
87  init();
88  }
89 
90  // //////////////////////////////////////////////////////////////////////
92  // Delete/Clean all the objects from memory
93  finalise();
94  }
95 
96  // //////////////////////////////////////////////////////////////////////
97  void STDAIR_Service::initServiceContext() {
98  // Initialise the service context
99  STDAIR_ServiceContext& lSTDAIR_ServiceContext =
101 
102  // Store the stdair service context
103  _stdairServiceContext = &lSTDAIR_ServiceContext;
104  }
105 
106  // //////////////////////////////////////////////////////////////////////
107  void STDAIR_Service::logInit (const BasLogParams& iLogParams) {
108  Logger::init (iLogParams);
109  }
110 
111  // //////////////////////////////////////////////////////////////////////
112  void STDAIR_Service::dbInit (const BasDBParams& iDBParams) {
113  DBSessionManager::init (iDBParams);
114 
115  // Store the database parameters into the StdAir service context
116  assert (_stdairServiceContext != NULL);
117  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
118  lSTDAIR_ServiceContext.setDBParams (iDBParams);
119  }
120 
121  // //////////////////////////////////////////////////////////////////////
122  void STDAIR_Service::init() {
123  }
124 
125  // //////////////////////////////////////////////////////////////////////
127  // Retrieve the StdAir service context
128  assert (_stdairServiceContext != NULL);
129  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
130  return lSTDAIR_ServiceContext.getBomRoot();
131  }
132 
133  // //////////////////////////////////////////////////////////////////////
135  // Retrieve the StdAir service context
136  assert (_stdairServiceContext != NULL);
137  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
138  return lSTDAIR_ServiceContext.getEventQueue();
139  }
140 
141  // //////////////////////////////////////////////////////////////////////
143  return Logger::getLogParams();
144  }
145 
146  // //////////////////////////////////////////////////////////////////////
148  // Retrieve the StdAir service context
149  assert (_stdairServiceContext != NULL);
150  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
151  return lSTDAIR_ServiceContext.getDBParams();
152  }
153 
154  // //////////////////////////////////////////////////////////////////////
157  // Retrieve the StdAir service context
158  assert (_stdairServiceContext != NULL);
159  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
160  return lSTDAIR_ServiceContext.getServiceInitialisationType();
161  }
162 
163  // //////////////////////////////////////////////////////////////////////
165  // Retrieve the StdAir service context
166  assert (_stdairServiceContext != NULL);
167  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
168 
169  // Retrieve the BOM tree root
170  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
171 
172  // Delegate the building process to the dedicated command
174  }
175 
176  // //////////////////////////////////////////////////////////////////////
177  void STDAIR_Service::
178  buildDummyInventory (const CabinCapacity_T& iCabinCapacity) {
179  // Retrieve the StdAir service context
180  assert (_stdairServiceContext != NULL);
181  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
182 
183  // Retrieve the BOM tree root
184  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
185 
186  // Delegate the building process to the dedicated command
187  CmdBomManager::buildDummyInventory (lBomRoot, iCabinCapacity);
188  }
189 
190  // //////////////////////////////////////////////////////////////////////
191  void STDAIR_Service::
193  // Build a sample list of travel solution structures
195  }
196 
197  // //////////////////////////////////////////////////////////////////////
198  void STDAIR_Service::
200  // Build a sample list of travel solution structures
201  CmdBomManager::buildSampleTravelSolutions (ioTravelSolutionList);
202  }
203 
204  // //////////////////////////////////////////////////////////////////////
206  buildSampleBookingRequest (const bool isForCRS) {
207 
208  // Build a sample booking request structure
209  if (isForCRS == true) {
210  return CmdBomManager::buildSampleBookingRequestForCRS();
211  }
212 
214  }
215 
216  // //////////////////////////////////////////////////////////////////////
217  std::string STDAIR_Service::
218  jsonExport (const stdair::AirlineCode_T& iAirlineCode,
219  const stdair::FlightNumber_T& iFlightNumber,
220  const stdair::Date_T& iDepartureDate) const {
221  std::ostringstream oStr;
222 
223  // Retrieve the StdAir service context
224  assert (_stdairServiceContext != NULL);
225  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
226 
227  // Retrieve the BOM tree root
228  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
229 
230  // Retrieve the flight-date object corresponding to the key
231  FlightDate* lFlightDate_ptr =
232  BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
233  iFlightNumber, iDepartureDate);
234 
235  // Dump the content of the whole BOM tree into the string
236  if (lFlightDate_ptr != NULL) {
237  BomJSONExport::jsonExport (oStr, *lFlightDate_ptr);
238 
239  } else {
240 #if BOOST_VERSION >= 104100
241  //
242  bpt::ptree lPropertyTree;
243 
244  // Build the appropriate message, so that the client may know that
245  // no flight-date can be found for that given key.
246  std::ostringstream oNoFlightDateStream;
247  oNoFlightDateStream << "No flight-date found for the given key: '"
248  << iAirlineCode << iFlightNumber
249  << " - " << iDepartureDate << "'";
250  const std::string oNoFlightDateString (oNoFlightDateStream.str());
251 
252  // Put in the property tree the fact that no flight-date has been found.
253  // \note That is not (necessary) an error.
254  lPropertyTree.put ("error", oNoFlightDateString.c_str());
255 
256  // Write the property tree into the JSON stream.
257  write_json (oStr, lPropertyTree);
258 #endif // BOOST_VERSION >= 104100
259  }
260 
261  return oStr.str();
262  }
263 
264  // //////////////////////////////////////////////////////////////////////
265  std::string STDAIR_Service::list (const AirlineCode_T& iAirlineCode,
266  const FlightNumber_T& iFlightNumber) const {
267  std::ostringstream oStr;
268 
269  // Retrieve the StdAir service context
270  assert (_stdairServiceContext != NULL);
271  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
272 
273  // Retrieve the BOM tree root
274  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
275 
276  // Dump the content of the whole BOM tree into the string
277  BomDisplay::list (oStr, lBomRoot, iAirlineCode, iFlightNumber);
278 
279  return oStr.str();
280  }
281 
282  // //////////////////////////////////////////////////////////////////////
284  std::ostringstream oStr;
285 
286  // Retrieve the StdAir service context
287  assert (_stdairServiceContext != NULL);
288  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
289 
290  // Retrieve the BOM tree root
291  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
292 
293  // Dump the content of the whole BOM tree into the string
294  BomDisplay::listAirportPairDateRange (oStr, lBomRoot);
295 
296  return oStr.str();
297  }
298 
299  // //////////////////////////////////////////////////////////////////////
300  bool STDAIR_Service::check (const AirlineCode_T& iAirlineCode,
301  const FlightNumber_T& iFlightNumber,
302  const stdair::Date_T& iDepartureDate) const {
303  std::ostringstream oStr;
304 
305  // Retrieve the StdAir service context
306  assert (_stdairServiceContext != NULL);
307  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
308 
309  // Retrieve the BOM tree root
310  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
311 
312  // Dump the content of the whole BOM tree into the string
313  const FlightDate* lFlightDate_ptr =
314  BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
315  iFlightNumber,
316  iDepartureDate);
317 
318  return (lFlightDate_ptr != NULL);
319  }
320 
321  // //////////////////////////////////////////////////////////////////////
323  const stdair::AirportCode_T& ioDestination,
324  const stdair::Date_T& ioDepartureDate) const {
325  std::ostringstream oStr;
326 
327  // Retrieve the StdAir service context
328  assert (_stdairServiceContext != NULL);
329  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
330 
331  // Retrieve the BOM tree root
332  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
333 
334  // Dump the content of the whole BOM tree into the string
335  stdair::DatePeriodList_T lDatePeriodList;
337  ioDestination,
338  ioDepartureDate,
339  lDatePeriodList);
340 
341  return (lDatePeriodList.size() != 0);
342  }
343 
344  // //////////////////////////////////////////////////////////////////////
345  std::string STDAIR_Service::csvDisplay() const {
346  std::ostringstream oStr;
347 
348  // Retrieve the StdAir service context
349  assert (_stdairServiceContext != NULL);
350  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
351 
352  // Retrieve the BOM tree root
353  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
354 
355  // Dump the content of the whole BOM tree into the string
356  BomDisplay::csvDisplay (oStr, lBomRoot);
357 
358  return oStr.str();
359  }
360 
361  // //////////////////////////////////////////////////////////////////////
362  std::string STDAIR_Service::
363  csvDisplay (const stdair::AirlineCode_T& iAirlineCode,
364  const stdair::FlightNumber_T& iFlightNumber,
365  const stdair::Date_T& iDepartureDate) const {
366  std::ostringstream oStr;
367 
368  // Retrieve the StdAir service context
369  assert (_stdairServiceContext != NULL);
370  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
371 
372  // Retrieve the BOM tree root
373  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
374 
375  // Retrieve the flight-date object corresponding to the key
376  FlightDate* lFlightDate_ptr =
377  BomRetriever::retrieveFlightDateFromKeySet (lBomRoot, iAirlineCode,
378  iFlightNumber, iDepartureDate);
379 
380  // Dump the content of the whole BOM tree into the string
381  if (lFlightDate_ptr != NULL) {
382  BomDisplay::csvDisplay (oStr, *lFlightDate_ptr);
383 
384  } else {
385  oStr << " No flight-date found for the given key: '"
386  << iAirlineCode << iFlightNumber << " - " << iDepartureDate << "'";
387  }
388 
389  return oStr.str();
390  }
391 
392  // //////////////////////////////////////////////////////////////////////
393  std::string STDAIR_Service::
394  csvDisplay (const TravelSolutionList_T& iTravelSolutionList) const {
395 
396  // Dump the content of the whole list of travel solutions into the string
397  std::ostringstream oStr;
398  BomDisplay::csvDisplay (oStr, iTravelSolutionList);
399 
400  return oStr.str();
401  }
402 
403  // //////////////////////////////////////////////////////////////////////
404  std::string STDAIR_Service::
406  const stdair::AirportCode_T& iDestination,
407  const stdair::Date_T& iDepartureDate) const {
408  std::ostringstream oStr;
409 
410  // Retrieve the StdAir service context
411  assert (_stdairServiceContext != NULL);
412  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
413 
414  // Retrieve the BOM tree root
415  BomRoot& lBomRoot = lSTDAIR_ServiceContext.getBomRoot();
416 
417  // Retrieve the flight-date object corresponding to the key
418  DatePeriodList_T lDatePeriodList;
420  iDestination, iDepartureDate,
421  lDatePeriodList);
422 
423  // Dump the content of the whole BOM tree into the string
424  if (lDatePeriodList.empty()) {
425  oStr << " No fare-rule found for the given key: '"
426  << iOrigin << "-" << iDestination << " - " << iDepartureDate << "'";
427  } else {
428  BomDisplay::csvDisplay (oStr, lDatePeriodList);
429  }
430 
431  return oStr.str();
432  }
433 
434  // //////////////////////////////////////////////////////////////////////
435  void STDAIR_Service::finalise() {
436  // Clean all the objects
438  }
439 
440  // ////////////////////////////////////////////////////////////////////
443 
444  // Retrieve the StdAir service context
445  assert (_stdairServiceContext != NULL);
446  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
447 
448  // Retrieve the event queue object instance
449  const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
450 
451  // Delegate the call to the dedicated command
452  const Count_T& oExpectedTotalNumberOfEventsToBeGenerated =
454 
455  //
456  return oExpectedTotalNumberOfEventsToBeGenerated;
457  }
458 
459  // ////////////////////////////////////////////////////////////////////
462 
463  // Retrieve the StdAir service context
464  assert (_stdairServiceContext != NULL);
465  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
466 
467  // Retrieve the event queue object instance
468  const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
469 
470  // Delegate the call to the dedicated command
471  const Count_T& oExpectedTotalNumberOfEventsToBeGenerated =
472  lQueue.getExpectedTotalNbOfEvents (iType);
473 
474  //
475  return oExpectedTotalNumberOfEventsToBeGenerated;
476  }
477 
478  // ////////////////////////////////////////////////////////////////////
481 
482  // Retrieve the StdAir service context
483  assert (_stdairServiceContext != NULL);
484  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
485 
486  // Retrieve the event queue object instance
487  const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
488 
489  // Delegate the call to the dedicated command
490  const Count_T& oActualTotalNumberOfEventsToBeGenerated =
491  lQueue.getActualTotalNbOfEvents();
492 
493  //
494  return oActualTotalNumberOfEventsToBeGenerated;
495  }
496 
497  // ////////////////////////////////////////////////////////////////////
500 
501  // Retrieve the StdAir service context
502  assert (_stdairServiceContext != NULL);
503  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
504 
505  // Retrieve the event queue object instance
506  const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
507 
508  // Delegate the call to the dedicated command
509  const Count_T& oActualTotalNumberOfEventsToBeGenerated =
510  lQueue.getActualTotalNbOfEvents (iType);
511 
512  //
513  return oActualTotalNumberOfEventsToBeGenerated;
514  }
515 
516  // ////////////////////////////////////////////////////////////////////
518 
519  // Retrieve the StdAir service context
520  assert (_stdairServiceContext != NULL);
521  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
522 
523  // Retrieve the event queue object instance
524  EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
525 
526  // Extract the next event from the queue
527  return lQueue.popEvent (ioEventStruct);
528  }
529 
530  // ////////////////////////////////////////////////////////////////////
532 
533  // Retrieve the StdAir service context
534  assert (_stdairServiceContext != NULL);
535  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
536 
537  // Retrieve the event queue object instance
538  const EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
539 
540  // Calculates whether the event queue has been fully emptied
541  const bool isQueueDone = lQueue.isQueueDone();
542 
543  //
544  return isQueueDone;
545  }
546 
547  // ////////////////////////////////////////////////////////////////////
548  void STDAIR_Service::reset() const {
549 
550  // Retrieve the StdAir service context
551  assert (_stdairServiceContext != NULL);
552  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
553 
554  // Retrieve the event queue object instance
555  EventQueue& lQueue = lSTDAIR_ServiceContext.getEventQueue();
556 
557  // Delegate the call to the event queue object
558  lQueue.reset();
559  }
560 
561 }