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.EventReminder;
|
||||||
import com.google.api.services.calendar.model.Events;
|
import com.google.api.services.calendar.model.Events;
|
||||||
import de.jeyp91.tippliga.TLWLeague;
|
import de.jeyp91.tippliga.TLWLeague;
|
||||||
|
import de.jeyp91.tippliga.TLWMatch;
|
||||||
import de.jeyp91.tippliga.TLWMatchday;
|
import de.jeyp91.tippliga.TLWMatchday;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@@ -22,6 +25,7 @@ import static de.jeyp91.googlecalendar.GoogleCalendarConnector.getSerivce;
|
|||||||
public class TippligaGoogleEventManager {
|
public class TippligaGoogleEventManager {
|
||||||
|
|
||||||
private static List<Event> allEvents = null;
|
private static List<Event> allEvents = null;
|
||||||
|
private static final Logger logger = LogManager.getLogger(TippligaGoogleEventManager.class);
|
||||||
|
|
||||||
public static List<Event> getAllEvents(Integer season) {
|
public static List<Event> getAllEvents(Integer season) {
|
||||||
if(allEvents == null) {
|
if(allEvents == null) {
|
||||||
@@ -75,7 +79,11 @@ public class TippligaGoogleEventManager {
|
|||||||
createNewEvent(matchday, deliveryDateString, deliverDateNumber);
|
createNewEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||||
}
|
}
|
||||||
else {
|
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);
|
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
logger.info("Create new google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||||
getSerivce().events().insert(calendarUrl, event).execute();
|
getSerivce().events().insert(calendarUrl, event).execute();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -97,6 +106,7 @@ public class TippligaGoogleEventManager {
|
|||||||
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
|
Event event = getEvent(matchday, deliveryDateString, deliverDateNumber);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
logger.info("Update google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||||
Calendar.Events events = getSerivce().events();
|
Calendar.Events events = getSerivce().events();
|
||||||
events.update(calendarUrl, oldEvent.getId(), event).execute();
|
events.update(calendarUrl, oldEvent.getId(), event).execute();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -110,6 +120,7 @@ public class TippligaGoogleEventManager {
|
|||||||
Event event = findEvent(getAllEvents(matchday.getSeason()), matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber);
|
Event event = findEvent(getAllEvents(matchday.getSeason()), matchday.getSeason(), matchday.getLeague(), matchday.getMatchday(), deliverDateNumber);
|
||||||
if(event != null) {
|
if(event != null) {
|
||||||
try {
|
try {
|
||||||
|
logger.info("Delete google calendar event for " + matchday.getSeason() + ", " + matchday.getLeague() + ", " + matchday.getMatchday() + ", " + deliverDateNumber);
|
||||||
getSerivce().events().delete(calendarUrl, event.getId()).execute();
|
getSerivce().events().delete(calendarUrl, event.getId()).execute();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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) {
|
private static Event getEvent(TLWMatchday matchday, String deliveryDateString, Integer deliverDateNumber) {
|
||||||
String matchdayName = matchday.getMatchdayName().equals("") ? matchday.getMatchday().toString() + ". Spieltag" : matchday.getMatchdayName();
|
String matchdayName = matchday.getMatchdayName().equals("") ? matchday.getMatchday().toString() + ". Spieltag" : matchday.getMatchdayName();
|
||||||
String summary = TLWLeague.getLeagueNameCalendar(matchday.getLeague()) + " " + matchdayName + " tippen!";
|
String summary = TLWLeague.getLeagueNameCalendar(matchday.getLeague()) + " " + matchdayName + " tippen!";
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import de.jeyp91.tippliga.TLWMatchday;
|
|||||||
import de.jeyp91.tippliga.TLWMatchdaysCreator;
|
import de.jeyp91.tippliga.TLWMatchdaysCreator;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -94,4 +95,9 @@ public class TippligaGoogleEventManagerTest {
|
|||||||
TippligaGoogleEventManager.deleteEvent(tlwMatchday, 3);
|
TippligaGoogleEventManager.deleteEvent(tlwMatchday, 3);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deleteAllEventsTest() throws IOException {
|
||||||
|
TippligaGoogleEventManager.deleteAllEvents(2023);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user