From 561cafe5fbad785945239e3922f3a8c6035ea5ca Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Sep 2015 15:28:47 +0200 Subject: [server] Manually detect if latest version changed before triggering mail --- .../openslx/bwlp/sat/database/mappers/DbImage.java | 43 ++++++++++++---------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'dozentenmodulserver') 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 bbbb8c8d..bf79e88f 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 @@ -760,35 +760,40 @@ public class DbImage { * * @param connection mysql connection to use * @param imageBaseId base id of image in question - * @param latest image version that is to become the latest version, or + * @param newLatest image version that is to become the latest version, or * null if there is no valid version * @return true if changed to a different, non-null image * @throws SQLException */ private static boolean setLatestVersion(MysqlConnection connection, String imageBaseId, - LocalImageVersion latest) throws SQLException { - // Debug - MysqlStatement ds = connection.prepareStatement("SELECT latestversionid FROM imagebase WHERE imagebaseid = :imagebaseid"); - ds.setString("imagebaseid", imageBaseId); - ResultSet drs = ds.executeQuery(); - if (drs.next()) { - LOGGER.debug("set latest on " + imageBaseId + ": from " + drs.getString("latestversionid") - + " to " + (latest == null ? null : latest.imageVersionId)); - } else { - LOGGER.debug("set latest: could not determine old latest for " + imageBaseId); - } + LocalImageVersion newLatest) throws SQLException { + // Determine manually if anything changed, as executeQuery() always returns 1 for some reason + boolean latestVersionChanged = true; + do { + MysqlStatement ds = connection.prepareStatement("SELECT latestversionid FROM imagebase WHERE imagebaseid = :imagebaseid"); + ds.setString("imagebaseid", imageBaseId); + ResultSet drs = ds.executeQuery(); + if (drs.next()) { + String currentLatest = drs.getString("latestversionid"); + if (currentLatest == null && (newLatest == null || newLatest.imageVersionId == null)) { + latestVersionChanged = false; + } else if (currentLatest != null && newLatest != null + && currentLatest.equals(newLatest.imageVersionId)) { + latestVersionChanged = false; + } + } + } while (false); // Update latestversionid reference in imagebase table MysqlStatement latestStmt = connection.prepareStatement("UPDATE imagebase SET latestversionid = :newversionid" + " WHERE imagebaseid = :imagebaseid"); - latestStmt.setString("newversionid", latest == null ? null : latest.imageVersionId); + latestStmt.setString("newversionid", newLatest == null ? null : newLatest.imageVersionId); latestStmt.setString("imagebaseid", imageBaseId); // If nothing changed (because the deleted version was not the latest), bail out - int updateCount = latestStmt.executeUpdate(); - LOGGER.debug(updateCount + " rows affected"); - if (updateCount == 0) + latestStmt.executeUpdate(); + if (!latestVersionChanged) return false; // It there is no valid version, bail out as a shortcut - queries below wouldn't do anything - if (latest == null) + if (newLatest == null) return true; // Latest version changed - update expire dates of related versions // Set short expire date for versions that are NOT the latest version but are still marked valid @@ -796,7 +801,7 @@ public class DbImage { MysqlStatement oldStmt = connection.prepareStatement("UPDATE imageversion SET" + " expiretime = If(expiretime < :shortexpire, expiretime, :shortexpire)" + " WHERE imagebaseid = :imagebaseid AND imageversionid <> :imageversionid AND isvalid = 1"); - oldStmt.setString("imageversionid", latest.imageVersionId); + oldStmt.setString("imageversionid", newLatest.imageVersionId); oldStmt.setString("imagebaseid", imageBaseId); oldStmt.setLong("shortexpire", shortExpire); oldStmt.executeUpdate(); @@ -804,7 +809,7 @@ public class DbImage { MysqlStatement newStmt = connection.prepareStatement("UPDATE imageversion SET" + " expiretime = If(createtime + :maxvalid > expiretime, createtime + :maxvalid, expiretime)" + " WHERE imageversionid = :imageversionid"); - newStmt.setString("imageversionid", latest.imageVersionId); + newStmt.setString("imageversionid", newLatest.imageVersionId); newStmt.setLong("maxvalid", RuntimeConfig.getMaxImageValiditySeconds()); newStmt.executeUpdate(); return true; -- cgit v1.2.3-55-g7522