/*
 * Created on 19 ��.�. 2549
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.csc.library.databean;

import com.csc.library.utilities.MyLog;

import java.sql.Statement;
import java.util.HashMap;

import com.csc.library.database.CscConnection;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.InitialRecord;
import com.csc.library.session.InitialTable;
import com.csc.library.session.RecordCmp;
import com.csc.library.utilities.CscCalendar;

/**
 * @author SassyBoy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Tbossforgetcard0 extends RecordCmp {
	
	DbTable dbChild=null;
	public Tbossforgetcard0() {
		super("Tbossforgetcard0", "Tbossforgetcard0 Table");
	}
	
	private void updateReturnDocument() {
		try {
			HashMap cparam = (HashMap) this.param.clone();
			cparam.put("returnstatus","true");
			cparam.put("__wf_status","8");
			cparam.put("wf_command","delete");
			cparam.remove("__oldWF");
			DbRecord rec = new InitialRecord(this.userPro).getDbRecord("TBOSSFORGETCARD0");			
			rec.setParam(cparam);
			rec.setColumn(rec.getListKey()+",wf_status");
			rec.set("fscardid",(String)cparam.get("__oldDocumentID"));
			rec.set("companyid",this.userPro.get("companyid"));
			rec.setWFOff();
			rec.save();
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	private void setEmployeeDetail(){
		try {
			DbInquiry inq = new InitialInquiry(this.userPro).getDbInquiry();
			inq.setParam(this.param);
			inq.setSchemaName(this.userPro.getSchemaName());
			inq.initMyTable("Memployee", " employeeid='"+this.getString("emp_request")+"'", "");
			inq.setColumn("employeeid, runno, branch, job, bossid, bu1, bu2, bu3, bu4, bu5, " +
					"salatype, workarea,  emp_position, emp_group, companyid");	 
			inq.refresh();
			while (inq.next()) {
				this.set("bu1",inq.getString("bu1"));
				this.set("bu2",inq.getString("bu2"));
				this.set("bu3",inq.getString("bu3"));
				this.set("bu4",inq.getString("bu4"));
				this.set("bu5",inq.getString("bu5"));
				this.set("job",inq.getString("job"));
				this.set("emp_group",inq.getString("emp_group"));
				this.set("emp_position",inq.getString("emp_position"));
				this.set("branch",inq.getString("branch"));
				this.set("bossid",inq.getString("bossid"));
				this.set("salatype",inq.getString("salatype"));
				this.set("workarea",inq.getString("workarea"));			
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}		
	}
	
	protected boolean preInsertTrigger() throws Exception {
		boolean result = super.preInsertTrigger();	
		
		if (!this.getString("emp_request").equals("")) {
			this.setEmployeeDetail();
		}
		
		if (this.param.containsKey("__oldWF")) {
			if (((String) this.param.get("__oldWF")).equals("7")) {
				this.updateReturnDocument();
			}
		}
		
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")
				|| this.getString("wf_status").equalsIgnoreCase("8") || this.getString("wf_status").equalsIgnoreCase("9")) {
            this.setWFOff();
        }
		
		if (!this.getString("wf_status").equalsIgnoreCase("9")) {
            this.preProcessWf();
        }
		this.updateWFChild(this.getString("wf_status"));
		return result;
	}
	
	protected boolean preUpdateTrigger() throws Exception {
		boolean result = super.preUpdateTrigger();	
		
		/* ź�����������駡�͹���кѹ�֡ŧ����� */
		this.delTimeTemp(this.getString("fscardid"));
		
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")
				|| this.getString("wf_status").equalsIgnoreCase("8") || this.getString("wf_status").equalsIgnoreCase("9")) {
            this.setWFOff();
        }
		
		if (!this.getString("wf_status").equalsIgnoreCase("9")) {
            this.preProcessWf();
        }	
		if (this.getString("wf_status").equalsIgnoreCase("8") || 
				this.getString("wf_status").equalsIgnoreCase("7") || 
				this.getString("wf_status").equalsIgnoreCase("B")) {	
			this.setColumn(this.getListKey()+",employeeid,wf_status");
			dbChild = (DbTable) this.getChild("Tbossforgetcard1");
			this.removeChild("Tbossforgetcard1");
		}		
		
		if (this.getString("wf_status").equalsIgnoreCase("4")||this.getString("wf_status").equalsIgnoreCase("6")) {
			this.setEmployeeDetail();
		} 
		this.updateWFChild(this.getString("wf_status"));
		return result;	
	}
	
	public void setProperties(){
		super.setProperties();
		try {
			 DbTable db = new InitialTable(this.userPro).getDbTable("Tbossforgetcard1");
			 this.putChild(db);
			 this.refreshChild();
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
	
	public int search() {
		return super.search();
	}
	
	protected void postInsertTrigger() throws Exception {
		if (this.param.containsKey("__wfcmd")) {
			if (this.param.get("__wfcmd").equals("true")) {
				this.setWFOn();			
			} else this.setWFOff();
		}
		super.postInsertTrigger();
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")) {
			this.setNewData();
		} else if (this.getString("wf_status").equalsIgnoreCase("8") || this.getString("wf_status").equalsIgnoreCase("9")) {
			this.delOldData();
		}		
	}
	
	protected void postUpdateTrigger() throws Exception {
		if (this.param.containsKey("__wfcmd")) {
			if (this.param.get("__wfcmd").equals("true")) {
				this.setWFOn();			
			} else this.setWFOff();
		}
		super.postUpdateTrigger();
		if (this.getString("wf_status").equalsIgnoreCase("4") || this.getString("wf_status").equalsIgnoreCase("6")) {
			this.setNewData();
		} else if (this.getString("wf_status").equalsIgnoreCase("8") || this.getString("wf_status").equalsIgnoreCase("9")) {
			this.delOldData();
		}		
	}
	
	private void updateWFChild(String wf) {
		try {
			if (this.dbChild != null) {
				this.putChild(this.dbChild);
			}
			if (this.getChild("Tbossforgetcard1")!=null) {
				DbTable db = (DbTable) this.getChild("Tbossforgetcard1");
				db.first();
				do{				
					DbRecord rec = (DbRecord) db.getCurrentRecordManager();				
					rec.set("WF_STATUS",wf);
					rec.set("APPROVE", this.getString("APPROVE"));
					db.next();				
				} while (!db.eof());		
				//db.save();
			} else {			
				DbInquiry inq = new InitialInquiry(this.userPro).getDbInquiry();
				inq.setParam(this.param);
				inq.setSchemaName(this.userPro.getSchemaName());
				inq.initMyTable("Tbossforgetcard1","", "");
				inq.setColumn("FSCARDID, COMPANYID, LINE_NO, WF_STATUS");			
				this.putChild(inq);
				this.refreshChild();
				DbInquiry inqChild = (DbInquiry) this.getChild("Tbossforgetcard1");
				inqChild.refresh();
				while (inqChild.next()) {
					DbRecord rec = inqChild.getCurrentDbRecord().cloneRecord(true);					
					rec.set("wf_status",wf);
					rec.set("APPROVE", this.getString("APPROVE"));
					rec.save();
				}
				this.removeChild("Tbossforgetcard1");
			}
		} catch (Exception e) {
			MyLog.error(this, e);
		}
	}
			
	/*
	 * �ӡ��ź��������� 
	 * �µ���ź㹵��ҧ�����š���ٴ�ѵô��� TTIMETEMP
	 */
	private void delOldData() {	
		try {
			DbTable dt;					
			if (this.getChild("Tbossforgetcard1")!=null) {
				dt = (DbTable) this.getChild("Tbossforgetcard1");			
			} else {
				DbTable db = new InitialTable(this.userPro).getDbTable("Tbossforgetcard1");
				db.setColumn("*");
				this.putChild(db);
				this.refreshChild("Tbossforgetcard1");
				dt = (DbTable) this.getChild("Tbossforgetcard1");	
			}
			dt.first();
			do{				
				DbRecord recChild = (DbRecord) dt.getCurrentRecordManager();
				CscCalendar csc = new CscCalendar(recChild.getString("FORGET_DATE"));
				String filter = " employeeid='"+this.getString("EMPLOYEEID")+"' " +
						"and swipedate='"+csc.getYYYYMMDD()+"' " +
						"and swipetime="+recChild.getDouble("FORGET_TIME");				
				DbInquiry inq = new InitialInquiry(this.userPro).getDbInquiry();
				inq.setParam(this.param);
				inq.setSchemaName(this.userPro.getSchemaName());
				inq.initMyTable("Ttimetemp", filter , "");			
				inq.setColumn("SWIPEDATE, EMPLOYEEID, SWIPETIME, COMPANYID, RUNNO");
				inq.refresh();
				while (inq.next()) {				
					DbRecord rec = inq.getCurrentDbRecord().cloneRecord(true);
					rec.delete();
				}				
				dt.next();				
			} while (!dt.eof());
		} catch (Exception e) {
			MyLog.error(this, e);	
		}
	}
	
	/* ź������� Timetemp ��͹�������������� */
	private void delTimeTemp(String filter){
		CscConnection conn = null;
		Statement stmt = null;
		StringBuffer sql = new StringBuffer();
		try {
			sql.append("delete from ttimetemp where docid = '").append(filter).append("'");
			
			conn = (CscConnection) StaticProperties.myConnection.getConnection(this.userPro.get("dbname"));
			stmt = conn.createStatement();
			stmt.executeQuery(sql.toString());
			
		} catch(Exception e){
			MyLog.error("!! Error : can't remove ttimetemp from Tbossforgetcard0.delTimeTemp !!");
		} finally {
			try {
				conn.close();
				stmt.close();
			} catch(Exception e) {
				MyLog.error("!! Error :  can't close connection or statement in Tbossforgetcard0.ttimetemp !!");
			}
		}
	}
	
	/*
	 * �ӡ�úѹ�֡�����š���ٴ�ѵ� ŧ�� TTIMETEMP 
	 * ���������͡���� Tbossforgetcard1
	 */
	private void setNewData() {
		try {		
			/* ź������� Timetemp ��͹�������������� */
			this.delTimeTemp(this.getString("fscardid"));
			
			DbRecord rec = new InitialRecord(this.userPro).getDbRecord("Ttimetemp");
			rec.setColumn("*");
			
			DbTable dt;					
			if (this.getChild("Tbossforgetcard1")!=null) {
				dt = (DbTable) this.getChild("Tbossforgetcard1");			
			} else {
				dt = new InitialTable(this.userPro).getDbTable("Tbossforgetcard1");
				dt.setColumn("*");
				this.putChild(dt);
				this.refreshChild();
			}
			dt.first();
			do{				
				DbRecord recChild = (DbRecord) dt.getCurrentRecordManager();
			
				rec.clearField();
				rec.set("COMPANYID", this.getString("COMPANYID"));			
				rec.set("EMPLOYEEID", recChild.getString("EMPLOYEEID"));		
				rec.set("SWIPEDATE", recChild.getString("FORGET_DATE"));
				rec.set("SWIPETIME", recChild.getString("FORGET_TIME"));
				rec.set("SWIPETYPE", recChild.getString("FORGET_TYPE"));
				rec.set("SOURCE", recChild.getString("SOURCE"));	
				rec.set("WF_REF_DOC", this.getString("WF_REF_DOC")); 
				//rec.set("APPROVE", this.getString("APPROVE")); 
				rec.set("DOCID", this.getString("FSCARDID"));
				rec.save();
				
				dt.next();				
			} while (!dt.eof());		
						
		} catch (Exception e) {
			MyLog.error(this, e);							
		}		
	}
	
	protected void postDeleteTrigger() throws Exception {		
		super.postDeleteTrigger();
		this.delOldData();
	}
	
	protected boolean preDeleteTrigger() throws Exception {		
		boolean result = super.preDeleteTrigger();
		
		/* ź�������ٴ�ѵ�� Timetemp */
		this.delTimeTemp(this.getString("fscardid"));
		
		return result;
	}
}