package market.market_util;

import java.io.*;
import java.net.*;
import java.util.*;
/**
 * This class contains all parameters and function for starting market_rwServer servlet
 * @author: Ivo
 */
 
public class market_wrConf 
{
	public  static String wrServerHostName = "www.market.esof";
	public  static String namewrServerHostName = "wrServerHostName";

	public  static int wrServerRMIPort = 1099;
	public  static String namewrServerRMIPort = "wrServerRMIPort";
		
 	public  static String S_wrServerHostName = "http://localhost:8080/servlet/wrServer?";	
	public  static String nameS_wrServerHostName = "S_wrServerHostName";

 	public  static String S_wrProcessorHostName = "http://www.market.esof:8080/servlet/MarketServlet?";		
	public  static String nameS_wrProcessorHostName = "S_wrProcessorHostName";		
		 
	public  static short csh_MaxCachFieldsInWR = 20;
	public  static String namecsh_MaxCachFieldsInWR = "csh_MaxCachFieldsInWR";
	
	public  static byte cb_CleaningFactor = 80;  // We must init with % value
	public  static String namecb_CleaningFactor = "cb_CleaningFactor";
	
	public  static byte cb_IncreasingFactor = 5;
	public static String namecb_IncreasingFactor = "cb_IncreasingFactor";
	
	public  static byte cb_Minutes_to_Download = 5;
	public  static String namecb_Minutes_to_Download = "cb_Minutes_to_Download";


	public  static String db_Drivers = "org.gjt.mm.mysql.Driver";
	public  static String namedb_Drivers = "db_Drivers";
	
	public  static String db_user = "root";
	public  static String namedb_user = "db_user";
	
	public  static String db_password = "";
	public  static String namedb_password = "db_password";	

	public  static String db_host = "jdbc:mysql://www.market.esof:3306/";
	public  static String namedb_host = "db_host";	

	public  static String wrcfgString = "";
	public  static String wrcfgPath = "d:\\client\\market_wrConf.cfg";

	public static String market_wrLogPath = "d:\\market\\logs\\market_wrLog";
	public static String namemarket_wrLogPath = "market_wrLogPath";	
	
	public static String nameScreenLogLevel = "ScreenLogLevel";
	public static int ScreenLogLevel = LogPrinter.ERROR;
	
