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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* pracrodaotest.cc
*
* Fri Aug 7 10:25:07 CEST 2009
* Copyright 2009 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Pracro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "pracrodaotest.h"
#include <stdlib.h>
#include <sstream>
#include "debug.h"
PracroDAOTest::PracroDAOTest(Data &data, bool ignore_fieldnames)
: PracroDAO("", "", "", "", "")
{
this->data = data;
this->ignore_fieldnames = ignore_fieldnames;
DEBUG(db, "New test (memory only) database\n");
}
PracroDAOTest::~PracroDAOTest()
{
DEBUG(db, "Delete test (memory only) database\n");
}
std::string PracroDAOTest::newSessionId()
{
return data.sessionseq.nextval();
}
void PracroDAOTest::commitTransaction(std::string sessionid,
Transaction &transaction,
Commit &commit,
Macro &_macro,
time_t now)
{
DEBUG(db, "(%s, %s, %s, <%u fields>, %ld)\n",
transaction.user.c_str(),
transaction.cpr.c_str(),
_macro.attributes["name"].c_str(),
commit.fields.size(),
now);
if(commit.fields.size() == 0) return;
std::string version = _macro.attributes["version"];
std::string macro = _macro.attributes["name"];
std::stringstream timestamp; timestamp << now;
dbrow_t t;
t["uid"] = data.trseq.nextval();
t["patientid"] = transaction.cpr;
t["template"] = commit.templ;
t["macro"] = macro;
t["version"] = version;
t["timestamp"] = timestamp.str();
t["user"] = transaction.user;
data.transactions.push_back(t);
// Iterate fields...
Fields::iterator fi = commit.fields.begin();
while(fi != commit.fields.end()) {
if(ignore_fieldnames == false) {
// Search for it in fieldnames table
dbtable_t::iterator ti = data.fieldnames.begin();
while(ti != data.fieldnames.end()) {
// If found, insert the field values into the fields table.
if(fi->first == (*ti)["name"]) {
dbrow_t f;
f["transaction"] = data.trseq.currval();
f["name"] = fi->first;
f["value"] = fi->second;
data.fields.push_back(f);
}
ti++;
}
} else {
dbrow_t f;
f["transaction"] = data.trseq.currval();
f["name"] = fi->first;
f["value"] = fi->second;
data.fields.push_back(f);
}
fi++;
}
}
Values PracroDAOTest::getLatestValues(std::string sessionid,
std::string patientid, Macro *macro,
Fieldnames &fieldnames, time_t oldest)
{
std::string macro_name = macro ? macro->attributes["name"].c_str() : "(null)";
DEBUG(db, "(%s, %s, <%u fieldnames>, %ld)\n",
patientid.c_str(),
macro_name.c_str(), fieldnames.size(),
oldest);
Values values;
// TODO: Take Macro* into account. If supplied (not NULL) the macro name, and
// optionally version number should match the transaction.
Fieldnames::iterator fi = fieldnames.begin();
while(fi != fieldnames.end()) {
std::string fieldname = *fi;
// Find matching transactions
dbtable_t::iterator ti = data.transactions.begin();
while(ti != data.transactions.end()) {
std::map<std::string, std::string> &transaction = *ti;
time_t timestamp = atol(transaction["timestamp"].c_str());
if(transaction["patientid"] == patientid && timestamp >= oldest && (transaction["macro"] == macro_name || macro == NULL)) {
std::string tid = transaction["uid"];
// Find transaction values
dbtable_t::iterator vi = data.fields.begin();
while(vi != data.fields.end()) {
std::map<std::string, std::string> &field = *vi;
// Upon match, insert it into values
if(field["transaction"] == tid && field["name"] == fieldname) {
if(values.find(fieldname) == values.end() || values[fieldname].timestamp < timestamp) {
values[fieldname].timestamp = timestamp;
values[fieldname].value = field["value"];
values[fieldname].source = "testdb";
}
}
vi++;
}
}
ti++;
}
fi++;
}
return values;
}
unsigned PracroDAOTest::nrOfCommits(std::string sessionid,
std::string patientid,
std::string macroname,
time_t oldest)
{
unsigned num = 0;
// Find and count matching transactions
dbtable_t::iterator ti = data.transactions.begin();
while(ti != data.transactions.end()) {
std::map<std::string, std::string> &transaction = *ti;
time_t timestamp = atol(transaction["timestamp"].c_str());
if(transaction["patientid"] == patientid &&
transaction["macro"] == macroname &&
timestamp >= oldest) {
num++;
}
ti++;
}
return num;
}
void PracroDAOTest::addFieldname(std::string name, std::string description)
{
dbrow_t fieldname;
fieldname["name"] = name;
fieldname["description"] = description;
char buf[256];
sprintf(buf, "%lu", time(NULL));
fieldname["timestamp"] = buf;
data.fieldnames.push_back(fieldname);
}
void PracroDAOTest::delFieldname(std::string name)
{
dbtable_t::iterator i = data.fieldnames.begin();
while(i != data.fieldnames.end()) {
dbrow_t &row = *i;
if(row["name"] == name) {
data.fieldnames.erase(i);
return;
}
i++;
}
}
std::vector<Fieldname> PracroDAOTest::getFieldnames()
{
std::vector<Fieldname> fieldnames;
dbtable_t::iterator i = data.fieldnames.begin();
while(i != data.fieldnames.end()) {
dbrow_t &row = *i;
Fieldname fn;
fn.name = row["name"];
fn.description = row["description"];
fn.timestamp = atoll(row["timestamp"].c_str());
fieldnames.push_back(fn);
i++;
}
return fieldnames;
}
bool PracroDAOTest::active(std::string sessionid)
{
dbtable_t::iterator i = data.commits.begin();
while(i != data.commits.end()) {
dbrow_t &commit = *i;
if(commit["sessionid"] == sessionid) {
return commit["status"] != "idle";
}
i++;
}
return false;
}
void PracroDAOTest::setActive(std::string sessionid, bool active)
{
dbtable_t::iterator i = data.commits.begin();
while(i != data.commits.end()) {
dbrow_t &commit = *i;
if(commit["sessionid"] == sessionid && commit["status"] != "committed") {
commit["status"] = active?"active":"idle";
}
i++;
}
}
#ifdef TEST_PRACRODAOTEST
//deps: debug.cc log.cc configuration.cc exception.cc pracrodao.cc
//cflags: -I..
//libs:
#include <test.h>
#include <time.h>
#define PATIENTID "1234567890"
#define MACRO "testmacro"
static bool vectorFind(std::vector<Fieldname> fs,
std::string name, std::string desc)
{
std::vector<Fieldname>::iterator i = fs.begin();
while(i != fs.end()) {
Fieldname &fn = *i;
if(fn.name == name &&
(desc == "" || fn.description == desc)) return true;
i++;
}
return false;
}
TEST_BEGIN;
Data data;
PracroDAOTest db(data);
db.addFieldname("field1", "desc1");
db.addFieldname("field2", "desc2");
db.addFieldname("field3", "desc3");
db.delFieldname("field3");
std::vector<Fieldname> fs = db.getFieldnames();
TEST_EQUAL_INT(fs.size(), 2, "Test fieldname size.");
TEST_TRUE(vectorFind(fs, "field1", "desc1"), "Test fieldname 'field1'.");
TEST_TRUE(vectorFind(fs, "field2", "desc2"), "Test fieldname 'field2'.");
TEST_FALSE(vectorFind(fs, "field3", ""), "Test fieldname 'field3'.");
std::string sid1 = db.newSessionId();
std::string sid2 = db.newSessionId();
TEST_NOTEQUAL_STR(sid1, sid2, "Do not produce the same uid each time.");
Transaction transaction;
transaction.cpr = PATIENTID;
transaction.user = "me";
Commit commit;
commit.fields["field1"] = "hello";
commit.fields["field2"] = "world";
commit.templ = "tester";
Macro macro;
macro.attributes["version"] = "1.0";
macro.attributes["name"] = MACRO;
time_t now = time(NULL);
db.commitTransaction(sid1, transaction, commit, macro, now);
TEST_EQUAL_INT(db.nrOfCommits(sid1, PATIENTID, MACRO, now), 1, "How many?");
Fieldnames fieldnames;
fieldnames.push_back("field1");
fieldnames.push_back("field_nop");
Values vals = db.getLatestValues(sid1, PATIENTID, ¯o, fieldnames, 0);
TEST_EQUAL_INT(vals.size(), 1, "One value");
TEST_NOTEQUAL(vals.find("field1"), vals.end(), "find value");
/*
time_t now = time(NULL);
Data data;
// Add some fieldnames
dbrow_t f;
f["name"] = "field1"; data.fieldnames.push_back(f);
f["name"] = "field2"; data.fieldnames.push_back(f);
f["name"] = "field3"; data.fieldnames.push_back(f);
PracroDAOTest db(data);
// Make a commit
Macro macro;
macro.attributes["version"] = "1.0";
macro.attributes["name"] = "testmacro";
Fields fields;
fields["field1"] = "testval1";
fields["field2"] = "testval2";
fields["field3"] = "testval3";
fields["field4"] = "testval4";
db.commitTransaction("testuser", PATIENTID, macro, fields, now);
// Retrieve the data again
unsigned num = db.nrOfCommits(PATIENTID, "testmacro", now);
if(num != 1) return 1;
Fieldnames fieldnames;
fieldnames.push_back("field1");
fieldnames.push_back("field2");
fieldnames.push_back("field3");
fieldnames.push_back("field4");
Values values = db.getLatestValues(PATIENTID, NULL, fieldnames, now);
Values::iterator i = values.begin();
while(i != values.end()) {
printf("%s => %s\n", i->first.c_str(), i->second.value.c_str());
i++;
}
if(values["field1"].value != "testval1") return 1;
if(values["field2"].value != "testval2") return 1;
if(values["field3"].value != "testval3") return 1;
// This value was not committed, since it wasn't in the fieldnames table.
if(values.find("field4") != values.end()) return 1;
// Make another commit (one second later)
fields["field1"] = "testval1-2";
fields["field2"] = "testval2-2";
fields["field3"] = "testval3-2";
fields["field4"] = "testval4-2";
db.commitTransaction("testuser", PATIENTID, macro, fields, now+1);
// Retrieve the data again
num = db.nrOfCommits(PATIENTID, "testmacro", now);
if(num != 2) return 1;
fieldnames.push_back("field1");
fieldnames.push_back("field2");
fieldnames.push_back("field3");
fieldnames.push_back("field4");
values = db.getLatestValues(PATIENTID, NULL, fieldnames, now);
i = values.begin();
while(i != values.end()) {
printf("%s => %s\n", i->first.c_str(), i->second.value.c_str());
i++;
}
if(values["field1"].value != "testval1-2") return 1;
if(values["field2"].value != "testval2-2") return 1;
if(values["field3"].value != "testval3-2") return 1;
// This value was not committed, since it wasn't in the fieldnames table.
if(values.find("field4") != values.end()) return 1;
*/
TEST_END;
#endif/*TEST_PRACRODAOTEST*/
|