@Override public void importCSV(String inputFile) { try { String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile))); csvData = csvData.replaceAll("\\r", ""); CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n")); for (CSVRecord record : parser) { Schedule schedule = new Schedule(); schedule.setId(Integer.parseInt(record.get(0))); schedule.setMatchNum(Integer.parseInt(record.get(1))); schedule.setB1(Integer.parseInt(record.get(2))); schedule.setB2(Integer.parseInt(record.get(3))); schedule.setB3(Integer.parseInt(record.get(4))); schedule.setR1(Integer.parseInt(record.get(5))); schedule.setR2(Integer.parseInt(record.get(6))); schedule.setR3(Integer.parseInt(record.get(7))); if (checkForMatch(schedule)) update(schedule); else create(schedule); } } catch (IOException e) { e.printStackTrace(); } }
@Override public void update(Schedule schedule) { Field[] fields = Schedule.class.getDeclaredFields(); String valuesSting = "", SQL; List<Object> values = new ArrayList<>(); try { for (int i = 1; i < fields.length; i++) { fields[i].setAccessible(true); values.add(fields[i].get(schedule)); if (i == fields.length - 1) { valuesSting += fields[i].getName() + "=?"; } else { valuesSting += fields[i].getName() + "=?, "; } } } catch (IllegalAccessException e) { e.printStackTrace(); } SQL = "UPDATE schedule SET " + valuesSting + " WHERE id=" + schedule.getId() + ";"; jdbcTemplateObject.update(SQL, values.toArray()); }
@Override public boolean checkForMatch(Schedule schedule) { String SQL = "SELECT count(*) FROM schedule WHERE matchNum = ?"; Integer count = jdbcTemplateObject.queryForObject(SQL, Integer.class, schedule.getMatchNum()); return count != null && count > 0; }