summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java
diff options
context:
space:
mode:
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.java98
1 files changed, 79 insertions, 19 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 23a71228..0f925371 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
@@ -8,6 +8,7 @@ import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
+import org.openslx.bwlp.sat.RuntimeConfig;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
@@ -15,6 +16,7 @@ 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.sat.util.Util;
import org.openslx.bwlp.thrift.iface.ImageBaseWrite;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
@@ -402,14 +404,15 @@ public class DbImage {
+ " v.isrestricted = :isrestricted" + " WHERE v.imageversionid = :versionid");
stmtVersion.setString("versionid", imageVersionId);
stmtVersion.setBoolean("isrestricted", image.isRestricted);
- stmtVersion.executeUpdate();
- // Then base table
- MysqlStatement stmtBase = connection.prepareStatement("UPDATE imagebase b SET"
- + " b.updaterid = :userid, b.updatetime = UNIX_TIMESTAMP()"
- + " WHERE b.imagebaseid = :baseid");
- stmtBase.setString("userid", user.userId);
- stmtBase.setString("baseid", baseId);
- stmtBase.executeUpdate();
+ if (stmtVersion.executeUpdate() != 0) {
+ // Then base table
+ MysqlStatement stmtBase = connection.prepareStatement("UPDATE imagebase b SET"
+ + " b.updaterid = :userid, b.updatetime = UNIX_TIMESTAMP()"
+ + " WHERE b.imagebaseid = :baseid");
+ stmtBase.setString("userid", user.userId);
+ stmtBase.setString("baseid", baseId);
+ stmtBase.executeUpdate();
+ }
connection.commit();
} catch (SQLException e) {
LOGGER.error("Query failed in DbImage.updateImageVersion()", e);
@@ -431,7 +434,7 @@ public class DbImage {
try (MysqlConnection connection = Database.getConnection()) {
// Disable version in question
MysqlStatement disableStmt = connection.prepareStatement("UPDATE imageversion SET"
- + " expiretime = UNIX_TIMESTAMP() - 86400 * 5, isvalid = 0"
+ + " expiretime = UNIX_TIMESTAMP() - 86400, isvalid = 0"
+ " WHERE imageversionid = :versionid");
disableStmt.setString("versionid", imageVersionId);
disableStmt.executeUpdate();
@@ -461,7 +464,8 @@ public class DbImage {
long fileSize, String filePath, ImageVersionWrite versionSettings, ChunkList chunks,
byte[] machineDescription) throws SQLException {
try (MysqlConnection connection = Database.getConnection()) {
- final long nowSecs = System.currentTimeMillis() / 1000;
+ final long nowSecs = Util.unixTime();
+ final long expireTime = nowSecs + RuntimeConfig.getMaxImageValiditySeconds();
MysqlStatement verStmt = connection.prepareStatement("INSERT INTO imageversion"
+ " (imageversionid, imagebaseid, createtime, expiretime, filesize, filepath, uploaderid,"
+ " isrestricted, isvalid, isprocessed, mastersha1, virtualizerconfig)"
@@ -471,7 +475,7 @@ public class DbImage {
verStmt.setString("imageversionid", imageVersionId);
verStmt.setString("imagebaseid", imageBaseId);
verStmt.setLong("createtime", nowSecs);
- verStmt.setLong("expiretime", nowSecs + 86400 * 365); // TODO: Config!
+ verStmt.setLong("expiretime", expireTime);
verStmt.setLong("filesize", fileSize);
verStmt.setString("filepath", filePath);
verStmt.setString("uploaderid", owner.userId);
@@ -488,7 +492,9 @@ public class DbImage {
baseStmt.setString("imageversionid", imageVersionId);
baseStmt.setString("imagebaseid", imageBaseId);
baseStmt.executeUpdate();
- DbLecture.autoUpdateUsedImage(connection, imageBaseId, imageVersionId);
+ LocalImageVersion liv = new LocalImageVersion(imageVersionId, imageBaseId, filePath, fileSize, nowSecs, expireTime, true);
+ DbLecture.autoUpdateUsedImage(connection, imageBaseId, liv);
+ setLatestVersion(connection, imageBaseId, liv);
connection.commit();
} catch (SQLException e) {
LOGGER.error("Query failed in DbImage.createImageVersion()", e);
@@ -496,7 +502,7 @@ public class DbImage {
}
}
- public static void markValid(MysqlConnection connection, boolean valid, LocalImageVersion... imageVersion)
+ protected static void markValid(MysqlConnection connection, boolean valid, LocalImageVersion... imageVersion)
throws SQLException {
if (imageVersion == null || imageVersion.length == 0)
return;
@@ -527,6 +533,19 @@ public class DbImage {
}
}
+ public static void deletePermanently(LocalImageVersion image) throws SQLException {
+ try (MysqlConnection connection = Database.getConnection()) {
+ MysqlStatement stmt = connection.prepareStatement("DELETE FROM imageversion"
+ + " WHERE imageversionid = :imageversionid");
+ stmt.setString("imageversionid", image.imageVersionId);
+ stmt.executeUpdate();
+ connection.commit();
+ } catch (SQLException e) {
+ LOGGER.error("Query failed in DbImage.deletePermanently()", e);
+ throw e;
+ }
+ }
+
private static void updateLatestVersionAsync(final LocalImageVersion... changingVersion) {
if (changingVersion == null || changingVersion.length == 0)
return;
@@ -548,7 +567,7 @@ public class DbImage {
@Override
public void fire() {
try (MysqlConnection connection = Database.getConnection()) {
- updateLatestVersion(connection, changingImageVersionId, null);
+ versionValidityChanged(connection, changingImageVersionId, null);
connection.commit();
} catch (SQLException | TNotFoundException e) {
LOGGER.error("Query failed in DbImage.updateLatestVersionAsync()", e);
@@ -564,7 +583,7 @@ public class DbImage {
return;
for (LocalImageVersion version : versions) {
try {
- updateLatestVersion(connection, version.imageVersionId,
+ versionValidityChanged(connection, version.imageVersionId,
version.imageBaseId);
} catch (TNotFoundException e) {
// Swallow - logging happens in called method
@@ -584,7 +603,7 @@ public class DbImage {
* @throws TNotFoundException
* @throws SQLException
*/
- private static void updateLatestVersion(MysqlConnection connection, String changingImageVersionId,
+ private static void versionValidityChanged(MysqlConnection connection, String changingImageVersionId,
String imageBaseId) throws TNotFoundException, SQLException {
if (imageBaseId == null) {
imageBaseId = DbImage.getBaseIdForVersionId(connection, changingImageVersionId);
@@ -613,16 +632,57 @@ public class DbImage {
} else {
// Switch any lectures linking to this version if applicable
if (oldVersion.isValid) {
- DbLecture.autoUpdateUsedImage(connection, imageBaseId, newVersion.imageVersionId);
+ DbLecture.autoUpdateUsedImage(connection, imageBaseId, newVersion);
} else {
DbLecture.forcefullySwitchUsedImage(connection, oldVersion, newVersion);
}
}
// Now update the latestversionid of the baseimage if applicable
+ if (setLatestVersion(connection, imageBaseId, newVersion)) {
+ // TODO: Latest version changed -> mail
+ //Something.versionDeleted(imageBaseId, oldVersion, newVersion);
+ }
+ }
+
+ /**
+ * Set the latest version id of the given base image. Returns true if and
+ * only if the latest version id of the base image did actually change through
+ * this call <b>and</b> the new latest version is <b>not null</b>.
+ *
+ * @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
+ * <code>null</code> 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 {
+ // Update latestversionid reference in imagebase table
MysqlStatement latestStmt = connection.prepareStatement("UPDATE imagebase SET latestversionid = :newversionid"
+ " WHERE imagebaseid = :imagebaseid");
- latestStmt.setString("newversionid", newVersion == null ? null : newVersion.imageVersionId);
+ latestStmt.setString("newversionid", latest == null ? null : latest.imageVersionId);
latestStmt.setString("imagebaseid", imageBaseId);
- latestStmt.executeUpdate();
+ // If nothing changed, or the latest version was set to NULL, bail out
+ if (latestStmt.executeUpdate() == 0 || latest == null)
+ return false;
+ // 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
+ long shortExpire = Util.unixTime() + RuntimeConfig.getOldVersionExpireSeconds();
+ 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("imagebaseid", imageBaseId);
+ oldStmt.setLong("shortexpire", shortExpire);
+ oldStmt.executeUpdate();
+ // Now set a long expire date for the latest version, as it might have been shortened before
+ MysqlStatement newStmt = connection.prepareStatement("UPDATE imageversion SET"
+ + " expiretime = If(createtime + :maxvalid > expiretime, createtime + :maxvalid, expiretime)"
+ + " WHERE imageversionid = :imageversionid");
+ newStmt.setString("imageversionid", latest.imageVersionId);
+ newStmt.setLong("maxvalid", RuntimeConfig.getMaxImageValiditySeconds());
+ newStmt.executeUpdate();
+ return true;
}
+
}