GnuCashew ~ GnuCash Enabled Web
GCW
Vars.cpp
Go to the documentation of this file.
1 #line 2 "src/Dbo/Vars.cpp"
2 
3 #include <Wt/Json/Parser.h>
4 #include <Wt/Json/Object.h>
5 #include <Wt/Json/Serializer.h>
6 
7 #include "../App.h"
8 #include "Vars.h"
9 
10 
11 const char * GCW::Dbo::Vars::s_tableName = "gcw_vars";
12 
13 
14 auto
16 get( const std::string & _keyValue, const std::string & _cfyValue, bool _add )-> GCW::Dbo::Vars::Item::Ptr
17 {
19 
20  /*
21  ** Build a 'where' that has both the key and cfy
22  ** fields represented.
23  **
24  */
25  auto where =
26  Wt::WString( "\"keyField\" = '{1}'" )
27  .arg( _keyValue )
28  .toUTF8()
29  ;
30 
31  if( _cfyValue != "*" )
32  where +=
33  Wt::WString( " AND \"cfyField\" = '{1}'" )
34  .arg( _cfyValue )
35  .toUTF8()
36  ;
37 
38  /*
39  ** find the item (or try to)
40  **
41  */
42  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
43  retVal =
44  GCW::app()-> gnucashew_session().find< GCW::Dbo::Vars::Item >()
45  .where( where )
46  .resultValue()
47  ;
48 
49  /*
50  ** if an item could not be found then create one, setting in
51  ** the key and cfy if available.
52  **
53  */
54  if( !retVal && _add )
55  {
56  retVal =
57  GCW::app()-> gnucashew_session().addNew< GCW::Dbo::Vars::Item >()
58  ;
59 
60  retVal.modify()-> setKeyField( _keyValue );
61 
62  if( _cfyValue != "*" )
63  retVal.modify()-> setCfyField( _cfyValue );
64 
65  retVal.flush();
66 
67  } // endif( !retVal )
68 
69  return retVal;
70 
71 } // endget( const std::string & _keyValue, const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Ptr
72 
73 auto
75 getByCfy( const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Vector
76 {
78 
79  Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
80  auto items =
81  GCW::app()-> gnucashew_session().find< GCW::Dbo::Vars::Item >()
82  .where( "\"cfyField\" = '" + _cfyValue + "'" )
83  .resultList()
84  ;
85 
86  for( auto item : items )
87  retVal.push_back( item );
88 
89  return retVal;
90 
91 } // endget( const std::string & _keyValue, const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Ptr
92 
93 auto
95 getVarString( const std::string & _field ) const-> std::string
96 {
97  std::string retVal;
98 
99  if( _field != "" )
100  {
101  Wt::Json::Object jobj;
102  try {
103  Wt::Json::parse( varField(), jobj );
104  }
105  catch( std::exception & e )
106  {
107  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
108  }
109 
110  retVal = jobj.get( _field ).orIfNull("");
111 
112  }
113 
114  return retVal;
115 
116 } // endgetVarString( const std::string & _field ) const-> std::string
117 
118 auto
120 getVarInt( const std::string & _field ) const-> int
121 {
122  int retVal = 0;
123 
124  if( _field != "" )
125  {
126  Wt::Json::Object jobj;
127  try {
128  Wt::Json::parse( varField(), jobj );
129  }
130  catch( std::exception & e )
131  {
132  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
133  }
134 
135  retVal = jobj.get( _field ).orIfNull( 0 );
136 
137  }
138 
139  return retVal;
140 
141 } // endgetVarInt( const std::string & _field ) const-> int
142 
143 auto
145 setVar( const std::string & _field, const char * _value )-> void
146 {
147  setVar( _field, std::string( _value ) );
148 
149 } // endsetVar( const std::string & _field, const char * _value )-> void
150 
151 auto
153 setVar( const std::string & _field, const Wt::WString & _value )-> void
154 {
155  setVar( _field, _value.toUTF8() );
156 
157 } // endsetVar( const std::string & _field, const Wt::WString & _value )-> void
158 
159 auto
161 setVar( const std::string & _field, const std::string & _value )-> void
162 {
163  /*
164  ** If there is no field specified, then there's nothing
165  ** to do.
166  **
167  */
168  if( _field == "" )
169  return;
170 
171  Wt::Json::Object jobj;
172  try {
173  Wt::Json::parse( varField(), jobj );
174 
175  }
176  catch( std::exception & e )
177  {
178  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
179  }
180 
181  jobj[_field] = Wt::WString( _value );
182 
183  setVarField( Wt::Json::serialize( jobj ) );
184 
185 } // endsetVar( const std::string & _field, const std::string & _value )-> void
186 
187 auto
189 setVar( const std::string & _field, int _value )-> void
190 {
191  /*
192  ** If there is no field specified, then there's nothing
193  ** to do.
194  **
195  */
196  if( _field == "" )
197  return;
198 
199  Wt::Json::Object jobj;
200  try {
201  Wt::Json::parse( varField(), jobj );
202  }
203  catch( std::exception & e )
204  {
205  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
206  }
207 
208  jobj[_field] = _value;
209 
210  setVarField( Wt::Json::serialize( jobj ) );
211 
212 } // endsetVar( const std::string & _field, int _value )-> void
213 
214 
215 
std::vector< Ptr > Vector
Definition: BaseItem.h:41
Wt::Dbo::ptr< Item > Ptr
Definition: BaseItem.h:39
Variables Item Class.
Definition: Vars.h:36
int getVarInt(const std::string &_field) const
Definition: Vars.cpp:120
void setVar(const std::string &_field, const char *_value)
Definition: Vars.cpp:145
std::string getVarString(const std::string &_field) const
Definition: Vars.cpp:95
const Wt::WFormModel::Field id
Definition: Definition.h:17
Wt::WFormModel::Field varField
Definition: Var.cpp:7
auto getByCfy(const std::string &_cfyValue) -> GCW::Dbo::Vars::Item::Vector
Definition: Vars.cpp:75
auto get(const std::string &_keyValue, const std::string &_cfyValue="*", bool _add=true) -> GCW::Dbo::Vars::Item::Ptr
Definition: Vars.cpp:16
const char * s_tableName
Definition: Vars.cpp:11
App * app()
Definition: App.cpp:67