Improve Google Calendar Logic

This commit is contained in:
2022-08-04 13:36:24 +02:00
parent 61d6597578
commit 0c63d45dd6
6 changed files with 60 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
package de.jeyp91; package de.jeyp91;
import de.jeyp91.apifootball.APIFootballUpdater; import de.jeyp91.apifootball.APIFootballUpdater;
import de.jeyp91.googlecalendar.TippligaGoogleEventManager;
import de.jeyp91.tippligaforum.MatchesListForumUpdater; import de.jeyp91.tippligaforum.MatchesListForumUpdater;
import de.jeyp91.tippliga.*; import de.jeyp91.tippliga.*;
import de.jeyp91.tippligaforum.TippligaConfigProvider; import de.jeyp91.tippligaforum.TippligaConfigProvider;
@@ -47,6 +48,7 @@ public class App {
TLWMatchdaysUpdater matchdaysUpdater = new TLWMatchdaysUpdater(season, league, configFile); TLWMatchdaysUpdater matchdaysUpdater = new TLWMatchdaysUpdater(season, league, configFile);
sql = matchdaysUpdater.getUpdateSql(); sql = matchdaysUpdater.getUpdateSql();
beautifulInfo = matchdaysUpdater.getBeautifulInfo(); beautifulInfo = matchdaysUpdater.getBeautifulInfo();
matchdaysUpdater.updateGoogleCalendar();
break; break;
case "MatchesCreatorFootball": case "MatchesCreatorFootball":
TLWMatchesCreatorFootball creator = new TLWMatchesCreatorFootball(season, league, configFile); TLWMatchesCreatorFootball creator = new TLWMatchesCreatorFootball(season, league, configFile);

View File

@@ -13,6 +13,7 @@ import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -22,9 +23,9 @@ public class TippligaGoogleEventManager {
private static List<Event> allEvents = null; private static List<Event> allEvents = null;
private static List<Event> getAllEvents(Integer season) { public static List<Event> getAllEvents(Integer season) {
if(allEvents == null) { if(allEvents == null) {
allEvents = getAllEventsStartingFromDateTime(new DateTime(season-1 + "-01-01T00:00:00Z")); allEvents = getAllEventsStartingFromDateTime(new DateTime(season-1 + "-07-01T00:00:00Z"));
} }
return allEvents; return allEvents;
} }
@@ -50,15 +51,13 @@ public class TippligaGoogleEventManager {
} }
assert events != null; assert events != null;
List<Event> items = events.getItems(); return events.getItems();
return items;
} }
public static Event findEvent(List<Event> allEvents, Integer season, Integer league, Integer matchday, Integer deliveryDateNumber) { public static Event findEvent(List<Event> allEvents, Integer season, Integer league, Integer matchday, Integer deliveryDateNumber) {
String description = getDescription(season, league, matchday, deliveryDateNumber); String description = getDescription(season, league, matchday, deliveryDateNumber);
for(int i = 0; i < allEvents.size(); i++) { for (Event allEvent : allEvents) {
if(allEvents.get(i).getDescription().equals(description)) return allEvents.get(i); if (allEvent.getDescription().equals(description)) return allEvent;
} }
return null; return null;
} }
@@ -72,11 +71,13 @@ public class TippligaGoogleEventManager {
public static void createOrUpdateEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) { public static void createOrUpdateEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) {
List<Event> allEvents = getAllEvents(matchday.getSeason()); List<Event> allEvents = getAllEvents(matchday.getSeason());
Event event = findEvent(allEvents, matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber); Event event = findEvent(allEvents, matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber);
if(event == null) if(event == null) {
createNewEvent(matchday, deliveryDateString, deliverDateNumber); createNewEvent(matchday, deliveryDateString, deliverDateNumber);
else }
else {
updateEvent(event, matchday, deliveryDateString, deliverDateNumber); updateEvent(event, matchday, deliveryDateString, deliverDateNumber);
} }
}
private static void createNewEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) { private static void createNewEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) {
String calendarUrl = CalendarConfigProvider.getCalendarUrl(); String calendarUrl = CalendarConfigProvider.getCalendarUrl();
@@ -90,13 +91,14 @@ public class TippligaGoogleEventManager {
} }
} }
private static void updateEvent(Event oldEvent, TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) { public static void updateEvent(Event oldEvent, TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) {
String calendarUrl = CalendarConfigProvider.getCalendarUrl(); String calendarUrl = CalendarConfigProvider.getCalendarUrl();
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber); Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
try { try {
getSerivce().events().update(calendarUrl, oldEvent.getId(), event).execute(); Calendar.Events events = getSerivce().events();
events.update(calendarUrl, oldEvent.getId(), event).execute();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -149,4 +151,20 @@ public class TippligaGoogleEventManager {
private static String getDescription(Integer season, Integer league, Integer matchday, Integer deliverDateNumber) { private static String getDescription(Integer season, Integer league, Integer matchday, Integer deliverDateNumber) {
return "Saison: " + season + "\n" + "Liga: " + league + "\n" + "Spieltag: " + matchday + "\n" + "Abgabeschluss: " + deliverDateNumber; return "Saison: " + season + "\n" + "Liga: " + league + "\n" + "Spieltag: " + matchday + "\n" + "Abgabeschluss: " + deliverDateNumber;
} }
public static void updateAllMatchdays(ArrayList<TLWMatchday> matchdays) {
matchdays.forEach(tlwMatchday -> {
createOrUpdateEvent(tlwMatchday, tlwMatchday.getDeliveryDate(), 1);
if(tlwMatchday.getDeliveryDate2().equals("")) {
deleteEvent(tlwMatchday, 2);
} else {
createOrUpdateEvent(tlwMatchday, tlwMatchday.getDeliveryDate2(), 2);
}
if(tlwMatchday.getDeliveryDate3().equals("")) {
deleteEvent(tlwMatchday, 3);
} else {
createOrUpdateEvent(tlwMatchday, tlwMatchday.getDeliveryDate3(), 3);
}
});
}
} }

View File

@@ -74,7 +74,6 @@ public class TLWMatchdaysUpdater {
condition; condition;
this.beautifulInfo += beautifulInfoStart + this.beautifulInfo += beautifulInfoStart +
"Tippabgabeschluss 1 zu '" + matchdayUpdated.getDeliveryDate() + "'.\n"; "Tippabgabeschluss 1 zu '" + matchdayUpdated.getDeliveryDate() + "'.\n";
createOrUpdateEvent(matchdayUpdated, matchdayUpdated.getDeliveryDate(), 1);
} }
if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) { if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) {
@@ -83,10 +82,6 @@ public class TLWMatchdaysUpdater {
condition; condition;
this.beautifulInfo += beautifulInfoStart + this.beautifulInfo += beautifulInfoStart +
"Tippabgabeschluss 2 zu '" + matchdayUpdated.getDeliveryDate2() + "'.\n"; "Tippabgabeschluss 2 zu '" + matchdayUpdated.getDeliveryDate2() + "'.\n";
if(!matchdayUpdated.getDeliveryDate2().equals(""))
createOrUpdateEvent(matchdayUpdated, matchdayUpdated.getDeliveryDate2(), 2);
else
deleteEvent(matchdayUpdated, 2);
} }
if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) { if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) {
@@ -95,9 +90,6 @@ public class TLWMatchdaysUpdater {
condition; condition;
this.beautifulInfo += beautifulInfoStart + this.beautifulInfo += beautifulInfoStart +
"Spieltagsname zu '" + matchdayUpdated.getMatchdayName() + "'.\n"; "Spieltagsname zu '" + matchdayUpdated.getMatchdayName() + "'.\n";
createOrUpdateEvent(matchdayUpdated, matchdayUpdated.getDeliveryDate(), 1);
if(!matchdayUpdated.getDeliveryDate2().equals(""))
createOrUpdateEvent(matchdayUpdated, matchdayUpdated.getDeliveryDate2(), 2);
} }
} }
} }
@@ -106,6 +98,10 @@ public class TLWMatchdaysUpdater {
return updateSql; return updateSql;
} }
public void updateGoogleCalendar() {
updateAllMatchdays(matchdaysUpdated);
}
public String getBeautifulInfo() { public String getBeautifulInfo() {
return this.beautifulInfo; return this.beautifulInfo;
} }

