diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java index 27bd5e3..6aaaef2 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java @@ -44,38 +44,52 @@ public class TLWMatchdaysCreator { public ArrayList getMatchdays() { ArrayList matchdays = new ArrayList<>(); - int matchdayCounter = 1; - while(getMatchesForMatchday(matches, matchdayCounter).size() > 0) { - ArrayList matchesOfMatchday = getMatchesForMatchday(matches, matchdayCounter); + JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig"); + for (Object matchdayConfig : matchdaysConfig) { + JSONObject matchdayConfigJson = (JSONObject) matchdayConfig; + + int matchdayNumber = Integer.parseInt(matchdayConfigJson.get("TLWMatchday").toString()); + String matchdayName = matchdayConfigJson.containsKey("matchdayName") ? matchdayConfigJson.get("matchdayName").toString() : ""; + + int numberOfMatches = this.matchesPerMatchday; + if(numberOfMatches == 0){ + numberOfMatches = Integer.parseInt(matchdayConfigJson.get("matchesPerMatchday").toString()); + } + String deliveryDate1 = null; String deliveryDate2 = null; - Date firstMatchDate = null; - for(TLWMatch match : matchesOfMatchday) { - if(deliveryDate1 == null) { - deliveryDate1 = match.getMatchDateTime(); - } - Date matchdate = null; - try { - matchdate = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10)); - } catch (java.text.ParseException e) { - e.printStackTrace(); - } - if(firstMatchDate == null) { - firstMatchDate = matchdate; - } - if(deliveryDate2 == null && getDaysDifference(firstMatchDate, matchdate) >= 1) { - deliveryDate2 = match.getMatchDateTime(); + + JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig"); + JSONObject matchesConfigOne = (JSONObject) matchesConfig.get(0); + if(matchesConfigOne.get("type").toString().equals("ToBeDefined")) { + deliveryDate1 = matchesConfigOne.get("placeholderDatetime").toString(); + } + else { + ArrayList matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber); + Date firstMatchDate = null; + for(TLWMatch match : matchesOfMatchday) { + if(deliveryDate1 == null) { + deliveryDate1 = match.getMatchDateTime(); + } + Date matchdate = null; + try { + matchdate = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10)); + } catch (java.text.ParseException e) { + e.printStackTrace(); + } + if(firstMatchDate == null) { + firstMatchDate = matchdate; + } + if(deliveryDate2 == null && getDaysDifference(firstMatchDate, matchdate) >= 1) { + deliveryDate2 = match.getMatchDateTime(); + } } } - int numberOfMatches = 0; - if(this.matchesPerMatchday == 0){ - numberOfMatches = matchesOfMatchday.size(); - } - String matchdayName = getMatchdayNameFromConfig(matchdayCounter); - TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayCounter, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches); + + TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayNumber, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches); matchdays.add(matchday); - matchdayCounter++; } + return matchdays; } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java index 6dc2553..d6550e4 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java @@ -1,2 +1,83 @@ -package de.jeyp91.tippliga;public class TLWMatchdaysUpdater { +package de.jeyp91.tippliga; + +import org.json.simple.JSONObject; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; + +public class TLWMatchdaysUpdater { + + int season; + int league; + ArrayList matchdaysOriginal; + ArrayList matchdaysUpdated; + + public TLWMatchdaysUpdater(int season, int league, String configPath) { + this.season = season; + this.league = league; + + TippligaSQLConnector conn = new TippligaSQLConnector(); + this.matchdaysOriginal = conn.getMatchdays(String.valueOf(season), String.valueOf(league)); + this.matchdaysOriginal.sort(Comparator.comparing(TLWMatchday::getMatchday)); + + TLWMatchdaysCreator creator = new TLWMatchdaysCreator(season, league, configPath); + this.matchdaysUpdated = creator.getMatchdays(); + this.matchdaysUpdated.sort(Comparator.comparing(TLWMatchday::getMatchday)); + } + + public String getUpdateSql() throws Exception { + String updateSql = ""; + + if(this.matchdaysUpdated.size() != this.matchdaysOriginal.size()) { + throw new Exception("Wrong matchdays config"); + } + + for(int i = 0; i < this.matchdaysUpdated.size(); i++) { + TLWMatchday matchdayOriginal = this.matchdaysOriginal.get(i); + TLWMatchday matchdayUpdated = this.matchdaysUpdated.get(i); + + if(matchdayOriginal.getMatchday() != matchdayUpdated.getMatchday()) { + throw new Exception("BUUUUG!"); + } + + Date now = new Date(System.currentTimeMillis()); + + Date earliestDate = null; + try { + earliestDate = new SimpleDateFormat("yyyy-MM-dd").parse(matchdayUpdated.getDeliveryDate().substring(0, 10)); + } catch (java.text.ParseException e) { + e.printStackTrace(); + } + + if(earliestDate.after(now)) { + + String updateStart = "UPDATE phpbb_footb_matches SET "; + String condition = "WHERE season = " + matchdayOriginal.getSeason() + " " + + "AND league = " + matchdayOriginal.getLeague() + " " + + "AND matchday = " + matchdayOriginal.getMatchday() + ";\n"; + + if (!matchdayOriginal.getDeliveryDate().equals(matchdayUpdated.getDeliveryDate())) { + updateSql += updateStart + + "delivery_date = '" + matchdayUpdated.getDeliveryDate() + "' " + + condition; + } + + if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) { + updateSql += updateStart + + "delivery_date_2 = '" + matchdayUpdated.getDeliveryDate2() + "' " + + condition; + } + + if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) { + updateSql += updateStart + + "matchday_name = '" + matchdayUpdated.getMatchdayName() + "' " + + condition; + } + } + } + + return updateSql; + } } diff --git a/src/main/java/de/jeyp91/tippliga/TippligaSQLConnector.java b/src/main/java/de/jeyp91/tippliga/TippligaSQLConnector.java index 277f07a..7fc7b3c 100644 --- a/src/main/java/de/jeyp91/tippliga/TippligaSQLConnector.java +++ b/src/main/java/de/jeyp91/tippliga/TippligaSQLConnector.java @@ -89,6 +89,21 @@ public class TippligaSQLConnector { return getMatches(queryString); } + public ArrayList getMatchdays(String season, String league) { + String queryString = "SELECT * FROM `phpbb_footb_matchdays` WHERE `season` = " + season + " AND `league` = " + league + ";"; + + ArrayList matchdays = new ArrayList<>(); + ResultSet rset = executeQuery(queryString); + try { + while (rset.next()) { + matchdays.add(new TLWMatchday(rset)); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + return matchdays; + } + public void updateMatchDateTime(String season, String league, String matchNo, String datetime) { String queryString = "UPDATE `phpbb_footb_matches` " + "SET match_datetime = " + datetime diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchdayUpdaterTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchdayUpdaterTest.java index 00e0b80..a67783a 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWMatchdayUpdaterTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchdayUpdaterTest.java @@ -1,2 +1,15 @@ -package de.jeyp91.tippliga;public class TLWMatchdayUpdaterTest { +package de.jeyp91.tippliga; + +import org.junit.Test; + +public class TLWMatchdayUpdaterTest { + + @Test + public void getUpdateSqlTest() throws Exception { + TLWMatchdaysUpdater matchdaysUpdater = new TLWMatchdaysUpdater(2021, 1, "Tippliga.json"); + + String sql = matchdaysUpdater.getUpdateSql(); + + System.out.println(sql); + } }