diff options
author | Simon Rettberg | 2015-08-21 20:26:09 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-08-21 20:26:09 +0200 |
commit | 1f8d2edf172c35b2396e568383d9e461c058a1fa (patch) | |
tree | 90662568b358a6c06df5533fbf67a236ed85fda3 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java | |
parent | [client] tried to fix teh broken (diff) | |
download | tutor-module-1f8d2edf172c35b2396e568383d9e461c058a1fa.tar.gz tutor-module-1f8d2edf172c35b2396e568383d9e461c058a1fa.tar.xz tutor-module-1f8d2edf172c35b2396e568383d9e461c058a1fa.zip |
[server] Implement missing lecture thrift calls, clean up transfer handling a bit
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 | 62 |
1 files changed, 45 insertions, 17 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 a6b6d4b8..5a002e76 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 @@ -1,5 +1,6 @@ package org.openslx.bwlp.sat.database.mappers; +import java.io.File; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -12,6 +13,7 @@ import org.openslx.bwlp.sat.database.MysqlConnection; import org.openslx.bwlp.sat.database.MysqlStatement; import org.openslx.bwlp.sat.database.Paginator; import org.openslx.bwlp.sat.database.models.LocalImageVersion; +import org.openslx.bwlp.sat.fileserv.FileServer; import org.openslx.bwlp.sat.permissions.User; import org.openslx.bwlp.thrift.iface.ImageBaseWrite; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; @@ -108,20 +110,36 @@ 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" - + " FROM imageversion WHERE imageversionid = :versionid LIMIT 1"); - stmt.setString("versionid", imageVersionId); + MysqlStatement stmt = connection.prepareStatement("SELECT imageversionid, imagebaseid, filepath, filesize, createtime, 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.getString("filepath"), rs.getLong("filesize"), rs.getLong("createtime"), + rs.getBoolean("isvalid")); } catch (SQLException e) { LOGGER.error("Query failed in DbImage.getLocalImageData()", e); throw e; } } + protected static List<LocalImageVersion> getLocalImageVersions(MysqlConnection connection, + String imageBaseId) throws SQLException { + MysqlStatement stmt = connection.prepareStatement("SELECT imageversionid, imagebaseid, filepath, filesize, createtime, 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.getBoolean("isvalid"))); + } + return list; + } + /** * Private helper to create an {@link ImageSummaryRead} instance from a * {@link ResultSet} @@ -456,13 +474,18 @@ public class DbImage { } } + public static void markValid(MysqlConnection connection, LocalImageVersion imageVersion, boolean valid) + throws SQLException { + MysqlStatement stmt = connection.prepareStatement("UPDATE imageversion SET isvalid = :valid" + + " WHERE imageversionid = :imageversionid"); + stmt.setString("imageversionid", imageVersion.imageVersionId); + stmt.setBoolean("valid", valid); + stmt.executeUpdate(); + } + public static void markValid(LocalImageVersion imageVersion, boolean valid) throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement stmt = connection.prepareStatement("UPDATE imageversion SET isvalid = :valid" - + " WHERE imageversionid = :imageversionid"); - stmt.setString("imageversionid", imageVersion.imageVersionId); - stmt.setBoolean("valid", valid); - stmt.executeUpdate(); + markValid(connection, imageVersion, valid); connection.commit(); } catch (SQLException e) { LOGGER.error("Query failed in DbImage.markInvalid()", e); @@ -505,15 +528,20 @@ public class DbImage { throw new TNotFoundException(); } // Determine new latest version, as we might have to update the imagebase and lecture tables - List<ImageVersionDetails> versions = DbImage.getImageVersions(connection, imageBaseId); - ImageVersionDetails newVersion = null; - ImageVersionDetails oldVersion = null; - for (ImageVersionDetails version : versions) { - if (version.versionId.equals(changingImageVersionId)) { + List<LocalImageVersion> versions = DbImage.getLocalImageVersions(connection, imageBaseId); + LocalImageVersion newVersion = null; + LocalImageVersion oldVersion = null; + for (LocalImageVersion version : versions) { + if (version.imageVersionId.equals(changingImageVersionId)) { oldVersion = version; } if (version.isValid && (newVersion == null || version.createTime > newVersion.createTime)) { - newVersion = version; + File versionFile = FileServer.composeAbsolutePath(version); + if (versionFile.canRead() && versionFile.length() == version.fileSize) { + newVersion = version; + } else { + markValid(connection, version, false); + } } } if (oldVersion == null) { @@ -521,7 +549,7 @@ public class DbImage { } else { // Switch any lectures linking to this version if applicable if (oldVersion.isValid) { - DbLecture.autoUpdateUsedImage(connection, imageBaseId, newVersion.versionId); + DbLecture.autoUpdateUsedImage(connection, imageBaseId, newVersion.imageVersionId); } else { DbLecture.forcefullySwitchUsedImage(connection, oldVersion, newVersion); } @@ -529,7 +557,7 @@ public class DbImage { // Now update the latestversionid of the baseimage if applicable MysqlStatement latestStmt = connection.prepareStatement("UPDATE imagebase SET latestversionid = :newversionid" + " WHERE imagebaseid = :imagebaseid"); - latestStmt.setString("newversionid", newVersion == null ? null : newVersion.versionId); + latestStmt.setString("newversionid", newVersion == null ? null : newVersion.imageVersionId); latestStmt.setString("imagebaseid", imageBaseId); latestStmt.executeUpdate(); } |