Improve google event update logic and add logging
This commit is contained in:
@@ -7,7 +7,10 @@ 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.TLWMatch;
|
||||
import de.jeyp91.tippliga.TLWMatchday;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -22,6 +25,7 @@ import static de.jeyp91.googlecalendar.GoogleCalendarConnector.getSerivce;
|
||||
public class TippligaGoogleEventManager {
|
||||
|
||||
private static List<Event> allEvents = null;
|
||||
private static final Logger logger = LogManager.getLogger(TippligaGoogleEventManager.class);
|
||||
|
||||
public static List<Event> getAllEvents(Integer season) {
|
||||
if(allEvents == null) {
|
||||
@@ -75,7 +79,11 @@ public class TippligaGoogleEventManager {
|
||||
createNewEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||
}
|
||||
else {
|
||||
updateEvent(event, matchday, deliveryDateString, deliverDateNumber);
|
||||
String googleEventStart = event.getStart().getDateTime().toString();
|
||||
String googleEventStartBeautified = googleEventStart.substring(0, 19).replace("T", " ");
|
||||
if(!googleEventStartBeautified.equals(deliveryDateString)) {
|
||||
updateEvent(event, matchday, deliveryDateString, deliverDateNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +93,7 @@ public class TippligaGoogleEventManager {
|
||||
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||
|
||||
try {
|
||||
logger.info("Create new google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||
getSerivce().events().insert(calendarUrl, event).execute();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -97,6 +106,7 @@ public class TippligaGoogleEventManager {
|
||||
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||
|
||||
try {
|
||||
logger.info("Update google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||
Calendar.Events events = getSerivce().events();
|
||||
events.update(calendarUrl, oldEvent.getId(), event).execute();
|
||||
} catch (IOException e) {
|
||||
@@ -110,6 +120,7 @@ public class TippligaGoogleEventManager {
|
||||
Event event = findEvent(getAllEvents(matchday.getSeason()), matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber);
|
||||
if(event != null) {
|
||||
try {
|
||||
logger.info("Delete google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||
getSerivce().events().delete(calendarUrl, event.getId()).execute();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -117,6 +128,14 @@ public class TippligaGoogleEventManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteAllEvents(int season) throws IOException {
|
||||
String calendarUrl = CalendarConfigProvider.getCalendarUrl();
|
||||
List<Event> events = getAllEvents(season);
|
||||
for(Event event : events ) {
|
||||
getSerivce().events().delete(calendarUrl, event.getId()).execute();
|
||||
}
|
||||
}
|
||||
|
||||
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!";
|
||||
|
||||
@@ -6,6 +6,7 @@ import de.jeyp91.tippliga.TLWMatchday;
|
||||
import de.jeyp91.tippliga.TLWMatchdaysCreator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -94,4 +95,9 @@ public class TippligaGoogleEventManagerTest {
|
||||
TippligaGoogleEventManager.deleteEvent(tlwMatchday, 3);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteAllEventsTest() throws IOException {
|
||||
TippligaGoogleEventManager.deleteAllEvents(2023);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user