package market.MDVServer;

import java.io.*;
import java.sql.*;
import java.net.*;
import java.net.URL.*;
import javax.servlet.*;
import javax.servlet.http.*;
import market.util.text.*;

/**
 * main class for the market day values server <br>
 * Creation date: (1/2/00 9:37:02 PM) <br>
 * @author: lex <br>
 * <br>
 * function: recieve and proceed wr info from rwServer <br>
 * commadn string: <br>
 * MDVServer?ReqType=1&wrKey=609&numWord=2&rwKey1=353&rwCounts1=3&rwKey2=609&rwCounts2=1 <br>
 * returns: <br>
 * nothing <br>
 *
 * function: setup the servlet <br>
 * commadn string: <br>
 * MDVServer?ReqType=2 <br>
 * returns: <br>
 * html <br>
 *
 * function: change servlet parameters <br>
 * commadn string: <br>
 * MDVServer?ReqType=3&name=MYSQLDriver&value=org.gjt.mm.mysql.Driver <br>
 * returns: <br>
 * html <br>
 *
 * function: saves servlet parameters to file<br>
 * commadn string: <br>
 * MDVServer?ReqType=4 <br>
 * returns: <br>
 * html <br>
 *
 * function: re-init <br>
 * commadn string: <br>
 * MDVServer?ReqType=5 <br>
 * returns: <br>
 * nothing <br>
 *
 */
