<commit_before>package rtdc.web.server.service;

import rtdc.core.Config;
import rtdc.core.model.User;

import java.sql.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AsteriskRealTimeService {

    // Run Asterisk requests on separate threads to not cause server latency
    private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(4);

    private static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String hostname = Config.ASTERISK_IP;
            String dbName = "asterisk_realtime";
            String dbUserName = "user";
            String dbPassword = "password";
            DriverManager.setLoginTimeout(3);   // The time (in seconds) it takes until we declare the connection request timed out
            return DriverManager.getConnection("jdbc:mysql://"+hostname+"/"+dbName, dbUserName, dbPassword);
        } catch (ClassNotFoundException e) {
            Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "MySQL JDBC Driver not found!");
        } catch (SQLException e) {
            Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "Could not connect to the Asterisk database; " + e.getMessage());
        }
        return null;
    }

    public static void addUser(final User user, final String password){
        executor.submit(new Runnable(){
            @Override
            public void run() {
                Connection connection = getConnection();
                if (connection == null) {
                    Logger.getLogger(AsteriskRealTimeService.class.getName()).log(
                            Level.SEVERE, user.getUsername() + " was NOT added to Asterisk; no connection was established");
                    return;
                }
                Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.INFO, "Adding user " + user.getUsername() + " to Asterisk with extension " + user.getId());
                String sipQuery = "INSERT INTO sip_buddies (" +
                        " NAME, defaultuser, callerid, secret, context, HOST, TYPE, allow) VALUES (" +
                        " ?," + " ?," + " ?," + " ?," +
                        " 'users', 'dynamic', 'friend'," +                      // context, host, type
                        " 'g729;ilbc;gsm;ulaw;alaw;vp8');";                     // allow (should be updated with which codecs we want users to be able to use)
                String extensionQuery = "INSERT INTO extensions (context, exten, priority, app, appdata) VALUES (" +
                        " 'users', ?, 1, 'Dial', ?);";
                try {
                    PreparedStatement sipStatement = connection.prepareStatement(sipQuery);
                    sipStatement.setString(1, user.getUsername());                                  // name
                    sipStatement.setString(2, user.getUsername());                                  // defaultuser
                    sipStatement.setString(3, user.getFirstName() + " " + user.getLastName());      // callerid
                    sipStatement.setString(4, password);                                            // secret
                    sipStatement.executeUpdate();

                    PreparedStatement extensionStatement = connection.prepareStatement(extensionQuery);
                    extensionStatement.setInt(1, user.getId());                                     // exten
                    extensionStatement.setString(2, "SIP/" + user.getUsername());                   // appdata
                    extensionStatement.executeUpdate();
                } catch (SQLException e) {
                    Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "SQL Query failed: " + sipQuery);
                    e.printStackTrace();
                } finally {<commit_msg>Revised Asterisk's addUser query<commit_after>package rtdc.web.server.service;

import rtdc.core.Config;
import rtdc.core.model.User;

import java.sql.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AsteriskRealTimeService {

    // Run Asterisk requests on separate threads to not cause server latency
    private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(4);

    private static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String hostname = Config.ASTERISK_IP;
            String dbName = "asterisk_realtime";
            String dbUserName = "user";
            String dbPassword = "password";
            DriverManager.setLoginTimeout(3);   // The time (in seconds) it takes until we declare the connection request timed out
            return DriverManager.getConnection("jdbc:mysql://"+hostname+"/"+dbName, dbUserName, dbPassword);
        } catch (ClassNotFoundException e) {
            Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "MySQL JDBC Driver not found!");
        } catch (SQLException e) {
            Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "Could not connect to the Asterisk database; " + e.getMessage());
        }
        return null;
    }

    public static void addUser(final User user, final String password){
        executor.submit(new Runnable(){
            @Override
            public void run() {
                Connection connection = getConnection();
                if (connection == null) {
                    Logger.getLogger(AsteriskRealTimeService.class.getName()).log(
                            Level.SEVERE, user.getUsername() + " was NOT added to Asterisk; no connection was established");
                    return;
                }
                Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.INFO, "Adding user " + user.getUsername() + " to Asterisk with extension " + user.getId());
                String sipQuery = "INSERT INTO sip_buddies (" +
                        " NAME, defaultuser, callerid, secret, context, HOST, TYPE) VALUES (" +
                        " ?," + " ?," + " ?," + " ?," +
                        " 'users', 'dynamic', 'friend');";          // context, host, type
                String extensionQuery = "INSERT INTO extensions (context, exten, priority, app, appdata) VALUES (" +
                        " 'users', ?, 1, 'Dial', ?);";
                try {
                    PreparedStatement sipStatement = connection.prepareStatement(sipQuery);
                    sipStatement.setString(1, user.getUsername());                                  // name
                    sipStatement.setString(2, user.getUsername());                                  // defaultuser
                    sipStatement.setString(3, user.getFirstName() + " " + user.getLastName());      // callerid
                    sipStatement.setString(4, password);                                            // secret
                    sipStatement.executeUpdate();

                    PreparedStatement extensionStatement = connection.prepareStatement(extensionQuery);
                    extensionStatement.setInt(1, user.getId());                                     // exten
                    extensionStatement.setString(2, "SIP/" + user.getUsername());                   // appdata
                    extensionStatement.executeUpdate();
                } catch (SQLException e) {
                    Logger.getLogger(AsteriskRealTimeService.class.getName()).log(Level.SEVERE, "SQL Query failed: " + sipQuery);
                    e.printStackTrace();
                } finally {
{'fraction_non_alphanumeric': 0.1049058537210196, 'fraction_numerical': 0.002817983860637889, 'mean_word_length': 1.8833087149187593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5713023', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1835, 'n_words': 438}