package system.util;

import java.io.*;
import java.net.*;
import java.util.*;
/**
 * This class contains all constants needed by wr and rw servlets
 * @author: Nikolai Rangelov
 */
 
public class rwConf 
{
	public  static String wr_rw_ServerHostName = "www.market.esof";
	public  static String namewr_rw_ServerHostName = "wr_rw_ServerHostName";

	public  static int wr_rw_ServerRMIPort = 1099;
	public  static String namewr_rw_ServerRMIPort = "wr_rw_ServerRMIPort";
	
	public static String S_wr_rw_ServerHostName = "http://localhost:8080/servlet/wr_rw_Server?";
  	public static String nameS_wr_rw_ServerHostName = "S_wr_rw_ServerHostName";
	
	public  static String S_RSRServer = "http://www.server.esof/esofia/ResourceServer?";
	public  static String nameS_RSRServer = "S_RSRServer";
	
	public  static short csh_MaxCachFieldsInRW = 100;
	public  static String namecsh_MaxCachFieldsInRW = "csh_MaxCachFieldsInRW";	
	
	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 rwcfgString = "";
	public  static String rwcfgPath = "d:\\client\\rwConf.cfg";
	
 	public static String rwLogPath = "d:\\system\\logs\\rwLog";
	public static String namerwLogPath = "rwLogPath";

	public static String wr_rwLogPath =  "d:\\system\\logs\\wr_rwLog";
	public static String namewr_rwLogPath = "wr_rwLogPath";

	public static String nameScreenLogLevel = "ScreenLogLevel";
	public static int ScreenLogLevel = LogPrinter.ERROR;
	
	public static String nameFileLogLevel = "FileLogLevel";
	public static int FileLogLevel = LogPrinter.ERROR;		
/**
 * 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

	rwcfgPath = cfgPath;
	if (rwLoadProperty(rwcfgPath)) {
		return true;
	}
	return false;
}
/**
 * Return Web page to browser. <br> This is the configuration page of rwServer <br> 
 * Creation date: (09/03/00 08:21:06 AM)
 * @param fileOut java.io.PrintWriter
 * @param String java.lang.String 
 */
public static void rwFormConfigParam(PrintWriter fileOut , String status ) {
	String serverName="rwServer?";
	fileOut.println("<HTML>");
	fileOut.println("<HEAD>");
	fileOut.println("<TITLE>rwServer 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>rwServer configuration interface</H3><HR>");
	fileOut.println("<TABLE  border = 2 cellpadding =5 cellspacing = 2 name=\"all\">");
	fileOut.println("<FORM action=\"rwServer\">");
	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(rwcfgString);  
	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 rwLoadProperty(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();
		rwcfgString = "";
		rwcfgString = line;
		line = cfgfile.readLine();
		while (line != null) {
			rwcfgString = rwcfgString + "\n" + line ;
			line = cfgfile.readLine();
		}
	   cfgfile.close();
		
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		
		wr_rw_ServerHostName = prop.getProperty(namewr_rw_ServerHostName, wr_rw_ServerHostName);
		S_wr_rw_ServerHostName = prop.getProperty(nameS_wr_rw_ServerHostName, S_wr_rw_ServerHostName);
		S_RSRServer = prop.getProperty(nameS_RSRServer, S_RSRServer);
		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);
		rwLogPath = prop.getProperty(namerwLogPath , rwLogPath );
		wr_rwLogPath = prop.getProperty(namewr_rwLogPath , wr_rwLogPath );
		try {
			csh_MaxCachFieldsInRW = Short.valueOf(prop.getProperty(namecsh_MaxCachFieldsInRW, tmp.valueOf(csh_MaxCachFieldsInRW))).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();
			wr_rw_ServerRMIPort = Integer.valueOf(prop.getProperty(namewr_rw_ServerRMIPort, tmp.valueOf(wr_rw_ServerRMIPort))).intValue();			
		} catch (Exception e) {
			System.out.println("rwConfig: Incorect parameter");
			return false;
		}
		return true;
	}
/**
 * Insert the method's description here.
 * Creation date: (09.3.2000 a. 18:52:27)
 */
public static boolean rwSaveCfgFile(String str) {
	try {
		File file = new File(rwcfgPath);
		if (file.exists()) {
			file.delete();
		}
		RandomAccessFile cfgfile = new RandomAccessFile(rwcfgPath, "rw");
		cfgfile.writeBytes(str);
		cfgfile.close();
	} catch (IOException e) {
		System.err.println("rwConf : Error in rwSaveCfgFile - file : " + rwcfgPath + " - " + e);		
		return false;
	}
	
return true;
}
}

