added auto reimport feature

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-25 23:32:18 +01:00
parent c90c28dd60
commit 57e248c79a
4 changed files with 95 additions and 7 deletions

View File

@ -4,6 +4,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import xyz.vallat.louis.managers.database.DBManager; import xyz.vallat.louis.managers.database.DBManager;
import xyz.vallat.louis.managers.discord.DiscordManager; import xyz.vallat.louis.managers.discord.DiscordManager;
import xyz.vallat.louis.timertasks.AutoReimport;
import java.util.Calendar;
import java.util.Timer;
import java.util.concurrent.TimeUnit;
public class App { public class App {
@ -18,6 +23,7 @@ public class App {
DBManager.testConnection(); DBManager.testConnection();
DBManager.initializeDatabase(); DBManager.initializeDatabase();
DiscordManager.login(); DiscordManager.login();
setTimers();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger.info("Received shut down signal. Bye!"); logger.info("Received shut down signal. Bye!");
@ -26,4 +32,15 @@ public class App {
DiscordManager.onDisconnect(); DiscordManager.onDisconnect();
} }
private static void setTimers() {
Calendar midday = Calendar.getInstance();
midday.set(Calendar.HOUR_OF_DAY, 12);
midday.set(Calendar.MINUTE, 30);
midday.set(Calendar.SECOND, 0);
Timer reimportCron = new Timer();
reimportCron.schedule(new AutoReimport(), midday.getTime(),
TimeUnit.MILLISECONDS.convert(12, TimeUnit.HOURS));
}
} }

View File

@ -8,8 +8,7 @@ import xyz.vallat.louis.managers.database.dao.Student;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.util.HashMap;
import java.time.ZoneOffset;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,16 +19,49 @@ public final class EventManager {
private EventManager() { private EventManager() {
} }
public static int reimportAllEvents() {
int importedEvents;
try (Connection connection = DBManager.getConnection()) {
connection.setAutoCommit(false);
flushEvents(connection);
Map<Student, List<VEvent>> studentEvents = new HashMap<>();
StudentManager.getStudentsFromDatabase()
.forEach(s -> studentEvents.put(s, CalendarManager.getEventsFromResource(s.getAde())));
importedEvents = importEvents(studentEvents);
if (importedEvents == 0) {
logger.error("No events were re-imported. Rolling back.");
connection.rollback();
} else {
try {
connection.commit();
} catch (SQLException e) {
connection.rollback();
}
}
} catch (SQLException e) {
logger.error("SQLError:", e);
return 0;
}
return importedEvents;
}
private static void flushEvents(Connection connection) throws SQLException {
String sql = "TRUNCATE events RESTART IDENTITY;";
try (Statement stmt = connection.createStatement()) {
stmt.executeUpdate(sql);
}
}
public static int importEvents(Map<Student, List<VEvent>> events) { public static int importEvents(Map<Student, List<VEvent>> events) {
int imported_events = 0; int importedEvents = 0;
try (Connection connection = DBManager.getConnection()) { try (Connection connection = DBManager.getConnection()) {
connection.setAutoCommit(false); connection.setAutoCommit(false);
for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) { for (Map.Entry<Student, List<VEvent>> e : events.entrySet()) {
if (e.getValue().isEmpty()) continue; if (e.getValue().isEmpty()) continue;
e.getKey().setId(StudentManager if (e.getKey().getId() == 0) e.getKey().setId(StudentManager
.addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection)); .addStudent(e.getKey().getSnowflake(), e.getKey().getAde(), connection));
for (VEvent event : e.getValue()) for (VEvent event : e.getValue())
if (importEvent(e.getKey(), event, connection) != 0) imported_events++; if (importEvent(e.getKey(), event, connection) != 0) importedEvents++;
} }
try { try {
connection.commit(); connection.commit();
@ -38,10 +70,10 @@ public final class EventManager {
return 0; return 0;
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Couldn't import events.", e); logger.error("SQLError while importing events.", e);
return 0; return 0;
} }
return imported_events; return importedEvents;
} }
private static int importEvent(Student student, VEvent event, Connection connection) throws SQLException { private static int importEvent(Student student, VEvent event, Connection connection) throws SQLException {

View File

@ -8,6 +8,8 @@ import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public final class StudentManager { public final class StudentManager {
@ -62,6 +64,25 @@ public final class StudentManager {
return null; return null;
} }
public static List<Student> getStudentsFromDatabase() {
List<Student> studentList = new ArrayList<>();
try (Connection connection = DBManager.getConnection()) {
String sql = "SELECT id, snowflake, ade_resource FROM students;";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
while (stmt.getResultSet().next()) {
studentList.add(new Student(stmt.getResultSet().getString("snowflake"),
stmt.getResultSet().getInt("ade_resource"))
.setId(stmt.getResultSet().getInt("id")));
}
}
} catch (SQLException e) {
logger.error("SQLException while getting all students.", e);
return new ArrayList<>();
}
return studentList;
}
public static boolean deleteFromSnowflake(String snowflake) { public static boolean deleteFromSnowflake(String snowflake) {
logger.debug("Deleting student {}.", snowflake); logger.debug("Deleting student {}.", snowflake);
try (Connection connection = DBManager.getConnection()) { try (Connection connection = DBManager.getConnection()) {

View File

@ -0,0 +1,18 @@
package xyz.vallat.louis.timertasks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.vallat.louis.managers.database.EventManager;
import java.util.TimerTask;
public class AutoReimport extends TimerTask {
private static final Logger logger = LoggerFactory.getLogger(AutoReimport.class.getCanonicalName());
@Override
public void run() {
logger.info("Reimporting all events.");
logger.info("Done reimporting {} events.", EventManager.reimportAllEvents());
}
}