summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java
blob: e092e944e3771723c3fd32667825f8c7da149fb6 (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
package org.openslx.bwlp.sat.database.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
import org.openslx.bwlp.thrift.iface.OperatingSystem;

public class DbOsVirt {

	private static final Logger LOGGER = Logger.getLogger(DbOsVirt.class);

	public static void storeOsList(List<OperatingSystem> list) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("INSERT INTO operatingsystem"
					+ " (osid, displayname, architecture) VALUES"
					+ " (:osid, :displayname, :architecture)"
					+ " ON DUPLICATE KEY UPDATE displayname = VALUES(displayname), architecture = VALUES(architecture)");
			for (OperatingSystem os : list) {
				stmt.setInt("osid", os.osId);
				stmt.setString("displayname", os.osName);
				stmt.setString("architecture", os.architecture);
				stmt.executeUpdate();
			}
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.writeOsList()", e);
			throw e;
		}
	}

	public static List<OperatingSystem> getOsList() throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("SELECT osid, displayname, architecture"
					+ " FROM operatingsystem");
			ResultSet rs = stmt.executeQuery();
			List<OperatingSystem> list = new ArrayList<>();
			while (rs.next()) {
				// TODO: virt mapping
				list.add(new OperatingSystem(rs.getInt("osid"), rs.getString("displayname"), null,
						rs.getString("architecture")));
			}
			return list;
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.getOsList()", e);
			throw e;
		}
	}

}