OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DbaPlace.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <exception>
6 #include <string>
7 #include <sstream>
8 #include <ctime>
9 // OpenTrep
11 #include <opentrep/bom/Place.hpp>
14 
15 namespace soci {
16 
17  // //////////////////////////////////////////////////////////////////////
19  from_base (values const& iPlaceValues, indicator /* ind */,
20  OPENTREP::Place& ioPlace) {
21  /*
22  select dts.iata_code, dts.location_type, dts.geoname_id,
23  icao_code, faa_code,
24  envelope_id, dts.name as utf_name, asciiname, latitude, longitude,
25  fclass, fcode, page_rank,
26  date(date_from) as date_from, date(date_until) as date_until,
27  comment,
28  country_code, cc2, country_name, continent_name,
29  admin1_code, admin1_UTF8_name, admin1_ASCII_name,
30  admin2_code, admin2_UTF8_name, admin2_ASCII_name,
31  admin3_code, admin4_code,
32  population, elevation, gtopo30,
33  time_zone, gmt_offset, dst_offset, raw_offset,
34  date(moddate) as moddate,
35  state_code, wiki_link,
36  alt.lang_code as alt_lang_code, alt.name as alt_name,
37  alt.specifiers as alt_spec,
38  city_iata_code, city_location_type, city_geoname_id,
39  city_UTF8_name, city_ASCII_name
40  from ori_por_public_details dts, ori_por_public_alt_names alt,
41  ori_por_public_served_cities cty
42  where dts.iata_code = alt.iata_code
43  and dts.location_type = alt.location_type
44  and dts.geoname_id = alt.geoname_id
45  and dts.iata_code = cty.iata_code
46  and dts.location_type = cty.location_type
47  and dts.geoname_id = cty.geoname_id
48  order by dts.iata_code, dts.location_type, dts.geoname_id
49  ;
50  */
51 
52  /*
53  * Retrieve the place key (IATA code, location type, Geonames ID).
54  * If ioPlace has already that key, then only the alternate names need
55  * to be added.
56  * Otherwise, all the fields of the Place need to be set.
57  */
58  // First, retrieve the key from the database fields
59  // IATA code
60  const std::string lIataCodeStr =
61  iPlaceValues.get<std::string> ("iata_code", "");
62  const OPENTREP::IATACode_T lIataCode (lIataCodeStr);
63  // Location type ('C' for city only, 'A' for airport only,
64  // 'CA' for airport/city)
65  const std::string lIataTypeStr =
66  iPlaceValues.get<std::string> ("location_type", "");
67  const OPENTREP::IATAType lIataType (lIataTypeStr);
68  // Geonames ID
69  const OPENTREP::GeonamesID_T lGeonameID =
70  iPlaceValues.get<int> ("geoname_id", 0);
71  //
72  const OPENTREP::LocationKey lNewPlaceKey (lIataCode, lIataType, lGeonameID);
73 
74  // Then, retrieve the key from the previous Place object
75  const OPENTREP::LocationKey lPlaceKey = ioPlace.getKey();
76 
77  // Compare the keys
78  if (lNewPlaceKey != lPlaceKey) {
79  // The place keys are different: the current place retrieved from
80  // the database is not the same as the one retrieved previously
81 
82  // Re-set the (STL) sets of terms for the Xapian index, spelling
83  // dictionary, etc.
84  ioPlace.resetIndexSets();
85 
86  // Reset the list of names of the given Place object
87  ioPlace.resetMatrix();
88 
89  // The IATA code will be set to the default value (empty string)
90  // when the column is null
91  ioPlace.setIataCode (lIataCode);
92 
93  // The ICAO code will be set to the default value (empty string)
94  // when the column is null
95  ioPlace.setIataType (lIataTypeStr);
96 
97  // Geonames ID
98  ioPlace.setGeonamesID (lGeonameID);
99 
100  // Envelope ID
101  const OPENTREP::EnvelopeID_T lEnvelopeID =
102  iPlaceValues.get<int> ("envelope_id");
103  ioPlace.setEnvelopeID (lEnvelopeID);
104 
105  // Beginning date of the validity range (empty when indefinitely valid)
106  const std::string lDateFromStr =
107  iPlaceValues.get<std::string> ("date_from", OPENTREP::DEFAULT_DATE_STR);
108  const OPENTREP::Date_T lDateFrom (boost::gregorian::from_string (lDateFromStr));
109  ioPlace.setDateFrom (lDateFrom);
110 
111  // End date of the validity range (empty when indefinitely valid)
112  const std::string lDateUntilStr =
113  iPlaceValues.get<std::string> ("date_until", OPENTREP::DEFAULT_DATE_STR);
114  const OPENTREP::Date_T lDateUntil (boost::gregorian::from_string (lDateUntilStr));
115  ioPlace.setDateEnd (lDateUntil);
116 
117  // The comment will be set to the default value (empty string)
118  // when the column is null
119  ioPlace.setComment (iPlaceValues.get<std::string> ("comment", ""));
120 
121  // The UTF8 name will be set to the default value (empty string)
122  // when the column is null
123  ioPlace.setCommonName (iPlaceValues.get<std::string> ("utf_name", ""));
124 
125  // The ASCII name will be set to the default value (empty string)
126  // when the column is null
127  ioPlace.setAsciiName (iPlaceValues.get<std::string> ("asciiname", ""));
128 
129  // The FAA code will be set to the default value (empty string)
130  // when the column is null
131  ioPlace.setFaaCode (iPlaceValues.get<std::string> ("faa_code", ""));
132 
133  // Feature class
134  const std::string lFeatClass = iPlaceValues.get<std::string>("fclass", "");
135  ioPlace.setFeatureClass (lFeatClass);
136  // Feature code
137  const std::string lFeatCode = iPlaceValues.get<std::string> ("fcode", "");
138  ioPlace.setFeatureCode (lFeatCode);
139 
140  // The state code will be set to the default value (empty string)
141  // when the column is null
142  ioPlace.setStateCode (iPlaceValues.get<std::string> ("state_code", ""));
143  ioPlace.setCountryCode (iPlaceValues.get<std::string> ("country_code"));
144  ioPlace.setCountryName (iPlaceValues.get<std::string> ("country_name"));
145  ioPlace.setAltCountryCode (iPlaceValues.get<std::string> ("cc2", ""));
146  ioPlace.setContinentName (iPlaceValues.get<std::string>("continent_name"));
147  ioPlace.setLatitude (iPlaceValues.get<double> ("latitude"));
148  ioPlace.setLongitude (iPlaceValues.get<double> ("longitude"));
149  ioPlace.setPageRank (iPlaceValues.get<double> ("page_rank",
151 
152  // The administrative level 1 details will be set to the default value
153  // (empty string) when the column is null
154  ioPlace.setAdmin1Code (iPlaceValues.get<std::string> ("admin1_code", ""));
155  ioPlace.setAdmin1UtfName (iPlaceValues.get<std::string> ("admin1_UTF8_name", ""));
156  ioPlace.setAdmin1AsciiName (iPlaceValues.get<std::string> ("admin1_ASCII_name", ""));
157 
158  // The administrative level 1 details will be set to the default value
159  // (empty string) when the column is null
160  ioPlace.setAdmin2Code (iPlaceValues.get<std::string> ("admin2_code", ""));
161  ioPlace.setAdmin2UtfName (iPlaceValues.get<std::string> ("admin2_UTF8_name", ""));
162  ioPlace.setAdmin2AsciiName (iPlaceValues.get<std::string> ("admin2_ASCII_name", ""));
163 
164  // The administrative level 3 code will be set to the default value
165  // (empty string) when the column is null
166  ioPlace.setAdmin3Code (iPlaceValues.get<std::string> ("admin3_code", ""));
167 
168  // The administrative level 4 code will be set to the default value
169  // (empty string) when the column is null
170  ioPlace.setAdmin4Code (iPlaceValues.get<std::string> ("admin4_code", ""));
171 
172  // Population
173  const OPENTREP::Population_T lPopulation =
174  iPlaceValues.get<int> ("population");
175  ioPlace.setPopulation (lPopulation);
176 
177  // Elevation
178  const OPENTREP::Elevation_T lElevation =
179  iPlaceValues.get<int> ("elevation");
180  ioPlace.setElevation (lElevation);
181 
182  // GTopo30
183  const OPENTREP::GTopo30_T lGTopo30 = iPlaceValues.get<int> ("gtopo30");
184  ioPlace.setGTopo30 (lGTopo30);
185 
186  // The time-zone code will be set to the default value (empty string)
187  // when the column is null
188  ioPlace.setTimeZone (iPlaceValues.get<std::string> ("time_zone", ""));
189 
190  // The time off-sets will be set to the default value (empty string)
191  // when the column is null
192  const OPENTREP::GMTOffset_T lGMTOffset =
193  iPlaceValues.get<int> ("gmt_offset", 0.0);
194  ioPlace.setGMTOffset (lGMTOffset);
195  const OPENTREP::DSTOffset_T lDSTOffset =
196  iPlaceValues.get<int> ("dst_offset", 0.0);
197  ioPlace.setDSTOffset (lDSTOffset);
198  const OPENTREP::RawOffset_T lRawOffset =
199  iPlaceValues.get<int> ("raw_offset", 0.0);
200  ioPlace.setRawOffset (lRawOffset);
201 
202  // Modification date
203  const std::string lModDateStr =
204  iPlaceValues.get<std::string> ("moddate", OPENTREP::DEFAULT_DATE_STR);
205  const OPENTREP::Date_T lModDate (boost::gregorian::from_string (lModDateStr));
206  ioPlace.setModificationDate (lModDate);
207 
208  // The Wikipedia link will be set to the default value (empty string)
209  // when the column is null
210  ioPlace.setWikiLink (iPlaceValues.get<std::string> ("wiki_link", ""));
211  }
212 
213  // Alternate name
214  const std::string& lLanguageString =
215  iPlaceValues.get<std::string> ("alt_lang_code", "");
216  /*
217  if (lLanguageString.empty() == true) {
218  std::ostringstream errorStr;
219  errorStr << "The language field is empty for the place "
220  << ioPlace.getIataCode();
221  OPENTREP_LOG_ERROR (errorStr.str());
222  throw OPENTREP::LanguageCodeNotDefinedInNameTableException(errorStr.str());
223  }
224  */
225 
226  // TODO: check that that alternate name does not already exist
227  // Convert the language code string (e.g., "en") into
228  // the corresponding enumeration
229  //const OPENTREP::Language::EN_Language lLanguageCode =
230  // OPENTREP::Language::getCode (lLanguageString);
231  const OPENTREP::LanguageCode_T lLanguageStr (lLanguageString);
232 
233  // Add the (UTF8) name only when the language code is standard English
234  //if (lLanguageCode == OPENTREP::Language::en) {
235  // UTF-8 name (which may be null/empty)
236  const std::string& lUtfName =
237  iPlaceValues.get<std::string> ("alt_name", "");
238  ioPlace.addName (lLanguageStr, lUtfName);
239  //}
240 
241  // TODO: check that that city code/name does not already exist, and add it
242  // The city code will be set to the default value (empty string)
243  // when the column is null
244  ioPlace.setCityCode (iPlaceValues.get<std::string> ("city_iata_code", ""));
245  // The city UTF8 name will be set to the default value (empty string)
246  // when the column is null
247  ioPlace.setCityUtfName (iPlaceValues.get<std::string>("city_UTF8_name", ""));
248  // The city ASCII name will be set to the default value (empty string)
249  // when the column is null
250  ioPlace.setCityAsciiName (iPlaceValues.get<std::string> ("city_ASCII_name", ""));
251 
252 
253  // DEBUG
254  /*
255  OPENTREP_LOG_DEBUG ("[Old:" << lPlaceKey << "][New:" << lNewPlaceKey << "] Lang: "
256  << lLanguageString << " => "
257  << OPENTREP::Language::getLongLabel (lLanguageCode)
258  << " -- Details: " << ioPlace);
259  */
260  }
261 
262  // //////////////////////////////////////////////////////////////////////
264  to_base (const OPENTREP::Place& iPlace, values& ioPlaceValues,
265  indicator& ioIndicator) {
266  // IATA code
267  const OPENTREP::IATACode_T& lIataCode = iPlace.getIataCode();
268  const std::string lIataCodeStr (lIataCode);
269  const indicator lIataCodeIndicator = lIataCodeStr.empty() ? i_null : i_ok;
270 
271  // ICAO code
272  const OPENTREP::ICAOCode_T& lIcaoCode = iPlace.getIcaoCode();
273  const std::string lIcaoCodeStr (lIcaoCode);
274  const indicator lIcaoCodeIndicator = lIcaoCodeStr.empty() ? i_null : i_ok;
275 
276  // FAA code
277  const OPENTREP::FAACode_T& lFaaCode = iPlace.getFaaCode();
278  const std::string lFaaCodeStr (lFaaCode);
279  const indicator lFaaCodeIndicator = lFaaCodeStr.empty() ? i_null : i_ok;
280 
281  // City IATA code
282  const OPENTREP::IATACode_T& lCityCode = iPlace.getCityCode();
283  const std::string lCityCodeStr (lCityCode);
284  const indicator lCityCodeIndicator = lCityCodeStr.empty() ? i_null : i_ok;
285 
286  // State code
287  const OPENTREP::StateCode_T& lStateCode = iPlace.getStateCode();
288  const std::string lStateCodeStr (lStateCode);
289  const indicator lStateCodeIndicator = lStateCodeStr.empty() ? i_null : i_ok;
290 
291  // Country code
292  const OPENTREP::CountryCode_T& lCountryCode = iPlace.getCountryCode();
293  const std::string lCountryCodeStr (lCountryCode);
294 
295  // Country name
296  const OPENTREP::CountryName_T& lCountryName = iPlace.getCountryName();
297  const std::string lCountryNameStr (lCountryName);
298 
299  // Continent name
300  const OPENTREP::ContinentName_T& lContinentName = iPlace.getContinentName();
301  const std::string lContinentNameStr (lContinentName);
302 
303  // Time-zone
304  const OPENTREP::TimeZone_T& lTimeZone = iPlace.getTimeZone();
305  const std::string lTimeZoneStr (lTimeZone);
306 
307  // Wikipedia link
308  const OPENTREP::WikiLink_T& lWikiLink = iPlace.getWikiLink();
309  const std::string lWikiLinkStr (lWikiLink);
310 
311  //
312  ioPlaceValues.set ("iata_code", lIataCodeStr, lIataCodeIndicator);
313  ioPlaceValues.set ("icao_code", lIcaoCodeStr, lIcaoCodeIndicator);
314  ioPlaceValues.set ("geoname_id", iPlace.getGeonamesID());
315  ioPlaceValues.set ("faa_code", lFaaCodeStr, lFaaCodeIndicator);
316  ioPlaceValues.set ("city_code", lCityCodeStr, lCityCodeIndicator);
317  ioPlaceValues.set ("state_code", lStateCodeStr, lStateCodeIndicator);
318  ioPlaceValues.set ("country_code", lCountryCodeStr);
319  ioPlaceValues.set ("country_name", lCountryNameStr);
320  ioPlaceValues.set ("continent_name", lContinentNameStr);
321  ioPlaceValues.set ("time_zone", lTimeZoneStr);
322  ioPlaceValues.set ("latitude", iPlace.getLatitude());
323  ioPlaceValues.set ("longitude", iPlace.getLongitude());
324  ioPlaceValues.set ("page_rank", iPlace.getPageRank());
325  ioPlaceValues.set ("wiki_link", lWikiLinkStr);
326  ioIndicator = i_ok;
327  }
328 
329 }
330 
331 namespace OPENTREP {
332 
333 }
const PageRank_T & getPageRank() const
Definition: Place.hpp:360
const Percentage_T K_DEFAULT_PAGE_RANK
Class modelling the primary key of a location/POR (point of reference).
Definition: LocationKey.hpp:21
void setAsciiName(const std::string &iName)
Definition: Place.hpp:561
void setCityUtfName(const std::string &iCityUtfName)
Definition: Place.hpp:610
void setModificationDate(const Date_T &iModDate)
Definition: Place.hpp:813
void setFeatureClass(const std::string &iFeatClass)
Definition: Place.hpp:715
const TimeZone_T & getTimeZone() const
Definition: Place.hpp:227
const GeonamesID_T & getGeonamesID() const
Definition: Place.hpp:79
const CityCode_T & getCityCode() const
Definition: Place.hpp:157
void addName(const LanguageCode_T &iLanguageCode, const std::string &iName)
Definition: Place.hpp:896
void setGTopo30(const GTopo30_T &iGTopo30)
Definition: Place.hpp:799
const Latitude_T & getLatitude() const
Definition: Place.hpp:255
void setDateEnd(const Date_T &iDate)
Definition: Place.hpp:589
void setPageRank(const PageRank_T &iPageRank)
Definition: Place.hpp:806
void setRawOffset(const RawOffset_T &iOffset)
Definition: Place.hpp:694
void setCityAsciiName(const std::string &iCityAsciiName)
Definition: Place.hpp:617
void setIataType(const std::string &iIATAType)
Definition: Place.hpp:525
void setContinentName(const std::string &iContinentName)
Definition: Place.hpp:666
void setCommonName(const std::string &iName)
Definition: Place.hpp:554
const FAACode_T & getFaaCode() const
Definition: Place.hpp:100
void setAdmin1UtfName(const std::string &iAdminName)
Definition: Place.hpp:736
void setWikiLink(const std::string &iWikiLink)
Definition: Place.hpp:820
boost::gregorian::date Date_T
void setCountryName(const std::string &iCountryName)
Definition: Place.hpp:652
void setAdmin2AsciiName(const std::string &iAdminName)
Definition: Place.hpp:764
void resetMatrix()
Definition: Place.hpp:904
void setPopulation(const Population_T &iPopulation)
Definition: Place.hpp:785
float GMTOffset_T
const ICAOCode_T & getIcaoCode() const
Definition: Place.hpp:93
const WikiLink_T & getWikiLink() const
Definition: Place.hpp:374
unsigned int Population_T
void setAdmin1AsciiName(const std::string &iAdminName)
Definition: Place.hpp:743
void setComment(const std::string &iComment)
Definition: Place.hpp:596
void setDSTOffset(const DSTOffset_T &iOffset)
Definition: Place.hpp:687
float RawOffset_T
void setLongitude(const Longitude_T &iLongitude)
Definition: Place.hpp:708
static void to_base(const OPENTREP::Place &, values &ioPlaceValues, indicator &)
Definition: DbaPlace.cpp:264
void setCityCode(const std::string &iCityCode)
Definition: Place.hpp:603
const CountryName_T & getCountryName() const
Definition: Place.hpp:206
void setDateFrom(const Date_T &iDate)
Definition: Place.hpp:582
float DSTOffset_T
const std::string DEFAULT_DATE_STR
Class modelling a place/POR (point of reference).
Definition: Place.hpp:28
const StateCode_T & getStateCode() const
Definition: Place.hpp:185
static void from_base(values const &iPlaceValues, indicator, OPENTREP::Place &)
Definition: DbaPlace.cpp:19
void setAdmin3Code(const std::string &iAdminCode)
Definition: Place.hpp:771
void setFeatureCode(const std::string &iFeatCode)
Definition: Place.hpp:722
const Longitude_T & getLongitude() const
Definition: Place.hpp:262
const ContinentName_T & getContinentName() const
Definition: Place.hpp:220
void setAltCountryCode(const std::string &iCountryCode)
Definition: Place.hpp:645
void setTimeZone(const std::string &iTimeZone)
Definition: Place.hpp:673
void setElevation(const Elevation_T &iElevation)
Definition: Place.hpp:792
void setFaaCode(const std::string &iFaaCode)
Definition: Place.hpp:546
const CountryCode_T & getCountryCode() const
Definition: Place.hpp:192
void setEnvelopeID(const EnvelopeID_T &iEnvelopeID)
Definition: Place.hpp:575
void setIataCode(const std::string &iIataCode)
Definition: Place.hpp:518
void resetIndexSets()
Definition: Place.cpp:218
Enumeration of output types.
Definition: IATAType.hpp:22
void setLatitude(const Latitude_T &iLatitude)
Definition: Place.hpp:701
void setGMTOffset(const GMTOffset_T &iOffset)
Definition: Place.hpp:680
const LocationKey & getKey() const
Definition: Place.hpp:58
void setAdmin1Code(const std::string &iAdminCode)
Definition: Place.hpp:729
void setGeonamesID(const GeonamesID_T &iGeonamesID)
Definition: Place.hpp:532
const IATACode_T & getIataCode() const
Definition: Place.hpp:65
void setCountryCode(const std::string &iCountryCode)
Definition: Place.hpp:638
void setStateCode(const std::string &iStateCode)
Definition: Place.hpp:631
void setAdmin2UtfName(const std::string &iAdminName)
Definition: Place.hpp:757
void setAdmin4Code(const std::string &iAdminCode)
Definition: Place.hpp:778
void setAdmin2Code(const std::string &iAdminCode)
Definition: Place.hpp:750