package org.openslx.imagemaster.db.mappers;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.imagemaster.db.Database;
import org.openslx.imagemaster.db.MysqlConnection;
import org.openslx.imagemaster.db.MysqlStatement;
import org.openslx.util.Json;
public class DbPendingSatellite
{
private static final Logger LOGGER = Logger.getLogger( DbPendingSatellite.class );
public static int add( UserInfo user, String displayName, List<String> address, String modulus, String exponent )
throws SQLException
{
try ( MysqlConnection connection = Database.getConnection() ) {
MysqlStatement stmt = connection.prepareStatement( "INSERT INTO satellite"
+ " (dateline, userid, organizationid, satellitename, addresses, publickey)"
+ " VALUES (UNIX_TIMESTAMP(), :userid, :organizationid, :satellitename, :addresses, :pubkey)", true );
stmt.setString( "userid", user.userId );
stmt.setString( "organizationid", user.organizationId );
stmt.setString( "satellitename", displayName );
stmt.setString( "addresses", Json.serialize( address ) );
stmt.setString( "pubkey", Json.serialize( new KeyWrapper( modulus, exponent ) ) );
stmt.executeUpdate();
int key = stmt.getGeneratedKeys();
connection.commit();
return key;
} catch ( SQLException e ) {
LOGGER.error( "Query failed in DbPendingSatellite.add()", e );
throw e;
}
}
private static class KeyWrapper
{
public String type;
public String modulus;
public String exponent;
public KeyWrapper( String modulus, String exponent )
{
this.type = "RSA";
this.modulus = modulus;
this.exponent = exponent;
}
}
}