Wtx ~ Wt Extension Library
WtxLib
BaseItemTpl.h
1 /**************************************************************************
2 ###########################################################################
3 ##
4 ## $SHOWOFFDB_BEGIN_LICENSE$
5 ## Copyright (C) 2011 Lorimark Solutions, LLC and/or its subsidiary(-ies).
6 ## All rights reserved.
7 ## Contact: Lorimark Solutions, LLC (info@showoff-db.org)
8 ##
9 ## This file is part of the Showoff Database Application Framework.
10 ##
11 ## Commercial Usage
12 ## Licensees holding valid ShowoffDB Commercial licenses may use this file in
13 ## accordance with the ShowoffDB Commercial License Agreement provided with the
14 ## Software or, alternatively, in accordance with the terms contained in
15 ## a written agreement between you and Lorimark Solutions, LLC.
16 ##
17 ## GNU Lesser General Public License Usage
18 ## Alternatively, this file may be used under the terms of the GNU Lesser
19 ## General Public License version 2.1 as published by the Free Software
20 ## Foundation and appearing in the file LICENSE.LGPL included in the
21 ## packaging of this file. Please review the following information to
22 ## ensure the GNU Lesser General Public License version 2.1 requirements
23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ##
25 ## In addition, as a special exception, Lorimark Solutions, LLC gives
26 ## you certain additional rights. These rights are described in the
27 ## Lorimark Solutions, LLC ShowoffDB LGPL Exception version 1.0, included in
28 ## the file LGPL_EXCEPTION.txt in this package.
29 ##
30 ## GNU General Public License Usage
31 ## Alternatively, this file may be used under the terms of the GNU
32 ## General Public License version 3.0 as published by the Free Software
33 ## Foundation and appearing in the file LICENSE.GPL included in the
34 ## packaging of this file. Please review the following information to
35 ## ensure the GNU General Public License version 3.0 requirements will be
36 ## met: http://www.gnu.org/copyleft/gpl.html.
37 ##
38 ## If you have questions regarding the use of this file, please contact
39 ## Lorimark Solutions, LLC at info@showoff-db.org.
40 ## $SHOWOFFDB_END_LICENSE$
41 ##
42 #############################################################################
43 ****************************************************************************/
44 
45 
46 #ifndef __WTX_DBO_BASEITEMTPL_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__
47 #define __WTX_DBO_BASEITEMTPL_H_234e0174_6b7d_11e8_b503_fb1ef3651ec0__
48 
49 #include <Wt/Dbo/Dbo.h>
50 #include <Wt/WFormModel.h>
51 #include <Wt/Dbo/WtSqlTraits.h>
52 
53 #include "BaseTableDef.h"
54 #include "FieldNumber.h"
55 #include "FieldTextLine.h"
56 #include "FieldInt.h"
57 #include "FieldVar.h"
58 #include "FieldDate.h"
59 
60 #include <Wt/Dbo/ptr.h>
61 
62 namespace Wtx {
63  namespace Dbo {
64 
65 template <class C> class BasePtr
66 : public Wt::Dbo::ptr<C>
67 {
68  public:
69 
70  int lastVersion() const
71  {
72  return m_lastVersion;
73  }
74 
75  void setLastVersion( int value )
76  {
77  m_lastVersion = value;
78  }
79 
80  int m_lastVersion = -1;
81 
82 };
83 
84 template <class C> class BaseItemTpl
85 : public Wt::Dbo::Dbo<C>
86 {
87  public:
88 
89  using Ptr = Wt::Dbo::ptr< C >;
90  using Collection = Wt::Dbo::collection< Ptr >;
91  using Vector = std::vector< Ptr >;
92 
93  BaseItemTpl()
94  : Wt::Dbo::Dbo<C>()
95  {
96  }
97 
98  BaseItemTpl( const std::string & key, const std::string & cfy = "" )
99  : BaseItemTpl<C>()
100  {
101  init();
102  keyField().setValue( key );
103  cfyField().setValue( cfy );
104  }
105 
106 #ifdef NEVER_BUGBUG_NEEDS_COPY_OPERATOR
107  BaseItemTpl( const C & other )
108  : Wt::Dbo::Dbo<C>()
109  {
110  init();
111  keyField.setValue( other.keyField.value() );
112  cfyField.setValue( other.cfyField.value() );
113  }
114 #endif
115 
116  virtual ~BaseItemTpl()
117  {
118  }
119 
120  static void MapClass( Wt::Dbo::Session & session )
121  {
122  Wt::Dbo::Transaction t(session);
123  session.mapClass<C>( C::TableDef().tableName() );
124  Wt::registerType<C>();
125  Wt::registerType< Wt::Dbo::ptr<C> >();
126  Wt::registerType< Wt::Dbo::ptr<C> >();
127 
128  }
129 
141  static void PostCreateTable( Wt::Dbo::Session & session )
142  {
143 
144  Wt::Dbo::Transaction t(session);
145  for( auto fieldDef : TableDef().fieldDefs() )
146  {
147  if( fieldDef-> isUnique() )
148  {
149 
150 #ifndef WTX_USING_POSTGRES
151 #ifndef WTX_USING_SQLITE
152 #ifndef WTX_USING_MSSQL
153 #error you must define either WTX_USING_POSTGRES or WTX_USING_SQLITE or WTX_USING_MSSQL
154 #endif
155 #endif
156 #endif
157 
158 #ifdef WTX_USING_POSTGRES
159  auto sql =
160  Wt::WString("ALTER TABLE \"{1}\" ADD UNIQUE (\"{2}\")")
161  .arg( TableDef().tableName() )
162  .arg( fieldDef-> fieldName() )
163  .toUTF8()
164  ;
165 #endif
166 
167 #ifdef WTX_USING_MSSQL
168  auto sql =
169  Wt::WString("ALTER TABLE \"{1}\" ADD UNIQUE (\"{2}\")")
170  .arg( TableDef().tableName() )
171  .arg( fieldDef-> fieldName() )
172  .toUTF8()
173  ;
174 #endif
175 
176 #ifdef WTX_USING_SQLITE
177  //
178  // this works for sqlite
179  //
180  auto sql =
181  Wt::WString
182  (
183  "CREATE UNIQUE INDEX \"{1}_{2}\" ON \"{1}\" (\"{2}\")"
184  )
185  .arg( TableDef().tableName() )
186  .arg( fieldDef-> fieldName() )
187  .toUTF8()
188  ;
189 #endif
190  session.execute( sql );
191  }
192 
193  }
194 
195  } // endstatic void PostCreateTable( Wt::Dbo::Session & session )
196 
197  static BaseTableDef & TableDef()
198  {
199  return C::TableDef();
200  }
201 
202  bool operator== ( const BaseItemTpl<C> & other ) const
203  {
204  return other.keyField().value() == keyField().value();
205  }
206 
207  bool operator< ( const BaseItemTpl<C> & other ) const
208  {
209  return other.keyField().value() < keyField().value();
210  }
211 
212  bool isInitialized() const
213  {
214  return m_initialized;
215  }
216 
217  int lastVersion() const
218  {
219  return m_lastVersion;
220  }
221 
222  void setLastVersion( int value ) const
223  {
224  m_lastVersion = value;
225  }
226 
234  virtual void preWrite()
235  {
236  }
237 
245  virtual void postRead()
246  {
247  }
248 
249  const Wtx::Dbo::Field<int> & versionSync() const
250  {
251  return m_versionSync;
252  }
253 
254  const Wtx::Dbo::Field<bool> & isActive() const
255  {
256  return m_isActive;
257  }
258 
259  const Wtx::Dbo::Field<bool> & isVisible() const
260  {
261  return m_isVisible;
262  }
263 
264  const Wtx::Dbo::Field<bool> & isDeleted() const
265  {
266  return m_isDeleted;
267  }
268 
269  const Wtx::Dbo::Field<std::string> & xid() const
270  {
271  return m_xid;
272  }
273 
274  const Wtx::Dbo::Field<std::string> & keyField() const
275  {
276  return m_keyField;
277  }
278 
279  const Wtx::Dbo::Field<std::string> & cfyField() const
280  {
281  return m_cfyField;
282  }
283 
284  const Wtx::Dbo::FieldVar & varField() const
285  {
286  return m_varField;
287  }
288 
289  std::string varField( const std::string & fieldName ) const
290  {
291  return varField().value( fieldName );
292  }
293 
294  Wtx::Dbo::Field<int> & versionSync()
295  {
296  return m_versionSync;
297  }
298 
299  Wtx::Dbo::Field<bool> & isActive()
300  {
301  return m_isActive;
302  }
303 
304  Wtx::Dbo::Field<bool> & isVisible()
305  {
306  return m_isVisible;
307  }
308 
309  Wtx::Dbo::Field<bool> & isDeleted()
310  {
311  return m_isDeleted;
312  }
313 
315  {
316  return m_xid;
317  }
318 
319  Wtx::Dbo::Field<std::string> & keyField()
320  {
321  return m_keyField;
322  }
323 
324  Wtx::Dbo::Field<std::string> & cfyField()
325  {
326  return m_cfyField;
327  }
328 
329  Wtx::Dbo::FieldVar & varField()
330  {
331  return m_varField;
332  }
333 
335  {
336  return m_tag;
337  }
338 
339  const Wtx::Dbo::Field<std::string> & tag() const
340  {
341  return m_tag;
342  }
343 
344 #ifdef THIS_DOESNT_WORK_PROPERLY_USE_THE_STATIC_VERSIONS_OF_THIS_PROGRAM
345  bool setId( int newId )
346  {
347  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
348 
349  if( !(this-> session()) )
350  return false;
351 
352  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
353 
354  int thisId = this-> self().id();
355  auto s = this-> session();
356  auto tn = this-> session()-> template tableName<C>();
357 
358  Wt::Dbo::Transaction t( *s );
359 
360  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
361 
362  this-> session()-> flush();
363  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
364 
365  this-> self().purge();
366 
367  std::cout << __FILE__ << ":" << __LINE__ << " " << thisId << " " << newId << std::endl;
368 
369  if( thisId != newId )
370  {
371  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
372 
373  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
374 
375  auto sql =
376  Wt::WString("UPDATE \"{1}\" SET id = ? WHERE id = ?")
377  .arg( tn )
378  .toUTF8()
379  ;
380 
381  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
382 
383  s->
384  execute( sql )
385  .bind( newId )
386  .bind( thisId )
387  ;
388 
389  }
390 
391  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
392 
393  return true;
394  }
395 #endif
396 
404  virtual std::unique_ptr<Wt::WWidget> createEditWidget( Wt::WFormModel::Field f ) const
405  {
406  auto s = dynamic_cast<Wtx::Dbo::Session*>( BaseItemTpl<C>::session() );
407 
408 // std::cout << __FILE__ << ":" << __LINE__ << " createEditWidget: '" << f << "'" << " sid:" << BaseItemTpl<C>::id() << std::endl;
409 
410  for( auto fieldDef : C::TableDef().fieldDefs() )
411  {
412  if( f == fieldDef-> fieldName() )
413  {
414  return fieldDef-> createEditWidget( BaseItemTpl<C>::id(), *s );
415  }
416  }
417 
418  std::cout << __FILE__ << ":" << __LINE__ << " unable to create widget: '" << f << "'" << std::endl;
419 
420  return std::make_unique<Wt::WLineEdit>();
421  }
422 
423  template<class Action> void persist( Action & a )
424  {
425  m_xid .persist ( a, &TableDef() .XidFieldDef );
426  m_versionSync .persist ( a, &TableDef() .VersionSyncFieldDef );
427  m_isActive .persist ( a, &TableDef() .IsActiveFieldDef );
428  m_isVisible .persist ( a, &TableDef() .IsVisibleFieldDef );
429  m_isDeleted .persist ( a, &TableDef() .IsDeletedFieldDef );
430  m_keyField .persist ( a, &TableDef() .KeyFieldDef );
431  m_cfyField .persist ( a, &TableDef() .CfyFieldDef );
432  m_varField .persist ( a, &TableDef() .VarFieldDef );
433  m_tag .persist ( a, &TableDef() .TagFieldDef );
434  }
435 
436  protected:
437 
438  void init()
439  {
440  versionSync() .setValue( -1 );
441  isActive() .setValue( true );
442  isVisible() .setValue( true );
443  isDeleted() .setValue( false );
444  m_initialized = true;
445  }
446 
447 
448  private:
449 
450  bool m_initialized = false;
451 
453  Wtx::Dbo::FieldInt m_versionSync;
454  Wtx::Dbo::Field<bool> m_isActive;
455  Wtx::Dbo::Field<bool> m_isVisible;
456  Wtx::Dbo::Field<bool> m_isDeleted;
457  Wtx::Dbo::Field<std::string> m_keyField;
458  Wtx::Dbo::Field<std::string> m_cfyField;
459  Wtx::Dbo::FieldVar m_varField;
461 
462  mutable int m_lastVersion = -1;
463 
464 }; // endtemplate <class C> class BaseItemTpl
465 
466 template <class C>
467 std::ostream & operator<< ( std::ostream & s, const BaseItemTpl<C> & o )
468 {
469  return s << o.keyField().value();
470 }
471 
472  } // endnamespace Dbo
473 } // endnamespace Wtx
474 
475 #endif
476 
Table Definition.
Definition: TableDef.h:72
virtual void postRead()
Post-read.
Definition: BaseItemTpl.h:245
static void PostCreateTable(Wt::Dbo::Session &session)
Post Table Create.
Definition: BaseItemTpl.h:141
virtual std::unique_ptr< Wt::WWidget > createEditWidget(Wt::WFormModel::Field f) const
Create an Edit Widget.
Definition: BaseItemTpl.h:404
virtual void preWrite()
Pre-write.
Definition: BaseItemTpl.h:234
witty extension library
Definition: Activity.h:51