summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/mappers/DbOsVirt.java
blob: ee8a39ad3e92a6e51e43ee720fffd5c63ef2bfda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
		}
	}

}