public class MDVServer extends javax.servlet.http.HttpServlet implements MDVInterFace {
	private static int mdvMarketKey = 0;
	private static Connection mdvDBConn;
	private static String mdvTableName;
	private static LogPrinter LogOutput;
	private static ConfigMDVServer mdvConfig;
	private static Object mdvDBSync = new Object();
	private static Object mdvRWSync = new Object();
	public final static String ver = "AAA003 | 000624";
/**
 * MDVServer constructor. <br>
 */
public MDVServer() {
	super();
}
/**
 * the destroy method of the servlet <br>
 * closes the connection to the db using the mdvDBDisConnect() method <br>
 * Creation date: (15.2.00 15:01:42) <br>
 */
public void destroy() 
{
 mdvDBDisConnect();
}
/**
 * this is the init method of the servlet. <br>
 * estaablishe a db connection using mdvDBConnect() method <br>
 * Creation date: (15.2.00 15:37:51) <br>
 */
public void init(ServletConfig config) throws ServletException {
	super.init(config); //This statement must always be here !!!
	mdvInit();
}
/**
 * Change param's value. part f the configuration part of this servlet <br>
 * Creation date: (07.3.2000 a. 16:38:17) <br>
 * @param req HttpServletRequest <br>
 */
public void mdvChangeParams(HttpServletRequest req) {
	String paramName = "";
	String paramValue = "";
	try {
		paramName = req.getParameter(MDVConstants.sName);
		paramValue = req.getParameter(MDVConstants.sValue);
	} catch (NumberFormatException e) {
		LogOutput.log("bad data type for config parameter name", LogOutput.ERROR);
		LogOutput.flush();
		return;
	}
	mdvConfig.setParam(paramName, paramValue);
}
/**
 * return false if the connection is null or closed <br>
 * Creation date: (3.5.00 16:14:03) <br>
 * @return boolean <br>
 */
private boolean mdvCheckConnection() {
	try {
		if (!mdvDBConn.isClosed() && mdvDBConn != null)
			return true;
		else
			return false;
	} catch (Exception E) {
		return false;
	}
}
/**
 * check if the table pointed by the mdvTableName field exist in the mdv database. <br>
 * Creation date: (1/3/00 11:53:12 PM) <br>
 * @return boolean - if the table exists, return true  <br>
 */
private boolean mdvCheckIfExist(String tableName) {
	String SQLQuery = "SHOW TABLES LIKE \"" + tableName + "\"";
	boolean Res = false;
	boolean exit = true;
	int errorr_counter = 0;
	do {
		try {
			Statement Stmt = mdvDBConn.createStatement();
			ResultSet RS = Stmt.executeQuery(SQLQuery);
			if (RS.next())
				Res = true;
			else
				Res = false;
			RS.close();
			Stmt.close();
			exit = true;
		} catch (SQLException E) {
			LogOutput.log("fail on checking the existance of the day table - " + E.getMessage(), LogOutput.ERROR);
			LogOutput.flush();
			exit = !mdvServeConnection();
			errorr_counter++;
		}
	} while (!exit && errorr_counter < 3);
	return Res;
}
/**
 * create a new table for today in the mDayValues db. <br>
 * Creation date: (1/3/00 1:11:49 PM) <br>
 */
private void mdvCreateDayTable(String tableName) {
	if (mdvCheckIfExist(tableName))
		return;
	int errorr_counter = 0;
	synchronized (mdvRWSync) {
		if (mdvCheckIfExist(tableName))
			return;
		String SQLStat = "CREATE TABLE " + tableName + " (" + MDVConstants.sDBFieldDCRwKey + " " + MDVConstants.sDBFieldTypeBIGINT + ", " + MDVConstants.sDBFieldDCRwCounts + " " + MDVConstants.sDBFieldTypeBIGINT + ", " + MDVConstants.sDBFieldDCRwVolatil + " " + MDVConstants.sDBFieldTypeBIGINT + ", " + MDVConstants.sDBFieldDCRwVulgarity + " " + MDVConstants.sDBFieldTypeBIGINT + ")";
		boolean exit = true;
		do {
			try {
				Statement Stmt = mdvDBConn.createStatement();
				Stmt.executeQuery(SQLStat);
				Stmt.close();
				exit = true;
			} catch (SQLException E) {
				LogOutput.log("fail on creating day table - SQLException: " + E.getMessage(), LogOutput.ERROR);
				LogOutput.flush();
				exit = !mdvServeConnection();
				errorr_counter++;
			}
		} while (!exit && errorr_counter < 3);
	}
}
/**
 * establish a connection with the db. <br>
 * Creation date: (1/3/00 1:24:36 PM) <br>
 */
private boolean mdvDBConnect() {
	int countOfTries = 3;
	boolean flag = true;
	try {
		if (mdvDBConn == null || mdvDBConn.isClosed()) {
			synchronized (mdvDBSync) {
				for (int counter = 0; counter < countOfTries && (mdvDBConn == null || mdvDBConn.isClosed()); counter++) {
					try {
						mdvDBConn = DriverManager.getConnection(mdvConfig.sDBLocation + mdvConfig.sDBName, mdvConfig.sDBUser, mdvConfig.sDBPass);
						LogOutput.log("db connected", LogOutput.INFO);
						LogOutput.flush();
						flag = true;
					} catch (SQLException E) {
						LogOutput.log("fail on connecting with the db - " + E.getMessage(), LogOutput.ERROR);
						LogOutput.flush();
						flag = false;
					}
				}
			}
		}
	} catch (SQLException E) {
		LogOutput.log("fail on connecting with the db - " + E.getMessage(), LogOutput.ERROR);
		LogOutput.flush();
		flag = false;
	}
	return flag;
}
/**
 * Closes the connection to the database <br>
 * Creation date: (1/3/00 1:24:58 PM) <br>
 */
private boolean mdvDBDisConnect() {
	try {
		if (mdvDBConn != null) mdvDBConn.close();
		LogOutput.log("db conn closed", LogOutput.ERROR);
	} catch (Exception e) {
		LogOutput.log("cannot close the db conn", LogOutput.ERROR);
		LogOutput.flush();
		return false;
	}
	return true;
}
/**
 * Return Web page to browser. <br> This is the configuration page of MDVServer <br> 
 * Creation date: (07/03/00 08:21:06 AM)
 * @param fileOut java.io.PrintWriter
 */
public void mdvFormConfigParam(PrintWriter fileOut) {
 String serverName ="MDVServer?";
	fileOut.println("<HTML>");
	fileOut.println("<HEAD>");
	fileOut.println("<TITLE>Param configurations of MDVServer</TITLE>");
	fileOut.println("<SCRIPT Language = \"JavaScript\">");
	fileOut.println("<!-- ");
	fileOut.println("function change(obj){");
	fileOut.println("this.window.location = \""+ serverName+ MDVConstants.sReqType + "=" + 3 +"&"+ MDVConstants.sName+"=\"+obj.name+\"&"+ MDVConstants.sValue+"=\"+obj.paramName.value; \n}");
//	fileOut.println("function b_over(){");	
//	fileOut.println("this.window.location = \""+ mdvConfig.sSDVServerLocation+ "?" + MDVConstants.sReqType + "=" + Const.GET_OVERVIEW_PAGE +"\";");	
//	fileOut.println("}");
//	fileOut.println("function b_conf(){");	
//	fileOut.println("this.window.location = \""+ mdvConfig.sSDVServerLocation+ "?" + MDVConstants.sReqType + "=" + Const.GET_CONF_PAGE +"\";");	
//	fileOut.println("}");
	fileOut.println("function save_to(){");	
	fileOut.println("this.window.location = \""+ serverName + MDVConstants.sReqType + "=" + 4+"\";");	
	fileOut.println("}");
//	fileOut.println("function load_from(){");	
//	fileOut.println("this.window.location = \""+ mdvConfig.sSDVServerLocation+ "?" + MDVConstants.sReqType + "=" + Const.LOAD_CONF +"\";");	
//	fileOut.println("}-->");
	fileOut.println("-->");	
	fileOut.println("</SCRIPT>");

	fileOut.println("</HEAD>");
	fileOut.println("<BODY  text = \"black\">");
	fileOut.println("<H3>Param configurations of MDVServer</H3><HR>");
	fileOut.println("<input type =\"button\" value=\"Save to file\" onClick =\"save_to();\">");
//	fileOut.println("<input type =\"button\" value=\"Main Config\" onClick =\"b_conf();\">");
//	fileOut.println("<input type =\"button\" value=\"Save to file\" onClick =\"save_to();\">");
//	fileOut.println("<input type =\"button\" value=\"Load from file\" onClick =\"load_from();\">");
	fileOut.println("<HR>");
	fileOut.println("<TABLE  border = 2 cellpadding =5 cellspacing = 2 name=\"all\">");

	fileOut.println("<TR>");
	fileOut.println("	<TD>");
	fileOut.println("				<TABLE name=\"MDVServer\" border >");
	fileOut.println("				<TR>");
	fileOut.println("				<TH COLSPAN = 3 ALIGN = \"CENTER\">market Day Value Servlet<TH>");
	fileOut.println("				</TR>");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesMYSQLDriver+"</TD>");	
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesMYSQLDriver+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sMYSQLDriver +"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");
	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesDBName +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesDBName+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sDBName +"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");
	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesDBLocation +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesDBLocation+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sDBLocation +"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");
	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesDBUser +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesDBUser+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sDBUser +"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");

	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesDBPass +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesDBPass+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sDBPass+"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");

	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesLogFilePath +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesLogFilePath+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sLogFilePath+"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");

	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesSDVServerLocation +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesSDVServerLocation+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sSDVServerLocation+"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");

	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.namesMarketServletLocation +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.namesMarketServletLocation+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sMarketServletLocation+"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");
