-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrescriptionsAdapter.cpp
572 lines (474 loc) · 18.5 KB
/
PrescriptionsAdapter.cpp
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// PrescriptionsAdapter.cpp
// AmiKo-wx
//
// Created by Alex Bettarini on 23 Jun 2020
// Copyright © 2020 Ywesee GmbH. All rights reserved.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <wx/wx.h>
#include <wx/arrstr.h>
#include <wx/wfstream.h>
#include <wx/base64.h>
#include <wx/url.h>
#include <wx/textfile.h>
#include <wx/dir.h>
#include <wx/utils.h>
#include <wx/mstream.h>
#include <nlohmann/json.hpp>
#include "PrescriptionsAdapter.hpp"
#include "Medication.hpp"
#include "PrescriptionItem.hpp"
#include "Patient.hpp"
//#include "Contacts.h"
#include "Operator.hpp"
#include "Utilities.hpp"
#include "DefaultsController.hpp"
#include "sync/GoogleSyncManager.hpp"
PrescriptionsAdapter::PrescriptionsAdapter()
: patient(nullptr)
, doctor(nullptr)
{
}
// 39
// Returns an array of filenames (wxString),
// just the basename with the extension ".amk" stripped off
wxArrayString PrescriptionsAdapter::listOfPrescriptionsForPatient(Patient *p)
{
wxArrayString amkFiles;
if (p == nullptr)
return amkFiles;
wxFileName patientDir( UTI::documentsDirectory(), wxEmptyString); // important for directory names: specify empty filename
// Check if patient has already a directory, TODO: if not create one
patientDir.AppendDir("amk");
patientDir.AppendDir(p->uniqueId);
if (wxDir::Exists(patientDir.GetFullPath())) {
// List content of directory
wxArrayString rzList;
wxDir::GetAllFiles(patientDir.GetFullPath(), &rzList, "*.amk");
for (auto rz : rzList) {
wxFileName fn(rz);
amkFiles.Add( fn.GetName());
}
// 70
// TODO: Sort
}
return amkFiles;
}
// 79
// Returns an array of filenames, the full path, including the extension .amk
wxArrayString PrescriptionsAdapter::listOfPrescriptionURLsForPatient(Patient *p)
{
wxArrayString amkURLs;
if (p == nullptr)
return amkURLs;
wxFileName patientDir( UTI::documentsDirectory(), wxEmptyString); // important for directory names: specify empty filename
// 92
// Check if patient has already a directory, if not create one
patientDir.AppendDir("amk");
patientDir.AppendDir(p->uniqueId);
if (wxDir::Exists(patientDir.GetFullPath())) {
// 98
// List content of directory
wxDir::GetAllFiles(patientDir.GetFullPath(), &amkURLs, "*.amk");
// 108
// TODO: Sort
}
return amkURLs;
}
// 117
void PrescriptionsAdapter::deletePrescriptionWithName_forPatient(wxString name, Patient *p)
{
if (p == nullptr)
return;
// Assign patient
patient = p;
wxFileName patientDir( UTI::documentsDirectory(), wxEmptyString); // important for directory names: specify empty filename
patientDir.AppendDir("amk");
patientDir.AppendDir(p->uniqueId);
patientDir.SetName(name);
patientDir.SetExt("amk");
// Delete file
if (wxFileName::Exists(patientDir.GetFullPath()))
wxRemoveFile(patientDir.GetFullPath());
GoogleSyncManager::Instance()->requestSync();
}
wxFileName PrescriptionsAdapter::amkPathForPatient(Patient *p) {
// Check if patient has already a directory, if not create one
wxFileName patientDir(UTI::documentsDirectory(), wxEmptyString);
patientDir.AppendDir("amk");
if (!wxDirExists(patientDir.GetFullPath())) {
wxMkdir(patientDir.GetFullPath());
}
patientDir.AppendDir(p->uniqueId);
if (!wxDirExists(patientDir.GetFullPath())) {
wxMkdir(patientDir.GetFullPath());
}
return patientDir;
}
// 160
// It will in any case create a new file
// if the overwrite flag is set, delete the original file
// Instead of using nested dictionaries like in amiko-osx, work with JSON directly.
wxURL PrescriptionsAdapter::savePrescriptionForPatient_withUniqueHash_andOverwrite(Patient *p, wxString hash, bool overwrite)
{
wxURL url;
if (!p) {
std::cerr << "savePrescriptionForPatient " << __LINE__ << ", patient not defined\n";
return url;
}
if (overwrite && currentFileName.IsEmpty()) {
std::cerr << "savePrescriptionForPatient " << __LINE__ << ", cannot overwrite an empty filename\n";
return url;
}
if (cart.size() < 1) {
std::cerr << "savePrescriptionForPatient " << __LINE__ << ", cart is empty\n";
return url;
}
// 181
// Assign patient
patient = p;
// Check if patient has already a directory, if not create one
wxFileName patientDir( UTI::documentsDirectory(), wxEmptyString);
patientDir.AppendDir("amk");
if (!wxDirExists(patientDir.GetFullPath())) {
wxMkdir(patientDir.GetFullPath());
}
patientDir.AppendDir(p->uniqueId);
if (!wxDirExists(patientDir.GetFullPath())) {
wxMkdir(patientDir.GetFullPath());
}
if (overwrite) {
// Delete old file
if (!wxRemoveFile(currentFileName)) {
std::cerr << "Error deleting: " << currentFileName << std::endl;
}
}
// 199
// Define a new filename
wxString currentTime = UTI::currentTime();
currentTime.Replace(":", "");
currentTime.Replace(".", "");
wxFileName pathBase64 = patientDir;
pathBase64.SetName("RZ_" + currentTime);
pathBase64.SetExt("amk");
#ifndef NDEBUG
wxFileName pathJson = patientDir;
pathJson.SetName("RZ_" + currentTime);
pathJson.SetExt("json");
#endif
currentFileName = pathBase64.GetFullPath();
#ifndef NDEBUG
std::clog << "savePrescriptionForPatient() new currentFileName:" << currentFileName << std::endl;
#endif
// 210
nlohmann::json jsonStr; // prescriptionDict
nlohmann::json patientDict;
patientDict[KEY_AMK_PAT_ID] = patient->uniqueId;
patientDict[KEY_AMK_PAT_SURNAME] = patient->familyName;
patientDict[KEY_AMK_PAT_NAME] = patient->givenName;
patientDict[KEY_AMK_PAT_BIRTHDATE] = patient->birthDate;
patientDict[KEY_AMK_PAT_GENDER] = patient->gender;
patientDict[KEY_AMK_PAT_WEIGHT] = wxString::Format("%d", patient->weightKg);
patientDict[KEY_AMK_PAT_HEIGHT] = wxString::Format("%d", patient->heightCm);
patientDict[KEY_AMK_PAT_ADDRESS] = patient->postalAddress;
patientDict[KEY_AMK_PAT_ZIP] = patient->zipCode;
patientDict[KEY_AMK_PAT_CITY] = patient->city;
patientDict[KEY_AMK_PAT_COUNTRY] = patient->country;
patientDict[KEY_AMK_PAT_PHONE] = patient->phoneNumber;
patientDict[KEY_AMK_PAT_EMAIL] = patient->emailAddress;
DefaultsController* defaults = DefaultsController::Instance();
nlohmann::json operatorDict;
wxString cityString;
try {
std::ifstream i((UTI::documentsDirectory() + wxFILE_SEP_PATH + DOC_JSON_FILENAME).ToStdString());
nlohmann::json json;
i >> json;
try {
operatorDict[KEY_AMK_DOC_TITLE] = json[DOC_JSON_TITLE].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_TITLE] = "";
}
try {
operatorDict[KEY_AMK_DOC_SURNAME] = json[DOC_JSON_SURNAME].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_SURNAME] = "";
}
try {
operatorDict[KEY_AMK_DOC_NAME] = json[DOC_JSON_NAME].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_NAME] = "";
}
try {
operatorDict[KEY_AMK_DOC_ADDRESS] = json[DOC_JSON_ADDRESS].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_ADDRESS] = "";
}
try {
operatorDict[KEY_AMK_DOC_ZIP] = json[DOC_JSON_ZIP].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_ZIP] = "";
}
try {
operatorDict[KEY_AMK_DOC_CITY] = json[DOC_JSON_CITY].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_CITY] = "";
}
try {
operatorDict[KEY_AMK_DOC_PHONE] = json[DOC_JSON_PHONE].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_PHONE] = "";
}
try {
operatorDict[KEY_AMK_DOC_EMAIL] = json[DOC_JSON_EMAIL].get<std::string>();
} catch (const std::exception &e) {
operatorDict[KEY_AMK_DOC_EMAIL] = "";
}
try {
cityString = json[DOC_JSON_CITY].get<std::string>();
} catch (const std::exception &e) {
cityString = "";
}
wxString pngFilePath = UTI::documentsDirectory() + wxFILE_SEP_PATH + DOC_SIGNATURE_FILENAME;
wxFile file(pngFilePath);
if(!file.IsOpened()){
std::clog << "Cannot open signature file";
} else {
wxFileOffset nSize = file.Length();
// read the whole file into memory
wxUint8* data = new wxUint8[nSize];
file.Read(data, (size_t) nSize);
file.Close();
//Base64 Encode
wxString enc64 = wxBase64Encode(data,nSize);
operatorDict[KEY_AMK_DOC_SIGNATURE] = enc64;
delete data;
}
} catch (const std::exception& e) {
// Just in case the file is not initialized
}
placeDate = wxString::Format("%s, %s",
cityString,
UTI::prettyTime());
// 253
nlohmann::json prescription = nlohmann::json::array();
for (PrescriptionItem *item : cart) {
nlohmann::json dict;
dict[KEY_AMK_MED_PROD_NAME] = item->title;
dict[KEY_AMK_MED_PACKAGE] = item->fullPackageInfo;
if (item->eanCode.length() > 0)
dict[KEY_AMK_MED_EAN] = item->eanCode;
dict[KEY_AMK_MED_COMMENT] = item->comment;
dict[KEY_AMK_MED_TITLE] = item->med->title;
dict[KEY_AMK_MED_OWNER] = item->med->auth;
dict[KEY_AMK_MED_REG_N] = item->med->regnrs;
dict[KEY_AMK_MED_ATC] = item->med->atccode;
prescription.push_back(dict);
}
jsonStr[KEY_AMK_PRESC_HASH] = hash;
jsonStr[KEY_AMK_PRESC_PLACE_DATE] = placeDate;
jsonStr[KEY_AMK_PRESC_PAT] = patientDict;
jsonStr[KEY_AMK_PRESC_DOC] = operatorDict;
jsonStr[KEY_AMK_PRESC_MEDS] = prescription;
//std::cerr << "===JSON AMK:\n" << jsonStr << "\n===\n";
#ifndef NDEBUG
// Crete JSON file
std::ofstream o1(pathJson.GetFullPath(), std::ios::trunc); // overwrite
o1 << std::setw(4) << jsonStr << std::endl;
#endif
// Crete amk file, encoded Base64
wxString o;
o << jsonStr.dump(4);
#ifndef NDEBUG
std::cerr << "Line " << __LINE__ << ", JSON length " << o.length()
//<<< " jsonStr\n" << o
<< std::endl;
#endif
wxCharBuffer buffer = o.ToUTF8();
wxMemoryOutputStream mos(buffer.data(), strlen(buffer.data()));
#ifndef NDEBUG
std::cerr << "Line " << __LINE__ << " mos.GetSize() " << mos.GetSize() << std::endl;
#endif
wxString base64Str = wxBase64Encode(mos.GetOutputStreamBuffer()->GetBufferStart(),
mos.GetSize());
#ifndef NDEBUG
std::cerr << "Line " << __LINE__
<< " encoded base64Str size " << base64Str.length()
//<< " base64Str\n" << base64Str
<< std::endl;
#endif
#if 0 //ndef NDEBUG
wxMemoryBuffer *buffy = new wxMemoryBuffer;
size_t len = wxBase64EncodedSize(o.length());
buffy->GetWriteBuf(len);
buffy->AppendData(buffer.data(), strlen(buffer.data()));
//wxString base64Str2 = wxBase64Encode(*buffy);
//std::cerr << "Line " << __LINE__ << " base64Str2\n" << base64Str2 << std::endl;
delete buffy;
#endif
wxFileOutputStream file( pathBase64.GetFullPath() );
file.Write(base64Str, base64Str.length());
file.Close();
GoogleSyncManager::Instance()->requestSync();
return wxURL(pathBase64.GetFullPath());
}
// 292
// see Generika importReceiptFromURL
// see AmiKo-osx loadPrescriptionFromFile
// see AmiKo-ios importFromURL
wxString PrescriptionsAdapter::loadPrescriptionFromFile(wxString filePath)
{
//std::clog << __FUNCTION__ << "\n\t" << filePath << std::endl;
std::string hash;
wxFFileInputStream file(filePath);
size_t FileSize = file.GetSize();
char *buffer = new char[FileSize];
if (!buffer) {
std::cerr << __FUNCTION__ << __LINE__ << " Couldn't allocate buffer\n";
return hash;
}
file.Read(buffer, FileSize);
// Specifying the file size for FromUTF8() is very important in this case
wxString base64Str = wxString::FromUTF8(buffer, FileSize);
if (base64Str.IsEmpty()) {
std::cerr << __FUNCTION__ << " line " << __LINE__
<< "\n\t file: " << filePath
<< "\n\t FileSize: " << FileSize
<< " Empty base64Str\n";
delete [] buffer;
return hash;
}
delete [] buffer;
if (FileSize != base64Str.length()) {
std::cerr << __FUNCTION__ << " line " << __LINE__
<< ", FileSize: " << FileSize
<< ", base64Str: " << base64Str.length()
<< std::endl;
return hash;
}
base64Str += wxT("\n"); // Linux parser seems to expect a line terminator
try {
wxMemoryBuffer buf = wxBase64Decode(base64Str.c_str(), wxNO_LEN, wxBase64DecodeMode_SkipWS);
wxString jsonStr((const char *)buf.GetData(), buf.GetDataLen());
if (jsonStr.IsEmpty()) {
#if 0 //ndef NDEBUG
std::cerr << __FUNCTION__ << " line " << __LINE__
<< "\n\tbase64Str:" << base64Str.length()
<< "\n" << base64Str << ">>> base64Str >>>\n";
#endif
std::cerr << __FUNCTION__ << __LINE__ << " Empty jsonStr\n";
return hash;
}
// 300
currentFileName = filePath;
nlohmann::json jsonDict;
try {
//auto jsonDict = nlohmann::json::parse((char *)buf.GetData());
jsonDict = nlohmann::json::parse((char *)jsonStr.char_str());
}
catch (const std::exception&e) {
std::cerr << __FUNCTION__ << " line " << __LINE__ << std::endl
#if 0 //ndef NDEBUG
<< "base64Str: " << base64Str.length() << "\n" << base64Str
<< ">>> base64Str >>>\n"
<< "jsonStr:" << jsonStr.length() << "\n" << jsonStr
<< ">>> jsonStr >>>\n"
<< "buf.GetDataLen(): " << buf.GetDataLen() << std::endl
<< "jsonStr.length(): " << jsonStr.length() << std::endl
<< "jsonStr: " << jsonStr << std::endl
#endif
<< "Exception " << e.what() << std::endl;
return hash;
}
#if 0 //ndef NDEBUG
for (auto & element : jsonDict)
std::clog << "element " << element.dump(4) << std::endl;
#endif
auto medicat = jsonDict[KEY_AMK_PRESC_MEDS];
#if 0 //ndef NDEBUG
std::clog << "medications: " << medicat.dump(4) << std::endl;
for (auto & op : medicat.items())
std::clog << op.key() << ", value: " << op.value() << std::endl;
#endif
// 303
// Prescription
std::vector<PrescriptionItem *> prescription;
for (auto & p : medicat) {
MED_DICT mediDict;
// For Medication:
mediDict[KEY_AMK_MED_TITLE] = p[KEY_AMK_MED_TITLE];
mediDict[KEY_AMK_MED_OWNER] = p[KEY_AMK_MED_OWNER];
mediDict[KEY_AMK_MED_REG_N] = p[KEY_AMK_MED_REG_N];
mediDict[KEY_AMK_MED_ATC] = p[KEY_AMK_MED_ATC];
// For PrescriptionItem:
mediDict[KEY_AMK_MED_PROD_NAME] = p[KEY_AMK_MED_PROD_NAME];
mediDict[KEY_AMK_MED_PACKAGE] = p[KEY_AMK_MED_PACKAGE];
// If the GTIN is missing
// the field KEY_AMK_MED_PACKAGE in the JSON file is missing altogether,
// (it's not an empty string). This will cause an exception
// that we need to catch
try {
mediDict[KEY_AMK_MED_EAN] = p[KEY_AMK_MED_EAN];
}
catch (const std::exception&e) {
std::cerr << "Missing " << KEY_AMK_MED_EAN << ", exception:"
<< e.what() << std::endl;
}
mediDict[KEY_AMK_MED_COMMENT] = p[KEY_AMK_MED_COMMENT];
Medication *med = new Medication;
med->importFromDict(mediDict);
PrescriptionItem *item = new PrescriptionItem;
item->importFromDict(mediDict);
item->med = med;
// 313
prescription.push_back(item);
}
cart = prescription;
// 317
{
auto patientDict = jsonDict[KEY_AMK_PRESC_PAT];
PAT_DICT dict;
dict[KEY_AMK_PAT_BIRTHDATE] = patientDict[KEY_AMK_PAT_BIRTHDATE];
dict[KEY_AMK_PAT_CITY] = patientDict[KEY_AMK_PAT_CITY];
dict[KEY_AMK_PAT_COUNTRY] = patientDict[KEY_AMK_PAT_COUNTRY];
dict[KEY_AMK_PAT_EMAIL] = patientDict[KEY_AMK_PAT_EMAIL];
dict[KEY_AMK_PAT_SURNAME] = patientDict[KEY_AMK_PAT_SURNAME];
dict[KEY_AMK_PAT_GENDER] = patientDict[KEY_AMK_PAT_GENDER];
dict[KEY_AMK_PAT_NAME] = patientDict[KEY_AMK_PAT_NAME];
dict[KEY_AMK_PAT_HEIGHT] = patientDict[KEY_AMK_PAT_HEIGHT];
dict[KEY_AMK_PAT_ID] = patientDict[KEY_AMK_PAT_ID];
dict[KEY_AMK_PAT_PHONE] = patientDict[KEY_AMK_PAT_PHONE];
dict[KEY_AMK_PAT_ADDRESS] = patientDict[KEY_AMK_PAT_ADDRESS];
dict[KEY_AMK_PAT_WEIGHT] = patientDict[KEY_AMK_PAT_WEIGHT];
dict[KEY_AMK_PAT_ZIP] = patientDict[KEY_AMK_PAT_ZIP];
patient = new Patient;
patient->importFromDict(dict);
}
// 321
{
auto operatorDict = jsonDict[KEY_AMK_PRESC_DOC];
OPER_DICT dict;
dict[KEY_AMK_DOC_CITY] = operatorDict[KEY_AMK_DOC_CITY];
dict[KEY_AMK_DOC_EMAIL] = operatorDict[KEY_AMK_DOC_EMAIL];
dict[KEY_AMK_DOC_SURNAME] = operatorDict[KEY_AMK_DOC_SURNAME];
dict[KEY_AMK_DOC_NAME] = operatorDict[KEY_AMK_DOC_NAME];
dict[KEY_AMK_DOC_PHONE] = operatorDict[KEY_AMK_DOC_PHONE];
dict[KEY_AMK_DOC_ADDRESS] = operatorDict[KEY_AMK_DOC_ADDRESS];
dict[KEY_AMK_DOC_SIGNATURE] = operatorDict[KEY_AMK_DOC_SIGNATURE];
dict[KEY_AMK_DOC_TITLE] = operatorDict[KEY_AMK_DOC_TITLE];
dict[KEY_AMK_DOC_ZIP] = operatorDict[KEY_AMK_DOC_ZIP];
doctor = new Operator;
doctor->importFromDict(dict);
}
// 325
placeDate = jsonDict[KEY_AMK_PRESC_PLACE_DATE];
if (placeDate.length() == 0)
placeDate = jsonDict[KEY_AMK_PRESC_PLACE_DATE_OLD]; // backward compatibility ?
hash = jsonDict[KEY_AMK_PRESC_HASH];
}
catch (const std::exception&e) {
std::cerr << "Error parsing: " << filePath << std::endl
<< e.what() << std::endl;
}
return hash;
}