From f5618c87e63deb99920710787f6dcd34d4b17425 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 13 Apr 2016 18:41:29 +0200 Subject: (WiP) Global image sync --- .../openslx/imagemaster/db/mappers/DbImage.java | 87 +++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) (limited to 'src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java') diff --git a/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java b/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java index 2f5394c..f4c3ddc 100644 --- a/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java +++ b/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java @@ -1,7 +1,17 @@ package org.openslx.imagemaster.db.mappers; -import org.openslx.bwlp.thrift.iface.ImagePublishData; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.ImagePublishData; +import org.openslx.bwlp.thrift.iface.InvocationError; +import org.openslx.bwlp.thrift.iface.TInvocationException; +import org.openslx.imagemaster.Globals; +import org.openslx.imagemaster.db.Database; +import org.openslx.imagemaster.db.MysqlConnection; +import org.openslx.imagemaster.db.MysqlStatement; +import org.openslx.util.Util; /** * Representing an image in the database. @@ -10,10 +20,83 @@ import org.openslx.bwlp.thrift.iface.ImagePublishData; public class DbImage { + private static final Logger LOGGER = Logger.getLogger( DbImage.class ); + public static ImagePublishData getImageVersion( String imageVersionId ) { - // TODO Auto-generated method stub return null; } + public static void createImageBase( ImagePublishData img ) throws TInvocationException + { + // Input seems valid + try ( MysqlConnection connection = Database.getConnection() ) { + MysqlStatement stmt = connection.prepareStatement( "SELECT virtid FROM imagebase WHERE imagebaseid = :baseid" ); + stmt.setString( "baseid", img.imageBaseId ); + ResultSet rs = stmt.executeQuery(); + if ( rs.next() ) { + if ( !img.virtId.equals( rs.getString( "virtid" ) ) ) { + throw new TInvocationException( InvocationError.INVALID_DATA, "Virtualizer id mismatch" ); + } + MysqlStatement stmt2 = connection.prepareStatement( "UPDATE imagebase SET" + + " displayname = :displayname, updaterid = :updaterid," + + " description = :description, osid = :osid, updatetime = UNIX_TIMESTAMP()," + + " istemplate = :istemplate WHERE imagebaseid = :baseid" ); + stmt2.setString( "baseid", img.imageBaseId ); + stmt2.setString( "displayname", img.imageName ); + stmt2.setString( "updaterid", img.user.userId ); + stmt2.setString( "description", img.description ); + stmt2.setInt( "osid", img.osId ); + stmt2.setBoolean( "istemplate", img.isTemplate ); + stmt2.executeUpdate(); + } else { + MysqlStatement stmt2 = connection.prepareStatement( "INSERT INTO imagebase" + + " (imagebaseid, latestversionid, displayname, description, osid," + + " virtid, createtime, updatetime, ownerid, updaterid, istemplate)" + + " VALUES " + + " (:imagebaseid, NULL, :displayname, :description, :osid," + + " :virtid, :createtime, UNIX_TIMESTAMP(), :ownerid, :updaterid, :istemplate)" ); + stmt2.setString( "imagebaseid", img.imageBaseId ); + stmt2.setString( "displayname", img.imageName ); + stmt2.setString( "description", img.description ); + stmt2.setInt( "osid", img.osId ); + stmt2.setString( "virtid", img.virtId ); + stmt2.setLong( "createtime", img.createTime ); + stmt2.setString( "ownerid", img.user.userId ); + stmt2.setString( "updaterid", img.user.userId ); + stmt2.setBoolean( "istemplate", img.isTemplate ); + stmt2.executeUpdate(); + } + connection.commit(); + } catch ( SQLException e ) { + LOGGER.error( "Query failed in DbImage.createImageBase()", e ); + throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database boo-boo" ); + } + } + + public static void createImageVersion( ImagePublishData img, String relLocalPath ) throws SQLException + { + try ( MysqlConnection connection = Database.getConnection() ) { + // Insert version + MysqlStatement verStmt = connection.prepareStatement( "INSERT INTO imageversion" + + " (imageversionid, imagebaseid, createtime, expiretime, filesize," + + " filepath, uploaderid, isvalid, isprocessed, mastersha1, virtualizerconfig)" + + " VALUES " + + " (:imageversionid, :imagebaseid, :createtime, :expiretime, :filesize," + + " :filepath, :uploaderid, 0, 0, NULL, NULL)" ); + verStmt.setString( "imageversionid", img.imageVersionId ); + verStmt.setString( "imagebaseid", img.imageBaseId ); + verStmt.setLong( "createtime", img.createTime ); + verStmt.setLong( "expiretime", Util.unixTime() + Globals.getImageValiditySeconds() ); + verStmt.setLong( "filesize", img.fileSize ); + verStmt.setString( "filepath", relLocalPath ); + verStmt.setString( "uploaderid", img.user.userId ); + verStmt.execute(); + connection.commit(); + } catch ( SQLException e ) { + LOGGER.error( "Query failed in DbImage.createImageVersion()", e ); + throw e; + } + } + } -- cgit v1.2.3-55-g7522