/*
	fileOut.println("\n");
	fileOut.println("				<TR>");
	fileOut.println("				<TD  width= 150>"+mdvConfig.sMDVCfgPath +"</TD>");
	fileOut.println("				<FORM NAME=\""+mdvConfig.sMDVCfgPath+"\">");
	fileOut.println("				<TD><INPUT TYPE=\"text\" SIZE = 50 NAME=\"paramName\" value=\""+ mdvConfig.sMDVCfgPath+"\"></TD>");
	fileOut.println("				<TD> <INPUT TYPE=\"button\" value=\"Change\" onClick= change(this.form)></TD>");
	fileOut.println("				</FORM>");
	fileOut.println("				</TR>");*/
	fileOut.println(" 			</TABLE>");
	fileOut.println("	</TD>");
	fileOut.println("</TR>");
	fileOut.println(" 			</TABLE>");
	fileOut.println("</BODY></HTML>\n");
	fileOut.close();
} // FormConfigPage
/**
 * Generates HTML overview page. part of the interface part of this servlet <br>
 * Creation date: (5.5.00 11:21:51) <br>
 * @param fileOut java.io.PrintWriter <br>
 */
public void mdvFormOverview(PrintWriter fileOut) {
	String serverNane="MDVServer?";
	fileOut.println("<HTML>");
	fileOut.println("<HEAD>");
	fileOut.println("<TITLE>MDVServer Overview</TITLE>");
	fileOut.println("<SCRIPT Language = \"JavaScript\">");
	fileOut.println("<!-- ");
	fileOut.println("function config(){");
	fileOut.println("this.window.location = \""+ serverNane + MDVConstants.sReqType + "=" + 2 +"\"; \n}");
	fileOut.println("function re_in(){");
	fileOut.println("this.window.location =\""+ serverNane + MDVConstants.sReqType + "=" + 5 + "\";\n}");
	fileOut.println("-->");
	fileOut.println("</SCRIPT>");
	fileOut.println("</HEAD>");
	fileOut.println("<BODY  text = \"black\">");
	fileOut.println("<HR><H3>MDVServer Overview</H3>");
	fileOut.println("<p>ver. " + ver);
	fileOut.println("<p>marketKey: " + mdvMarketKey);
	fileOut.println("<HR>");
	fileOut.println("<FORM name=\"logs\">");
	fileOut.println("<input type=\"button\" value=\"Configurations\" onClick=\"config();\">");
	fileOut.println("<input type=\"button\" value=\"Re_Init\"onClick=\"re_in();\">");
	fileOut.println("</FORM>");
	fileOut.println("</BODY></HTML>\n");
	fileOut.close();
}
/**
 * sends http request to the marketServlet for recieving market Key <br>
 * Creation date: (9.2.00 19:51:58) <br>
 */
