GnuCashew ~ GnuCash Enabled Web
GCW
Commodities.cpp
Go to the documentation of this file.
1 #line 2 "src/Dbo/Commodities/Commodities.cpp"
2 
3 #include <fstream>
4 
5 #include "../Glb/Core.h"
6 #include "../App.h"
7 #include "../../3rd/RapidXML/rapidxml.hpp"
8 #include "Commodities.h"
9 
10 namespace {
11 
12 /*
13 ** This is a simple heap dynamic buffer, that deletes when it goes out of scope.
14 */
15 class DynBuf
16 {
17  public:
18 
19  char * buffer;
20 
21  DynBuf( int size )
22  {
23  buffer = new char[size];
24  }
25 
26  ~DynBuf()
27  {
28  delete buffer;
29  }
30 };
31 
32 }
33 
34 const char * GCW::Dbo::Commodities::s_tableName = "commodities";
35 
36 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::guid = "guid" ; // text(32) PRIMARY KEY NOT NULL
37 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::nameSpace = "namespace" ; // text(2048) NOT NULL
38 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::mnemonic = "mnemonic" ; // text(2048) NOT NULL
39 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::fullname = "fullname" ; // text(2048)
40 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::cusip = "cusip" ; // text(2048)
41 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::fraction = "fraction" ; // integer NOT NULL
42 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::quote_flag = "quote_flag" ; // integer NOT NULL
43 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::quote_source = "quote_source" ; // text(2048)
44 const Wt::WFormModel::Field GCW::Dbo::Commodities::Field::quote_tz = "quote_tz" ; // text(2048)
45 
46 auto
48 load( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
49 {
51 
52  if( _guid != "" )
53  {
54 
55  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
56 
57  try
58  {
59  retVal =
60  GCW::app()-> gnucashew_session().load< GCW::Dbo::Commodities::Item >( _guid )
61  ;
62  }
63  catch( std::exception & e )
64  {
65  std::cout << __FILE__ << ":" << __LINE__ << " " << e.what() << std::endl;
66  }
67  }
68 
69  return retVal;
70 
71 } // endload( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
72 
73 auto
75 byGuid( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
76 {
77  return GCW::Dbo::Commodities::load( _guid );
78 
79 } // endbyGuid( const std::string & _guid )-> GCW::Dbo::Commodities::Item::Ptr
80 
81 
82 
83 auto
85 getIso4217Commodities()-> std::vector< Commodity_t >
86 {
87  std::vector< Commodity_t > retVal;
88 
89  /*ยท
90  ** Open the file and read it all in to memory
91  **
92  */
93  std::ifstream inFile;
94  inFile.open( wApp-> docRoot() + "/iso-4217-currencies.xml" );
95 
96  if( inFile.fail() )
97  return retVal;
98 
99  inFile.seekg( 0, std::ios::end );
100  std::streampos length = inFile.tellg();
101  inFile.seekg( 0, std::ios::beg );
102 
103  /*
104  ** make a little empty space in the buffer for adding
105  ** zero terminated strings, or whatever it is that the
106  ** rapid xml does to the content to manipulate it. Note
107  ** that it's possible to cause rapidxml to not modify
108  ** the buffer at all, but then every string that gets pulled
109  ** out of the buffer has to be tested for actual length
110  ** and any strings derived from that need to include that
111  ** length parameter.
112  **
113  */
114  DynBuf db(((int)length)+10000);
115  inFile.read( db.buffer, length );
116  inFile.close();
117 
118  db.buffer[length] = 0;
119 
120  /*
121  ** Parse-in the XML file
122  **
123  */
124  rapidxml::xml_document<> doc;
125  try
126  {
127  doc.parse<0>( db.buffer );
128  }
129  catch( std::exception & err )
130  {
131  std::cout << __FILE__ << ":" << __LINE__ << " " << err.what() << std::endl;
132  return retVal;
133  }
134 
135  auto _processCurrency = [&]( rapidxml::xml_node<> * topNode )
136  {
137  for( auto node = topNode-> first_node(); node; node = node-> next_sibling() )
138  {
139  Commodity_t commodity =
140  {
141  node-> first_attribute( "isocode" )-> value(),
142  node-> first_attribute( "fullname" )-> value(),
143  node-> first_attribute( "unitname" )-> value(),
144  node-> first_attribute( "partname" )-> value(),
145  node-> first_attribute( "namespace" )-> value(),
146  node-> first_attribute( "exchange-code" )-> value(),
147  node-> first_attribute( "parts-per-unit" )-> value(),
148  node-> first_attribute( "smallest-fraction" )-> value(),
149  node-> first_attribute( "local-symbol" )-> value()
150  };
151 
152  retVal.push_back( commodity );
153 
154  } // endfor( auto node = topNode-> first_node(); node; node = node-> next_sibling() )
155 
156  }; // endauto _processObservation = [=]( rapidxml::xml_node<> * topNode )
157 
158  for( auto node = doc.first_node(); node; node = node-> next_sibling() )
159  {
160  if( std::string(node-> name()) == "currencylist" )
161  _processCurrency( node );
162  }
163 
164  return retVal;
165 
166 } // endgetIso4217Commodities()-> std::vector< Commodity_t >
167 
168 
169 
170 
Wt::Dbo::ptr< Item > Ptr
Definition: BaseItem.h:39
Commodity Item Class.
Definition: Item.h:82
const Wt::WFormModel::Field name
Definition: Accounts.cpp:47
const Wt::WFormModel::Field cusip
Definition: Commodities.cpp:40
const Wt::WFormModel::Field fullname
Definition: Commodities.cpp:39
const Wt::WFormModel::Field nameSpace
Definition: Commodities.cpp:37
const Wt::WFormModel::Field guid
Definition: Commodities.cpp:36
const Wt::WFormModel::Field fraction
Definition: Commodities.cpp:41
const Wt::WFormModel::Field mnemonic
Definition: Commodities.cpp:38
const Wt::WFormModel::Field quote_source
Definition: Commodities.cpp:43
const Wt::WFormModel::Field quote_tz
Definition: Commodities.cpp:44
const Wt::WFormModel::Field quote_flag
Definition: Commodities.cpp:42
const char * s_tableName
Definition: Commodities.cpp:34
auto byGuid(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
Definition: Commodities.cpp:75
auto getIso4217Commodities() -> std::vector< Commodity_t >
Get Commodities.
Definition: Commodities.cpp:85
auto load(const std::string &_guid) -> Item::Ptr
Load Account by GUID.
Definition: Commodities.cpp:48
App * app()
Definition: App.cpp:67