-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataReader.java
268 lines (234 loc) · 13.4 KB
/
DataReader.java
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
import java.io.FileReader;
import java.util.ArrayList;
import java.util.UUID;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class DataReader extends DataConstants {
/**
* Reads in user JSON file and populates array list of users
*
* @return Returns array list of users of type guardian, director, and staff
*/
public static ArrayList<User> getAllUsers() {
ArrayList<User> users = new ArrayList<User>();
try {
FileReader reader = new FileReader(USER_FILE_NAME);
JSONArray peopleJSON = (JSONArray)new JSONParser().parse(reader);
ArrayList<Child> childrenList = getChildren();
for(int i = 0; i < peopleJSON.size(); i++) {
JSONObject personJSON = (JSONObject)peopleJSON.get(i);
UUID id = UUID.fromString((String)personJSON.get(USER_ID));
String firstName = (String)personJSON.get(USER_FIRST_NAME);
String lastName = (String)personJSON.get(USER_LAST_NAME);
String email = (String)personJSON.get(USER_EMAIL);
String password = (String)personJSON.get(USER_PASSWORD);
String dateOfBirth = ((String)personJSON.get(USER_DOB));
int phoneNumber = ((Long)personJSON.get(USER_PHONE_NUMBER)).intValue();
String type = (String)personJSON.get(USER_TYPE);
if(type.equals("GuardianUser")) {
int campDuration = ((Long)personJSON.get(USER_CAMP_DURATION)).intValue();
JSONArray campDatesJSON = (JSONArray)personJSON.get(USER_CAMP_DATES);
ArrayList<String> campDates = new ArrayList<String>();
for(int j = 0; j < campDatesJSON.size(); j++) {
String campDate = campDatesJSON.get(j).toString();
campDates.add(campDate);
}
JSONArray childrenJSON = (JSONArray)personJSON.get(USER_CHILDREN);
ArrayList<Child> children = new ArrayList<Child>();
for(int j=0; j<childrenList.size(); j++) {
UUID childUUID = UUID.fromString((String)childrenJSON.get(j));
if(childrenList.get(j).getUUID() == childUUID) {
children.add(ChildList.getChild(childUUID));
break;
}
}
users.add(new GuardianUser(firstName, lastName, phoneNumber, email, password, dateOfBirth, id, children, campDuration, campDates));
}
else if(type.equals("DirectorUser")) {
users.add(new DirectorUser(firstName, lastName, phoneNumber, email, password, dateOfBirth, id));
}
else if(type.equals("StaffUser")) {
JSONObject doctorJSON = (JSONObject)personJSON.get(USER_DOCTOR);
String doctorFirstName = (String)doctorJSON.get(CONTACT_FIRST_NAME);
String doctorLastName = (String)doctorJSON.get(CONTACT_LAST_NAME);
int doctorPhoneNumber = ((Long)doctorJSON.get(CONTACT_PHONE_NUMBER)).intValue();
String doctorEmail = (String)doctorJSON.get(CONTACT_EMAIL);
Contact doctor = new Contact(doctorFirstName, doctorLastName, doctorPhoneNumber, doctorEmail);
JSONArray emergencyContactsJSON = (JSONArray)personJSON.get(USER_CONTACT);
ArrayList<Contact> emergencyContacts = new ArrayList<Contact>();
for(int j = 0; j < emergencyContactsJSON.size(); j++) {
JSONObject emergencyContactJSON = (JSONObject)emergencyContactsJSON.get(j);
//string name string int string
String contactFirstName = (String)emergencyContactJSON.get(CONTACT_FIRST_NAME);
String contactLastName = (String)emergencyContactJSON.get(CONTACT_LAST_NAME);
int contactPhoneNumber = ((Long)emergencyContactJSON.get(CONTACT_PHONE_NUMBER)).intValue();
String contactEmail = (String)emergencyContactJSON.get(CONTACT_EMAIL);
emergencyContacts.add(new Contact(contactFirstName, contactLastName, contactPhoneNumber, contactEmail));
}
JSONArray medicationsJSON = (JSONArray)personJSON.get(USER_MEDS);
ArrayList<Medication> medications = new ArrayList<Medication>();
for(int j = 0; j < medicationsJSON.size(); j++) {
JSONObject medicationJSON = (JSONObject)medicationsJSON.get(j);
String medType = (String)medicationJSON.get(MED_TYPE);
String medDose = (String)medicationJSON.get(MED_DOSE);
String medTime = (String)medicationJSON.get(MED_TIME);
medications.add(new Medication(medType, medDose, medTime));
}
JSONArray allergiesJSON = (JSONArray)personJSON.get(USER_ALLERGIES);
ArrayList<String> allergies = new ArrayList<String>();
for(int j = 0; j < allergiesJSON.size(); j++) {
String allergy = allergiesJSON.get(j).toString();
allergies.add(allergy);
}
users.add(new StaffUser(firstName, lastName, phoneNumber, email, password, dateOfBirth, id, doctor, emergencyContacts, medications, allergies));
}
}
return users;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Reads in child JSON file and populates array list of children
*
* @return Returns array list of children
*/
public static ArrayList<Child> getChildren() {
ArrayList<Child> children = new ArrayList<Child>();
try {
FileReader reader = new FileReader(CHILD_FILE_NAME);
JSONArray peopleJSON = (JSONArray)new JSONParser().parse(reader);
for(int i = 0; i < peopleJSON.size(); i++) {
JSONObject personJSON = (JSONObject)peopleJSON.get(i);
String firstName = (String)personJSON.get(CHILD_FIRST_NAME);
String lastName = (String)personJSON.get(CHILD_LAST_NAME);
String dateOfBirth = (String)personJSON.get(CHILD_DOB);
UUID id = UUID.fromString((String)personJSON.get(CHILD_ID));
JSONObject doctorJSON = (JSONObject)personJSON.get(CHILD_DOCTOR);
String doctorFirstName = (String)doctorJSON.get(CONTACT_FIRST_NAME);
String doctorLastName = (String)doctorJSON.get(CONTACT_LAST_NAME);
int doctorPhoneNumber = ((Long)doctorJSON.get(CONTACT_PHONE_NUMBER)).intValue();
String doctorEmail = (String)doctorJSON.get(CONTACT_EMAIL);
Contact doctor = new Contact(doctorFirstName, doctorLastName, doctorPhoneNumber, doctorEmail);
JSONArray emergencyContactsJSON = (JSONArray)personJSON.get(CHILD_CONTACTS);
ArrayList<Contact> emergencyContacts = new ArrayList<Contact>();
for(int j = 0; j < emergencyContactsJSON.size(); j++) {
JSONObject emergencyContactJSON = (JSONObject)emergencyContactsJSON.get(j);
String contactFirstName = (String)emergencyContactJSON.get(CONTACT_FIRST_NAME);
String contactLastName = (String)emergencyContactJSON.get(CONTACT_LAST_NAME);
int contactPhoneNumber = ((Long)emergencyContactJSON.get(CONTACT_PHONE_NUMBER)).intValue();
String contactEmail = (String)emergencyContactJSON.get(CONTACT_EMAIL);
emergencyContacts.add(new Contact(contactFirstName, contactLastName, contactPhoneNumber, contactEmail));
}
JSONArray medicationsJSON = (JSONArray)personJSON.get(CHILD_MEDS);
ArrayList<Medication> medications = new ArrayList<Medication>();
for(int j = 0; j < medicationsJSON.size(); j++) {
JSONObject medicationJSON = (JSONObject)medicationsJSON.get(j);
String type = (String)medicationJSON.get(MED_TYPE);
String dose = (String)medicationJSON.get(MED_DOSE);
String time = (String)medicationJSON.get(MED_TIME);
medications.add(new Medication(type, dose, time));
}
JSONArray allergiesJSON = (JSONArray)personJSON.get(CHILD_ALLERGIES);
ArrayList<String> allergies = new ArrayList<String>();
for(int j = 0; j < allergiesJSON.size(); j++) {
String allergy = allergiesJSON.get(j).toString();
allergies.add(allergy);
}
children.add(new Child(firstName, lastName, dateOfBirth, id, allergies, doctor, emergencyContacts, medications));
}
return children;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Reads in cabin JSON file and populates array list of cabins
*
* @return Returns array list of cabins
*/
public static ArrayList<Cabin> getCabins() {
ArrayList<Cabin> cabins = new ArrayList<Cabin>();
try {
FileReader reader = new FileReader(CABIN_FILE_NAME);
JSONArray cabinsJSON = (JSONArray)new JSONParser().parse(reader);
ArrayList<Child> childrenList = getChildren();
for(int i = 0; i < cabinsJSON.size(); i++) {
JSONObject cabinJSON = (JSONObject)cabinsJSON.get(i);
String name = (String)cabinJSON.get(CABIN_NAME);
UUID id = UUID.fromString((String)cabinJSON.get(CABIN_ID));
JSONArray childrenJSON = (JSONArray)cabinJSON.get(CABIN_CAMP_GROUP);
ArrayList<Child> camperGroup = new ArrayList<Child>();
for(int j = 0; j<childrenList.size(); j++) {
UUID childUUID = UUID.fromString((String)childrenJSON.get(j));
if(childrenList.get(j).getUUID() == childUUID) {
camperGroup.add(ChildList.getChild(childUUID));
break;
}
}
UUID staffUUID = UUID.fromString((String)cabinJSON.get(CABIN_STAFF_USER));
StaffUser staffUser = UserList.getStaffUser(staffUUID);
int minAge = ((Long)cabinJSON.get(CABIN_MIN_AGE)).intValue();
int maxAge = ((Long)cabinJSON.get(CABIN_MAX_AGE)).intValue();
//arraylist activities, day enum
JSONArray schedulesJSON = (JSONArray)cabinJSON.get(CABIN_SCHEDULE);
Schedule[] schedule = new Schedule[7];
for(int j = 0; j < schedulesJSON.size(); j++) {
JSONObject scheduleJSON = (JSONObject)schedulesJSON.get(j);
Day day = Day.valueOf((String)scheduleJSON.get(CABIN_DAY));
JSONArray activitiesJSON = (JSONArray)scheduleJSON.get(CABIN_ACTIVITIES);
ArrayList<Activity> activities = new ArrayList<Activity>();
for(int k=0; k < activitiesJSON.size(); k++) {
JSONObject activityJSON = (JSONObject)activitiesJSON.get(k);
Type type = Type.valueOf((String)activityJSON.get(ACTIVITY_TYPE));
String activityName = (String)activityJSON.get(ACTIVITY_NAME);
String time = (String)activityJSON.get(ACTIVITY_TIME);
activities.add(new Activity(type, activityName, time));
}
schedule[j] = new Schedule(activities, day);
}
cabins.add(new Cabin(name, id, staffUser, camperGroup, minAge, maxAge, schedule));
}
return cabins;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Reads in camp JSON file
*
* @return Returns camp
*/
public static Camp getCamp() {
try {
FileReader reader = new FileReader(CAMP_FILE_NAME);
JSONObject campJSON = (JSONObject)new JSONParser().parse(reader);
ArrayList<Cabin> cabinsList = getCabins();
String name = (String)campJSON.get(CAMP_NAME);
JSONArray sessionsJSON = (JSONArray)campJSON.get(CAMP_SESSIONS);
ArrayList<Session> sessions = new ArrayList<Session>();
for(int j=0; j<sessionsJSON.size(); j++) {
JSONObject sessionJSON = (JSONObject)new JSONParser().parse(reader);
String theme = (String)sessionJSON.get(CAMP_SESSION_THEME);
JSONArray cabinsJSON = (JSONArray)new JSONParser().parse(reader);
ArrayList<Cabin> cabins = new ArrayList<Cabin>();
for(int k=0; k<cabinsList.size(); k++) {
UUID cabinUUID = (UUID)cabinsJSON.get(j);
if(cabinsList.get(j).getUUID() == cabinUUID) {
cabins.add(CabinList.getCabin(cabinUUID));
break;
}
}
sessions.add(new Session(theme, cabins));
}
return new Camp(name, sessions);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}