summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/mappers/DbOrganization.java
blob: 4c094fe71edf20588b7f9a1ebbde4cd0de012d61 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.openslx.imagemaster.db.mappers;

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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.imagemaster.db.Database;
import org.openslx.imagemaster.db.MysqlConnection;
import org.openslx.imagemaster.db.MysqlStatement;

/**
 * Represents an organization in the database.
 * Is used to authenticate the organization.
 */
public class DbOrganization
{
	private static final Logger LOGGER = LogManager.getLogger( DbOrganization.class );

	private static final String organizationBaseSql = "SELECT"
			+ " o.organizationid, o.name, o.authmethod, o.publickey"
			+ " FROM organization o";

	private static final String suffixListFromOrgSql = "SELECT suffix FROM organization_suffix"
			+ " WHERE organizationid = :organizationid";

	private static Organization fromResultSet( MysqlConnection connection, ResultSet rs ) throws SQLException
	{
		String organizationId = rs.getString( "organizationid" );
		String ecpUrl = rs.getString( "authmethod" );
		if ( ecpUrl != null && !ecpUrl.startsWith( "http" ) ) {
			ecpUrl = null;
		}
		return new Organization( organizationId, rs.getString( "name" ), ecpUrl, getSuffixList( connection,
				organizationId ) );
	}

	/**
	 * Get organization by id. Returns null if not found.
	 * 
	 * @param organizationId
	 * @return
	 * @throws SQLException
	 */
	public static Organization fromOrganizationId( String organizationId ) throws SQLException
	{
		try ( MysqlConnection connection = Database.getConnection() ) {
			MysqlStatement stmt = connection.prepareStatement( organizationBaseSql + " WHERE o.organizationid = :organizationid" );
			stmt.setString( "organizationid", organizationId );
			ResultSet rs = stmt.executeQuery();
			if ( !rs.next() )
				return null;
			return fromResultSet( connection, rs );
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbOrganization.fromOrganizationId()", e );
			throw e;
		}
	}

	public static Organization fromSuffix( String suffix ) throws SQLException
	{
		try ( MysqlConnection connection = Database.getConnection() ) {
			MysqlStatement stmt = connection.prepareStatement( organizationBaseSql
					+ " INNER JOIN organization_suffix s USING (organizationid)"
					+ " WHERE s.suffix = :suffix" );
			stmt.setString( "suffix", suffix );
			ResultSet rs = stmt.executeQuery();
			if ( !rs.next() )
				return null;
			return fromResultSet( connection, rs );
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbOrganization.fromSuffix()", e );
			throw e;
		}
	}

	private static List<String> suffixForOrg( MysqlStatement stmt, String organizationId ) throws SQLException
	{
		stmt.setString( "organizationid", organizationId );
		ResultSet rs = stmt.executeQuery();
		List<String> list = new ArrayList<>();
		while ( rs.next() ) {
			list.add( rs.getString( "suffix" ) );
		}
		return list;
	}

	/**
	 * Return all known satellites/organizations as List of {@link OrganizationData}, which can be
	 * used directly by the thrift API.
	 * 
	 * @return list of all known organizations/satellites
	 * @throws SQLException
	 */
	public static List<Organization> getAll() throws SQLException
	{
		try ( MysqlConnection connection = Database.getConnection() ) {
			MysqlStatement stmt = connection.prepareStatement( organizationBaseSql );
			ResultSet rsOrg = stmt.executeQuery();
			MysqlStatement stmtSuffix = connection.prepareStatement( suffixListFromOrgSql );
			List<Organization> list = new ArrayList<>();
			while ( rsOrg.next() ) {
				String organizationId = rsOrg.getString( "organizationid" );
				String ecpUrl = rsOrg.getString( "authmethod" );
				if ( ecpUrl != null && !ecpUrl.startsWith( "http" ) ) {
					ecpUrl = null;
				}
				List<String> suffixList = suffixForOrg( stmtSuffix, organizationId );
				list.add( new Organization( organizationId, rsOrg.getString( "name" ), ecpUrl, suffixList ) );
			}
			return list;
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbOrganization.getAll()", e );
			throw e;
		}
	}

	public static DbOrganization fromPrefix( String prefix )
	{
		return null;
	}

	public static List<String> getSuffixList( MysqlConnection connection, String organizationId ) throws SQLException
	{
		List<String> list = new ArrayList<>();
		MysqlStatement stmt = connection.prepareStatement( suffixListFromOrgSql );
		stmt.setString( "organizationid", organizationId );
		ResultSet rs = stmt.executeQuery();
		while ( rs.next() ) {
			list.add( rs.getString( "suffix" ) );
		}
		return list;
	}

}