Wtx ~ Wt Extension Library
WtxLib
Person.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/Core/Core.h>
46 #include <Wtx/Util/Util.h>
47 #include <Wtx/Dbo/QueryModel.h>
48 
49 #include "Item.h"
50 #include "Person.h"
51 
52 void Wtx::Crm::Person::mapClasses( Wtx::Dbo::Session & session )
53 {
54  Wtx::Crm::Person::Item::MapClass( session );
55 }
56 
57 void Wtx::Crm::Person::postCreateTables( Wtx::Dbo::Session & session )
58 {
60 
61 }
62 
63 std::string Wtx::Crm::Person::key( const std::string & lastName, const std::string & firstName )
64 {
65  return
66  Wt::WString("{1}, {2}")
67  .arg( lastName )
68  .arg( firstName )
69  .toUTF8()
70  ;
71 }
72 
73 
74 Wtx::Crm::Person::Item::Ptr Wtx::Crm::Person::load( int id, Wtx::Dbo::Session & session )
75 {
76  Wt::Dbo::Transaction t( session );
77  return
78  session.load< Wtx::Crm::Person::Item >(id)
79  ;
80 }
81 
82 Wtx::Crm::Person::Item::Ptr Wtx::Crm::Person::load( const std::string & idstring, Wtx::Dbo::Session & session )
83 {
84  Wtx::Crm::Person::Item::Ptr retVal;
85 
86  if( idstring.length() < 0 )
87  return retVal;
88 
89  if( idstring.at(0) != '[' )
90  return retVal;
91 
92  auto ids = Wtx::Core::split( idstring, ':' );
93 
94  if( ids.size() != 2 )
95  return retVal;
96 
97  int id = -1;
98  try
99  {
100  id = Wtx::stoi( ids.at(1) );
101  }
102  catch(...)
103  {
104  std::cout << __FILE__ << ":" << __LINE__ << " " << std::endl;
105  }
106 
107  if( id > -1 )
108  return load( id, session );
109 
110  std::cout << __FILE__ << ":" << __LINE__ << " " << ids.size() << std::endl;
111  std::cout << __FILE__ << ":" << __LINE__ << " " << ids.at(0) << std::endl;
112  std::cout << __FILE__ << ":" << __LINE__ << " " << ids.at(1) << std::endl;
113 
114 // Wt::Dbo::Transaction t( session );
115 // return
116 // session.load< Wtx::Crm::Person::Item >(id)
117 // ;
118 
119  return retVal;
120 
121 }
122 
123 Wtx::Crm::Person::Item::Ptr Wtx::Crm::Person::xfind( const std::string & xid, Wtx::Dbo::Session & session )
124 {
125  Wt::Dbo::Transaction t( session );
126  return
127  session.find< Wtx::Crm::Person::Item >()
128  .where( "xid = ? AND xid != ''")
129  .bind( xid )
130  .resultValue()
131  ;
132 }
133 
134 
135 Wtx::Crm::Person::Item::Ptr Wtx::Crm::Person::add( const std::string & lastName, const std::string & firstName, Wtx::Dbo::Session & session )
136 {
137  Wt::Dbo::Transaction t( session );
138  return
139  session.addNew<Wtx::Crm::Person::Item>( Wtx::Crm::Person::key( lastName, firstName ) );
140 }
141 
142 std::shared_ptr<Wt::WAbstractItemModel> Wtx::Crm::Person::getComboBoxModel( int sid, int tid, const std::string & where, Wtx::Dbo::Session & session )
143 {
144  auto retVal = std::make_shared< Wtx::Dbo::QueryModel< std::tuple<int,std::string> > >();
145 
146  Wt::Dbo::Transaction t( session );
147 
148  auto w = where;
149  if( tid != -1 )
150  w = Wt::WString("WHERE id={1}").arg(tid).toUTF8();
151 
152 #ifdef NEVER
153  std::cout << __FILE__ << ":" << __LINE__
154  << "\n sid:" << sid
155  << "\n tid:" << tid
156  << "\n where:" << w
157  << std::endl
158  ;
159 #endif
160 
161  auto sql =
162  Wt::WString
163  (
164  "SELECT id,\"keyField\" || ' ~ ' || \"cfyField\" AS key FROM \"crmPerson\""
165  " {1}"
166  " UNION "
167  "SELECT -1 as id, '' as key"
168  " ORDER BY key"
169  )
170  .arg( w )
171  .toUTF8()
172  ;
173 
174 
175  auto query =
176  session.query< std::tuple<int,std::string> >( sql )
177  ;
178 
179  retVal-> setQuery( query );
180  retVal-> addColumn("id");
181  retVal-> addColumn("key");
182 
183  return retVal;
184 }
185 
186 
187 
static void PostCreateTable(Wt::Dbo::Session &session)
Post Table Create.
Definition: BaseItemTpl.h:141
int stoi(const std::string &value)
Convert a String to an Integer.
Definition: Util.cpp:295