bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
DbOrganization.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.db.mappers;
2 
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7 
8 import org.apache.logging.log4j.LogManager;
9 import org.apache.logging.log4j.Logger;
10 import org.openslx.bwlp.thrift.iface.Organization;
14 
19 public class DbOrganization
20 {
21  private static final Logger LOGGER = LogManager.getLogger( DbOrganization.class );
22 
23  private static final String organizationBaseSql = "SELECT"
24  + " o.organizationid, o.name, o.authmethod, o.publickey"
25  + " FROM organization o";
26 
27  private static final String suffixListFromOrgSql = "SELECT suffix FROM organization_suffix"
28  + " WHERE organizationid = :organizationid";
29 
30  private static Organization fromResultSet( MysqlConnection connection, ResultSet rs ) throws SQLException
31  {
32  String organizationId = rs.getString( "organizationid" );
33  String ecpUrl = rs.getString( "authmethod" );
34  if ( ecpUrl != null && !ecpUrl.startsWith( "http" ) ) {
35  ecpUrl = null;
36  }
37  return new Organization( organizationId, rs.getString( "name" ), ecpUrl, getSuffixList( connection,
38  organizationId ) );
39  }
40 
48  public static Organization fromOrganizationId( String organizationId ) throws SQLException
49  {
50  try ( MysqlConnection connection = Database.getConnection() ) {
51  MysqlStatement stmt = connection.prepareStatement( organizationBaseSql + " WHERE o.organizationid = :organizationid" );
52  stmt.setString( "organizationid", organizationId );
53  ResultSet rs = stmt.executeQuery();
54  if ( !rs.next() )
55  return null;
56  return fromResultSet( connection, rs );
57  } catch ( SQLException e ) {
58  LOGGER.error( "Query failed in DbOrganization.fromOrganizationId()", e );
59  throw e;
60  }
61  }
62 
63  public static Organization fromSuffix( String suffix ) throws SQLException
64  {
65  try ( MysqlConnection connection = Database.getConnection() ) {
66  MysqlStatement stmt = connection.prepareStatement( organizationBaseSql
67  + " INNER JOIN organization_suffix s USING (organizationid)"
68  + " WHERE s.suffix = :suffix" );
69  stmt.setString( "suffix", suffix );
70  ResultSet rs = stmt.executeQuery();
71  if ( !rs.next() )
72  return null;
73  return fromResultSet( connection, rs );
74  } catch ( SQLException e ) {
75  LOGGER.error( "Query failed in DbOrganization.fromSuffix()", e );
76  throw e;
77  }
78  }
79 
80  private static List<String> suffixForOrg( MysqlStatement stmt, String organizationId ) throws SQLException
81  {
82  stmt.setString( "organizationid", organizationId );
83  ResultSet rs = stmt.executeQuery();
84  List<String> list = new ArrayList<>();
85  while ( rs.next() ) {
86  list.add( rs.getString( "suffix" ) );
87  }
88  return list;
89  }
90 
98  public static List<Organization> getAll() throws SQLException
99  {
100  try ( MysqlConnection connection = Database.getConnection() ) {
101  MysqlStatement stmt = connection.prepareStatement( organizationBaseSql );
102  ResultSet rsOrg = stmt.executeQuery();
103  MysqlStatement stmtSuffix = connection.prepareStatement( suffixListFromOrgSql );
104  List<Organization> list = new ArrayList<>();
105  while ( rsOrg.next() ) {
106  String organizationId = rsOrg.getString( "organizationid" );
107  String ecpUrl = rsOrg.getString( "authmethod" );
108  if ( ecpUrl != null && !ecpUrl.startsWith( "http" ) ) {
109  ecpUrl = null;
110  }
111  List<String> suffixList = suffixForOrg( stmtSuffix, organizationId );
112  list.add( new Organization( organizationId, rsOrg.getString( "name" ), ecpUrl, suffixList ) );
113  }
114  return list;
115  } catch ( SQLException e ) {
116  LOGGER.error( "Query failed in DbOrganization.getAll()", e );
117  throw e;
118  }
119  }
120 
121  public static DbOrganization fromPrefix( String prefix )
122  {
123  return null;
124  }
125 
126  public static List<String> getSuffixList( MysqlConnection connection, String organizationId ) throws SQLException
127  {
128  List<String> list = new ArrayList<>();
129  MysqlStatement stmt = connection.prepareStatement( suffixListFromOrgSql );
130  stmt.setString( "organizationid", organizationId );
131  ResultSet rs = stmt.executeQuery();
132  while ( rs.next() ) {
133  list.add( rs.getString( "suffix" ) );
134  }
135  return list;
136  }
137 
138 }
static List< Organization > getAll()
Return all known satellites/organizations as List of OrganizationData, which can be used directly by ...
static List< String > suffixForOrg(MysqlStatement stmt, String organizationId)
static MysqlConnection getConnection()
Get a connection to the database.
Definition: Database.java:92
ResultSet executeQuery()
Executes the statement, which must be a query.
Represents an organization in the database.
void setString(String name, String value)
Sets a parameter.
Class for creating PreparedStatements with named parameters.
static DbOrganization fromPrefix(String prefix)
static Organization fromSuffix(String suffix)
static Organization fromResultSet(MysqlConnection connection, ResultSet rs)
static List< String > getSuffixList(MysqlConnection connection, String organizationId)
static Organization fromOrganizationId(String organizationId)
Get organization by id.