/*
 * Created on 26.05.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package test.james;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Eugene Drobitko
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CreateUsers {

	public static final String IP = "127.0.0.1";
	public static final int PORT = 4555;


	private Socket socket;
	private BufferedReader in = null;
	private PrintWriter out = null;

	private URL url = null;

	boolean errorOnServer = false;

	private Connection conn;
	
//	public static final String JDBC_CLASS = "com.mckoi.JDBCDriver";
	public static final String JDBC_CLASS = "org.gjt.mm.mysql.Driver";
	
//	public static final String JDBC_URL = "jdbc:mckoi://localhost/";
	public static final String JDBC_URL = "jdbc:mysql://localhost/JMail?user=otp&password=otp";


	/**
	 * 
	 */
	public CreateUsers() throws SQLException, ClassNotFoundException {
		super();
		Class.forName(JDBC_CLASS);

		conn = DriverManager.getConnection(JDBC_URL);
	}

	private String read() {
		String tmp = "{read} : ";
		try {
			tmp += in.readLine();
			while (in.ready()) {
				tmp += "\n\t" + in.readLine();
			}
		} catch (IOException oops) {
			tmp += "Exception while reading " + oops.getMessage();
		}

		//		if (!errorOnServer)
		//			errorOnServer =
		//				tmp.startsWith("{read} : 50") || tmp.startsWith("{read} : 55") || tmp.startsWith("{read} : 45");
		return tmp;
	}

	private String write(String string) {
		String tmp = "{write} : " + string;
		out.println(string);
		return tmp;
	}

	/**
	 * 
	 */
	public void create(String prefix, int begNumber, int endNumber) {
		errorOnServer = false;

		try {
			url = new URL("http://" + IP + ":" + PORT);
		} catch (Exception oops) {
		}

		System.out.println("Start inet process for url : " + url);

		try {
			socket = new Socket(url.getHost(), url.getPort());
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out =
				new PrintWriter(
					new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
					true);

			System.out.println(read());
			System.out.println(write("root"));
			System.out.println(read());
			System.out.println(write("root"));
			System.out.println(read());

			for (int i = begNumber; i <= endNumber; i++) {
				String action = "adduser " + prefix + i + " " + i;
				System.out.println(write(action));
				System.out.println(read());
			}

			System.out.println(write("quit"));

		} catch (Exception oops) {
			oops.printStackTrace();
		} finally {
			if (socket != null)
				try {
					socket.close();
				} catch (IOException e) {
				}
		}
	}

	public void addToList(String listName, String prefix, int begNumber, int endNumber) {
		try {

			Thread.currentThread().sleep(10000);
			Statement st = conn.createStatement();
			System.out.println("Start adding users to mail-list");
			for (int i = begNumber; i <= endNumber; i++) {
				String str =
					"INSERT INTO lists (listName, listSubscriber) VALUES('list-james', '"
						+ prefix
						+ i
						+ "@localhost')";
				System.out.println("Execute SQL = " + str);
				try {
					st.executeUpdate(str);
					System.out.println("OK.");
				} catch (Exception oops) {
					System.out.println(oops.getMessage());
				}
			}

		} catch (Exception oops) {
			oops.printStackTrace();
		}

	}

	public static void main(String[] args) {
		try {
			CreateUsers cu = new CreateUsers();
			long begTime = System.currentTimeMillis();			
			cu.create("testUser", 1, 3500);			
			cu.addToList("list-james", "testUser", 1, 3500);
			System.out.println(
				"Was done in " + ((System.currentTimeMillis() - begTime) / (1000)) + " seconds");
			System.out.println("Done.");
		} catch (Throwable oops) {
			oops.printStackTrace();
		}
	}
}
