diff options
author | Simon Rettberg | 2015-08-22 17:29:15 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-08-22 17:29:15 +0200 |
commit | 5b94675225077ed8c88f39d15279e920f267d954 (patch) | |
tree | 8b55ee6d7a8a881fe49695f23bc5234832a85031 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java | |
parent | [client] synced image details fetching (diff) | |
download | tutor-module-5b94675225077ed8c88f39d15279e920f267d954.tar.gz tutor-module-5b94675225077ed8c88f39d15279e920f267d954.tar.xz tutor-module-5b94675225077ed8c88f39d15279e920f267d954.zip |
[server] Foundations for the maintenance module
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java | 96 |
1 files changed, 80 insertions, 16 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index 5a002e76..23a71228 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -110,14 +110,15 @@ public class DbImage { public static LocalImageVersion getLocalImageData(String imageVersionId) throws TNotFoundException, SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement stmt = connection.prepareStatement("SELECT imageversionid, imagebaseid, filepath, filesize, createtime, isvalid" + MysqlStatement stmt = connection.prepareStatement("SELECT" + + " imageversionid, imagebaseid, filepath, filesize, createtime, expiretime, isvalid" + " FROM imageversion WHERE imageversionid = :imageversionid"); stmt.setString("imageversionid", imageVersionId); ResultSet rs = stmt.executeQuery(); if (!rs.next()) throw new TNotFoundException(); return new LocalImageVersion(rs.getString("imageversionid"), rs.getString("imagebaseid"), - rs.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), + rs.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), rs.getLong("expiretime"), rs.getBoolean("isvalid")); } catch (SQLException e) { LOGGER.error("Query failed in DbImage.getLocalImageData()", e); @@ -127,19 +128,40 @@ public class DbImage { protected static List<LocalImageVersion> getLocalImageVersions(MysqlConnection connection, String imageBaseId) throws SQLException { - MysqlStatement stmt = connection.prepareStatement("SELECT imageversionid, imagebaseid, filepath, filesize, createtime, isvalid" + MysqlStatement stmt = connection.prepareStatement("SELECT" + + " imageversionid, imagebaseid, filepath, filesize, createtime, expiretime, isvalid" + " FROM imageversion WHERE imagebaseid = :imagebaseid"); stmt.setString("imagebaseid", imageBaseId); ResultSet rs = stmt.executeQuery(); List<LocalImageVersion> list = new ArrayList<>(); while (rs.next()) { list.add(new LocalImageVersion(rs.getString("imageversionid"), rs.getString("imagebaseid"), - rs.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), + rs.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), rs.getLong("expiretime"), rs.getBoolean("isvalid"))); } return list; } + public static List<LocalImageVersion> getExpiringLocalImageVersions(int maxRemainingDays) throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("SELECT" + + " imageversionid, imagebaseid, filepath, filesize, createtime, expiretime, isvalid" + + " FROM imageversion WHERE expiretime < :deadline"); + stmt.setLong("deadline", (System.currentTimeMillis() / 1000) + (maxRemainingDays * 86400)); + ResultSet rs = stmt.executeQuery(); + List<LocalImageVersion> list = new ArrayList<>(); + while (rs.next()) { + list.add(new LocalImageVersion(rs.getString("imageversionid"), rs.getString("imagebaseid"), + rs.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), rs.getLong("expiretime"), + rs.getBoolean("isvalid"))); + } + return list; + } catch (SQLException e) { + LOGGER.error("Query failed in DbImage.getAllLocalImages()", e); + throw e; + } + } + /** * Private helper to create an {@link ImageSummaryRead} instance from a * {@link ResultSet} @@ -418,7 +440,7 @@ public class DbImage { LOGGER.error("Query failed in DbImage.markForDeletion()", e); throw e; } - updateLatestVersionAsync(imageVersionId, null); + updateLatestVersionAsync(imageVersionId); } public static void setShareMode(String imageBaseId, ImageBaseWrite newData) throws SQLException { @@ -474,39 +496,81 @@ public class DbImage { } } - public static void markValid(MysqlConnection connection, LocalImageVersion imageVersion, boolean valid) + public static void markValid(MysqlConnection connection, boolean valid, LocalImageVersion... imageVersion) throws SQLException { + if (imageVersion == null || imageVersion.length == 0) + return; MysqlStatement stmt = connection.prepareStatement("UPDATE imageversion SET isvalid = :valid" + " WHERE imageversionid = :imageversionid"); - stmt.setString("imageversionid", imageVersion.imageVersionId); - stmt.setBoolean("valid", valid); - stmt.executeUpdate(); + for (LocalImageVersion version : imageVersion) { + stmt.setString("imageversionid", version.imageVersionId); + stmt.setBoolean("valid", valid); + stmt.executeUpdate(); + } } - public static void markValid(LocalImageVersion imageVersion, boolean valid) throws SQLException { + public static void markValid(boolean valid, boolean async, LocalImageVersion... imageVersion) throws SQLException { + if (imageVersion == null || imageVersion.length == 0) + return; try (MysqlConnection connection = Database.getConnection()) { - markValid(connection, imageVersion, valid); + markValid(connection, valid, imageVersion); + if (!async) { + updateLatestVersion(connection, imageVersion); + } connection.commit(); } catch (SQLException e) { LOGGER.error("Query failed in DbImage.markInvalid()", e); throw e; } - updateLatestVersionAsync(imageVersion.imageVersionId, imageVersion.imageBaseId); + if (async) { + updateLatestVersionAsync(imageVersion); + } + } + + private static void updateLatestVersionAsync(final LocalImageVersion... changingVersion) { + if (changingVersion == null || changingVersion.length == 0) + return; + QuickTimer.scheduleOnce(new Task() { + @Override + public void fire() { + try (MysqlConnection connection = Database.getConnection()) { + updateLatestVersion(connection, changingVersion); + connection.commit(); + } catch (SQLException e) { + LOGGER.error("Query failed in DbImage.updateLatestVersionAsync()", e); + } + } + }); } - private static void updateLatestVersionAsync(final String changingImageVersionId, final String imageBaseId) { + private static void updateLatestVersionAsync(final String changingImageVersionId) { QuickTimer.scheduleOnce(new Task() { @Override public void fire() { try (MysqlConnection connection = Database.getConnection()) { - updateLatestVersion(connection, changingImageVersionId, imageBaseId); + updateLatestVersion(connection, changingImageVersionId, null); connection.commit(); } catch (SQLException | TNotFoundException e) { - LOGGER.error("Query failed in DbImage.updateLatestVersion()", e); + LOGGER.error("Query failed in DbImage.updateLatestVersionAsync()", e); } } }); } + + private static void updateLatestVersion(MysqlConnection connection, + LocalImageVersion... versions) throws + SQLException { + if (versions == null || versions.length == 0) + return; + for (LocalImageVersion version : versions) { + try { + updateLatestVersion(connection, version.imageVersionId, + version.imageBaseId); + } catch (TNotFoundException e) { + // Swallow - logging happens in called method + } + } + } /** * Makes sure the latestVersionId-field of the given base image is @@ -540,7 +604,7 @@ public class DbImage { if (versionFile.canRead() && versionFile.length() == version.fileSize) { newVersion = version; } else { - markValid(connection, version, false); + markValid(connection, false, version); } } } |