@@ -25,7 +25,7 @@ public class Parser {
25
25
26
26
// functions
27
27
public Parser () {
28
-
28
+
29
29
}
30
30
31
31
public Employee parseEmployee (String file ) throws Exception {
@@ -81,8 +81,9 @@ public Employee parseEmployee(String file) throws Exception {
81
81
return new Employee (name , onyen , capacity , gender .equals ("M" ) ? false : true , level , availability );
82
82
}
83
83
84
- public List <Schedule > parseSchedules (String scheduleFolderPath , String staff_dir , String leadsFile ) throws Exception {
85
-
84
+ public List <Schedule > parseSchedules (String scheduleFolderPath , String staff_dir , String leadsFile )
85
+ throws Exception {
86
+
86
87
List <Schedule > schedules = new ArrayList <Schedule >();
87
88
88
89
// read in the json file
@@ -105,18 +106,21 @@ public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir
105
106
json = scanner .nextLine ();
106
107
} catch (Exception e ) {
107
108
System .err .println (e .toString ());
108
- throw new Exception ("Parser::parseSchedule(): Error reading schedule file= " + scheduleJson .getAbsolutePath ());
109
+ throw new Exception (
110
+ "Parser::parseSchedule(): Error reading schedule file= " + scheduleJson .getAbsolutePath ());
109
111
}
110
112
scanner .close ();
111
113
112
114
// create the json week object
113
115
JsonWeek jsonweek = gson .fromJson (json , JsonWeek .class );
114
116
if (jsonweek == null ) {
115
- throw new Exception ("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath );
117
+ throw new Exception (
118
+ "Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath );
116
119
}
117
- if (jsonweek .getShifts () == null ){
120
+ if (jsonweek .getShifts () == null ) {
118
121
// some problem parsing json
119
- throw new Exception ("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath );
122
+ throw new Exception (
123
+ "Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath );
120
124
}
121
125
122
126
// now we have the json we need to reconstruct the schedule object
@@ -140,25 +144,34 @@ public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir
140
144
Employee employee = this .getEmployeeByOnyen (onyen , staff );
141
145
if (employee == null ) {
142
146
throw new Exception (
143
- "Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen );
147
+ "Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen="
148
+ + onyen );
144
149
}
145
150
// add the employee to the shift
146
151
shifts [day ][hour ].add (employee );
147
152
}
148
153
}
149
154
}
150
155
Leads leads = this .parseLeads (leadsFile , staff );
151
- schedules .add (new Schedule (staff , week , leads , scheduleJson .getName ().substring (9 , scheduleJson .getName ().length ()))); // grab just the date portion of file name
156
+ schedules .add (new Schedule (staff , week , leads ,
157
+ scheduleJson .getName ().substring (9 , scheduleJson .getName ().length ()).split ("\\ ." )[0 ])); // grab
158
+ // just
159
+ // the
160
+ // date
161
+ // portion
162
+ // of
163
+ // file
164
+ // name
152
165
}
153
-
166
+
154
167
if (schedules .size () == 0 ) {
155
- throw new Exception ("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath );
168
+ throw new Exception ("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath );
156
169
} else {
157
170
return schedules ;
158
171
}
159
172
}
160
-
161
- public void writeFile (Employee employee , String filename ) throws Exception {
173
+
174
+ public void writeFile (Employee employee , String filename ) throws Exception {
162
175
163
176
// check if employee is null first
164
177
if (employee == null ) {
@@ -168,7 +181,7 @@ public void writeFile(Employee employee, String filename) throws Exception{
168
181
// write to file
169
182
PrintWriter fw = null ;
170
183
try {
171
- System .out .println ("Writing Schedule to" + filename );
184
+ // System.out.println("Writing Schedule to" + filename);
172
185
fw = new PrintWriter (filename );
173
186
StringBuilder sb = new StringBuilder ();
174
187
@@ -216,20 +229,20 @@ public void writeFile(Employee employee, String filename) throws Exception{
216
229
fw .close ();
217
230
} catch (Exception e ) {
218
231
// unable to open file
219
- if (fw != null ){
232
+ if (fw != null ) {
220
233
fw .close ();
221
234
}
222
235
throw new Exception ("Parser::writeFile(): Unable to open file for writing. File=" + filename );
223
236
}
224
237
}
225
238
226
- private Staff parseStaff (String dir ) throws Exception {
239
+ private Staff parseStaff (String dir ) throws Exception {
227
240
// create staff object
228
241
Staff staff = new Staff ();
229
242
230
243
// get a file object to the directory containing all the csv's
231
244
File csvDirectory = new File (dir );
232
-
245
+
233
246
// create an employee from each csv
234
247
for (File csv : csvDirectory .listFiles ()) {
235
248
Employee employee = null ;
@@ -256,7 +269,7 @@ private Employee getEmployeeByOnyen(String onyen, Staff staff) {
256
269
// not able to find employee
257
270
return null ;
258
271
}
259
-
272
+
260
273
private Leads parseLeads (String leadsFile , Staff staff ) throws Exception {
261
274
BufferedReader reader = null ;
262
275
Leads leads = new Leads (staff );
@@ -279,41 +292,42 @@ private Leads parseLeads(String leadsFile, Staff staff) throws Exception {
279
292
}
280
293
281
294
public void writeScheduleToJson (List <Schedule > schedules , String path ) throws Exception {
282
- if (schedules .size () == 0 ){
295
+ if (schedules .size () == 0 ) {
283
296
throw new Exception ("Parser::writeScheduleToJson(): Schedule list is empty" );
284
297
}
285
- if (path .equals ("" ) || path == null ){
298
+ if (path .equals ("" ) || path == null ) {
286
299
throw new Exception ("Parser::writeScheduleToJson(): Path is invalid" );
287
300
}
288
-
301
+
289
302
// create gson object
290
303
for (Schedule schedule : schedules ) {
291
- try {
304
+ try {
292
305
Gson gson = new Gson ();
293
- JsonWeek jsonWeek = schedule .getWeek ().toJsonWeek ();
294
- BufferedOutputStream writer = new BufferedOutputStream (new FileOutputStream (new File (path + File .pathSeparator + "schedule-" + schedule .getDatesValid () + ".json" )), 65536 );
295
- String json = gson .toJson (jsonWeek );
296
- writer .write (json .getBytes ());
297
- writer .close ();
298
- } catch (Exception e ){
299
- throw new Exception ("Parser::writeScheduleToJson(): Unable to open file=" + path );
300
- }
306
+ JsonWeek jsonWeek = schedule .getWeek ().toJsonWeek ();
307
+ File f = new File (path + "schedule_" + schedule .getDatesValid () + ".json" );
308
+ BufferedOutputStream writer = new BufferedOutputStream (new FileOutputStream (f ), 65536 );
309
+ String json = gson .toJson (jsonWeek );
310
+ writer .write (json .getBytes ());
311
+ writer .close ();
312
+ } catch (Exception e ) {
313
+ throw new Exception ("Parser::writeScheduleToJson(): Unable to open file=" + path );
314
+ }
301
315
}
302
316
303
317
}
304
-
318
+
305
319
public String parseCurrentVersion (String versionPath ) {
306
320
BufferedReader reader = null ;
307
321
try {
308
322
reader = new BufferedReader (new FileReader (versionPath ));
309
323
} catch (FileNotFoundException e ) {
310
- return "" ; //if we can't find the file just carry on
324
+ return "" ; // if we can't find the file just carry on
311
325
}
312
326
String currentVersion = null ;
313
327
try {
314
328
currentVersion = reader .readLine ();
315
329
reader .close ();
316
- } catch (IOException e ) { //This should never happen
330
+ } catch (IOException e ) { // This should never happen
317
331
System .err .println ("lol this should never happen" );
318
332
e .printStackTrace ();
319
333
}
0 commit comments