private boolean mdvGetMarketKey() throws ServletException, IOException {
	int b;
	String Res = "";
	InputStream in;
	try {
		URL ServletURL = new URL(mdvConfig.sMarketServletLocation + MDVConstants.sMarketServletCommString);
		in = ServletURL.openStream(); // Open Input stream
		while ((b = in.read()) != -1) {
			Res = Res + String.valueOf((char) b);
		}
		in.close();
	} catch (IOException E) {
		LogOutput.log("Error connecting to marketServlet - Exception: " + E.getMessage(), LogOutput.ERROR);
		LogOutput.flush();
		return false;
	}
	try {
		mdvMarketKey = Integer.valueOf(Res).intValue();
	} catch (Exception E) {
		LogOutput.log("bad data type for marketKey from marketServlet: " + E.getMessage(), LogOutput.ERROR);
		LogOutput.flush();
		return false;
	}
	LogOutput.log("marketKey recieved" + mdvMarketKey, LogOutput.INFO);
	return true;
}
/**
 * contains all the init routines for the servlet <br>
 * Creation date: (12.3.2000 a. 20:14:04) <br>
 */
private void mdvInit() {
	mdvConfig = new ConfigMDVServer();
	String tempCfgPath = getInitParameter(MDVConstants.sCfgPath);
	if ( tempCfgPath != null )
		mdvConfig.sMDVCfgPath = tempCfgPath;
	mdvConfig.cfgLoadProperty(mdvConfig.sMDVCfgPath);
	LogOutput = LogPrinter.makeLogPrinter(mdvConfig.sLogFilePath);
	LogOutput.setLogLevel(LogPrinter.INFO);
	LogOutput.setScreenLogLevel(LogPrinter.ALL);
	mdvLoadDriver();
	if (mdvDBConnect())
		LogOutput.log("DB Connected", LogOutput.INFO);
	LogOutput.log("Init Servlet", LogOutput.INFO);
	LogOutput.flush();
	mdvMarketKey = 1;
}
/**
 * Loads the db driver <br>
 * it must be executed before trying to establish a connection with the db <br>
 * Creation date: (3.5.00 16:07:25) <br>
 * @return boolean <br>
 */
private boolean mdvLoadDriver() {
	// load the driver
	try {
		Class.forName(mdvConfig.sMYSQLDriver).newInstance();
		return true;
	} catch (Exception E) {
		LogOutput.log("fail on loading dbDriver " + E, LogOutput.ERROR);
		LogOutput.flush();
		return false;
	}
}
/**
 * put all the recieved info from the wrProcessor in the proper rwDayValuesTable <br>
 * Creation date: (1/3/00 1:19:21 AM) <br>
 */
