46 FormModel<C>::~FormModel()
51 Wt::Dbo::ptr<C> FormModel<C>::load(
int id, Wt::Dbo::Session * s )
54 return Wt::Dbo::ptr<C>();
56 Wt::Dbo::Transaction t(*s);
57 return s-> load<C>(id);
61 FormModel<C>::FormModel(
int id, Wt::Dbo::Session * s )
62 :
Wtx::FormModel( C::TableDef() ),
66 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
72 FormModel<C>::FormModel(
const Wt::Dbo::ptr<C> & itm )
73 :
Wtx::FormModel( C::TableDef() ),
77 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
83 std::string FormModel<C>::tableName()
const 86 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
88 return C::TableDef().tableName();
92 int FormModel<C>::itemId()
const 95 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
101 const Wt::Dbo::ptr<C> & FormModel<C>::item()
const 103 #ifdef FM_DEBUG_TRACE 104 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
110 Wt::Dbo::ptr<C> & FormModel<C>::item()
112 #ifdef FM_DEBUG_TRACE 113 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
119 Wt::Dbo::Session * FormModel<C>::session()
const 121 #ifdef FM_DEBUG_TRACE 122 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
124 return item().session();
128 bool FormModel<C>::remove()
130 #ifdef FM_DEBUG_TRACE 131 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
135 Wt::Dbo::Transaction t(*
session());
145 int FormModel<C>::read()
147 #ifdef FM_DEBUG_TRACE 148 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
152 Wt::Dbo::Transaction t(*
session());
158 void FormModel<C>::refresh()
160 #ifdef FM_DEBUG_TRACE 161 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
167 const_cast<C*
>(item().get())-> persist(*
this);
168 const_cast<C*
>(item().get())-> postRead();
172 std::unique_ptr<Wt::WWidget> FormModel<C>::createEditWidget( Wt::WFormModel::Field f )
const 174 #ifdef FM_DEBUG_TRACE 175 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << f << std::endl;
178 return item()-> createEditWidget( f );
180 return std::make_unique<Wt::WLineEdit>();
184 void FormModel<C>::staleRefresh()
186 #ifdef FM_DEBUG_TRACE 187 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
191 Wt::Dbo::Transaction t(*
session());
198 bool FormModel<C>::isStale()
const 200 #ifdef FM_DEBUG_TRACE 201 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
206 return diskVersion() != itemVersion();
210 int FormModel<C>::diskVersion()
const 212 #ifdef FM_DEBUG_TRACE 213 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
218 Wt::Dbo::Transaction t(*
session());
220 template query<int>(
"SELECT version FROM \"" + tableName() +
"\"")
221 .where(
"id=?").bind( item().
id() )
228 int FormModel<C>::itemVersion()
const 230 #ifdef FM_DEBUG_TRACE 231 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
233 return m_item.version();
237 const Wt::WFormModel::Field FormModel<C>::field(
const std::string & fieldName )
const 239 #ifdef FM_DEBUG_TRACE 240 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
243 std::string c =
"if:";
244 std::string f = fieldName;
245 if( f.substr( 0, c.size() ) == c )
247 f = f.substr(c.size());
250 for(
unsigned int i=0; i< fields().size(); i++ )
251 if( fields()[i] == f )
255 auto fieldDef = item()-> TableDef().fieldDef(fieldName);
258 return fieldDef-> fieldName();
270 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 271 <<
"WARNING (possibly fatal): cannot find: " << tableName() <<
"::" << fieldName <<
" in fields array" 280 const Wt::cpp17::any & FormModel<C>::value(
const std::string & fieldName )
const 282 #ifdef FM_DEBUG_TRACE 283 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
285 return Wtx::FormModel::value( field(fieldName) );
291 #ifdef FM_DEBUG_TRACE 292 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
294 return Wtx::FormModel::value( fieldDef.fieldName() );
298 int FormModel<C>::accept()
300 #ifdef FM_DEBUG_TRACE 301 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
312 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 314 <<
"\"" << tableName() <<
"\" " 315 <<
"id=" << item().id() <<
" " 316 <<
"itemVersion=" << itemVersion() <<
" " 317 <<
"diskVersion=" << diskVersion() <<
" " 325 m_isReadMode =
false;
326 Wt::Dbo::Transaction t(*
session());
331 item().modify()-> persist(*
this);
332 item().modify()-> preWrite();
341 void FormModel<C>::reset ()
343 #ifdef FM_DEBUG_TRACE 344 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
346 Wtx::FormModel::reset();
350 bool FormModel<C>::validate ()
352 #ifdef FM_DEBUG_TRACE 353 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
355 return Wtx::FormModel::validate();
359 bool FormModel<C>::isVisible( Wt::WFormModel::Field field )
const 361 #ifdef FM_DEBUG_TRACE 362 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
364 return Wtx::FormModel::isVisible( field );
368 bool FormModel<C>::isReadOnly( Wt::WFormModel::Field field )
const 370 #ifdef FM_DEBUG_TRACE 371 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
373 return Wtx::FormModel::isReadOnly( field );
379 #ifdef FM_DEBUG_TRACE 380 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
386 void FormModel<C>::setValue( Wt::WFormModel::Field field,
const Wt::cpp17::any &value )
388 #ifdef FM_DEBUG_TRACE 389 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << field <<
" " << Wt::asString(value) << std::endl;
391 Wtx::FormModel::setValue( field, value );
395 const Wt::cpp17::any & FormModel<C>::value( Wt::WFormModel::Field field )
const 397 #ifdef FM_DEBUG_TRACE 398 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << field << std::endl;
400 return Wtx::FormModel::value( field );
404 Wt::WString FormModel<C>::valueText( Wt::WFormModel::Field field )
const 406 #ifdef FM_DEBUG_TRACE 407 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
409 return Wtx::FormModel::valueText( field );
413 void FormModel<C>::setValidator (Wt::WFormModel::Field field,
const std::shared_ptr< Wt::WValidator > &validator )
415 #ifdef FM_DEBUG_TRACE 416 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
418 Wtx::FormModel::setValidator( field, validator );
422 std::shared_ptr< Wt::WValidator > FormModel<C>::validator( Wt::WFormModel::Field field )
const 424 #ifdef FM_DEBUG_TRACE 425 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
427 return Wtx::FormModel::validator( field );
431 bool FormModel<C>::validateField( Wt::WFormModel::Field field )
433 #ifdef FM_DEBUG_TRACE 434 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
436 return Wtx::FormModel::validateField( field );
440 void FormModel<C>::setValidated (Wt::WFormModel::Field field,
bool validated)
442 #ifdef FM_DEBUG_TRACE 443 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
445 Wtx::FormModel::setValidated( field, validated );
449 bool FormModel<C>::isValidated (Wt::WFormModel::Field field)
const 451 #ifdef FM_DEBUG_TRACE 452 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
454 return Wtx::FormModel::isValidated( field );
458 void FormModel<C>::setValidation( Wt::WFormModel::Field field,
const Wt::WValidator::Result &result )
460 #ifdef FM_DEBUG_TRACE 461 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
463 Wtx::FormModel::setValidation( field, result );
467 template <
typename T>
void FormModel<C>::act( Wt::Dbo::FieldRef<T> ref )
469 #ifdef FM_DEBUG_TRACE 470 std::cout << __LINE__ <<
":" << __FILE__ <<
" " << std::endl;
472 #ifdef FM_DEBUG_ACTIVITY 473 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 474 <<
"read=" << (m_isReadMode? std::string(
"true"):std::string(
"false")) <<
" " 475 <<
"id=" << itemId() <<
" " 476 <<
"name=" << ref.name() <<
" " 477 <<
"value=" << item()-> keyField().value() <<
" " 478 << field(ref.name()) <<
" " 480 <<
"'" <<
Wt::asString( value( field( ref.name() ) ) ) <<
"' " 490 Wt::cpp17::any(ref.value())
528 Wt::cpp17::any_cast<T>
561 #ifdef FM_DEBUG_ACTIVITY 562 if( std::string(ref.name()) ==
"dateStart" || std::string(ref.name()) ==
"keyField" || std::string(ref.name()) ==
"cfyField" )
563 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 564 << std::string( isReadMode?
"read":
"write" ) <<
" " 566 <<
"'" << Wt::asString( value( field( ref.name() ) ) ) <<
"' " 567 <<
"'" << Wt::asString( ref.value() ) <<
"' " 571 if( std::string(ref.name()) ==
"dateStart" )
573 Wt::WString s = Wt::asString( value( field( ref.name() ) ) );
574 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 575 << Wt::WLocale::currentLocale().dateFormat().toUTF8() <<
" " 577 << Wt::WDate::fromString(s,Wt::WLocale::currentLocale().dateFormat()).toString()
592 template <
typename T>
void FormModel<C>::actPtr(
const Wt::Dbo::PtrRef<T> & ref )
601 #ifdef FM_DEBUG_ACTIVITY 602 std::cout << __FILE__ <<
" " << __LINE__ <<
" read: " 603 <<
"ref.type=" << ref.type()-> name() <<
" " 604 <<
"ref.name=" << ref.name() <<
" " 605 <<
"id=" << ref.id() <<
" " 606 <<
"value.type=" << value( field( ref.name() ) ).type().name() <<
" " 607 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 613 field(ref.name()), ref.value()
617 #ifdef FM_DEBUG_ACTIVITY 618 std::cout << __FILE__ <<
" " << __LINE__ <<
" read: " 619 <<
"ref.type=" << ref.type()-> name() <<
" " 620 <<
"ref.name=" << ref.name() <<
" " 621 <<
"id=" << ref.id() <<
" " 622 <<
"value.type=" << value( field( ref.name() ) ).type().name() <<
" " 623 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 642 Wt::Dbo::ptr<T> newPtr;
644 if( value(field(ref.name())).type() ==
typeid(Wt::Dbo::ptr<T>) )
646 newPtr = Wt::cpp17::any_cast< Wt::Dbo::ptr<T> >
658 if( value(field(ref.name())).type() ==
typeid(std::string) )
660 std::cout << __FILE__ <<
" " << __LINE__ <<
" converting from std::string " << std::endl;
674 if( value(field(ref.name())).type() ==
typeid(Wt::WString) )
676 auto idv = Wt::asString( value( field( ref.name() ) ) ).toUTF8();
678 if( idv !=
"[null]" )
680 auto v = Wtx::Core::split( idv,
':' );
681 int idVal = std::stoi(v.at(1));
684 newPtr =
session()->
template load<T>(idVal);
688 #ifdef FM_DEBUG_ACTIVITY 689 std::cout << __FILE__ <<
" " << __LINE__ <<
" write: " 690 <<
"ref.name=" << ref.name() <<
" " 691 <<
"value.typename=" << value( field( ref.name() ) ).type().name() <<
" " 692 <<
"value.type=" << value( field( ref.name() ) ).type().hash_code() <<
" " 693 <<
"typeid<T>=" <<
typeid(Wt::Dbo::ptr<T>).hash_code() <<
" " 694 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 695 <<
"newPtr=" << newPtr.id()
709 ref.value() = newPtr;
721 template <
typename T>
void FormModel<C>::actWeakPtr(
const Wt::Dbo::WeakPtrRef<T> & ref )
731 #ifdef FM_DEBUG_ACTIVITY 732 std::cout << __FILE__ <<
" " << __LINE__ <<
" read: " 733 <<
"ref.type=" << ref.type()-> name() <<
" " 734 <<
"ref.name=" << ref.name() <<
" " 735 <<
"id=" << ref.id() <<
" " 736 <<
"value.type=" << value( field( ref.name() ) ).type().name() <<
" " 737 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 743 field(ref.name()), ref.value()
747 #ifdef FM_DEBUG_ACTIVITY 748 std::cout << __FILE__ <<
" " << __LINE__ <<
" read: " 749 <<
"ref.type=" << ref.type()-> name() <<
" " 750 <<
"ref.name=" << ref.name() <<
" " 751 <<
"id=" << ref.id() <<
" " 752 <<
"value.type=" << value( field( ref.name() ) ).type().name() <<
" " 753 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 772 Wt::Dbo::ptr<T> newPtr;
774 if( value(field(ref.name())).type() ==
typeid(Wt::Dbo::ptr<T>) )
776 newPtr = Wt::cpp17::any_cast< Wt::Dbo::ptr<T> >
788 if( value(field(ref.name())).type() ==
typeid(std::string) )
790 std::cout << __FILE__ <<
" " << __LINE__ <<
" converting from std::string " << std::endl;
804 if( value(field(ref.name())).type() ==
typeid(Wt::WString) )
806 auto idv = Wt::asString( value( field( ref.name() ) ) ).toUTF8();
808 if( idv !=
"[null]" )
810 int idVal = std::stoi( idv );
813 newPtr =
session()->
template load<T>(idVal);
817 #ifdef FM_DEBUG_ACTIVITY 818 std::cout << __FILE__ <<
" " << __LINE__ <<
" write: " 819 <<
"ref.name=" << ref.name() <<
" " 820 <<
"value.typename=" << value( field( ref.name() ) ).type().name() <<
" " 821 <<
"value.type=" << value( field( ref.name() ) ).type().hash_code() <<
" " 822 <<
"typeid<T>=" <<
typeid(Wt::Dbo::ptr<T>).hash_code() <<
" " 823 <<
"value=" << Wt::asString( value( field( ref.name() ) ) ) <<
" " 824 <<
"newPtr=" << newPtr.id()
838 ref.value() = newPtr;
847 template <
typename T>
void FormModel<C>::actCollection(
const Wt::Dbo::CollectionRef<T> & ref )
850 #ifdef STILL_WORKING_ON_THIS 851 #ifndef DEBUG_ACTIVITY 852 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 853 << ref.value().size() <<
" " 854 << ref.joinName() <<
" " 855 << ref.joinId() <<
" " 856 << ref.literalJoinId() <<
" " 857 << (ref.type() == Wt::Dbo::RelationType::ManyToOne? std::string(
"mto"):std::string(
"mtm")) <<
" " 858 << ref.fkConstraints() <<
" " 862 for(
auto item : ref.value() )
863 std::cout << __FILE__ <<
" " << __LINE__ <<
" " 876 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << ref.joinName() << std::endl;
878 std::vector<int> ids;
879 for(
auto item : ref.value() )
880 ids.push_back( item.id() );
883 std::cout << __FILE__ <<
":" << __LINE__ <<
" " << m_collections[ref.joinName()].size() << std::endl;
885 setValue( field(ref.joinName()), boost::any(ids) );
Wt::Dbo::ptr< Wtx::Crm::Activity::Item > add(const std::string &key, const Wt::WDate &date, Wtx::Dbo::Session &session)
Add an item to the lookup table.
Wtx::Dbo::Session * session()
Current Session.