Improve matchdays updater
This commit is contained in:
@@ -44,11 +44,28 @@ public class TLWMatchdaysCreator {
|
||||
public ArrayList<TLWMatchday> getMatchdays() {
|
||||
ArrayList<TLWMatchday> matchdays = new ArrayList<>();
|
||||
|
||||
int matchdayCounter = 1;
|
||||
while(getMatchesForMatchday(matches, matchdayCounter).size() > 0) {
|
||||
ArrayList<TLWMatch> 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;
|
||||
|
||||
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<TLWMatch> matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber);
|
||||
Date firstMatchDate = null;
|
||||
for(TLWMatch match : matchesOfMatchday) {
|
||||
if(deliveryDate1 == null) {
|
||||
@@ -67,15 +84,12 @@ public class TLWMatchdaysCreator {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
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) {
|
||||
String queryString = "UPDATE `phpbb_footb_matches` "
|
||||
+ "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