public boolean mdvProcInfo(WordElement[] rws) throws IOException {
	// setting the tables names
	mdvSetTableName();
	String SecCommString = "";
	for (int Counter = 0; Counter < rws.length; Counter++) {
		mdvUpdateRW(rws[Counter].rwKey, rws[Counter].rwCount);
		SecCommString = SecCommString + "&" + MDVConstants.sRwKey + (Counter + 1) + "=" + rws[Counter].rwKey + "&" + MDVConstants.sRwCounts + (Counter + 1) + "=" + rws[Counter].rwCount;
	}
	String CommString = MDVConstants.sReqType + "=" + MDVConstants.sSDVServerReqType + "&" + MDVConstants.sMarketKey + "=" + mdvMarketKey + "&" + MDVConstants.sNumWord + "=" + rws.length + SecCommString;
	try {
		URL ServletURL = new URL(mdvConfig.sSDVServerLocation + CommString);
		InputStream in = ServletURL.openStream(); // Open Input stream
		in.close();
	} catch (IOException E) {
		LogOutput.log("fail on proceeding the info - IOException: " + E.getMessage(), LogOutput.ERROR);
		LogOutput.flush();
		return false;
	}
	return true;
}
/**
 * Saves the cfg file <br>
 * Creation date: (07.3.2000 a. 16:38:17) <br>
 * @param req HttpServletRequest  <br>
 */
public void mdvSaveCfgFile() {
	mdvConfig.cfgSaveProperty( mdvConfig.sMDVCfgPath );
}
/**
 * checks what is the state of the db connection and restores/establish it if needed <br>
 * Creation date: (3.5.00 16:18:25) <br>
 * @return boolean <br>
 */
private boolean mdvServeConnection() {
	if (!mdvCheckConnection()) {
		if (!mdvDBConnect()) {
			LogOutput.log("fail on restoring the db-connection", LogOutput.ERROR);
			LogOutput.flush();
			return false;
		} else {
			LogOutput.log("db-connection restored", LogOutput.INFO);
			LogOutput.flush();
			return true;
		}
	} else {
		LogOutput.log("db-connection checked, found active", LogOutput.INFO);
		LogOutput.flush();
		return true;
	}
}
/**
 * method invoked through the MDVInterFace from the marketServlet <br>
 * sets the marketKey which is part of the request to the system <br>
 * Creation date: (4.5.00 0:17:22) <br>
 * @param marketKey int <br>
 */
public void mdvSetMarketKey(int marketKey) {
	mdvMarketKey = marketKey;
}
/**
 * compose a proper table name according to the date <br>
 * check if such table exist and if not, creates it <br>
 * put the table's name in the mdvTableName field <br>
 * Creation date: (1/4/00 8:03:46 PM) <br>
 */
private void mdvSetTableName() {
	String ToDay = String.valueOf(new java.sql.Date(System.currentTimeMillis()));
	ToDay = ToDay.substring(0, 4) + ToDay.substring(5, 7) + ToDay.substring(8, 10);
	String newTableName = MDVConstants.sDBDC_DateTableName + ToDay;
	if (mdvTableName == null || !mdvTableName.equals(newTableName)) {
		synchronized (mdvDBSync) {
			mdvTableName = newTableName;
		}
		if (!mdvCheckIfExist(mdvTableName)) {
			mdvCreateDayTable(mdvTableName);
		}
	} else {
		if (mdvTableName.equals(newTableName)) {
			if (!mdvCheckIfExist(mdvTableName)) {
				mdvCreateDayTable(mdvTableName);
			}
		}
	}
}
/**
 * input the rw info in the mDayValues db <br>
 * try to update an existing info <br>
 * if there is not, insert a new record in the day table <br>
 * Creation date: (1/6/00 12:33:05 AM) <br>
 * @param rwKey int <br>
 * @param RWCounts int <br>
 */
