protected void readHeaderCell(String value) { XSSFCell cell = currentRow == null ? null : nextStringCell(); if (cell == null || !cell.getStringCellValue().equals(value)) { throw new IllegalStateException(currentPosition() + ": The cell (" + (cell == null ? null : cell.getStringCellValue()) + ") does not contain the expected value (" + value + ")."); } }
private Room extractRoom(Meeting meeting, Map<String, Room> roomMap) { String roomName = nextStringCell().getStringCellValue(); if (!roomName.isEmpty()) { Room room = roomMap.get(roomName); if (room == null) { throw new IllegalStateException(currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a roomName (" + roomName + ") that doesn't exist in the other sheet (Rooms)."); } return room; } return null; }
private Room extractRoom(Map<String, Room> roomMap, Talk talk) { String roomName = nextStringCell().getStringCellValue(); if (!roomName.isEmpty()) { Room room = roomMap.get(roomName); if (room == null) { throw new IllegalStateException(currentPosition() + ": The talk with code (" + talk.getCode() + ") has a roomName (" + roomName + ") that doesn't exist in the other sheet (Rooms)."); } return room; } return null; }
protected XSSFCell nextNumericCellOrBlank() { XSSFCell cell = nextCell(); if (cell.getCellTypeEnum() == CellType.BLANK || (cell.getCellTypeEnum() == CellType.STRING && cell.getStringCellValue().isEmpty())) { return null; } if (cell.getCellTypeEnum() == CellType.STRING) { throw new IllegalStateException(currentPosition() + ": The cell with value (" + cell.getStringCellValue() + ") has a string type but should be numeric."); } return cell; }
protected XSSFCell nextNumericCell() { XSSFCell cell = nextCell(); if (cell.getCellTypeEnum() == CellType.STRING) { throw new IllegalStateException(currentPosition() + ": The cell with value (" + cell.getStringCellValue() + ") has a string type but should be numeric."); } return cell; }
private Timeslot extractTimeslot(Map<Pair<LocalDateTime, LocalDateTime>, Timeslot> timeslotMap, Talk talk) { Timeslot assignedTimeslot; String dateString = nextStringCell().getStringCellValue(); String startTimeString = nextStringCell().getStringCellValue(); String endTimeString = nextStringCell().getStringCellValue(); if (!dateString.isEmpty() || !startTimeString.isEmpty() || !endTimeString.isEmpty()) { LocalDateTime startDateTime; LocalDateTime endDateTime; try { startDateTime = LocalDateTime.of(LocalDate.parse(dateString, DAY_FORMATTER), LocalTime.parse(startTimeString, TIME_FORMATTER)); endDateTime = LocalDateTime.of(LocalDate.parse(dateString, DAY_FORMATTER), LocalTime.parse(endTimeString, TIME_FORMATTER)); } catch (DateTimeParseException e) { throw new IllegalStateException(currentPosition() + ": The talk with code (" + talk.getCode() + ") has a timeslot date (" + dateString + "), startTime (" + startTimeString + ") and endTime (" + endTimeString + ") that doesn't parse as a date or time.", e); } assignedTimeslot = timeslotMap.get(Pair.of(startDateTime, endDateTime)); if (assignedTimeslot == null) { throw new IllegalStateException(currentPosition() + ": The talk with code (" + talk.getCode() + ") has a timeslot date (" + dateString + "), startTime (" + startTimeString + ") and endTime (" + endTimeString + ") that doesn't exist in the other sheet (Timeslots)."); } return assignedTimeslot; } return null; }
private TimeGrain extractTimeGrain(Meeting meeting, Map<LocalDateTime, TimeGrain> timeGrainMap) { String dateString = nextStringCell().getStringCellValue(); String startTimeString = nextStringCell().getStringCellValue(); if (!dateString.isEmpty() || !startTimeString.isEmpty()) { LocalDateTime dateTime; try { dateTime = LocalDateTime.of(LocalDate.parse(dateString, DAY_FORMATTER), LocalTime.parse(startTimeString, TIME_FORMATTER)); } catch (DateTimeParseException e) { throw new IllegalStateException(currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a timeGrain date (" + dateString + ") and startTime (" + startTimeString + ") that doesn't parse as a date or time.", e); } TimeGrain timeGrain = timeGrainMap.get(dateTime); if (timeGrain == null) { throw new IllegalStateException(currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a timeGrain date (" + dateString + ") and startTime (" + startTimeString + ") that doesn't exist in the other sheet (Day)."); } return timeGrain; } return null; }
private void readSpeakerList(Map<String, Person> personMap, Meeting meeting, List<Attendance> speakerAttendanceList, Set<Person> speakerSet) { meeting.setSpeakerList(Arrays.stream(nextStringCell().getStringCellValue().split(", ")) .filter(speaker -> !speaker.isEmpty()) .map(speakerName -> { Person speaker = personMap.get(speakerName); if (speaker == null) { throw new IllegalStateException( currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a speaker (" + speakerName + ") that doesn't exist in the Persons list."); } if (speakerSet.contains(speaker)) { throw new IllegalStateException( currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a duplicate speaker (" + speakerName + ")."); } speakerSet.add(speaker); RequiredAttendance speakerAttendance = new RequiredAttendance(); speakerAttendance.setMeeting(meeting); speakerAttendance.setPerson(speaker); speakerAttendanceList.add(speakerAttendance); return speaker; }).collect(toList())); }
private void readTimeslotDays() { columnIndexToDateMap.clear(); String previousDateString = null; for (int i = 0; i < currentRow.getLastCellNum(); i++) { XSSFCell cell = currentRow.getCell(i); if (!cell.getStringCellValue().isEmpty() || previousDateString != null) { if (!cell.getStringCellValue().isEmpty()) { previousDateString = cell.getStringCellValue(); } try { columnIndexToDateMap.put(i, LocalDate.parse(previousDateString, DAY_FORMATTER)); } catch (DateTimeParseException e) { throw new IllegalStateException(currentPosition() + ": The date (" + cell.getStringCellValue() + ") does not parse as a date."); } } } }
protected XSSFCell nextBooleanCell() { XSSFCell cell = nextCell(); if (cell.getCellTypeEnum() == CellType.STRING) { throw new IllegalStateException(currentPosition() + ": The cell with value (" + cell.getStringCellValue() + ") has a string type but should be boolean."); } if (cell.getCellTypeEnum() == CellType.NUMERIC) { throw new IllegalStateException(currentPosition() + ": The cell with value (" + cell.getNumericCellValue() + ") has a numeric type but should be a boolean."); } return cell; }
private List<RequiredAttendance> getRequiredAttendanceList(Meeting meeting, Map<String, Person> personMap, Set<Person> speakerSet, Set<Person> requiredPersonSet) { return Arrays.stream(nextStringCell().getStringCellValue().split(", ")) .filter(requiredAttendee -> !requiredAttendee.isEmpty()) .map(personName -> { RequiredAttendance requiredAttendance = new RequiredAttendance(); Person person = personMap.get(personName); if (person == null) { throw new IllegalStateException( currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a required attendee (" + personName + ") that doesn't exist in the Persons list."); } if (requiredPersonSet.contains(person)) { throw new IllegalStateException( currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a duplicate required attendee (" + personName + ")."); } if (speakerSet.contains(person)) { throw new IllegalStateException( currentPosition() + ": The meeting with id (" + meeting.getId() + ") has a required attendee (" + personName + ") who is also the speaker."); } requiredPersonSet.add(person); requiredAttendance.setMeeting(meeting); requiredAttendance.setPerson(person); return requiredAttendance; }) .collect(toList()); }
protected void readIntConstraintParameterLine(String name, Consumer<Integer> consumer, String constraintDescription) { nextRow(); readHeaderCell(name); XSSFCell weightCell = nextCell(); if (consumer != null) { if (weightCell.getCellTypeEnum() != CellType.NUMERIC) { throw new IllegalArgumentException(currentPosition() + ": The value (" + weightCell.getStringCellValue() + ") for constraint (" + name + ") must be a number and the cell type must be numeric."); } double value = weightCell.getNumericCellValue(); if (((double) ((int) value)) != value) { throw new IllegalArgumentException(currentPosition() + ": The value (" + value + ") for constraint (" + name + ") must be an integer."); } consumer.accept((int) value); } else { if (weightCell.getCellTypeEnum() == CellType.NUMERIC || !weightCell.getStringCellValue().equals("n/a")) { throw new IllegalArgumentException(currentPosition() + ": The value (" + weightCell.getStringCellValue() + ") for constraint (" + name + ") must be an n/a."); } } readHeaderCell(constraintDescription); }
protected void readLongConstraintParameterLine(String name, Consumer<Long> consumer, String constraintDescription) { nextRow(); readHeaderCell(name); XSSFCell weightCell = nextCell(); if (consumer != null) { if (weightCell.getCellTypeEnum() != CellType.NUMERIC) { throw new IllegalArgumentException(currentPosition() + ": The value (" + weightCell.getStringCellValue() + ") for constraint (" + name + ") must be a number and the cell type must be numeric."); } double value = weightCell.getNumericCellValue(); if (((double) ((long) value)) != value) { throw new IllegalArgumentException(currentPosition() + ": The value (" + value + ") for constraint (" + name + ") must be a (long) integer."); } consumer.accept((long) value); } else { if (weightCell.getCellTypeEnum() == CellType.NUMERIC || !weightCell.getStringCellValue().equals("n/a")) { throw new IllegalArgumentException(currentPosition() + ": The value (" + weightCell.getStringCellValue() + ") for constraint (" + name + ") must be an n/a."); } } readHeaderCell(constraintDescription); }
protected <Score_ extends Score<Score_>> Score_ readScoreConstraintLine( String constraintName, String constraintDescription) { nextRow(); readHeaderCell(constraintName); String scoreString = nextStringCell().getStringCellValue(); readHeaderCell(constraintDescription); return (Score_) scoreDefinition.parseScore(scoreString); }
private void readPersonList() { nextSheet("Persons"); nextRow(false); readHeaderCell("Full name"); List<Person> personList = new ArrayList<>(currentSheet.getLastRowNum() - 1); long id = 0L; while (nextRow()) { Person person = new Person(); person.setId(id++); person.setFullName(nextStringCell().getStringCellValue()); if (!VALID_NAME_PATTERN.matcher(person.getFullName()).matches()) { throw new IllegalStateException( currentPosition() + ": The person name (" + person.getFullName() + ") must match to the regular expression (" + VALID_NAME_PATTERN + ")."); } personList.add(person); } solution.setPersonList(personList); }
private void readSkillList() { nextSheet("Skills"); nextRow(false); readHeaderCell("Name"); List<Skill> skillList = new ArrayList<>(currentSheet.getLastRowNum() - 1); skillMap = new HashMap<>(currentSheet.getLastRowNum() - 1); long id = 0L; while (nextRow()) { Skill skill = new Skill(); skill.setId(id++); skill.setName(nextStringCell().getStringCellValue()); skillMap.put(skill.getName(), skill); skillList.add(skill); } solution.setSkillList(skillList); }
private void readRoomList() { nextSheet("Rooms"); nextRow(); readHeaderCell("Name"); readHeaderCell("Capacity"); List<Room> roomList = new ArrayList<>(currentSheet.getLastRowNum() - 1); long id = 0L; while (nextRow()) { Room room = new Room(); room.setId(id++); room.setName(nextStringCell().getStringCellValue()); if (!VALID_NAME_PATTERN.matcher(room.getName()).matches()) { throw new IllegalStateException( currentPosition() + ": The room name (" + room.getName() + ") must match to the regular expression (" + VALID_NAME_PATTERN + ")."); } double capacityDouble = nextNumericCell().getNumericCellValue(); if (capacityDouble <= 0 || capacityDouble != Math.floor(capacityDouble)) { throw new IllegalStateException( currentPosition() + ": The room with name (" + room.getName() + ") has a capacity (" + capacityDouble + ") that isn't a strictly positive integer number."); } room.setCapacity((int) capacityDouble); roomList.add(room); } solution.setRoomList(roomList); } }
private void readAirportList() { nextSheet("Airports"); nextRow(false); readHeaderCell("Code"); readHeaderCell("Name"); readHeaderCell("Latitude"); readHeaderCell("Longitude"); List<Airport> airportList = new ArrayList<>(currentSheet.getLastRowNum() - 1); airportMap = new HashMap<>(currentSheet.getLastRowNum() - 1); long id = 0L; while (nextRow()) { Airport airport = new Airport(); airport.setId(id++); airport.setCode(nextStringCell().getStringCellValue()); airport.setName(nextStringCell().getStringCellValue()); airport.setLatitude(nextNumericCell().getNumericCellValue()); airport.setLongitude(nextNumericCell().getNumericCellValue()); airportMap.put(airport.getCode(), airport); airportList.add(airport); } solution.setAirportList(airportList); }
private void readBus() { nextSheet("Bus"); RockBus bus = new RockBus(); bus.setId(0L); nextRow(); readHeaderCell(""); readHeaderCell("City name"); readHeaderCell("Latitude"); readHeaderCell("Longitude"); readHeaderCell("Date"); nextRow(); readHeaderCell("Bus start"); String startCityName = nextStringCell().getStringCellValue(); double startLatitude = nextNumericCell().getNumericCellValue(); double startLongitude = nextNumericCell().getNumericCellValue(); bus.setStartLocation(new RockLocation(startCityName, startLatitude, startLongitude)); bus.setStartDate(LocalDate.parse(nextStringCell().getStringCellValue(), DAY_FORMATTER)); nextRow(); readHeaderCell("Bus end"); String endCityName = nextStringCell().getStringCellValue(); double endLatitude = nextNumericCell().getNumericCellValue(); double endLongitude = nextNumericCell().getNumericCellValue(); bus.setEndLocation(new RockLocation(endCityName, endLatitude, endLongitude)); bus.setEndDate(LocalDate.parse(nextStringCell().getStringCellValue(), DAY_FORMATTER)); solution.setBus(bus); }
private void readConfiguration() { nextSheet("Configuration"); nextRow(false); readHeaderCell("Schedule start UTC Date"); solution.setScheduleFirstUTCDate(LocalDate.parse(nextStringCell().getStringCellValue(), DAY_FORMATTER)); nextRow(false); readHeaderCell("Schedule end UTC Date"); solution.setScheduleLastUTCDate(LocalDate.parse(nextStringCell().getStringCellValue(), DAY_FORMATTER)); nextRow(false); nextRow(false); readHeaderCell("Constraint"); readHeaderCell("Weight"); readHeaderCell("Description"); FlightCrewParametrization parametrization = new FlightCrewParametrization(); parametrization.setId(0L); readLongConstraintParameterLine(LOAD_BALANCE_FLIGHT_DURATION_TOTAL_PER_EMPLOYEE, parametrization::setLoadBalanceFlightDurationTotalPerEmployee, "Soft penalty per 0.001 minute difference with the average flight duration total per employee."); readIntConstraintParameterLine(REQUIRED_SKILL, null, "Hard penalty per missing required skill for a flight assignment"); readIntConstraintParameterLine(FLIGHT_CONFLICT, null, "Hard penalty per 2 flights of an employee that directly overlap"); readIntConstraintParameterLine(TRANSFER_BETWEEN_TWO_FLIGHTS, null, "Hard penalty per 2 sequential flights of an employee with no viable transfer from the arrival airport to the departure airport"); readIntConstraintParameterLine(EMPLOYEE_UNAVAILABILITY, null, "Hard penalty per flight assignment to an employee that is unavailable"); solution.setParametrization(parametrization); }