package com.csc.library.mail;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import javax.ejb.CreateException;
import javax.ejb.SessionContext;

import com.csc.library.database.ConnectDB;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.UProfile;
/**
 * @author SoCool
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class MailService implements DbMail {
	SessionContext sessionContext;
	protected HashMap data;
	protected UProfile upf=null;
	private String mailTable = "";
	private String mailServer = "203.121.145.65";
	//private String dbName = "";
	//private String schemaName = "";
	public MailService() {
		//schemaName = new InitialEnvironment("GLOBAL").getValue("schema-name");
		data = new HashMap();
		this.mailTable = "Mmail";
	}
	public void setUProfile(UProfile upf) {
		this.upf = upf;
	}
	
	protected boolean execute() {
		try {
			//SendMail sm = new SendMail("203.121.145.65", this.data.get("mfrom").toString(),this.data.get("msendto").toString(), "", "test", "Hello, test mail");
			SendMail sm = new SendMail();
			sm.setData("hostname", this.mailServer);
			sm.setData("from", this.data.get("mfrom").toString());
			sm.setData("to", this.data.get("msendto").toString());
			sm.setData("cc", this.data.get("mcc").toString());
			sm.setData("subject", this.data.get("msubject").toString());
			sm.setData("content", this.data.get("mmessage").toString());
			sm.send();
			return true;
		} catch (Exception e) {
			MyLog.error(this, this.upf, e); 
			return false;
		}
	}
	public void run(HashMap data) {
		this.data = data;
		this.runningState();
		if (this.execute())
			this.completeState();
		else
			this.errorState();
	}
	private String getPath() {
		return new InitialEnvironment("GLOBAL").getValue("temp-dir");
	}
	public void runningState() {
		try {
			Connection con = this.initConnection();
			Statement st = con.createStatement();
			st.executeUpdate("update " + new InitialEnvironment("GLOBAL").getValue("schema-name") + "." + this.mailTable + " set mstatus='R'");
		} catch (Exception ex) {
			MyLog.error(this, this.upf, ex); 
		}
	}
	public void errorState() {
		try {
			Connection con = this.initConnection();
			Statement st = con.createStatement();
			st.executeUpdate("update " + new InitialEnvironment("GLOBAL").getValue("schema-name") + "." + this.mailTable + " set mstatus='E'");
		} catch (Exception ex) {
			MyLog.error(this, this.upf, ex); 
		}
	}
	public void completeState() {
		try {
			Connection con = this.initConnection();
			Statement st = con.createStatement();
			st.executeUpdate("update " + new InitialEnvironment("GLOBAL").getValue("schema-name") + "." + this.mailTable + " set mstatus='C'");
		} catch (Exception ex) {
			MyLog.error(this, this.upf, ex); 
		}
	}
	private void closeConnection(Connection con, Statement st) {
		try {
			if (st != null)
				st.close();
			if (con != null)
				con.close();
		} catch (Exception ex) {
			MyLog.error(this, this.upf, ex); 
		}
	}
	public void ejbRemove() {
		/**@todo Complete this method*/
	}
	public void ejbActivate() {
		/**@todo Complete this method*/
	}
	public void ejbPassivate() {
		/**@todo Complete this method*/
	}
	public void setSessionContext(SessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}
	public void ejbCreate() throws CreateException {
		/**@todo Complete this method*/
	}
	private Connection initConnection() {
		Connection con = null;
//		con = new ConnectDB(new InitialEnvironment("GLOBAL").getValue("db-name")).getConnection();
		try {
			con = StaticProperties.myConnection.getConnection(new InitialEnvironment("GLOBAL").getValue("db-name"),upf);
		} catch (Exception e) {
			MyLog.error(this, this.upf, e); 
		}
		return con;
	}
	public HashMap getProcess() {
		HashMap rec = new HashMap();
		try {
			Connection con = this.initConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from " + new InitialEnvironment("GLOBAL").getValue("schema-name") + "." + this.mailTable + " where mstatus='A'");
			int i = 0;
			while (rs.next()) {
		//		System.out.println("Next In Process .....");
				data.put("mailid", rs.getString("mailid"));
				data.put("msubject", rs.getString("msubject"));
				data.put("mfrom", rs.getString("mfrom"));
				data.put("msender", rs.getString("msender"));
				data.put("mtime", rs.getTime("mtime"));
				data.put("msendto", rs.getString("msendto"));
				data.put("mcc", rs.getString("mcc"));
				data.put("mbcc", rs.getString("mbcc"));
				data.put("mforward", rs.getString("mforward"));
				data.put("mattach", rs.getString("mattach"));
				data.put("mmessage", rs.getString("mmessage"));
				data.put("mcondition", rs.getString("mcondition"));
				data.put("mstatus", rs.getString("mstatus"));
				data.put("mtype", rs.getString("mtype"));
				data.put("mformat", rs.getString("mformat"));
				data.put("senddatetime", rs.getTimestamp("senddatetime"));
				rec.put(new Integer(i++), data);
			}
		} catch (Exception e) {
			MyLog.error(this, this.upf, e); 
		}
		return rec;
	}
	/**
	 * Returns the mailServer.
	 * @return String
	 */
	public String getMailServer() {
		return mailServer;
	}
	/**
	 * Returns the mailTable.
	 * @return String
	 */
	public String getMailTable() {
		return mailTable;
	}
	/**
	 * Sets the mailServer.
	 * @param mailServer The mailServer to set (ip address)
	 */
	public void setMailServer(String mailServer) {
		this.mailServer = mailServer;
	}
	/**
	 * Sets the mailTable.
	 * @param mailTable The mailTable to set
	 */
	public void setMailTable(String mailTable) {
		this.mailTable = mailTable;
	}
}