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 getVarString( const std::string & _field ) const-> std::string
76 {
77  std::string retVal;
78 
79  if( _field != "" )
80  {
81  Wt::Json::Object jobj;
82  try {
83  Wt::Json::parse( varField(), jobj );
84  }
85  catch( std::exception & e )
86  {
87  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
88  }
89 
90  retVal = jobj.get( _field ).orIfNull("");
91 
92  }
93 
94  return retVal;
95 
96 } // endgetVarString( const std::string & _field ) const-> std::string
97 
98 auto
100 getVarInt( const std::string & _field ) const-> int
101 {
102  int retVal = 0;
103 
104  if( _field != "" )
105  {
106  Wt::Json::Object jobj;
107  try {
108  Wt::Json::parse( varField(), jobj );
109  }
110  catch( std::exception & e )
111  {
112  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
113  }
114 
115  retVal = jobj.get( _field ).orIfNull( 0 );
116 
117  }
118 
119  return retVal;
120 
121 } // endgetVarInt( const std::string & _field ) const-> int
122 
123 auto
125 setVar( const std::string & _field, const char * _value )-> void
126 {
127  setVar( _field, std::string( _value ) );
128 
129 } // endsetVar( const std::string & _field, const char * _value )-> void
130 
131 auto
133 setVar( const std::string & _field, const Wt::WString & _value )-> void
134 {
135  setVar( _field, _value.toUTF8() );
136 
137 } // endsetVar( const std::string & _field, const Wt::WString & _value )-> void
138 
139 auto
141 setVar( const std::string & _field, const std::string & _value )-> void
142 {
143  /*
144  ** If there is no field specified, then there's nothing
145  ** to do.
146  **
147  */
148  if( _field == "" )
149  return;
150 
151  Wt::Json::Object jobj;
152  try {
153  Wt::Json::parse( varField(), jobj );
154 
155  }
156  catch( std::exception & e )
157  {
158  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
159  }
160 
161  jobj[_field] = Wt::WString( _value );
162 
163  setVarField( Wt::Json::serialize( jobj ) );
164 
165 } // endsetVar( const std::string & _field, const std::string & _value )-> void
166 
167 auto
169 setVar( const std::string & _field, int _value )-> void
170 {
171  /*
172  ** If there is no field specified, then there's nothing
173  ** to do.
174  **
175  */
176  if( _field == "" )
177  return;
178 
179  Wt::Json::Object jobj;
180  try {
181  Wt::Json::parse( varField(), jobj );
182  }
183  catch( std::exception & e )
184  {
185  std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
186  }
187 
188  jobj[_field] = _value;
189 
190  setVarField( Wt::Json::serialize( jobj ) );
191 
192 } // endsetVar( const std::string & _field, int _value )-> void
193 
194 
195 
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:100
void setVar(const std::string &_field, const char *_value)
Definition: Vars.cpp:125
std::string getVarString(const std::string &_field) const
Definition: Vars.cpp:75
const Wt::WFormModel::Field id
Definition: Definition.h:17
Wt::WFormModel::Field varField
Definition: Var.cpp:7
GCW::Dbo::Vars::Item::Ptr get(const std::string &_keyValue, const std::string &_cfyValue="*", bool _add=true)
Definition: Vars.cpp:16
const char * s_tableName
Definition: Vars.cpp:11
App * app()
Definition: App.cpp:67