package org.openslx.imagemaster.db.mappers; 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. * Is used to modify/update database entries. */ public class DbImage { private static final Logger LOGGER = Logger.getLogger( DbImage.class ); public static ImagePublishData getImageVersion( String imageVersionId ) { 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; } } }