	public static String nameFileLogLevel = "FileLogLevel";
	public static int FileLogLevel = LogPrinter.ERROR;
/**
 * Constructor of the wr configuration file
 * Creation date: (09.3.2000 a. 18:33:49)
 */
public market_wrConf() {
	super();
	}
/**
 * Load propertys from market.cfg file and create logPrinter .
 * Creation date: (29.2.2000 a. 22:31:42)
 * @return boolean
 * @param cfgPath java.lang.String
 */
public synchronized static boolean init(String cfgPath) {
// Load file: market.cfg ( marketName, marketDescribtion, ... ) in static field on ConfigMarket

	wrcfgPath = cfgPath;

	if ( wrLoadProperty( wrcfgPath ) ){
		return true;
	}
	return false;
}
/**
 * Return Web page to browser. <br> This is the configuration page of wrServer <br> 
 * Creation date: (09/03/00 08:21:06 AM)
 * @param fileOut java.io.PrintWriter
 * @param String java.lang.String 
 */
public static void wrFormConfigParam(PrintWriter fileOut , String status ) {
	String serverName= "market_wrServer?";
	fileOut.println("<HTML>");
	fileOut.println("<HEAD>");
	fileOut.println("<TITLE>market_wrServer configuration</TITLE>");
	fileOut.println("<SCRIPT Language = \"JavaScript\">");
	fileOut.println("<!-- ");
	fileOut.println("function r_load(){");
	fileOut.println("this.window.location =\""+serverName+Constants.cs_ReqType+"="+Constants.TASK_RELOAD+"\";");
	fileOut.println("}");
	fileOut.println("-->");
	fileOut.println("</SCRIPT>");		
	fileOut.println("</HEAD>");
	fileOut.println("<BODY  text = \"black\">");
	fileOut.println("<H3>market_wrServer configuration interface</H3><HR>");
	fileOut.println("<TABLE  border = 2 cellpadding =5 cellspacing = 2 name=\"all\">");
	fileOut.println("<FORM action=\"market_wrServer\">");
	fileOut.println("<input type=\"button\" value=\"Reload\" onClick=\" r_load(); \">");
	fileOut.println("<input type=\"submit\" value=\"Save to file\">");
	fileOut.println("<B>Status : </B><I>" + status + "</I><HR>");
	fileOut.println("<INPUT TYPE=\"HIDDEN\" NAME=\""+Constants.cs_ReqType+"\" VALUE="+Constants.TASK_SAVE_CONFIG+">");				
	fileOut.println("<TEXTAREA name=\"value\" cols=\"90\" rows=\"13\" wrap=\"off\">");
	fileOut.print(wrcfgString);  
	fileOut.println("</TEXTAREA>");
	fileOut.println("</FORM>");
	fileOut.println("</TABLE>");
	fileOut.println("</BODY></HTML>\n");
	fileOut.close();

} // FormConfigPage
/**
 * Load file whit market init data
 * Creation date: (1/6/00 4:03:18 PM)
 * @return boolean
 * @param fileProperty java.lang.String
 */
public static boolean wrLoadProperty(String fileProperty) {
	String tmp = new String();
	Properties prop = new Properties();
	FileInputStream propStream;
	try {
		propStream = new FileInputStream(fileProperty);
		prop.load(propStream);
		propStream.close();
		RandomAccessFile cfgfile = new RandomAccessFile(fileProperty, "r");
		String line = cfgfile.readLine();
		wrcfgString = "";
		wrcfgString = line;
		line = cfgfile.readLine();
		while (line != null) {
			wrcfgString = wrcfgString + "\n" + line ;
			line = cfgfile.readLine();
		}
	   cfgfile.close();
	} catch (FileNotFoundException e) {
		return false;
	} catch (IOException e) {
		return false;
	}
	S_wrServerHostName = prop.getProperty(nameS_wrServerHostName, S_wrServerHostName);
	S_wrProcessorHostName = prop.getProperty(nameS_wrProcessorHostName, S_wrProcessorHostName);
	db_Drivers = prop.getProperty(namedb_Drivers, db_Drivers);
	db_user = prop.getProperty(namedb_user, db_user);
	db_password = prop.getProperty(namedb_password, db_password);
	db_host = prop.getProperty(namedb_host, db_host);
	market_wrLogPath = prop.getProperty(namemarket_wrLogPath, market_wrLogPath);
	wrServerHostName = prop.getProperty(namewrServerHostName, wrServerHostName);
	try {
		csh_MaxCachFieldsInWR = Short.valueOf(prop.getProperty(namecsh_MaxCachFieldsInWR, tmp.valueOf(csh_MaxCachFieldsInWR))).shortValue();
		cb_CleaningFactor = Byte.valueOf(prop.getProperty(namecb_CleaningFactor, tmp.valueOf(cb_CleaningFactor))).byteValue();
		cb_IncreasingFactor = Byte.valueOf(prop.getProperty(namecb_IncreasingFactor, tmp.valueOf(cb_IncreasingFactor))).byteValue();
		cb_Minutes_to_Download = Byte.valueOf(prop.getProperty(namecb_Minutes_to_Download, tmp.valueOf(cb_Minutes_to_Download))).byteValue();
		ScreenLogLevel = Integer.valueOf(prop.getProperty(nameScreenLogLevel, tmp.valueOf(ScreenLogLevel))).intValue();
		FileLogLevel = Integer.valueOf(prop.getProperty(nameFileLogLevel, tmp.valueOf(FileLogLevel))).intValue();
		wrServerRMIPort = Integer.valueOf(prop.getProperty(namewrServerRMIPort, tmp.valueOf(wrServerRMIPort))).intValue();		
	} catch (Exception e) {
		System.out.println("wrConfig: Incorect parameter");
		return false;
	}
	return true;
}
/**
 * This method saves properties to conf file
 * Creation date: (09.3.2000 a. 18:52:27)
 */
public static boolean wrSaveCfgFile(String str) {
	try {
		File file = new File(wrcfgPath);
		if (file.exists()) {
			file.delete();
		}
		RandomAccessFile cfgfile = new RandomAccessFile(wrcfgPath, "rw");
		cfgfile.writeBytes(str);
		cfgfile.close();
	} catch (IOException e) {
		return false;
	}
	
return true;
}
}

