/** * Returns a client model from a ResultSet * * @param result * @return * @throws Exception */ private Client getClientFromResultSet(ResultSet result) throws Exception { Client client = new Client(); client.setId(result.getInt(Constants.GENERIC_ID)); client.setUUID(result.getString(Constants.CLIENT_CLIENT_UUID)); client.setFriendlyName(result.getString(Constants.CLIENT_FRIENDLY_NAME)); client.setProfile( ProfileService.getInstance().findProfile(result.getInt(Constants.GENERIC_PROFILE_ID))); client.setIsActive(result.getBoolean(Constants.CLIENT_IS_ACTIVE)); client.setActiveServerGroup(result.getInt(Constants.CLIENT_ACTIVESERVERGROUP)); return client; }
/** * Create a new client for profile There is a limit of Constants.CLIENT_CLIENTS_PER_PROFILE_LIMIT * If this limit is reached an exception is thrown back to the caller * * @param profileId * @return * @throws Exception */ public Client add(int profileId) throws Exception { Client client = null; ArrayList<Integer> pathsToCopy = new ArrayList<Integer>(); String clientUUID = getUniqueClientUUID(); // get profile for profileId Connection sqlConnection = null; Profile profile = ProfileService.getInstance().findProfile(profileId); PreparedStatement statement = null; ResultSet rs = null; try { sqlConnection = sqlService.getConnection(); // get the current count of clients statement = sqlConnection.prepareStatement( "SELECT COUNT(" + Constants.GENERIC_ID + ") FROM " + Constants.DB_TABLE_CLIENT + " WHERE " + Constants.GENERIC_PROFILE_ID + "=?"); statement.setInt(1, profileId); int clientCount = -1; rs = statement.executeQuery(); if (rs.next()) { clientCount = rs.getInt(1); } statement.close(); rs.close(); // check count if (clientCount == -1) { throw new Exception("Error querying clients for profileId=" + profileId); } if (clientCount >= Constants.CLIENT_CLIENTS_PER_PROFILE_LIMIT) { throw new Exception( "Profile(" + profileId + ") already contains 50 clients. Please remove clients before adding new ones."); } statement = sqlConnection.prepareStatement( "INSERT INTO " + Constants.DB_TABLE_CLIENT + " (" + Constants.CLIENT_CLIENT_UUID + ", " + Constants.CLIENT_IS_ACTIVE + ", " + Constants.CLIENT_PROFILE_ID + ")" + " VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS); statement.setString(1, clientUUID); statement.setBoolean(2, false); statement.setInt(3, profile.getId()); statement.executeUpdate(); rs = statement.getGeneratedKeys(); int clientId = -1; if (rs.next()) { clientId = rs.getInt(1); } else { // something went wrong throw new Exception("Could not add client"); } rs.close(); statement.close(); // adding entries into request response table for this new client for every path // basically a copy of what happens when a path gets created statement = sqlConnection.prepareStatement( "SELECT * FROM " + Constants.DB_TABLE_REQUEST_RESPONSE + " WHERE " + Constants.GENERIC_PROFILE_ID + " = ?" + " AND " + Constants.GENERIC_CLIENT_UUID + " = ?"); statement.setInt(1, profile.getId()); statement.setString(2, Constants.PROFILE_CLIENT_DEFAULT_ID); rs = statement.executeQuery(); while (rs.next()) { // collect up the pathIds we need to copy pathsToCopy.add(rs.getInt(Constants.REQUEST_RESPONSE_PATH_ID)); } client = new Client(); client.setIsActive(false); client.setUUID(clientUUID); client.setId(clientId); client.setProfile(profile); } catch (SQLException e) { throw e; } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } try { if (statement != null) statement.close(); } catch (Exception e) { } } // add all of the request response items for (Integer pathId : pathsToCopy) { PathOverrideService.getInstance() .addPathToRequestResponseTable(profile.getId(), client.getUUID(), pathId); } return client; }