summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/mappers/DbPendingSatellite.java
blob: 472d2499643d5e492a5abbd324f776d09e70afa4 (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
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;
		}
	}

}