<%@ page import="javax.naming.InitialContext,
                 javax.sql.DataSource,
                 javax.naming.Context,
                 java.util.Hashtable,
                 javax.naming.NamingException,
                 java.sql.*,
                 java.util.Enumeration,
                 java.util.StringTokenizer" %>

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
    <title>Reset null emails addresses to dummyvalue@nowhere.org</title>

</head>

<body>
    <p>
    <h3>Atlassian Database Debug Utility.</h3>
    <p>
    Use this JSP to reset null emails addresses to dummyvalue@nowhere.org
    </p>
    <%

        String resetNullEmailsSQL = "update os_propertyentry set string_val= 'dummyvalue@nowhere.org'\n" +
"where oid in (select os_propertyentry.oid from os_user, os_propertyentry\n" +
"where os_propertyentry.entity_id = os_user.id \n" +
"and os_propertyentry.entity_key='email' \n" +
"and os_propertyentry.string_val is NULL);";

        String showResetEmailAccounts = "select username from os_user where id in (select entity_id from os_propertyentry where string_val='dummyvalue@nowhere.org');";

        String operation = request.getParameter("operation");
        String driverStr = request.getParameter("driver");
        String jdbcURL = request.getParameter("jdbcURL");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String initialContext = request.getParameter("initialContext");
        String provider = request.getParameter("provider");
        String ds = request.getParameter("ds");
        String query = request.getParameter("query");

        if (operation == null)
        {
            out.println("Please choose an operation below: ");
        }
        else if ("reset".equals(operation))
        {
            operation = null;
            driverStr = null;
            jdbcURL = null;
            username = null;
            password = null;
            initialContext = null;
            provider = null;
            ds = null;
        }
        else if (operation != null && "standard".equals(operation))
        {
            out.println("Connecting to database via JDBC URL:");

            try
            {
                out.println("<ol>");
                out.print("<li>Found driver (" + driverStr + ") on classpath? ");
                Driver driver = null;
                try
                {
                    driver = (Driver) Class.forName(driverStr).newInstance();

                    if (driver == null)
                        throw new ClassNotFoundException("Could not find " + driverStr + " on classpath.");

                    out.println("<strong class=\"good\">ok</strong>");
                }
                catch (java.lang.ClassNotFoundException e)
                {
                    out.println("<em class=\"bad\">Driver failed to load. Check your classpath: " + e + "</em>");
                }

                out.print("<li>Open a connection to the database via JDBC (" + jdbcURL + ")? ");

                try
                {
                    Class.forName(driverStr);
                    Connection conn = DriverManager.getConnection(jdbcURL, username, password);
                    Statement st = conn.createStatement();

                    try
                    {
                        int changed = st.executeUpdate(resetNullEmailsSQL);

                        if (changed != 0)
                        {
                            out.println("<em class=\"bad\"><strong>Done</strong> &hellip; listing alterations.</em>");

                            ResultSet rs = st.executeQuery(showResetEmailAccounts);

                            out.println(" <br/> <ul>");

                            while (rs.next())
                            {
                                out.println("<li> " + rs.getString("username"));
                            }

                            out.println("</ol>");
                        }
                        else
                        {
                            out.println("<em class=\"bad\">No users with null emails</em>");
                        }
                    }
                    catch (SQLException e)
                    {
                        throw e;
                    }


                    conn.close();
                }
                catch (SQLException e)
                {
                    out.println("<em class=\"bad\">Connection failed to open on the JDBC URL: " + e + "</em>");
                }
            }
            catch(Exception e)
            {
                out.println(e);
            }
                out.println("</ol>");
        }


        if (driverStr == null)
            driverStr = "";

        if (jdbcURL == null)
            jdbcURL = "";

        if (username == null)
            username = "";

        if (password == null)
            password = "";

        if (ds == null)
            ds = "";

        if (initialContext == null)
            initialContext = "";

        if (provider == null)
            provider = "";

    %>
    <p/>
    <p/>
    Enter your database connection details to convert null email addresses to 'dummyvalue@nowhere.org'.
    <p/>
        <form name="dbform" method="post" action="reset_nullemails.jsp">
            <input type="hidden" name="operation" value="standard" />
            <table border="0" width="100%">
                <tr>
                    <td width="20%">
                        Driver Class Name: &nbsp;
                     </td>
                     <td width="80%">
                        <input type="text" name="driver" value="<%=driverStr %>" size="40"/>
                    </td>
                </tr>
                <tr>
                     <td>
                         JDBC URL: &nbsp;
                     </td>
                     <td>
                         <input type="text" name="jdbcURL" value="<%=jdbcURL %>" size="60"/>
                     </td>
                 </tr>
                <tr>
                    <td>
                        DB username: &nbsp;
                     </td>
                     <td>
                        <input type="text" name="username" value="<%=username %>" size="40"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        DB password: &nbsp;
                     </td>
                     <td>
                        <input type="text" name="password" value="<%=password %>" size="40"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        Query: &nbsp;
                     </td>

                </tr>

                <tr>
                    <td>
                        <input type="submit" value="Change null emails to dummy@value.org" />
                    </td>
                </tr>
          </table>
        </form>
    <p/>

    <p/>
        <form method="post" action="reset_nullemails.jsp">
            <input type="hidden" name="operation" value="reset" />
            <input type="submit" value="Reset Everything" />
        </form>

    <p>

    <p/>


</body>
</html>