-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerController.cpp
More file actions
339 lines (290 loc) · 12.1 KB
/
CustomerController.cpp
File metadata and controls
339 lines (290 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* CustomerController.cpp
*
* Created on: 14 Feb 2012
* Author: Max Foster
*/
using namespace std;
#include "CustomerController.h"
#include "JobController.h"
#include "Databases.h"
#include "Customer.h"
#include "Job.h"
#include "dialogs/customer/CustomerIndex.h"
#include "dialogs/customer/CustomerShow.h"
#include "dialogs/customer/CustomerForm.h"
void CustomerController::Index(QWidget *caller)
{
// Attempt to get a list of all the Customer records in the database.
// If there is an error then show it in a dialog box and terminate the function
Database<Customer>::recordListPtr customers;
try { customers = Databases::customers().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
CustomerIndex(*customers, caller).exec();
}
void CustomerController::Show(const int customerId, QWidget *caller)
{
// Attempt to get the Customer record with ID customerId. If there are any problems show a dialog box and finish
Customer customer;
try { customer = Databases::customers().findRecord("id", customerId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (customer.null())
{
showErrorDialog(("Customer with ID " + toString(customerId) + " could not be found").c_str());
return;
}
// Pass the Customer to the overloaded Show function to keep code DRY
Show(customer, caller);
}
void CustomerController::Show(Customer &customer, QWidget *caller)
{
// Get the Customer records associated jobs (if any), showing a dialog if there is an error
Database<Job>::recordListPtr jobs;
try { jobs = Databases::jobs().findRecords("customerId", customer.getId()); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
CustomerShow(customer, *jobs, caller).exec();
}
Customer CustomerController::New(QWidget *caller)
{
// Create a blank customer for the Customer Form to use. If the 'cancel' button is clicked then return a blank
// Customer record, otherwise return the Customer record with its attributes
Customer customer;
CustomerForm view(customer, caller);
return (view.exec() == QDialog::Rejected ? Customer() : customer);
}
void CustomerController::Edit(const int customerId, QWidget *caller)
{
// Find the Customer record with ID customerId, showing a dialog if there are any errors
Customer customer;
try { customer = Databases::customers().findRecord("id", customerId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (customer.null())
{
showErrorDialog(("Customer with ID " + toString(customerId) + " could not be found").c_str());
return;
}
Edit(customer, caller);
}
void CustomerController::Edit(Customer &customer, QWidget *caller)
{
// Pass a copy of the customer to the Customer Form just in case the user presses the 'cancel' button.
// If the edit was successful, then copy the attributes of the updated Customer copy to the passed Customer
// If not, then the passed customer will remain unchanged
Customer tempCustomer = customer;
CustomerForm view(tempCustomer, caller);
if (view.exec() == CustomerForm::Accepted) customer = tempCustomer;
}
bool CustomerController::Create(Customer &customerAttributes, QWidget *)
{
// Add a new record with the given attributes to the database. If there were any errors then show them in a dialog
// and return false (i.e. unsuccessful)
try { Databases::customers().addRecord(customerAttributes); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (customerAttributes.null())
{
showErrorDialog("There was an error with adding the customer to the database");
return false;
}
return true;
}
bool CustomerController::Update(const Customer &customer, QWidget *)
{
// Database::updateRecord returns a boolean value so keep track of this value and use it to see if the update
// operation was successful. If there are any problems show a dialog
bool success = false;
try { success = Databases::customers().updateRecord(customer); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with updating the customer in the database");
return success;
}
bool CustomerController::Destroy(const int customerId, QWidget * caller)
{
// Delete all associated jobs first, recording any errors
vector<string> errors;
Database<Job>::recordListPtr jobs = Databases::jobs().findRecords("customerId", customerId);
errors.reserve(jobs->size());
for (unsigned i = 0; i < jobs->size(); ++i) JobController::Destroy(jobs->at(i), caller);
// If there were errors, report them
if (errors.size() > 0) showErrorDialog(errors);
else // otherwise try and delete the customer record
{
// Database::deleteRecord returns a boolean value, so keep track of it and use it to see if the operation was
// successful
bool success = false;
try { success = Databases::customers().deleteRecord(customerId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with removing the customer from the database");
return success;
}
return false;
}
bool CustomerController::Destroy(Customer &customer, QWidget * caller)
{
// Destroy the Customer record with the ID of the passed Customer. If the operation was successful then clear the
// attributes of the passed Customer, which makes it 'null'
if (Destroy(customer.getId(), caller))
{
customer = Customer();
return true;
}
return false;
}
Customer CustomerController::getCustomer(const int customerId)
{
// Get the customer with the ID customerId from the database, reporting any errors to the user. If there was an
// error, then return a blank Customer record
Customer customer;
try { customer = Databases::customers().findRecord("id", customerId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Customer();
}
return customer;
}
Database<Customer>::recordListPtr CustomerController::getAllCustomers()
{
// Get a memory managed pointer to a list of all the customers in the database, reporting any errors to the user.
// If there was an error, then return an empty list
Database<Customer>::recordListPtr customers;
try { customers = Databases::customers().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Customer>::recordListPtr(new Database<Customer>::recordList);
}
return customers;
}
Database<Job>::recordListPtr CustomerController::getCustomerJobs(const int customerId)
{
// Get all of the jobs whose customerId field matches the value of customerId. If there was an error, report it to
// the user and return an empty list
Database<Job>::recordListPtr jobs;
try { jobs = Databases::jobs().findRecords("customerId", customerId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Job>::recordListPtr(new Database<Job>::recordList);
}
return jobs;
}
void CustomerController::sortCustomersBySurname(Database<Customer>::recordList &customers, bool ascending)
{
struct NestedFunctions
{
// Not DRY but can't be done any other way
static int surnameCompareAsc(const Customer &customer1, const Customer &customer2)
{
// Memory to store copies of the surnames of each customer. We need copies because the surnames need to be
// converted into lowercase without affecting the original Customer records
static char surname1[64] = { 0 }, surname2[64] = { 0 };
static const int aTakeA = 'a' - 'A'; // for lowercase conversion
// Copy over the surnames
strcpy(surname1, customer1.getSurname());
strcpy(surname2, customer2.getSurname());
const size_t length1 = strlen(surname1), length2 = strlen(surname2);
// Loop through the characters of each surname, converting them to lowercase if necessary, and comparing
// them with each other. If any surname character not equal to the other, then return whether the surname
// comes 'before' (-1) or 'after' (1)
for (unsigned i = 0; (i < length1) && (i < length2); ++i)
{
if (surname1[i] >= 'a') surname1[i] -= aTakeA;
if (surname2[i] >= 'a') surname2[i] -= aTakeA;
if (surname1[i] < surname2[i]) return -1;
if (surname1[i] > surname2[i]) return 1;
}
// No differences yet so compare the lengths. The shorter surname comes 'before'
if (length1 < length2) return -1;
if (length1 > length2) return 1;
// Surnames are the same, so compare the forenames
return forenameCompare(customer1, customer2, true);
}
static int surnameCompareDec(const Customer &customer1, const Customer &customer2)
{
// Same as above but surnames that have characters later in the alphabet or are longer come 'before'
static char surname1[64] = { 0 }, surname2[64] = { 0 };
static const int aTakeA = 'a' - 'A';
strcpy(surname1, customer1.getSurname());
strcpy(surname2, customer2.getSurname());
const size_t length1 = strlen(surname1), length2 = strlen(surname2);
for (unsigned i = 0; (i < length1) && (i < length2); ++i)
{
if (surname1[i] >= 'a') surname1[i] -= aTakeA;
if (surname2[i] >= 'a') surname2[i] -= aTakeA;
if (surname1[i] < surname2[i]) return 1;
if (surname1[i] > surname2[i]) return -1;
}
if (length1 < length2) return 1;
if (length1 > length2) return -1;
return forenameCompare(customer1, customer2, false);
}
static int forenameCompare(const Customer &customer1, const Customer &customer2, const bool ascending)
{
// Same as above but this time a boolean can be passed that decides whether the list is ascending or
// decending, which decides what forename comes 'before' the other
static char forename1[64] = { 0 }, forename2[64] = { 0 };
static const int aTakeA = 'a' - 'A';
strcpy(forename1, customer1.getForename());
strcpy(forename2, customer2.getForename());
const size_t length1 = strlen(forename1), length2 = strlen(forename2);
for (unsigned i = 0; (i < length1) && (i < length2); ++i)
{
if (forename1[i] >= 'a') forename1[i] -= aTakeA;
if (forename2[i] >= 'a') forename2[i] -= aTakeA;
if (ascending)
{
if (forename1[i] < forename2[i]) return -1;
if (forename1[i] > forename2[i]) return 1;
}
else
{
if (forename1[i] < forename2[i]) return 1;
if (forename1[i] > forename2[i]) return -1;
}
}
if (ascending)
{
if (length1 < length2) return -1;
if (length1 > length2) return 1;
}
else
{
if (length1 < length2) return 1;
if (length1 > length2) return -1;
}
return 0;
}
};
// Sort the passed list of customers, passing the relevant comparison function depending on whether the list is
// to be sorted with surnames ascending or decending
Databases::customers().sortRecords(customers, 0, customers.size() - 1, ascending ?
NestedFunctions::surnameCompareAsc : NestedFunctions::surnameCompareDec);
}