-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLegacyPatientDBAdapter.m
164 lines (134 loc) · 5.26 KB
/
LegacyPatientDBAdapter.m
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
/*
Copyright (c) 2017 Max Lungarella <[email protected]>
Created on 21/06/2017.
This file is part of AmiKo for OSX.
AmiKo for OSX 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------ */
#import "LegacyPatientDBAdapter.h"
#import "MLSQLiteDatabase.h"
#import "MLUtilities.h"
#import "MLContacts.h"
static NSString *KEY_ROWID = @"_id";
static NSString *KEY_TIMESTAMP = @"time_stamp";
static NSString *KEY_UID = @"uid";
static NSString *KEY_FAMILYNAME = @"family_name";
static NSString *KEY_GIVENNAME = @"given_name";
static NSString *KEY_BIRTHDATE = @"birthdate";
static NSString *KEY_GENDER = @"gender";
static NSString *KEY_WEIGHT_KG = @"weight_kg";
static NSString *KEY_HEIGHT_CM = @"height_cm";
static NSString *KEY_ZIPCODE = @"zip";
static NSString *KEY_CITY = @"city";
static NSString *KEY_COUNTRY = @"country";
static NSString *KEY_ADDRESS = @"address";
static NSString *KEY_PHONE = @"phone";
static NSString *KEY_EMAIL = @"email";
static NSString *DATABASE_TABLE = @"patients";
/** Table columns for fast queries
*/
static NSString *ALL_COLUMNS = nil;
@implementation LegacyPatientDBAdapter
{
MLSQLiteDatabase *myPatientDb;
}
/** Class function
*/
#pragma mark Class functions
+ (LegacyPatientDBAdapter *)sharedInstance
{
NSLog(@"%s", __FUNCTION__);
__strong static id sharedObject = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedObject = [[self alloc] init];
if (![sharedObject openDatabase]) {
NSLog(@"Could not open patient DB!");
sharedObject = nil;
}
});
return sharedObject;
}
+ (void) initialize
{
if (self == [LegacyPatientDBAdapter class]) {
if (ALL_COLUMNS == nil) {
ALL_COLUMNS = [NSString stringWithFormat: @"%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@", KEY_ROWID, KEY_TIMESTAMP, KEY_UID, KEY_FAMILYNAME, KEY_GIVENNAME, KEY_BIRTHDATE, KEY_GENDER, KEY_WEIGHT_KG, KEY_HEIGHT_CM, KEY_ZIPCODE, KEY_CITY, KEY_COUNTRY, KEY_ADDRESS, KEY_PHONE, KEY_EMAIL];
}
}
}
/** Instance functions
*/
#pragma mark Instance functions
- (BOOL) openDatabase
{
if (myPatientDb == nil) {
// Patient DB should be in the user's documents folder
NSFileManager *fileManager = [NSFileManager defaultManager];
// Get documents directory
NSString *filePath = [self dbPath];
// Check if database exists
if (filePath!=nil) {
// Load database if it exists already
if ([fileManager fileExistsAtPath:filePath]) {
NSLog(@"Patient DB found in user's documents folder %@", filePath);
myPatientDb = [[MLSQLiteDatabase alloc] initReadWriteWithPath:filePath];
return TRUE;
}
}
}
return FALSE;
}
- (NSString *)dbPath {
NSString *documentsDir = [MLUtilities documentsDirectory];
return [documentsDir stringByAppendingPathComponent:@"patient_db.db"];
}
- (void) closeDatabase
{
// The database is open as long as the app is open
if (myPatientDb)
[myPatientDb close];
}
- (NSArray *) getAllPatients
{
NSMutableArray *listOfPatients = [NSMutableArray array];
NSString *query = [NSString stringWithFormat:@"select %@ from %@", ALL_COLUMNS, DATABASE_TABLE];
NSArray *results = [myPatientDb performQuery:query];
if ([results count]>0) {
for (NSArray *cursor in results) {
[listOfPatients addObject:[self cursorToPatient:cursor]];
}
// Sort alphabetically
NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"familyName" ascending:YES];
[listOfPatients sortUsingDescriptors:[NSArray arrayWithObject:nameSort]];
}
return listOfPatients;
}
- (MLPatient *) cursorToPatient:(NSArray *)cursor
{
MLPatient *patient = [[MLPatient alloc] init];
patient.uniqueId = (NSString *)[cursor objectAtIndex:2];
patient.familyName = (NSString *)[cursor objectAtIndex:3];
patient.givenName = (NSString *)[cursor objectAtIndex:4];
patient.birthDate = (NSString *)[cursor objectAtIndex:5];
patient.gender = (NSString *)[cursor objectAtIndex:6];
patient.weightKg = [[cursor objectAtIndex:7] intValue];
patient.heightCm = [[cursor objectAtIndex:8] intValue];
patient.zipCode = (NSString *)[cursor objectAtIndex:9];
patient.city = (NSString *)[cursor objectAtIndex:10];
patient.country = (NSString *)[cursor objectAtIndex:11];
patient.postalAddress = (NSString *)[cursor objectAtIndex:12];
patient.phoneNumber = (NSString *)[cursor objectAtIndex:13];
patient.emailAddress = (NSString *)[cursor objectAtIndex:14];
patient.databaseType = eLocal;
return patient;
}
@end