StdAir Logo  1.00.0
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
14 #include <stdair/stdair_json.hpp>
22 #include <stdair/bom/BomRoot.hpp>
33 
34 #if BOOST_VERSION >= 104100
35 namespace bpt = boost::property_tree;
36 #else // BOOST_VERSION >= 104100
37 namespace bpt {
38  typedef char ptree;
39 }
40 #endif // BOOST_VERSION >= 104100
41 
42 namespace stdair {
43 
44  // //////////////////////////////////////////////////////////////////////
45  STDAIR_Service::STDAIR_Service() : _stdairServiceContext (NULL) {
46 
47  // Initialise the service context
48  initServiceContext();
49 
50  // Initialise the (remaining of the) context
51  init();
52  }
53 
54  // //////////////////////////////////////////////////////////////////////
56  : _stdairServiceContext (NULL) {
57  assert (false);
58  }
59 
60  // //////////////////////////////////////////////////////////////////////
62  : _stdairServiceContext (NULL) {
63 
64  // Initialise the service context
65  initServiceContext();
66 
67  // Set the log file
68  logInit (iLogParams);
69 
70  // Initialise the (remaining of the) context
71  init();
72  }
73 
74  // //////////////////////////////////////////////////////////////////////
76  const BasDBParams& iDBParams)
77  : _stdairServiceContext (NULL) {
78 
79  // Initialise the service context
80  initServiceContext();
81 
82  // Set the log file
83  logInit (iLogParams);
84 
85  // Create a database session
86  dbInit (iDBParams);
87 
88  // Initialise the (remaining of the) context
89  init();
90  }
91 
92  // //////////////////////////////////////////////////////////////////////
94  // Delete/Clean all the objects from memory
95  finalise();
96  }
97 
98  // //////////////////////////////////////////////////////////////////////
99  void STDAIR_Service::initServiceContext() {
100  // Initialise the service context
101  STDAIR_ServiceContext& lSTDAIR_ServiceContext =
103 
104  // Store the stdair service context
105  _stdairServiceContext = &lSTDAIR_ServiceContext;
106  }
107 
108  // //////////////////////////////////////////////////////////////////////
109  void STDAIR_Service::logInit (const BasLogParams& iLogParams) {
110  Logger::init (iLogParams);
111  }
112 
113  // //////////////////////////////////////////////////////////////////////
114  void STDAIR_Service::dbInit (const BasDBParams& iDBParams) {
115  DBSessionManager::init (iDBParams);
116 
117  // Store the database parameters into the StdAir service context
118  assert (_stdairServiceContext != NULL);
119  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
120  lSTDAIR_ServiceContext.setDBParams (iDBParams);
121  }
122 
123  // //////////////////////////////////////////////////////////////////////
124  void STDAIR_Service::init() {
125  }
126 
127  // //////////////////////////////////////////////////////////////////////
129  // Retrieve the StdAir service context
130  assert (_stdairServiceContext != NULL);
131  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
132  *_stdairServiceContext;
133  // Return the clone built-in Bom root
134  return lSTDAIR_ServiceContext.getCloneBomRoot();
135  }
136 
137  // //////////////////////////////////////////////////////////////////////
139  // Retrieve the StdAir service context
140  assert (_stdairServiceContext != NULL);
141  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
142  *_stdairServiceContext;
143  // Return the persistent built-in Bom root
144  return lSTDAIR_ServiceContext.getPersistentBomRoot();
145  }
146 
147  // //////////////////////////////////////////////////////////////////////
149  return Logger::getLogParams();
150  }
151 
152  // //////////////////////////////////////////////////////////////////////
154  // Retrieve the StdAir service context
155  assert (_stdairServiceContext != NULL);
156  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
157  *_stdairServiceContext;
158  return lSTDAIR_ServiceContext.getDBParams();
159  }
160 
161  // //////////////////////////////////////////////////////////////////////
164  // Retrieve the StdAir service context
165  assert (_stdairServiceContext != NULL);
166  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
167  *_stdairServiceContext;
168  return lSTDAIR_ServiceContext.getServiceInitialisationType();
169  }
170 
171  // //////////////////////////////////////////////////////////////////////
173  // Retrieve the StdAir service context
174  assert (_stdairServiceContext != NULL);
175  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
176  *_stdairServiceContext;
177 
178  // Retrieve the BOM tree root
179  BomRoot& lPersistentBomRoot = lSTDAIR_ServiceContext.getPersistentBomRoot();
180 
181  // Delegate the building process to the dedicated command
182  CmdBomManager::buildSampleBom (lPersistentBomRoot);
183  }
184 
185  // //////////////////////////////////////////////////////////////////////
186  void STDAIR_Service::
187  buildDummyInventory (const CabinCapacity_T& iCabinCapacity) {
188  // Retrieve the StdAir service context
189  assert (_stdairServiceContext != NULL);
190  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
191  *_stdairServiceContext;
192 
193  // Retrieve the BOM tree root
194  BomRoot& lPersistentBomRoot = lSTDAIR_ServiceContext.getPersistentBomRoot();
195 
196  // Delegate the building process to the dedicated command
197  CmdBomManager::buildDummyInventory (lPersistentBomRoot, iCabinCapacity);
198  CmdBomManager::buildCompleteDummyInventoryForFareFamilies (lPersistentBomRoot);
199 
200  }
201 
202  // //////////////////////////////////////////////////////////////////////
203  void STDAIR_Service::
205  // Retrieve the StdAir service context
206  assert (_stdairServiceContext != NULL);
207 
208  // Delegate the building process to the dedicated command
210 
211  }
212 
213  // //////////////////////////////////////////////////////////////////////
214  void STDAIR_Service::
216  // Build a sample list of travel solution structures
218  }
219 
220  // //////////////////////////////////////////////////////////////////////
221  void STDAIR_Service::
223  // Build a sample list of travel solution structures
224  CmdBomManager::buildSampleTravelSolutions (ioTravelSolutionList);
225  }
226 
227  // //////////////////////////////////////////////////////////////////////
229  buildSampleBookingRequest (const bool isForCRS) {
230 
231  // Build a sample booking request structure
232  if (isForCRS == true) {
233  return CmdBomManager::buildSampleBookingRequestForCRS();
234  }
235 
237  }
238 
239 
240  // //////////////////////////////////////////////////////////////////////
241  std::string STDAIR_Service::
243  const FlightNumber_T& iFlightNumber) const {
244  std::ostringstream oStr;
245 
246  // Retrieve the StdAir service context
247  assert (_stdairServiceContext != NULL);
248  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
249  *_stdairServiceContext;
250 
251  // Retrieve the BOM tree root
252  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
253 
254  BomJSONExport::jsonExportFlightDateList (oStr, lCloneBomRoot,
255  iAirlineCode, iFlightNumber);
256 
257  return oStr.str();
258  }
259 
260  // //////////////////////////////////////////////////////////////////////
261  std::string STDAIR_Service::
263  const stdair::FlightNumber_T& iFlightNumber,
264  const stdair::Date_T& iDepartureDate) const {
265  std::ostringstream oStr;
266 
267  // Retrieve the StdAir service context
268  assert (_stdairServiceContext != NULL);
269  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
270  *_stdairServiceContext;
271 
272  // Retrieve the BOM tree root
273  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
274 
275  // Retrieve the flight-date object corresponding to the key
276  FlightDate* lFlightDate_ptr =
277  BomRetriever::retrieveFlightDateFromKeySet (lCloneBomRoot, iAirlineCode,
278  iFlightNumber,
279  iDepartureDate);
280 
281  // Dump the content of the whole BOM tree into the string
282  if (lFlightDate_ptr != NULL) {
283  BomJSONExport::jsonExportFlightDateObjects (oStr, *lFlightDate_ptr);
284 
285  } else {
286 #if BOOST_VERSION >= 104100
287  //
288  bpt::ptree lPropertyTree;
289 
290  // Build the appropriate message, so that the client may know that
291  // no flight-date can be found for that given key.
292  std::ostringstream oNoFlightDateStream;
293  oNoFlightDateStream << "No flight-date found for the given key: '"
294  << iAirlineCode << iFlightNumber
295  << " - " << iDepartureDate << "'";
296  const std::string oNoFlightDateString (oNoFlightDateStream.str());
297 
298  // Put in the property tree the fact that no flight-date has been found.
299  // \note That is not (necessary) an error.
300  lPropertyTree.put ("error", oNoFlightDateString.c_str());
301 
302  // Write the property tree into the JSON stream.
303  write_json (oStr, lPropertyTree);
304 #endif // BOOST_VERSION >= 104100
305  }
306 
307  return oStr.str();
308  }
309 
310  // //////////////////////////////////////////////////////////////////////
311  std::string STDAIR_Service::
312  jsonExportEventObject (const EventStruct& iEventStruct) const {
313 
314  std::ostringstream oStr;
315 
316  const EventType::EN_EventType& lEventType =
317  iEventStruct.getEventType();
318 
319  switch (lEventType) {
320  case EventType::BKG_REQ:{
322  break;
323  }
324  case EventType::CX:
327  case EventType::SKD_CHG:
328  case EventType::SNAPSHOT:
329  case EventType::RM:
330  break;
331  case EventType::BRK_PT:
332  BomJSONExport::jsonExportBreakPointObject (oStr, iEventStruct);
333  break;
334  default:
335  break;
336  }
337  return oStr.str();
338  }
339 
340  // //////////////////////////////////////////////////////////////////////
341  bool STDAIR_Service::
342  jsonImportConfiguration (const JSONString& iJSONString) const {
343 
344  // Retrieve the StdAir service context
345  assert (_stdairServiceContext != NULL);
346  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
347  *_stdairServiceContext;
348 
349  // Retrieve the config holder
350  ConfigHolderStruct& lConfigHolder =
351  lSTDAIR_ServiceContext.getConfigHolder();
352 
353  // Import the JSON string in the configuration holder
354  return BomJSONImport::jsonImportConfig (iJSONString, lConfigHolder);
355  }
356 
357  // //////////////////////////////////////////////////////////////////////
358  std::string STDAIR_Service::
360 
361  // Retrieve the StdAir service context
362  assert (_stdairServiceContext != NULL);
363  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
364  *_stdairServiceContext;
365 
366  // Retrieve the config holder
367  ConfigHolderStruct& lConfigHolder =
368  lSTDAIR_ServiceContext.getConfigHolder();
369 
370  // Export the configuration tree in a JSon format
371  return lConfigHolder.jsonExport();
372  }
373 
374  // //////////////////////////////////////////////////////////////////////
375  void STDAIR_Service::importINIConfig (const ConfigINIFile& iConfigINIFile) {
376 
377  // Retrieve the StdAir service context
378  assert (_stdairServiceContext != NULL);
379  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
380  *_stdairServiceContext;
381 
382  // Retrieve the config holder
383  ConfigHolderStruct& lConfigHolder =
384  lSTDAIR_ServiceContext.getConfigHolder();
385 
386  // Try to import the configuration
387  stdair::BomINIImport::importINIConfig (lConfigHolder, iConfigINIFile);
388  }
389 
390  // //////////////////////////////////////////////////////////////////////
391  void STDAIR_Service::importConfigValue (const std::string& iValue,
392  const std::string& iPath) {
393 
394  // Retrieve the StdAir service context
395  assert (_stdairServiceContext != NULL);
396  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
397  *_stdairServiceContext;
398 
399  // Retrieve the config holder
400  ConfigHolderStruct& lConfigHolder =
401  lSTDAIR_ServiceContext.getConfigHolder();
402 
403  // Add the given value to the configuration
404  lConfigHolder.addValue (iValue, iPath);
405  }
406 
407  // //////////////////////////////////////////////////////////////////////
409 
410  // Retrieve the StdAir service context
411  assert (_stdairServiceContext != NULL);
412  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
413  *_stdairServiceContext;
414 
415  // Retrieve the config holder
416  ConfigHolderStruct& lConfigHolder =
417  lSTDAIR_ServiceContext.getConfigHolder();
418 
419  // Retrieve the persistent BOM tree root
420  BomRoot& lPersistentBomRoot =
421  lSTDAIR_ServiceContext.getPersistentBomRoot();
422 
423  // Add the given value to the configuration
424  lConfigHolder.updateAirlineFeatures (lPersistentBomRoot);
425  }
426 
427  // //////////////////////////////////////////////////////////////////////
428  std::string STDAIR_Service::list (const AirlineCode_T& iAirlineCode,
429  const FlightNumber_T& iFlightNumber) const {
430  std::ostringstream oStr;
431 
432  // Retrieve the StdAir service context
433  assert (_stdairServiceContext != NULL);
434  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
435 
436  // Retrieve the BOM tree root
437  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
438 
439  // Dump the content of the whole BOM tree into the string
440  BomDisplay::list (oStr, lCloneBomRoot, iAirlineCode, iFlightNumber);
441 
442  return oStr.str();
443  }
444 
445  // //////////////////////////////////////////////////////////////////////
447  std::ostringstream oStr;
448 
449  // Retrieve the StdAir service context
450  assert (_stdairServiceContext != NULL);
451  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
452 
453  // Retrieve the BOM tree root
454  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
455 
456  // Dump the content of the whole BOM tree into the string
457  BomDisplay::listAirportPairDateRange (oStr, lCloneBomRoot);
458 
459  return oStr.str();
460  }
461 
462  // //////////////////////////////////////////////////////////////////////
463  bool STDAIR_Service::check (const AirlineCode_T& iAirlineCode,
464  const FlightNumber_T& iFlightNumber,
465  const stdair::Date_T& iDepartureDate) const {
466  std::ostringstream oStr;
467 
468  // Retrieve the StdAir service context
469  assert (_stdairServiceContext != NULL);
470  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
471 
472  // Retrieve the BOM tree root
473  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
474 
475  // Dump the content of the whole BOM tree into the string
476  const FlightDate* lFlightDate_ptr =
477  BomRetriever::retrieveFlightDateFromKeySet (lCloneBomRoot, iAirlineCode,
478  iFlightNumber,
479  iDepartureDate);
480 
481  return (lFlightDate_ptr != NULL);
482  }
483 
484  // //////////////////////////////////////////////////////////////////////
486  const stdair::AirportCode_T& ioDestination,
487  const stdair::Date_T& ioDepartureDate) const {
488  std::ostringstream oStr;
489 
490  // Retrieve the StdAir service context
491  assert (_stdairServiceContext != NULL);
492  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
493 
494  // Retrieve the BOM tree root
495  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
496 
497  // Dump the content of the whole BOM tree into the string
498  stdair::DatePeriodList_T lDatePeriodList;
499  BomRetriever::retrieveDatePeriodListFromKeySet (lCloneBomRoot, ioOrigin,
500  ioDestination,
501  ioDepartureDate,
502  lDatePeriodList);
503 
504  return (lDatePeriodList.size() != 0);
505  }
506 
507  // //////////////////////////////////////////////////////////////////////
508  std::string STDAIR_Service::configDisplay () const {
509 
510  // Retrieve the StdAir service context
511  assert (_stdairServiceContext != NULL);
512  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
513  *_stdairServiceContext;
514 
515  // Retrieve the config holder
516  ConfigHolderStruct& lConfigHolder =
517  lSTDAIR_ServiceContext.getConfigHolder();
518 
519  // Display (dump in the returned string) the configuration.
520  return lConfigHolder.describe();
521 
522  }
523 
524  // //////////////////////////////////////////////////////////////////////
525  std::string STDAIR_Service::csvDisplay () const {
526 
527  // Retrieve the StdAir service context
528  assert (_stdairServiceContext != NULL);
529  const STDAIR_ServiceContext& lSTDAIR_ServiceContext =
530  *_stdairServiceContext;
531 
532  // Retrieve the persistent BOM tree root
533  const BomRoot& lPersistentBomRoot =
534  lSTDAIR_ServiceContext.getPersistentBomRoot();
535 
536  // Call the dedicated service
537  return csvDisplay (lPersistentBomRoot);
538  }
539 
540  // //////////////////////////////////////////////////////////////////////
541  std::string STDAIR_Service::csvDisplay (const BomRoot& iBomRoot) const {
542  std::ostringstream oStr;
543 
544  // Retrieve the StdAir service context
545  assert (_stdairServiceContext != NULL);
546 
547  // Dump the content of the whole BOM tree into the string
548  BomDisplay::csvDisplay (oStr, iBomRoot);
549 
550  return oStr.str();
551  }
552 
553  // //////////////////////////////////////////////////////////////////////
554  std::string STDAIR_Service::
555  csvDisplay (const stdair::AirlineCode_T& iAirlineCode,
556  const stdair::FlightNumber_T& iFlightNumber,
557  const stdair::Date_T& iDepartureDate) const {
558  std::ostringstream oStr;
559 
560  // Retrieve the StdAir service context
561  assert (_stdairServiceContext != NULL);
562  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
563 
564  // Retrieve the BOM tree root
565  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
566 
567  // Retrieve the flight-date object corresponding to the key
568  FlightDate* lFlightDate_ptr =
569  BomRetriever::retrieveFlightDateFromKeySet (lCloneBomRoot, iAirlineCode,
570  iFlightNumber,
571  iDepartureDate);
572 
573  // Dump the content of the whole BOM tree into the string
574  if (lFlightDate_ptr != NULL) {
575  BomDisplay::csvDisplay (oStr, *lFlightDate_ptr);
576 
577  } else {
578  oStr << " No flight-date found for the given key: '"
579  << iAirlineCode << iFlightNumber << " - " << iDepartureDate << "'";
580  }
581 
582  return oStr.str();
583  }
584 
585  // //////////////////////////////////////////////////////////////////////
586  std::string STDAIR_Service::
587  csvDisplay (const TravelSolutionList_T& iTravelSolutionList) const {
588 
589  // Dump the content of the whole list of travel solutions into the string
590  std::ostringstream oStr;
591  BomDisplay::csvDisplay (oStr, iTravelSolutionList);
592 
593  return oStr.str();
594  }
595 
596  // //////////////////////////////////////////////////////////////////////
597  std::string STDAIR_Service::
599  const stdair::AirportCode_T& iDestination,
600  const stdair::Date_T& iDepartureDate) const {
601  std::ostringstream oStr;
602 
603  // Retrieve the StdAir service context
604  assert (_stdairServiceContext != NULL);
605  const STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
606 
607  // Retrieve the BOM tree root
608  const BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
609 
610  // Retrieve the flight-date object corresponding to the key
611  DatePeriodList_T lDatePeriodList;
612  BomRetriever::retrieveDatePeriodListFromKeySet (lCloneBomRoot, iOrigin,
613  iDestination,
614  iDepartureDate,
615  lDatePeriodList);
616 
617  // Dump the content of the whole BOM tree into the string
618  if (lDatePeriodList.empty()) {
619  oStr << " No fare-rule found for the given key: '"
620  << iOrigin << "-" << iDestination << " - " << iDepartureDate << "'";
621  } else {
622  BomDisplay::csvDisplay (oStr, lDatePeriodList);
623  }
624 
625  return oStr.str();
626  }
627 
628  // //////////////////////////////////////////////////////////////////////
629  void STDAIR_Service::finalise() {
630  // Clean all the objects
632  }
633 
634  // //////////////////////////////////////////////////////////////////////
636 
637  // Retrieve the StdAir service context
638  assert (_stdairServiceContext != NULL);
639  STDAIR_ServiceContext& lSTDAIR_ServiceContext = *_stdairServiceContext;
640 
641  // Clean all the cloned objects
643 
644  // Init the root of the clone BOM tree
645  lSTDAIR_ServiceContext.initCloneBomRoot();
646 
647  // Retrieve the persistent BOM tree root and the clone BOM tree root
648  const BomRoot& lPersistentBomRoot =
649  lSTDAIR_ServiceContext.getPersistentBomRoot();
650  BomRoot& lCloneBomRoot = lSTDAIR_ServiceContext.getCloneBomRoot();
651 
652  // Call the dedicated service to clone the whole BOM
653  CmdCloneBomManager::cloneBomRoot (lPersistentBomRoot, lCloneBomRoot);
654 
655  }
656 
657 }