package com.csc.library.process;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.HashMap;

import com.csc.library.database.ConnectDB;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.GenFileName;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.UProfile;

public class ReportProcess implements DbReport {
	private DbRecord dbRecord;
	private HashMap prop;
	private String FILENAME;
	private CscCalendar now = new CscCalendar();
	private UProfile uProfile=null;
	private InitialEnvironment env;

	private java.sql.Connection con = null;
	private java.sql.Statement st = null;
	private java.sql.ResultSet rs = null;
	private HashMap rec ;


	public void setUProfile(UProfile uProfile) {
		this.uProfile = uProfile;
	}

	private void keepHash() {
		String FileName = this.getPath() + "reports/condition/" + this.FILENAME ;
		try {
			FileOutputStream fu = new FileOutputStream(FileName);
			ObjectOutputStream ou = new ObjectOutputStream(fu);
			ou.writeObject(this.prop);
			ou.close();
		} catch (IOException ex) {
			MyLog.error(this, this.uProfile, ex);
		}

	}

	private String getPath() {
		return new InitialEnvironment("GLOBAL").getValue("temp-dir");
	}

	private String newFilename() {
		String filename = new GenFileName().getFileName();
		return filename;
	}

	// For getReport
	
	public HashMap getReport() {
		this.findLog();
		return this.rec;
	}
	
	
	private void initConnection() {
		try {
			String dbName = new InitialEnvironment("GLOBAL").getValue("db-name");
			con = StaticProperties.myConnection.getConnection(dbName,uProfile);
			st = con.createStatement();
		} catch (SQLException ex) {
			MyLog.error(this, this.uProfile, ex);
		}
	}

	private void findLog() {
		int i = 0;
		String schemaName = new InitialEnvironment("GLOBAL").getValue("schema-name");
		this.rec=new HashMap();
		try {
			this.initConnection();
			rs = st.executeQuery("SELECT * FROM " + schemaName + ".lreport_log WHERE status = 'A'");
			if (rs != null) {
				while (rs.next()) {
					String[] data = new String[7];
					data[0] = rs.getString("reportid");
					data[1] = rs.getString("name");
					data[2] = rs.getString("class_name");
					data[3] = rs.getString("event");
					data[4] = rs.getString("status");
					data[5] = rs.getString("owner");
					data[6] = rs.getString("condition");
					rec.put(new Integer(i++), data);
				}
			}
			this.release();
		} catch (SQLException ex) {
			MyLog.error(this, this.uProfile, ex);
		}
	}

	public int updateLog(String reportid, String data) {
		int ok = 0;
		String schemaName = new InitialEnvironment("GLOBAL").getValue("schema-name");
		try {
			
			this.initConnection();
			String sql="";
			if(data.equals("C")){
				data="UPDATE " + schemaName + ".lreport_log set status='" + data + "',end_time='"+new CscCalendar().getTimestamp()+"' WHERE reportid  = '" + reportid + "'";
			}else{
				data="UPDATE " + schemaName + ".lreport_log set status='" + data + "' WHERE reportid  = '" + reportid + "'";
			}		
			ok = st.executeUpdate(data);
			this.release();
			
		} catch (SQLException ex) {
			MyLog.error(this, this.uProfile, ex);
		}
		return ok;
	}

	


	private void release() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException ex) {
			MyLog.error(this, this.uProfile, ex);
		}
	}


	private void initEnv() {
		env = new InitialEnvironment("GLOBAL");
	}

}