View File

@@ -40,6 +40,22 @@ public class TippligaGoogleEventManagerTest {
matchdays.forEach(TippligaGoogleEventManager::createOrUpdateEventsForMatchday); matchdays.forEach(TippligaGoogleEventManager::createOrUpdateEventsForMatchday);
} }
@Test
public void findEventTest() {
List<Event> allEvents = getAllEvents(2023);
Event event = findEvent(allEvents, 2023, 1, 1, 2);
assertEquals(event.getStart(), 2);
}
@Test
public void updateEventTest() {
List<Event> allEvents = getAllEvents(2023);
Event event = findEvent(allEvents, 2023, 1, 1, 1);
TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-05 20:30:00", "", "", "1. Spieltag", 12);
updateEvent(event, matchday, matchday.getDeliveryDate(), 1);
assertEquals(event.size(), 18);
}
@Test @Test
public void createOrUpdateEventsForMatchdayWTLPokalTest() { public void createOrUpdateEventsForMatchdayWTLPokalTest() {
TLWMatchdaysCreator creator = new TLWMatchdaysCreator(2022, 48, "WTL-Pokal"); TLWMatchdaysCreator creator = new TLWMatchdaysCreator(2022, 48, "WTL-Pokal");
@@ -50,10 +66,9 @@ public class TippligaGoogleEventManagerTest {
} }
@Test @Test
public void updateEventTest() { public void createOrUpdateEventTest() {
TLWMatchdaysCreator creator = new TLWMatchdaysCreator(2022, 1, "Tippliga"); TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-06 20:30:00", "", "", "1. Spieltag", 12);
ArrayList<TLWMatchday> matchdays = creator.getMatchdays(); createOrUpdateEvent(matchday, matchday.getDeliveryDate(), 1);
createOrUpdateEvent(matchdays.get(28), matchdays.get(28).getDeliveryDate(), 1);
} }
@Test @Test

View File

@@ -23,4 +23,10 @@ public class TLWMatchdayUpdaterTest {
// System.out.println(sql); // System.out.println(sql);
// System.out.println(matchdaysUpdater.getBeautifulInfo()); // System.out.println(matchdaysUpdater.getBeautifulInfo());
} }
@Test
public void updateGoogleCalendarTest() {
TLWMatchdaysUpdater matchdaysUpdater = new TLWMatchdaysUpdater(2023, 1, "Tippliga");
matchdaysUpdater.updateGoogleCalendar();
}
} }

Binary file not shown.