private void mdvUpdateRW(long rwKey, long rwCounts) {
	String SQLStat;
	Statement Stmt;
	int ResCount = 0;
	boolean exit = true;
	int errorr_counter = 0;
	do {
		try {
			SQLStat = "UPDATE " + mdvTableName + " SET " + MDVConstants.sDBFieldDCRwCounts + " = " + MDVConstants.sDBFieldDCRwCounts + " + " + rwCounts + " where " + MDVConstants.sDBFieldDCRwKey + " = " + rwKey;
			Stmt = mdvDBConn.createStatement();
			ResCount = Stmt.executeUpdate(SQLStat);
			Stmt.close();
			if (ResCount == 0) {
				synchronized (mdvRWSync) {
					SQLStat = "UPDATE " + mdvTableName + " SET " + MDVConstants.sDBFieldDCRwCounts + " = " + MDVConstants.sDBFieldDCRwCounts + " + " + rwCounts + " where " + MDVConstants.sDBFieldDCRwKey + " = " + rwKey;
					Stmt = mdvDBConn.createStatement();
					ResCount = Stmt.executeUpdate(SQLStat);
					Stmt.close();
					if (ResCount == 0) {
						SQLStat = "INSERT INTO " + mdvTableName + " (" + MDVConstants.sDBFieldDCRwKey + ", " + MDVConstants.sDBFieldDCRwCounts + ")" + " VALUES (" + rwKey + ", " + rwCounts + ")";
						Stmt = mdvDBConn.createStatement();
						ResCount = Stmt.executeUpdate(SQLStat);
						Stmt.close();
					}
				}
			}
			exit = true;
		} catch (SQLException E) {
			LogOutput.log("fail on updating rw info in the local db - SQLException: " + E.getMessage(), LogOutput.ERROR);
			LogOutput.flush();
			exit = !mdvServeConnection();
			errorr_counter++;
		}
	} while (!exit && errorr_counter < 3);
}
/**
 * recieve and proceed all the http requests <br>
 * Creation date: (1/2/00 9:37:59 PM) <br>
 */
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException {
	int sTask;
	int numWord;
	PrintWriter out = res.getWriter();
	try {
		sTask = Integer.valueOf(req.getParameter(MDVConstants.sReqType)).intValue();
	} catch (NumberFormatException e) {
		out.println("false");
		out.close();
		LogOutput.log("bad data type of the reqType", LogOutput.ERROR);
		LogOutput.flush();
		return;
	}
	if (sTask < 0 && sTask > 5) {
		out.println("false");
		out.close();
		LogOutput.log("bad value of the reqType", LogOutput.ERROR);
		LogOutput.flush();
		return;
	}
	LogOutput.log("service requested", LogOutput.DEBUG);
	
	switch (sTask) {
		case 0 :
			// overview
			mdvFormOverview(out);
			break;
		case 1 :
			// get and proceed a wr info pack
			LogOutput.log("proceeding a wr pack...", LogOutput.DEBUG);
			// this is a temporary line
			if (mdvDBConn == null) {
				LogOutput.log("DB not connected at init. trying again...", LogOutput.ERROR);
				mdvDBConnect();
				if (mdvDBConn == null) {
					out.println("false");
					out.close();
					LogOutput.log("DB not connected at second try. abort", LogOutput.ERROR);
					LogOutput.flush();
					return;
				}
			}
			try {
				numWord = Integer.valueOf(req.getParameter(MDVConstants.sNumWord)).intValue();
			} catch (NumberFormatException e) {
				out.println("false");
				out.close();
				LogOutput.log("bad data type for numWord", LogOutput.ERROR);
				LogOutput.flush();
				return;
			}
			if (!(numWord > 0)) {
				out.println("false");
				out.close();
				LogOutput.log("numWord equals 0", LogOutput.ERROR);
				LogOutput.flush();
				return;
			}
			WordElement[] rws = new WordElement[numWord];
			for (int counter = 0; counter < numWord; counter++) {
				rws[counter] = new WordElement();
				rws[counter].rwKey = Long.valueOf(req.getParameter(MDVConstants.sRwKey + String.valueOf(counter + 1))).longValue();
				rws[counter].rwCount = Byte.valueOf(req.getParameter(MDVConstants.sRwCounts + String.valueOf(counter + 1))).byteValue();
				if (!(rws[counter].rwKey > 0 && rws[counter].rwCount > 0)) {
					out.println("false");
					out.close();
					LogOutput.log("rwKey or RwCount equals 0", LogOutput.ERROR);
					LogOutput.flush();
					return;
				}
			}
			// proceed the information
			if (!mdvProcInfo(rws)) {
				out.println("false");
				out.close();
				LogOutput.log("unable to proceed the info", LogOutput.ERROR);
				LogOutput.flush();
				return;
			}
			LogOutput.log("wr pack proceeded", LogOutput.DEBUG);
			break;
		case 2 :
			mdvFormConfigParam(out);
			break;
		case 3 :
			mdvChangeParams(req);
			mdvFormConfigParam(out);
			break;
		case 4 :
			mdvSaveCfgFile();
			mdvFormConfigParam(out);
			break;
		case 5 :
			// re - init the servlet
			mdvInit();
			mdvFormOverview(out);
			break;
	}
	LogOutput.flush();
	out.close();
}
}

