diff --git a/src/main/java/de/jeyp91/googlecalendar/TippligaGoogleEventManager.java b/src/main/java/de/jeyp91/googlecalendar/TippligaGoogleEventManager.java index 7e110e4..f255523 100644 --- a/src/main/java/de/jeyp91/googlecalendar/TippligaGoogleEventManager.java +++ b/src/main/java/de/jeyp91/googlecalendar/TippligaGoogleEventManager.java @@ -4,10 +4,9 @@ import com.google.api.client.util.DateTime; import com.google.api.services.calendar.Calendar; import com.google.api.services.calendar.model.Event; import com.google.api.services.calendar.model.EventDateTime; -import com.google.api.services.calendar.model.EventReminder; import com.google.api.services.calendar.model.Events; -import de.jeyp91.tippliga.TLWLeague; import de.jeyp91.tippliga.TLWMatchday; +import de.jeyp91.tippligaforum.TippligaSQLConnector; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -16,7 +15,7 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashMap; import java.util.List; import static de.jeyp91.googlecalendar.GoogleCalendarConnector.getSerivce; @@ -26,6 +25,8 @@ public class TippligaGoogleEventManager { private static List allEvents = null; private static final Logger logger = LogManager.getLogger(TippligaGoogleEventManager.class); + private static final HashMap allLeagueNames = new HashMap<>(); + public static List getAllEvents(Integer season) { if(allEvents == null) { allEvents = getAllEventsStartingFromDateTime(new DateTime(season-1 + "-07-01T00:00:00Z")); @@ -144,7 +145,7 @@ public class TippligaGoogleEventManager { private static Event getEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) { String matchdayName = matchday.getMatchdayName().equals("") ? matchday.getMatchday().toString() + ". Spieltag" : matchday.getMatchdayName(); - String summary = TLWLeague.getLeagueNameCalendar(matchday.getLeague()) + " " + matchdayName + " tippen!"; + String summary = getLeagueNameCalendar(matchday.getSeason(), matchday.getLeague()) + " " + matchdayName + " tippen!"; if(deliverDateNumber > 1) summary += " " + deliverDateNumber + ". Chance \uD83D\uDE43"; String description = getDescription(matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber); String location = "https://tippliga-wuerzburg.de/app.php/football/bet"; @@ -195,4 +196,13 @@ public class TippligaGoogleEventManager { } }); } + + private static String getLeagueNameCalendar(Integer season, Integer league) { + String seasonLeagueId = season.toString() + league.toString(); + + if(!allLeagueNames.containsKey(seasonLeagueId)) { + allLeagueNames.put(seasonLeagueId, TippligaSQLConnector.getInstance().getLeague(season.toString(), league.toString()).getLeagueNameCalendar()); + } + return allLeagueNames.get(seasonLeagueId); + } } \ No newline at end of file diff --git a/src/main/java/de/jeyp91/tippliga/TLWLeague.java b/src/main/java/de/jeyp91/tippliga/TLWLeague.java index 18dd09a..bb43db4 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWLeague.java +++ b/src/main/java/de/jeyp91/tippliga/TLWLeague.java @@ -1,75 +1,50 @@ package de.jeyp91.tippliga; +import java.sql.ResultSet; +import java.sql.SQLException; + public class TLWLeague { - public static String getLeagueName(int id) { - String leagueName = ""; - switch (id) { - case 1: - leagueName = "Tippliga"; - break; - case 46: - leagueName = "Elfmeter"; - break; - case 47: - leagueName = "Relegation"; - break; - case 48: - leagueName = "WTL-Pokal"; - break; - case 49: - leagueName = "Liga-Cup"; - break; - default: break; + + private Integer season = null; + private Integer league = null; + private String leagueName = null; + private String leagueNameShort = null; + + public TLWLeague(ResultSet rset) { + final int SEASON = 1; + final int LEAGUE = 2; + final int LEAGUE_NAME = 3; + final int LEAGUE_NAME_SHORT = 4; + try { + this.season = Integer.parseInt(rset.getString(SEASON)); + this.league = Integer.parseInt(rset.getString(LEAGUE)); + this.leagueName = rset.getString(LEAGUE_NAME); + this.leagueNameShort = rset.getString(LEAGUE_NAME_SHORT); + } catch (SQLException e) { + e.printStackTrace(); } - return leagueName; } - public static String getLeagueNameShort(int id) { - String leagueName = ""; - switch (id) { - case 1: - leagueName = "1. TLW"; - break; - case 2: - leagueName = "2. TLW"; - break; - case 46: - leagueName = "ELF"; - break; - case 47: - leagueName = "REL"; - break; - case 48: - leagueName = "WTL"; - break; - case 49: - leagueName = "LC"; - break; - default: break; - } - return leagueName; + public Integer getSeason() { + return this.season; } - public static String getLeagueNameCalendar(int id) { - String leagueName = ""; - switch (id) { - case 1: - case 2: - leagueName = "Tippliga"; - break; - case 46: - leagueName = "Elfmeter"; - break; - case 47: - leagueName = "Relegation"; - break; - case 48: - leagueName = "WTL-Pokal"; - break; - case 49: - leagueName = "Liga Cup"; - break; - default: break; + public Integer getLeague() { + return this.league; + } + + public String getLeagueName() { + return this.leagueName; + } + + public String getLeagueNameShort() { + return this.leagueNameShort; + } + + public String getLeagueNameCalendar() { + String leagueName = this.leagueName; + if (leagueName.equals("1. Tippliga") || leagueName.equals("2. Tippliga")) { + leagueName = "Tippliga"; } return leagueName; } diff --git a/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java b/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java index 9b6e425..06ada02 100644 --- a/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java +++ b/src/main/java/de/jeyp91/tippligaforum/TippligaSQLConnector.java @@ -1,6 +1,7 @@ package de.jeyp91.tippligaforum; import de.jeyp91.StatusHolder; +import de.jeyp91.tippliga.TLWLeague; import de.jeyp91.tippliga.TLWMatch; import de.jeyp91.tippliga.TLWMatchday; import de.jeyp91.tippliga.TLWTeam; @@ -140,6 +141,20 @@ public class TippligaSQLConnector { return matchdays; } + public TLWLeague getLeague(String season, String league) { + String queryString = "SELECT * FROM `phpbb_footb_leagues` WHERE `season` = " + season + " AND `league` = " + league + ";"; + ResultSet rset = executeQuery(queryString); + TLWLeague tlwLeague = null; + try { + while (rset.next()) { + tlwLeague = new TLWLeague(rset); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + return tlwLeague; + } + public ArrayList getUpdatedMatchdaysBasedOnMatches(String season, String league) { String queryString = """ SELECT @@ -252,7 +267,7 @@ public class TippligaSQLConnector { public void updatePost(int postId, String postText) { String postChecksum = getChecksum(postText); - final long postEditTime = Instant.now().getEpochSecond();; + final long postEditTime = Instant.now().getEpochSecond(); final String postEditReason = "Update by tool."; int postEditUser = 2; String query = getPostUpdateQuery(postId, postText, postChecksum, postEditTime, postEditReason, postEditUser);