<commit_before>package org.tsugi.impl.jdbc;

import org.tsugi.*;

import org.tsugi.base.*;

import org.tsugi.util.TsugiUtils;
import org.tsugi.util.TsugiLTIUtils;

import java.util.Properties;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;  

import org.apache.commons.lang3.StringUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Tsugi_JDBC extends BaseTsugi implements Tsugi
{

    private Log log = LogFactory.getLog(Tsugi_JDBC.class);

    // The prefix for all of the tables
    private String prefix = null;

   /**
     * Get a tsugi-suitable connection
     */
    public Connection getConnection()
    {
        String jdbc = System.getProperty("tsugi.datasource.url");
        String username = System.getProperty("tsugi.datasource.username");
        String password = System.<PASSWORD>("<PASSWORD>");
        String className = System.getProperty("tsugi.datasource.driverClassName");
        prefix = System.getProperty("tsugi.datasource.tablePrefix");

        if ( jdbc == null | username == null || password == null || className == null )
        {
            Properties props = TsugiUtils.loadProperties("/tsugi.properties");
            if ( props != null ) {
                jdbc = props.getProperty("tsugi.datasource.url");
                username = props.getProperty("tsugi.datasource.username");
                password = props.getProperty("tsugi.datasource.password");
                className = props.getProperty("tsugi.datasource.driverClassName");
                prefix = props.getProperty("tsugi.datasource.tablePrefix");
            }
        }

        if ( jdbc == null | username == null || password == null || className == null )
        {
            log.error("Please add to system properties or /tsugi.properties in classpath");
            log.error("tsugi.datasource.url="+jdbc);
            log.error("tsugi.datasource.username="+jdbc);
            log.error("tsugi.datasource.password=*****");
            log.error("tsugi.datasource.driverClassName="+className);
            log.error("tsugi.datasource.tablePrefix="+prefix);
            throw new java.lang.RuntimeException("Missing essential Tsugi JDBC properties:");
        }

        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            log.error("Missing JDBC Driver: "+className);
            throw new java.lang.RuntimeException("Missing JDBC Driver: "+className);
        }

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(jdbc, username, password);

        } catch (SQLException e) {
            log.error("Your database server may be down.  Or if it is up your database is missing or inaccessible.");
            log.error("Install the PHP version of Tsugi from www.tsugi.org and use that tool to provision an empty tsugi database.");
            throw new java.lang.RuntimeException("Database server is down or tsugi database is missing");
        }

        return connection;
    }

   /**
     * Fire up the launch.  If req is null - we behave as if this is a test.
     */
    public Launch getLaunch(HttpServletRequest req, Properties props, HttpServletResponse res)
    {
        
        HttpSession session = null;
        if ( req != null ) {  // Allow for testing
            session = req.getSession();
        }

        BaseLaunch launch = new BaseLaunch();
        String x = null;

        Connection c = getConnection();
        if ( c == null ) {
            log.error("Unable to upen database connection");
            launch.error_message = "Unable to upen database connection";
            return launch;
        }

        if ( ! TsugiLTIUtils.isRequest(props) ) {
            System.out.println("TODO: Pull in from session");
            if ( session == null ) {  // Test harness
                launch.error_message = "This tool must be launched using LTI";
                return launch;
            }
            // Pull in the previous row if it is in the session
            Properties sess_row = (Properties) session.getAttribute("lti_row");
            if ( sess_row == null ) {
                launch.error_message = "This tool must be launched using LTI";
                return launch;
            }
            x = TsugiUtils.dumpProperties(sess_row);
            System.out.println("Session Properties:");
            System.out.println(x);
            buildLaunch(c, launch, req, res, sess_row);
            return launch;
        }

        // Start fresh
        if ( session != null ) session.removeAttribute("lti_row");
        // x = TsugiUtils.dumpProperties(props);
        // System.out.println("Input POST Properties:");
        // System.out.println(x);

        Properties post = extractPost(props);
        if ( post == null ) {
            log.error("Missing essential POST data");
            launch.error_message = "Missing essential POST data";
            return launch;
        }
        x = TsugiUtils.dumpProperties(post);<commit_msg>Remove debug<commit_after>package org.tsugi.impl.jdbc;

import org.tsugi.*;

import org.tsugi.base.*;

import org.tsugi.util.TsugiUtils;
import org.tsugi.util.TsugiLTIUtils;

import java.util.Properties;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;  

import org.apache.commons.lang3.StringUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Tsugi_JDBC extends BaseTsugi implements Tsugi
{

    private Log log = LogFactory.getLog(Tsugi_JDBC.class);

    // The prefix for all of the tables
    private String prefix = null;

   /**
     * Get a tsugi-suitable connection
     */
    public Connection getConnection()
    {
        String jdbc = System.getProperty("tsugi.datasource.url");
        String username = System.getProperty("tsugi.datasource.username");
        String password = System.<PASSWORD>("<PASSWORD>.datasource.password");
        String className = System.getProperty("tsugi.datasource.driverClassName");
        prefix = System.getProperty("tsugi.datasource.tablePrefix");

        if ( jdbc == null | username == null || password == null || className == null )
        {
            Properties props = TsugiUtils.loadProperties("/tsugi.properties");
            if ( props != null ) {
                jdbc = props.getProperty("tsugi.datasource.url");
                username = props.getProperty("tsugi.datasource.username");
                password = props.getProperty("tsugi.datasource.password");
                className = props.getProperty("tsugi.datasource.driverClassName");
                prefix = props.getProperty("tsugi.datasource.tablePrefix");
            }
        }

        if ( jdbc == null | username == null || password == null || className == null )
        {
            log.error("Please add to system properties or /tsugi.properties in classpath");
            log.error("tsugi.datasource.url="+jdbc);
            log.error("tsugi.datasource.username="+jdbc);
            log.error("tsugi.datasource.password=*****");
            log.error("tsugi.datasource.driverClassName="+className);
            log.error("tsugi.datasource.tablePrefix="+prefix);
            throw new java.lang.RuntimeException("Missing essential Tsugi JDBC properties:");
        }

        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            log.error("Missing JDBC Driver: "+className);
            throw new java.lang.RuntimeException("Missing JDBC Driver: "+className);
        }

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(jdbc, username, password);

        } catch (SQLException e) {
            log.error("Your database server may be down.  Or if it is up your database is missing or inaccessible.");
            log.error("Install the PHP version of Tsugi from www.tsugi.org and use that tool to provision an empty tsugi database.");
            throw new java.lang.RuntimeException("Database server is down or tsugi database is missing");
        }

        return connection;
    }

   /**
     * Fire up the launch.  If req is null - we behave as if this is a test.
     */
    public Launch getLaunch(HttpServletRequest req, Properties props, HttpServletResponse res)
    {
        
        HttpSession session = null;
        if ( req != null ) {  // Allow for testing
            session = req.getSession();
        }

        BaseLaunch launch = new BaseLaunch();
        String x = null;

        Connection c = getConnection();
        if ( c == null ) {
            log.error("Unable to upen database connection");
            launch.error_message = "Unable to upen database connection";
            return launch;
        }

        if ( ! TsugiLTIUtils.isRequest(props) ) {
            if ( session == null ) {  // Test harness
                launch.error_message = "This tool must be launched using LTI";
                return launch;
            }
            // Pull in the previous row if it is in the session
            Properties sess_row = (Properties) session.getAttribute("lti_row");
            if ( sess_row == null ) {
                launch.error_message = "This tool must be launched using LTI";
                return launch;
            }
            x = TsugiUtils.dumpProperties(sess_row);
            System.out.println("Session Properties:");
            System.out.println(x);
            buildLaunch(c, launch, req, res, sess_row);
            return launch;
        }

        // Start fresh
        if ( session != null ) session.removeAttribute("lti_row");
        // x = TsugiUtils.dumpProperties(props);
        // System.out.println("Input POST Properties:");
        // System.out.println(x);

        Properties post = extractPost(props);
        if ( post == null ) {
            log.error("Missing essential POST data");
            launch.error_message = "Missing essential POST data";
            return launch;
        }
        x = TsugiUtils.dumpProperties(post);
{'fraction_non_alphanumeric': 0.10715970191491368, 'fraction_numerical': 0.00018866144703329874, 'mean_word_length': 2.2956170345041964, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7480813', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2775, 'n_words': 719}