|
7 | 7 | import java.io.FileOutputStream;
|
8 | 8 | import java.io.FileReader;
|
9 | 9 | import java.io.IOException;
|
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
10 | 12 | import java.util.Scanner;
|
11 | 13 | import com.google.gson.Gson;
|
12 | 14 | import java.io.PrintWriter;
|
@@ -79,63 +81,75 @@ public Employee parseEmployee(String file) throws Exception {
|
79 | 81 | return new Employee(name, onyen, capacity, gender.equals("M") ? false : true, level, availability);
|
80 | 82 | }
|
81 | 83 |
|
82 |
| - public Schedule parseSchedule(String jsonFile, String staff_dir, String leadsFile) throws Exception { |
| 84 | + public List<Schedule> parseSchedule(String scheduleFolderPath, String staff_dir, String leadsFile) throws Exception { |
| 85 | + |
| 86 | + List<Schedule> schedules = new ArrayList<Schedule>(); |
83 | 87 |
|
84 | 88 | // read in the json file
|
85 | 89 | Gson gson = new Gson();
|
86 | 90 | String json = "";
|
87 | 91 |
|
88 |
| - Scanner scanner = null; |
| 92 | + File scheduleFile = null; |
89 | 93 | try {
|
90 |
| - File file = new File(jsonFile); |
91 |
| - scanner = new Scanner(file); |
| 94 | + scheduleFile = new File(scheduleFolderPath); |
| 95 | + |
| 96 | + } catch (Exception e) { |
| 97 | + throw new Exception("Parser::parseSchedule(): Error reading schedule folder=" + scheduleFolderPath); |
| 98 | + } |
| 99 | + |
| 100 | + for (File scheduleJson : scheduleFile.listFiles()) { |
| 101 | + Scanner scanner = new Scanner(scheduleFile); |
92 | 102 | scanner.useDelimiter("\\Z");
|
93 | 103 | json = scanner.next();
|
94 | 104 | scanner.close();
|
95 |
| - } catch (FileNotFoundException e) { |
96 |
| - throw new Exception("Parser::parseSchedule(): Error reading json file=" + jsonFile); |
97 |
| - } |
98 | 105 |
|
99 |
| - // create the json week object |
100 |
| - JsonWeek jsonweek = gson.fromJson(json, JsonWeek.class); |
101 |
| - if (jsonweek == null) { |
102 |
| - throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + jsonFile); |
103 |
| - } |
104 |
| - if (jsonweek.getShifts() == null){ |
105 |
| - // some problem parsing json |
106 |
| - throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + jsonFile); |
107 |
| - } |
| 106 | + // create the json week object |
| 107 | + JsonWeek jsonweek = gson.fromJson(json, JsonWeek.class); |
| 108 | + if (jsonweek == null) { |
| 109 | + throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath); |
| 110 | + } |
| 111 | + if (jsonweek.getShifts() == null){ |
| 112 | + // some problem parsing json |
| 113 | + throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath); |
| 114 | + } |
108 | 115 |
|
109 |
| - // now we have the json we need to reconstruct the schedule object |
110 |
| - // first we will build the staff object |
111 |
| - Staff staff = null; |
112 |
| - try { |
113 |
| - staff = parseStaff(staff_dir); |
114 |
| - } catch (Exception e) { |
115 |
| - throw new Exception("Parser::parseSchedule(): " + e.toString()); |
116 |
| - } |
| 116 | + // now we have the json we need to reconstruct the schedule object |
| 117 | + // first we will build the staff object |
| 118 | + Staff staff = null; |
| 119 | + try { |
| 120 | + staff = parseStaff(staff_dir); |
| 121 | + } catch (Exception e) { |
| 122 | + throw new Exception("Parser::parseSchedule(): " + e.toString()); |
| 123 | + } |
117 | 124 |
|
118 |
| - // now build the week object |
119 |
| - Week week = new Week("Current Schedule"); |
120 |
| - |
121 |
| - // grab a reference to the shifts array |
122 |
| - Shift[][] shifts = week.getShifts(); |
123 |
| - for (int day = 0; day < NUMBER_DAYS; day++) { |
124 |
| - for (int hour = 0; hour < NUMBER_HOURS; hour++) { |
125 |
| - for (int i = 0; i < jsonweek.getShifts()[day][hour].getScheduled().length; i++) { |
126 |
| - String onyen = jsonweek.getShifts()[day][hour].getScheduled()[i]; |
127 |
| - Employee employee = this.getEmployeeByOnyen(onyen, staff); |
128 |
| - if (employee == null) { |
129 |
| - throw new Exception( |
130 |
| - "Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen); |
| 125 | + // now build the week object |
| 126 | + Week week = new Week("Current Schedule"); |
| 127 | + |
| 128 | + // grab a reference to the shifts array |
| 129 | + Shift[][] shifts = week.getShifts(); |
| 130 | + for (int day = 0; day < NUMBER_DAYS; day++) { |
| 131 | + for (int hour = 0; hour < NUMBER_HOURS; hour++) { |
| 132 | + for (int i = 0; i < jsonweek.getShifts()[day][hour].getScheduled().length; i++) { |
| 133 | + String onyen = jsonweek.getShifts()[day][hour].getScheduled()[i]; |
| 134 | + Employee employee = this.getEmployeeByOnyen(onyen, staff); |
| 135 | + if (employee == null) { |
| 136 | + throw new Exception( |
| 137 | + "Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen); |
| 138 | + } |
| 139 | + // add the employee to the shift |
| 140 | + shifts[day][hour].add(employee); |
131 | 141 | }
|
132 |
| - // add the employee to the shift |
133 |
| - shifts[day][hour].add(employee); |
134 | 142 | }
|
135 | 143 | }
|
| 144 | + Leads leads = this.parseLeads(leadsFile, staff); |
| 145 | + schedules.add(new Schedule(staff, week, leads, scheduleJson.getName().substring(9, scheduleJson.getName().length()))); // grab just the date portion of file name |
| 146 | + } |
| 147 | + |
| 148 | + if (schedules.size() == 0) { |
| 149 | + throw new Exception ("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath); |
| 150 | + } else { |
| 151 | + return schedules; |
136 | 152 | }
|
137 |
| - Leads leads = this.parseLeads(leadsFile, staff); |
138 |
| - return new Schedule(staff, week, leads); |
139 | 153 | }
|
140 | 154 |
|
141 | 155 | public void writeFile(Employee employee, String filename) throws Exception{
|
@@ -258,25 +272,28 @@ private Leads parseLeads(String leadsFile, Staff staff) throws Exception {
|
258 | 272 | return leads;
|
259 | 273 | }
|
260 | 274 |
|
261 |
| - public void writeScheduleToJson(Schedule schedule, String path) throws Exception{ |
262 |
| - if (schedule == null){ |
263 |
| - throw new Exception("Parser::writeScheduleToJson(): Schedule is null"); |
| 275 | + public void writeScheduleToJson(List<Schedule> schedules, String path) throws Exception { |
| 276 | + if (schedules.size() == 0){ |
| 277 | + throw new Exception("Parser::writeScheduleToJson(): Schedule list is empty"); |
264 | 278 | }
|
265 | 279 | if (path.equals("") || path == null){
|
266 | 280 | throw new Exception("Parser::writeScheduleToJson(): Path is invalid");
|
267 | 281 | }
|
268 | 282 |
|
269 | 283 | // create gson object
|
270 |
| - try{ |
271 |
| - Gson gson = new Gson(); |
272 |
| - JsonWeek jsonWeek = schedule.getWeek().toJsonWeek(); |
273 |
| - BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path)), 65536); |
274 |
| - String json = gson.toJson(jsonWeek); |
275 |
| - writer.write(json.getBytes()); |
276 |
| - writer.close(); |
277 |
| - } catch (Exception e){ |
278 |
| - throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path); |
| 284 | + for (Schedule schedule : schedules) { |
| 285 | + try{ |
| 286 | + Gson gson = new Gson(); |
| 287 | + JsonWeek jsonWeek = schedule.getWeek().toJsonWeek(); |
| 288 | + BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path + File.pathSeparator + "schedule-" + schedule.getDatesValid() + ".json")), 65536); |
| 289 | + String json = gson.toJson(jsonWeek); |
| 290 | + writer.write(json.getBytes()); |
| 291 | + writer.close(); |
| 292 | + } catch (Exception e){ |
| 293 | + throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path); |
| 294 | + } |
279 | 295 | }
|
| 296 | + |
280 | 297 | }
|
281 | 298 |
|
282 | 299 | public String parseCurrentVersion(String versionPath) {
|
|
0 commit comments