Improve matchdays updater
This commit is contained in:
@@ -44,38 +44,52 @@ public class TLWMatchdaysCreator {
|
|||||||
public ArrayList<TLWMatchday> getMatchdays() {
|
public ArrayList<TLWMatchday> getMatchdays() {
|
||||||
ArrayList<TLWMatchday> matchdays = new ArrayList<>();
|
ArrayList<TLWMatchday> matchdays = new ArrayList<>();
|
||||||
|
|
||||||
int matchdayCounter = 1;
|
JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig");
|
||||||
while(getMatchesForMatchday(matches, matchdayCounter).size() > 0) {
|
for (Object matchdayConfig : matchdaysConfig) {
|
||||||
ArrayList<TLWMatch> matchesOfMatchday = getMatchesForMatchday(matches, matchdayCounter);
|
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 deliveryDate1 = null;
|
||||||
String deliveryDate2 = null;
|
String deliveryDate2 = null;
|
||||||
Date firstMatchDate = null;
|
|
||||||
for(TLWMatch match : matchesOfMatchday) {
|
JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig");
|
||||||
if(deliveryDate1 == null) {
|
JSONObject matchesConfigOne = (JSONObject) matchesConfig.get(0);
|
||||||
deliveryDate1 = match.getMatchDateTime();
|
if(matchesConfigOne.get("type").toString().equals("ToBeDefined")) {
|
||||||
}
|
deliveryDate1 = matchesConfigOne.get("placeholderDatetime").toString();
|
||||||
Date matchdate = null;
|
}
|
||||||
try {
|
else {
|
||||||
matchdate = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10));
|
ArrayList<TLWMatch> matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber);
|
||||||
} catch (java.text.ParseException e) {
|
Date firstMatchDate = null;
|
||||||
e.printStackTrace();
|
for(TLWMatch match : matchesOfMatchday) {
|
||||||
}
|
if(deliveryDate1 == null) {
|
||||||
if(firstMatchDate == null) {
|
deliveryDate1 = match.getMatchDateTime();
|
||||||
firstMatchDate = matchdate;
|
}
|
||||||
}
|
Date matchdate = null;
|
||||||
if(deliveryDate2 == null && getDaysDifference(firstMatchDate, matchdate) >= 1) {
|
try {
|
||||||
deliveryDate2 = match.getMatchDateTime();
|
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){
|
TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayNumber, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches);
|
||||||
numberOfMatches = matchesOfMatchday.size();
|
|
||||||
}
|
|
||||||
String matchdayName = getMatchdayNameFromConfig(matchdayCounter);
|
|
||||||
TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayCounter, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches);
|
|
||||||
matchdays.add(matchday);
|
matchdays.add(matchday);
|
||||||
matchdayCounter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return matchdays;
|
return matchdays;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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<TLWMatchday> matchdaysOriginal;
|
||||||
|
ArrayList<TLWMatchday> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,6 +89,21 @@ public class TippligaSQLConnector {
|
|||||||
return getMatches(queryString);
|
return getMatches(queryString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<TLWMatchday> getMatchdays(String season, String league) {
|
||||||
|
String queryString = "SELECT * FROM `phpbb_footb_matchdays` WHERE `season` = " + season + " AND `league` = " + league + ";";
|
||||||
|
|
||||||
|
ArrayList<TLWMatchday> 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) {
|
public void updateMatchDateTime(String season, String league, String matchNo, String datetime) {
|
||||||
String queryString = "UPDATE `phpbb_footb_matches` "
|
String queryString = "UPDATE `phpbb_footb_matches` "
|
||||||
+ "SET match_datetime = " + datetime
|
+ "SET match_datetime = " + datetime
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user