Wtx ~ Wt Extension Library
WtxLib
Company.cpp
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 #include <Wtx/Util/Util.h>
46 #include <Wtx/Dbo/TableView.h>
47 
48 #include "Item.h"
49 #include "Company.h"
50 
51 void Wtx::Crm::Company::mapClasses( Wtx::Dbo::Session & session )
52 {
53  Wtx::Crm::Company::Item::MapClass( session );
54 }
55 
56 void Wtx::Crm::Company::postCreateTables( Wtx::Dbo::Session & session )
57 {
59 
60 }
61 
62 std::string Wtx::Crm::Company::key( const std::string & lastName, const std::string & firstName )
63 {
64  return
65  Wt::WString("{1}, {2}")
66  .arg( lastName )
67  .arg( firstName )
68  .toUTF8()
69  ;
70 }
71 
72 
73 Wt::Dbo::ptr< Wtx::Crm::Company::Item > Wtx::Crm::Company::xfind( const std::string & xid, Wtx::Dbo::Session & session )
74 {
75  Wt::Dbo::Transaction t( session );
76  return
77  session.find<Wtx::Crm::Company::Item>()
78  .where( "xid = ?")
79  .bind( xid )
80  .resultValue()
81  ;
82 }
83 
84 
85 Wt::Dbo::ptr< Wtx::Crm::Company::Item > Wtx::Crm::Company::add( const std::string & name, Wtx::Dbo::Session & session )
86 {
87  Wt::Dbo::Transaction t( session );
88  return
89  session.addNew<Wtx::Crm::Company::Item>( name );
90 }
91 
92 
93 #ifdef NEVER
94 std::shared_ptr<Wt::WAbstractItemModel> Wtx::Crm::Company::getCompanyModel( int sid, int tid, const std::string & filter, Wtx::Dbo::Session & session )
95 {
96  auto retVal =
97  std::make_shared< Wtx::Dbo::QueryModel< std::tuple<int,std::string> > >();
98 
99  Wt::Dbo::Transaction t(session);
100 
101  std::string w;
102 
103  if( tid != -1 )
104  w = Wt::WString("WHERE id={1}").arg(tid).toUTF8();
105 
106  else
107  {
108  w =
109  Wt::WString
110  (
111  "WHERE \"isActive\""
112  )
113  .toUTF8()
114  ;
115 
116  if( filter != "" )
117  {
118  w += Wt::WString
119  (
120  " AND (UPPER(\"keyField\") LIKE '%{1}%' OR "
121  " UPPER(\"cfyField\") LIKE '%{1}%')"
122  )
123  .arg( Wtx::ucase(filter) )
124  .toUTF8()
125  ;
126  }
127  }
128 
129  auto sql =
130  Wt::WString
131  (
132  "SELECT "
133  "id, "
134  "\"keyField\" || ' ~ ' || "
135  "\"cfyField\" "
136  "AS key "
137  "FROM \"crmPerson\" "
138  " {1}"
139  " ORDER BY key"
140  )
141  .arg( w )
142  .toUTF8()
143  ;
144 
145 #ifdef NEVER
146  std::cout << __FILE__ << ":" << __LINE__
147  << "\n sid:" << sid
148  << "\n tid:" << tid
149  << "\n flt:" << filter
150  << "\n sql:" << sql
151 // << "\n row:" << retVal-> rowCount()
152  << std::endl
153  ;
154 #endif
155 
156  auto query =
157  session.query< std::tuple<int,std::string> >( sql )
158  ;
159 
160  retVal-> setQuery( query );
161  retVal-> addColumn( "id" );
162  retVal-> addColumn( "key" );
163 
164  return retVal;
165 
166 } // endstd::shared_ptr<Wt::WAbstractItemModel> getCompanyModel( int sid, int tid, const std::string & filter, Wtx::Dbo::Session & session )
167 
168 std::unique_ptr<Wt::WTableView> Wtx::Crm::Company::getCompanyTableView( int sid, int tid, const std::string & filter, Wtx::Dbo::Session & session )
169 {
171  {
172  /* title */ "",
173  /* tip */ "",
174  /* headerCount */ 0,
175  /* selectionMode */ Wt::SelectionMode::Single,
176  /* columnResizeEnabled */ true,
177  /* alternatingRowColors */ true,
178  /* sql */ "",
179  /* filter */ "",
180  /* subFilter */ "",
181  /* idin */ {},
182  /* doubleClick */ 0,
183  /* showHeader */ true,
184  /* showFooter */ true,
185  /* showPrevNext */ false,
186  /* hideSearch */ true,
187  /* allowFilter */ false,
188  /* allowAdd */ true,
189  /* allowChange */ false,
190  /* allowDelete */ false,
191  /* allowExport */ true,
192  /* allowSearch */ true,
193  /* allowPrint */ false,
194  /* allowUnlock */ false,
195  /* additional */ "",
196  /* itemDelegate */ nullptr,
197 
198  /* sortFields */
199  {
200  "\"keyField\", id"
201  },
202 
203  /* search fields */
204  {
205  },
206 
207  /* column defs */
208  {
209  { "id", "", 50, Wt::AlignmentFlag::Left, "", nullptr },
210  { "keyField", "", 150, Wt::AlignmentFlag::Left, "", nullptr },
211  { "cfyField", "", 350, Wt::AlignmentFlag::Left, "", nullptr },
212 
213  },
214 
215  /* column names */
216  {
217  "id",
218  "keyField",
219  "cfyField",
220  }
221  };
222 
223  Wt::Dbo::Transaction t(session);
224 
225  auto retVal = std::make_unique< Wtx::Dbo::TableView< Wtx::Crm::Company::Item > >( viewDef, &session );
226  // retVal-> setModel( getProductModel( sid, tid, "", session ) );
227  retVal-> setMaximumSize( Wt::WLength::Auto, 300 );
228  retVal-> setColumnHidden( 0,true );
229  if( filter.length() > 2 )
230  {
231  auto f =
232  Wt::WString
233  (
234  "(UPPER(\"keyField\") LIKE UPPER('%{1}%')) OR "
235  "(UPPER(\"cfyField\") LIKE UPPER('%{1}%'))"
236  )
237  .arg(filter)
238  .toUTF8()
239  ;
240 
241  retVal-> setFilter( f );
242 
243  }
244 
245  return retVal;
246 
247 } // endstd::unique_ptr<Wt::WTableView> getCompanyTableView( int sid, int tid, const std::string & filter, Wtx::Dbo::Session & session )
248 
249 
250 int Wtx::Crm::Company::CompanyProvider::find_id( const std::string & value )
251 {
252  auto v = "%" + Wtx::ucase(value) + "%";
253 
254  Wt::Dbo::Transaction t( *session() );
255  auto items =
256  session()-> find< Wtx::Crm::Person::Item >()
257  .where
258  (
259  " (UPPER( \"keyField\" ) LIKE ?"
260  " OR UPPER( \"cfyField\" ) LIKE ?)"
261  )
262  .bind( v )
263  .bind( v )
264  .resultList()
265  ;
266 
267  std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "():"
268  << " value=" << value
269  << " size:" << items.size()
270  << std::endl
271  ;
272 
273  if( items.size() != 1 )
274  return -1;
275 
276  return items.begin()-> id();
277 
278 } // endint CompanyProvider::find_id( const std::string & value )
279 
280 std::string Wtx::Crm::Company::CompanyProvider::get_string( int id )
281 {
282  Wt::Dbo::Transaction t( *session() );
283 
284  auto companyItem = session()-> load< Wtx::Crm::Company::Item >( id );
285 
286  return
287  Wt::WString("{1} ~ {2}")
288  .arg( companyItem-> cfyField().value() )
289  .arg( companyItem-> keyField().value() )
290  .toUTF8()
291  ;
292 
293 } // endstd::string CompanyProvider::get_string( int id )
294 
295 std::string Wtx::Crm::Company::CompanyProvider::get_toolTip( int id )
296 {
297  Wt::Dbo::Transaction t( *session() );
298  auto companyItem = session()-> load< Wtx::Crm::Company::Item >( id );
299 
300  auto companyKey = companyItem-> keyField().value();
301  auto companyCfy = companyItem-> cfyField().value();
302  auto companyPhone = companyItem-> phone().value();
303  auto companyEmail = companyItem-> email().value();
304 
305  return "Wtx::Crm::Company::CompanyProvider::get_toolTip";
306 
307 #ifdef NEVER
308  std::string companyInfo;
309  if( auto companyItem = personItem-> contactForCompany().value() )
310  {
311  companyInfo =
312  Wt::WString
313  (
314  "\n~~~ COMPANY INFO:"
315  "\n{1} ~ {2}"
316  "\nphone:{3}"
317  "\n{4}"
318  "\n{5}"
319  "\n{6} {7}"
320  )
321  .arg( companyItem-> keyField ().value() )
322  .arg( companyItem-> cfyField ().value() )
323  .arg( companyItem-> mainPhone ().value() )
324  .arg( companyItem-> addr ().value() )
325  .arg( companyItem-> addr2 ().value() )
326  .arg( companyItem-> city ().value() )
327  .arg( companyItem-> zip ().value() )
328  .toUTF8()
329  ;
330 
331  }
332 
333  return
334  Wt::WString
335  (
336  "~~~ CONTACT INFO:"
337  "\n{1} ~ {2}"
338  "\nphone:{3}"
339  "\nemail:{4}"
340  "\n{5}"
341  )
342  .arg( personCfy )
343  .arg( personKey )
344  .arg( personPhone )
345  .arg( personEmail )
346  .arg( companyInfo )
347  .toUTF8()
348  ;
349 #endif
350 
351 } // endstd::string ProductProvider::get_string( int id )
352 
353 std::shared_ptr< Wt::WAbstractItemModel > Wtx::Crm::Company::CompanyProvider::model( int sid, int tid, const std::string & filter )
354 {
355 #ifdef NEVER
356  std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "():"
357  << " sid:" << sid << "(" << m_sid << ")"
358  << " tid:" << tid << "(" << m_tid << ")"
359  << " filter:'" << filter << "' (" << filter << ")"
360  << std::endl
361  ;
362  #endif
363 
364  /*
365  ** If nothing has changed, then just return the model.
366  **
367  */
368  if( m_sid == sid
369  && m_tid == tid
370  && m_filter == filter )
371  {
372  return m_model;
373  }
374 
375  /*
376  ** Remember things.
377  **
378  */
379  m_sid = sid;
380  m_tid = tid;
381  m_filter = filter;
382 
383  /*
384  ** build the query as the return value.
385  **
386  */
387  m_model = std::make_shared< Model >();
388 
389  /*
390  ** Grab things from the system
391  **
392  */
393  Wt::Dbo::Transaction t( *session() );
394 
395  std::string w;
396 
397  if( tid != -1 )
398  w = Wt::WString("WHERE id={1}").arg(tid).toUTF8();
399 
400  else
401  {
402  if( filter != "" )
403  {
404  w += Wt::WString
405  (
406  " AND"
407  " (UPPER(\"keyField\") LIKE '%{1}%' OR "
408  " UPPER(\"cfyField\") LIKE '%{1}%')"
409  )
410  .arg( Wtx::ucase(filter) )
411  .toUTF8()
412  ;
413  }
414  }
415 
416  auto sql =
417  Wt::WString
418  (
419  " {1}"
420  " {2}"
421  " ORDER BY \"keyField\""
422  )
423  .arg( "SELECT id,\"keyField\" as key FROM \"crmPerson\"" )
424  .arg( w )
425  .toUTF8()
426  ;
427 
428  auto query =
429  session()-> query< std::tuple<int,std::string> >( sql )
430  ;
431 
432  m_model-> setQuery( query );
433  m_model-> addColumn( "id" );
434  m_model-> addColumn( "key" );
435 
436 #ifdef NEVER
437  std::cout << __FILE__ << ":" << __LINE__
438  << "\n sid:" << sid
439  << "\n tid:" << tid
440  << "\n flt:" << filter
441  << "\n row:" << m_model-> rowCount()
442  << "\n sql:" << sql
443  << std::endl
444  ;
445 #endif
446 
447  return m_model;
448 
449 } // endstd::shared_ptr< Wt::WAbstractItemModel > CompanyProvider::model( int sid, int tid, const std::string & filter )
450 
451 std::unique_ptr<Wt::WTableView> Wtx::Crm::Company::CompanyProvider::getTableView( int sid, int tid, const std::string & filter )
452 {
453  return std::make_unique< Wt::WTableView >();
454 }
455 
456 std::unique_ptr< Wtx::Crm::Company::CompanyProvider > Rtm::Dbo::Person::makeCompanyProvider()
457 {
458  return std::make_unique< Wtx::Crm::Company::CompanyProvider >();
459 }
460 
461 #endif
std::string ucase(const std::string &value)
Upper Case a string.
Definition: Util.cpp:441
static void PostCreateTable(Wt::Dbo::Session &session)
Post Table Create.
Definition: BaseItemTpl.h:141
Table View Definition.
Definition: TableViewDef.h:171