-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobController.cpp
More file actions
291 lines (252 loc) · 7.7 KB
/
JobController.cpp
File metadata and controls
291 lines (252 loc) · 7.7 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
/*
* JobController.cpp
*
* Created on: 14 Feb 2012
* Author: Max Foster
*/
using namespace std;
#include "JobController.h"
#include "PartController.h"
#include "TaskController.h"
#include "Databases.h"
#include "Database.h"
#include "Job.h"
#include "Customer.h"
#include "Part.h"
#include "Task.h"
#include "dialogs/job/JobIndex.h"
#include "dialogs/job/JobShow.h"
#include "dialogs/job/JobForm.h"
namespace DateFunctions
{
static time_t dateLowerBound, dateUpperBound;
static bool isJobDateWithinBounds(const Job &job, void *)
{
return (job.getDate() >= dateLowerBound) && (job.getDate() <= dateUpperBound);
}
}
void JobController::Index(const QDate &date, QWidget *caller)
{
Database<Job>::recordListPtr jobs;
try { jobs = Databases::jobs().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
DateFunctions::dateLowerBound = Date(0, 0, 1, date.month(), date.year());
DateFunctions::dateUpperBound = time_t(Date(0, 0, 1, date.month() + 1, date.year())) - 1;
Databases::jobs().keepRecords(*jobs, DateFunctions::isJobDateWithinBounds, NULL);
const QString title = "Jobs for " + QDate::longMonthName(date.month()) + ' ' + toString(date.year()).c_str();
JobIndex(*jobs, title, caller).exec();
}
void JobController::Show(const int jobId, QWidget *caller)
{
Job job;
try { job = Databases::jobs().findRecord("id", jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (job.null())
{
showErrorDialog(("Job with ID " + toString(jobId) + " could not be found").c_str());
return;
}
Show(job, caller);
}
void JobController::Show(Job &job, QWidget *caller)
{
Database<Part>::recordListPtr parts;
Database<Task>::recordListPtr tasks;
try
{
parts = Databases::parts().findRecords("jobId", job.getId());
tasks = Databases::tasks().findRecords("jobId", job.getId());
}
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
JobShow(job, *parts, *tasks, caller).exec();
}
Job JobController::New(QWidget *caller)
{
Job job;
Database<Part>::recordList parts;
Database<Task>::recordList tasks;
Database<Customer>::recordListPtr customers;
try { customers = Databases::customers().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return job;
}
JobForm view(job, *customers, parts, tasks, caller);
return (view.exec() == QDialog::Rejected ? Job() : job);
}
void JobController::Edit(const int jobId, QWidget *caller)
{
Job job;
try { job = Databases::jobs().findRecord("id", jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
if (job.null())
{
showErrorDialog(("Job with ID " + toString(jobId) + " could not be found").c_str());
return;
}
Edit(job, caller);
}
void JobController::Edit(Job &job, QWidget *caller)
{
Database<Customer>::recordListPtr customers;
Database<Part>::recordListPtr parts;
Database<Task>::recordListPtr tasks;
try
{
customers = Databases::customers().allRecords();
parts = Databases::parts().findRecords("jobId", job.getId());
tasks = Databases::tasks().findRecords("jobId", job.getId());
}
catch (const std::exception &e)
{
showErrorDialog(e.what());
return;
}
Job tempJob = job;
JobForm view(tempJob, *customers, *parts, *tasks, caller);
if (view.exec() == JobForm::Accepted) job = tempJob;
}
bool JobController::Create(Job &jobAttributes, QWidget *)
{
try { Databases::jobs().addRecord(jobAttributes); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (jobAttributes.null())
{
showErrorDialog("There was an error with adding the job to the database");
return false;
}
return true;
}
bool JobController::Update(const Job &job, QWidget *)
{
bool success = false;
try { success = Databases::jobs().updateRecord(job); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with updating the job in the database");
return success;
}
bool JobController::Destroy(const int jobId, QWidget * caller)
{
// Delete all associated parts and tasks first, recording any errors
vector<string> errors;
Database<Part>::recordListPtr parts = Databases::parts().findRecords("jobId", jobId);
Database<Task>::recordListPtr tasks = Databases::tasks().findRecords("jobId", jobId);
errors.reserve(parts->size() + tasks->size());
for (unsigned i = 0; i < parts->size(); ++i) PartController::Destroy(parts->at(i), caller);
for (unsigned i = 0; i < tasks->size(); ++i) TaskController::Destroy(tasks->at(i), caller);
// If there were errors, report them
if (errors.size() > 0) showErrorDialog(errors);
else // otherwise try and delete the job record
{
bool success = false;
try { success = Databases::jobs().deleteRecord(jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return false;
}
if (!success) showErrorDialog("There was an error with removing the job from the database");
return success;
}
return false;
}
bool JobController::Destroy(Job &job, QWidget *caller)
{
if (Destroy(job.getId(), caller))
{
job = Job();
return true;
}
return false;
}
Job JobController::getJob(const int jobId)
{
Job job;
try { job = Databases::jobs().findRecord("id", jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Job();
}
return job;
}
Database<Job>::recordListPtr JobController::getAllJobs()
{
Database<Job>::recordListPtr jobs;
try { jobs = Databases::jobs().allRecords(); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Job>::recordListPtr(new Database<Job>::recordList);
}
return jobs;
}
Database<Part>::recordListPtr JobController::getJobParts(const int jobId)
{
Database<Part>::recordListPtr parts;
try { parts = Databases::parts().findRecords("jobId", jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Part>::recordListPtr(new Database<Part>::recordList);
}
return parts;
}
Database<Task>::recordListPtr JobController::getJobTasks(const int jobId)
{
Database<Task>::recordListPtr tasks;
try { tasks = Databases::tasks().findRecords("jobId", jobId); }
catch (const std::exception &e)
{
showErrorDialog(e.what());
return Database<Task>::recordListPtr(new Database<Task>::recordList);
}
return tasks;
}
void JobController::sortJobsByDate(Database<Job>::recordList &jobs, bool ascending)
{
struct NestedFunctions
{
static int dateCompareAsc(const Job &job1, const Job &job2)
{
const time_t date1 = job1.getDate(), date2 = job2.getDate();
if (date1 < date2) return -1;
if (date1 > date2) return 1;
return 0;
}
static int dateCompareDec(const Job &job1, const Job &job2)
{
const time_t date1 = job1.getDate(), date2 = job2.getDate();
if (date1 < date2) return 1;
if (date1 > date2) return -1;
return 0;
}
};
Databases::jobs().sortRecords(jobs, 0, jobs.size() - 1, ascending ?
NestedFunctions::dateCompareAsc : NestedFunctions::dateCompareDec);
}