package org.openslx.imagemaster.db.mappers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.imagemaster.db.Database;
import org.openslx.imagemaster.db.MysqlConnection;
import org.openslx.imagemaster.db.MysqlStatement;
import org.openslx.imagemaster.util.Util;
public class DbOsVirt
{
private static final Logger LOGGER = Logger.getLogger( DbOsVirt.class );
public static List<OperatingSystem> getOsList() throws SQLException
{
try ( MysqlConnection connection = Database.getConnection() ) {
// Query OSs
MysqlStatement stmt = connection.prepareStatement( "SELECT"
+ " osid, displayname, architecture, maxmem, maxcpu" + " FROM operatingsystem" );
ResultSet rs = stmt.executeQuery();
List<OperatingSystem> list = new ArrayList<>();
Map<Integer, Map<String, String>> osVirtMappings = getOsVirtMappings( connection );
while ( rs.next() ) {
int osId = rs.getInt( "osid" );
list.add( new OperatingSystem( osId, rs.getString( "displayname" ), osVirtMappings.get( osId ),
rs.getString( "architecture" ), rs.getInt( "maxmem" ), rs.getInt( "maxcpu" ) ) );
}
return list;
} catch ( SQLException e ) {
LOGGER.error( "Query failed in DbOsVirt.getOsList()", e );
throw e;
}
}
private static Map<Integer, Map<String, String>> getOsVirtMappings( MysqlConnection connection )
throws SQLException
{
MysqlStatement stmt = connection.prepareStatement( "SELECT osid, virtid, virtoskeyword FROM os_x_virt" );
ResultSet rs = stmt.executeQuery();
Map<Integer, Map<String, String>> map = new HashMap<>();
while ( rs.next() ) {
Integer osId = rs.getInt( "osid" );
Map<String, String> osMap = map.get( osId );
if ( osMap == null ) {
osMap = new HashMap<>();
map.put( osId, osMap );
}
osMap.put( rs.getString( "virtid" ), rs.getString( "virtoskeyword" ) );
}
return map;
}
public static List<Virtualizer> getVirtualizerList() throws SQLException
{
try ( MysqlConnection connection = Database.getConnection() ) {
MysqlStatement stmt = connection.prepareStatement( "SELECT virtid, virtname" + " FROM virtualizer" );
ResultSet rs = stmt.executeQuery();
List<Virtualizer> list = new ArrayList<>();
while ( rs.next() ) {
list.add( new Virtualizer( rs.getString( "virtid" ), rs.getString( "virtname" ) ) );
}
return list;
} catch ( SQLException e ) {
LOGGER.error( "Query failed in DbOsVirt.getVirtualizerList()", e );
throw e;
}
}
public static boolean osExists( int osId )
{
if ( osId <= 0 )
return false;
try ( MysqlConnection connection = Database.getConnection() ) {
MysqlStatement stmt = connection.prepareStatement( "SELECT osid FROM operatingsystem WHERE osid = :osid" );
stmt.setInt( "osid", osId );
ResultSet rs = stmt.executeQuery();
return rs.next();
} catch ( SQLException e ) {
LOGGER.error( "Query failed in DbOsVirt.exists()", e );
return false;
}
}
public static boolean virtExists( String virtId )
{
if ( Util.isEmpty( virtId ) )
return false;
try ( MysqlConnection connection = Database.getConnection() ) {
MysqlStatement stmt = connection.prepareStatement( "SELECT virtid FROM virtualizer WHERE virtid = :virtid" );
stmt.setString( "virtid", virtId );
ResultSet rs = stmt.executeQuery();
return rs.next();
} catch ( SQLException e ) {
LOGGER.error( "Query failed in DbOsVirt.virtExists()", e );
return false;
}
}
}