package com.csc.library.process;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;
import java.io.*;
import java.util.Scanner;

import com.csc.library.cscimport.MyImport;
import com.csc.library.database.CscConnection;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.MyLog;

public class ImportMtime2 extends MyProcess {
	private static int COLUMNS = 38; // �ӹǹ�������������������¡���� ',' (comma) ���� �е�ͧ�� 37 �������
	private int ERROR = 0; // �纨ӹǹ�á���촷�� error
	private int SUCCESSED = 0; // �纨ӹǹ�á���촷�� success
	private int INDEX =0; // �纨ӹǹ�á���촷�����Ҩҡ��������
	private String COMPANYID = "100";
	private DbRecord exitData = null; // �红��������㹡óշ���Ѿഷ���
	
	// ��˹���ͤ��� Error
	private String[] MESSAGE = {
			"Error Colum format",
			"Error month or year format",
			"Employeeid invalid",
			"ShiftID not in Master",
			"ShiftID not match",
			"Colum Shift value 1,2,3 only",
			"Error system not process please contact consult or programmer"
	}; 
	
	private File fFile;
	private HashMap hmTime0 = new HashMap();  // �����ʡ� ����繵��ҧ��÷ӧҹ (status = 0)
	private HashMap hmTime1 = new HashMap();  // �����ʡ� ����繵��ҧ������ (status = 1)
	private Vector vError = new Vector(); // ���á���줷�� Error

	public Vector getProcessList() {
		Vector v = new Vector();
		v.addElement("All");
		return v;
	}
	
	public void process() throws Exception {
		Vector vt = this.processList;
		for (int i=0;i<vt.size() && !this.isCancle();i++) {
			String id = (String) vt.elementAt(i);
			
			try {
				InitialEnvironment en = new InitialEnvironment("GLOBAL");
				String importPath = en.getValue("dirimport-dir"); //path �������������� import 
				String fileName = (String) this.data.get("fileName");
				
				ReadWithScanner(fileName); // ����� ����������ԧ�������
				processLineByLine(); // ��ҹ���к�÷�մ �����觺ѹ�֡������
			} catch (Exception e) {
				MyLog.error(this, e);				
			}			
		}

		setSummary(); // �͡��§ҹ�š�ù���Ң�����
	}
	
	private void setSummary(){
		StringBuffer msg = new StringBuffer();
		try {
			msg.append("!!!!!!!!!!  Import Summary   !!!!!!!!!!\r\n");
			msg.append("\r\n");
			msg.append("\r\n");
			msg.append("Input : ").append(INDEX).append(" record(s).\r\n\r\n");
			msg.append("Successed : ").append(SUCCESSED).append(" record(s).\r\n\r\n");
			msg.append("ERROR : ").append(ERROR).append(" record(s).\r\n");
			
			// �֧�á���촷�� Error �͡���ʴ� ��觶١������ Vector �ٻẺ�� String Array : [���ʾ�ѡ�ҹ , �˵ؼŷ�� Error]
			if(ERROR > 0){
				for(int i=0;i < vError.size();i++){
					String[] msgErr = (String[]) vError.get(i);
					msg.append("\r\n").append(msgErr[0]).append("    ").append(msgErr[1]);
				}
			}
			
			msg.append("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
			msg.append("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
			msg.append("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
			
		}catch(Exception e){
			MyLog.error("!! Error : can't print summary !!",e);
		} finally {
			setFullMessage(msg.toString());
			if(ERROR == 0){
				setMessage("Completed");
			}else{
				setMessage("!! Error !!");
			}
		}
	}
	
	/**
	 * @param aFileName
	 *            full name of an existing, readable file.
	 */
	public void ReadWithScanner(String aFileName) {
		fFile = new File(aFileName);
	}

	/** Template method that calls {@link #processLine(String)}. 
	 * @throws FileNotFoundException */
	public final void processLineByLine() throws FileNotFoundException{
		// Note that FileReader is used, not File, since File is not Closeable
		Scanner scanner = new Scanner(new FileReader(fFile));
		CscConnection conn = null;

		try {
			conn = (CscConnection) StaticProperties.myConnection.getConnection(this.upf.get("dbname"));
			
			boolean loadTime = LoadTime0(conn); // ��Ŵ���ʵ��ҧ��÷ӧҹ���������� �������礡Ѻ���ʷ��������� ����������ԧ�������

			if(loadTime){ // �����Ŵ���ʡ� ������������ ��ӧҹ��� 
			if(scanner.hasNextLine()){}
				while (scanner.hasNextLine()) {
					INDEX++; // �纨ӹǹ�á���촷�����
	
					String msg = scanner.nextLine(); // ��ҹ����������к�÷Ѵ
					String[] data = new String[38];
					data = msg.split(","); //�¡�������͡�� Column �µ�����¡�� ',' (comma sign)
					System.out.print("data.length :: "+data.length);
					if(chkFormat(data)){ // ��Ҽ�ҹ������ٻẺ������ �֧�зӧҹ��� ��������кѹ�֡ Error ��Т���价Ӻ�÷Ѵ�Ѵ�
						processLine(data, conn); // �ѹ�֡������ŧ MTIME2 ��� update MEMPLOYEE on field Time0
					}
	
					this.alive("Process Data: [" + INDEX + "]");
				} 
			} else {
				MyLog.debug("!! Can't Load MTIME0 and process forced to stop immediately !!");
			}
		}catch(Exception e) {
				MyLog.error("!! Error reading file : !! ", e);
		} finally {
			// ensure the underlying stream is always closed
			// this only has any effect if the item passed to the Scanner
			// constructor implements Closeable (which it does in this case).
			scanner.close();
			try {
				conn.close();
			} catch (SQLException e) {
				MyLog.error("!! Error : can't close connection !! ", e);
			}
		}
	}
	
	/* ��Ŵ MTIME0 ����������� HashMap */
	private boolean LoadTime0(CscConnection conn){
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "select time0id,status from mtime0";
		
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				String time0 = rs.getString("time0id");
				String status = rs.getString("status");
				if(status.equals("0")){
					hmTime0.put(time0, time0);
				}else{
					hmTime1.put(time0, time0);
				}
			}
		} catch(Exception e){
			MyLog.error("!! Error : Can't load data from MTIME0 !! ", e);
			return false;
		} finally {
			try {
				if(rs != null) rs.close();
				if(stmt != null) stmt.close();
			}catch(Exception e){
				MyLog.error("!! Error : Can't close result ,stmt or conn !!", e);
			}
		}
		return true;
	}

	// �ش�����ŷ��������� ��ͧ������ٻẺ�����ҹ�� (������ 37 �������)
	// �״�͹-��,���ʾ�ѡ�ҹ,����,���ʡ��,���ʡ�(��� Employee),�ӴѺ��(�����ѹ�Ҩ�����ҡ���� 1 ��),���ʡе���ѹ��� 1,..,31
	// 09-2010,30-0013,AUMPOL,PUMPOUNG,office03,1,02,02,,,,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,,02
	protected void processLine(String[] data,CscConnection conn) {
		PreparedStatement pEmp = null;
		PreparedStatement pTime = null;
		
		StringBuffer sqlEmp = new StringBuffer();
		StringBuffer sqlTime = new StringBuffer();
		boolean chkTime2 = false;
		
		try {
			//String[] dateKey = data[0].split("\\-"); // �¡�����͹��л� �´٨ҡ����ͧ���� '-'
			//deleteTime2(data[0],data[4]); // �觤�� ��͹-�� ������ʡ� ����ź�����������駡�͹�����������
			chkTime2 = chkTime2Exist(data); // ������բ�������������������� ���ͷ������ҧ sql ��ҵ�ͧ insert ���� update 
			
			// sql ����Ѻ update field time0 � MEMPLOYEE
			sqlEmp.append("update memployee set time0 = ? where employeeid = '").append(data[0]).append("'");
			pEmp = conn.prepareStatement(sqlEmp.toString());
			pEmp.setString(1, data[5]);
			
			// sql ����Ѻ insert ���� update ������㹵��ҧ MTIME2
			if(chkTime2){
				if(data[6].equals("1")){
					sqlTime.append("update mtime2 set time011 = ?,time021 = ?,time031 = ?,time041 = ?,time051 = ?,time061 = ?,time071 = ?,time081 = ?,time091 = ?,time101 = ?,time111 = ?,time121 = ?,time131 = ?,time141 = ?,time151 = ?,time161 = ?,time171 = ?,time181 = ?,time191 = ?,time201 = ?,time211 = ?,time221 = ?,time231 = ?,time241 = ?,time251 = ?,time261 = ?,time271 = ?,time281 = ?,time291 = ?,time301 = ?,time311 = ? ");
				}else if(data[6].equals("2")){
					sqlTime.append("update mtime2 set time012 = ?,time022 = ?,time032 = ?,time042 = ?,time052 = ?,time062 = ?,time072 = ?,time082 = ?,time092 = ?,time102 = ?,time112 = ?,time122 = ?,time132 = ?,time142 = ?,time152 = ?,time162 = ?,time172 = ?,time182 = ?,time192 = ?,time202 = ?,time212 = ?,time222 = ?,time232 = ?,time242 = ?,time252 = ?,time262 = ?,time272 = ?,time282 = ?,time292 = ?,time302 = ?,time312 = ? ");
				}else if(data[6].equals("3")){
					sqlTime.append("update mtime2 set time013 = ?,time023 = ?,time033 = ?,time043 = ?,time053 = ?,time063 = ?,time073 = ?,time083 = ?,time093 = ?,time103 = ?,time113 = ?,time123 = ?,time133 = ?,time143 = ?,time153 = ?,time163 = ?,time173 = ?,time183 = ?,time193 = ?,time203 = ?,time213 = ?,time223 = ?,time233 = ?,time243 = ?,time253 = ?,time263 = ?,time273 = ?,time283 = ?,time293 = ?,time303 = ?,time313 = ? ");
				}
				sqlTime.append(" where monthid = '").append(data[0]).append("'");
				sqlTime.append(" and yearid = '").append(data[1]).append("'");
				sqlTime.append(" and time0id = '").append(data[5]).append("'");
				
				pTime = conn.prepareStatement(sqlTime.toString());
				
				String[] data2 = new String[38];
				for(int i=0 ; i<data2.length ; i++){
					data2[i] = "";
				}
				for(int i=0 ; i<data.length ; i++){
					data2[i] = data[i];
				}
				try {
					// field Time011 ,Time012, Time013 .... Time311, Time312, Time313
					pTime.setString(1, (data2[7].trim().length()>0)?data2[7]:exitData.getString("time01"+data2[6]));	
					pTime.setString(2, (data2[8].trim().length()>0)?data2[8]:exitData.getString("time01"+data2[6]));		
					pTime.setString(3, (data2[9].trim().length()>0)?data2[9]:exitData.getString("time01"+data2[6]));		
					pTime.setString(4, (data2[10].trim().length()>0)?data2[10]:exitData.getString("time01"+data2[6]));		
					pTime.setString(5, (data2[11].trim().length()>0)?data2[11]:exitData.getString("time01"+data2[6]));		
					pTime.setString(6, (data2[12].trim().length()>0)?data2[12]:exitData.getString("time01"+data2[6]));	
					pTime.setString(7, (data2[13].trim().length()>0)?data2[13]:exitData.getString("time01"+data2[6]));		
					pTime.setString(8, (data2[14].trim().length()>0)?data2[14]:exitData.getString("time01"+data2[6]));		
					pTime.setString(9, (data2[15].trim().length()>0)?data2[15]:exitData.getString("time01"+data2[6]));		
					pTime.setString(10, (data2[16].trim().length()>0)?data2[16]:exitData.getString("time01"+data2[6]));		
					pTime.setString(11, (data2[17].trim().length()>0)?data2[17]:exitData.getString("time01"+data2[6]));		
					pTime.setString(12, (data2[18].trim().length()>0)?data2[18]:exitData.getString("time01"+data2[6]));		
					pTime.setString(13, (data2[19].trim().length()>0)?data2[19]:exitData.getString("time01"+data2[6]));		
					pTime.setString(14, (data2[20].trim().length()>0)?data2[20]:exitData.getString("time01"+data2[6]));		
					pTime.setString(15, (data2[21].trim().length()>0)?data2[21]:exitData.getString("time01"+data2[6]));		
					pTime.setString(16, (data2[22].trim().length()>0)?data2[22]:exitData.getString("time01"+data2[6]));		
					pTime.setString(17, (data2[23].trim().length()>0)?data2[23]:exitData.getString("time01"+data2[6]));	
					pTime.setString(18, (data2[24].trim().length()>0)?data2[24]:exitData.getString("time01"+data2[6]));	
					pTime.setString(19, (data2[25].trim().length()>0)?data2[25]:exitData.getString("time01"+data2[6]));		
					pTime.setString(20, (data2[26].trim().length()>0)?data2[26]:exitData.getString("time01"+data2[6]));		
					pTime.setString(21, (data2[27].trim().length()>0)?data2[27]:exitData.getString("time01"+data2[6]));		
					pTime.setString(22, (data2[28].trim().length()>0)?data2[28]:exitData.getString("time01"+data2[6]));	
					pTime.setString(23, (data2[29].trim().length()>0)?data2[29]:exitData.getString("time01"+data2[6]));		
					pTime.setString(24, (data2[30].trim().length()>0)?data2[30]:exitData.getString("time01"+data2[6]));		
					pTime.setString(25, (data2[31].trim().length()>0)?data2[31]:exitData.getString("time01"+data2[6]));		
					pTime.setString(26, (data2[32].trim().length()>0)?data2[32]:exitData.getString("time01"+data2[6]));		
					pTime.setString(27, (data2[33].trim().length()>0)?data2[33]:exitData.getString("time01"+data2[6]));		
					pTime.setString(28, (data2[34].trim().length()>0)?data2[34]:exitData.getString("time01"+data2[6]));	
					pTime.setString(29, (data2[35].trim().length()>0)?data2[35]:exitData.getString("time01"+data2[6]));		
					pTime.setString(30, (data2[36].trim().length()>0)?data2[36]:exitData.getString("time01"+data2[6]));		
					pTime.setString(31, (data2[37].trim().length()>0)?data2[37]:exitData.getString("time01"+data2[6]));		
				}catch(Exception e){
					MyLog.error(this,e);
				}
			}else{
				if(data[6].equals("1")){
					sqlTime.append("insert into mtime2 (time0id,monthid,yearid,companyid,time011,time021,time031,time041,time051,time061,time071,time081,time091,time101,time111,time121,time131,time141,time151,time161,time171,time181,time191,time201,time211,time221,time231,time241,time251,time261,time271,time281,time291,time301,time311)");
				}else if(data[6].equals("2")){
					sqlTime.append("insert into mtime2 (time0id,monthid,yearid,companyid,time012,time022,time032,time042,time052,time062,time072,time082,time092,time102,time112,time122,time132,time142,time152,time162,time172,time182,time192,time202,time212,time222,time232,time242,time252,time262,time272,time282,time292,time302,time312)");
				}else if(data[6].equals("3")){
					sqlTime.append("insert into mtime2 (time0id,monthid,yearid,companyid,time013,time023,time033,time041,time053,time063,time073,time083,time093,time103,time113,time123,time133,time143,time153,time163,time173,time183,time193,time203,time213,time223,time233,time243,time253,time263,time273,time283,time293,time303,time313)");
				}
				sqlTime.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
				
				String[] data2 = new String[38];
				for(int i=0 ; i<data2.length ; i++){
					data2[i] = "";
				}
				for(int i=0 ; i<data.length ; i++){
					data2[i] = data[i];
				}
				
				pTime = conn.prepareStatement(sqlTime.toString());
				pTime.setString(1, data2[5]); 	// Time0id
				pTime.setString(2, data2[0]); // monthid
				pTime.setString(3, data2[1]); // yearid
				pTime.setString(4, COMPANYID); 	// companyid
				
				// field Time011 ,Time012, Time013 .... Time311, Time312, Time313
				/*int indexStr = 5;
				for(int i = 5;i<data.length;i++){
					pTime.setString(indexStr, data[i]);	
					indexStr++;
				}*/
				pTime.setString(5, data2[7]);	
				pTime.setString(6, data2[8]);	
				pTime.setString(7, data2[9]);	
				pTime.setString(8, data2[10]);	
				pTime.setString(9, data2[11]);	
				pTime.setString(10, data2[12]);	
				pTime.setString(11, data2[13]);	
				pTime.setString(12, data2[14]);	
				pTime.setString(13, data2[15]);	
				pTime.setString(14, data2[16]);	
				pTime.setString(15, data2[17]);	
				pTime.setString(16, data2[18]);	
				pTime.setString(17, data2[19]);	
				pTime.setString(18, data2[20]);	
				pTime.setString(19, data2[21]);	
				pTime.setString(20, data2[22]);	
				pTime.setString(21, data2[23]);	
				pTime.setString(22, data2[24]);	
				pTime.setString(23, data2[25]);	
				pTime.setString(24, data2[26]);	
				pTime.setString(25, data2[27]);	
				pTime.setString(26, data2[28]);	
				pTime.setString(27, data2[29]);	
				pTime.setString(28, data2[30]);	
				pTime.setString(29, data2[31]);	
				pTime.setString(30, data2[32]);	
				pTime.setString(31, data2[33]);	
				pTime.setString(32, data2[34]);	
				pTime.setString(33, data2[35]);	
				pTime.setString(34, data2[36]);	
				pTime.setString(35, data2[37]);	
			}
			
			// ��� execute sql
			pEmp.executeUpdate();
			pTime.executeUpdate();
			
			SUCCESSED++; // ��Һѹ�֡����纤�����㹵���� Successes
			
		} catch (SQLException e) {
			ERROR++;
    		setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[5],MESSAGE[6]);
			MyLog.error("!! Error in processLine invalid sql command !!" , e);
		} finally {
			try {
				if(pEmp != null) pEmp.close();
				if(pTime != null) pTime.close();
			} catch(Exception e){
				MyLog.error("!! Error : can't close statement !!", e);
			}
		}
	}
	
	/* ������բ���������� Time2 ������� ���ͧ�ҡ���� 1 �ѹ�ա�÷ӧҹ�ҡ���� 1 �Т���ٷ������� �����ҡѺ�ӹǹ�Тͧ�ѹ���  � 
	 * 09-2010,30-0013,AUMPOL,PUMPOUNG,office03,  1  ,02,02,,,,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,,02
	 * 09-2010,30-0013,AUMPOL,PUMPOUNG,office03,  2  ,02,02,,,,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,,02
	 * �ҡ�����Ţ�ҧ�� ��ѹ������ͧ�� �ѧࡵ�ҡ��������� 5 (������Ѻ��� 0 �繤�������á) ��÷Ѵ�á�� 1 (�з�� 1) ��÷Ѵ��� 2 (�з�� 2)
	 * ����͢����Ţ�ҧ���������к� ��÷Ѵ�á �����������㹰ҹ�����Ũ�������á���� ����դ��� 09 , 2010 , 30-0013 � Time2 �֧�ѹ�֡������ŧ�
	 * ������ͺ�÷Ѵ����ͧ����� ��Ŵ����ʡз���ͧ ��������á�������ǡѺ ��÷Ѵ�á �����դ�������͹�ѹ ��§����繿�Ŵ�з���ͧ (Time012,...Time312)
	 * �֧��ͧ��� Update ᷹������� Insert ����͹��÷Ѵ�á 
	 * */
	private boolean chkTime2Exist(String[] data){
		try {
			//String[] dateKey = data[0].split("\\-");
	    	DbRecord time2 = new InitialRecord(this.upf).getDbRecord("MTIME2");
	    	time2.set("monthid", data[0]);
	    	time2.set("yearid", data[1]);
	    	time2.set("time0id", data[5]);
	    	time2.set("companyid", COMPANYID);
	    	time2.setColumn("*");
	    	if(time2.search() == 1) {
	    		exitData = time2.cloneRecord(false);
	    		return true;
	    	}
		} catch(Exception e){
			MyLog.error("!! Error : chkTime2Exist !!", e);
			return false;
		}
		return false;
	}
	
	/* ź���������� MTIME2 */
	private void deleteTime2(String mmyy, String time0){
		CscConnection conn = null;
		Statement stmt = null;
		try {
			String[] dateKey = mmyy.split("\\-");
			
			StringBuffer sql = new StringBuffer(); 
				sql.append("delete mtime2 where yearid = '").append(dateKey[1]).append("'");
				sql.append(" and monthid = '").append(dateKey[0]).append("'");
				sql.append(" and time0id = '").append(time0).append("'");
			
			conn = (CscConnection) StaticProperties.myConnection.getConnection(this.upf.get("dbname"));
			stmt = conn.createStatement();
			stmt.executeUpdate(sql.toString());
		} catch(Exception e) {
			MyLog.error("!! Can't delete MTIME@ with " + mmyy, e);
		} finally {
			try {
				if (stmt != null) stmt.close();
				if (conn != null) conn.close();
			} catch(Exception e){
				MyLog.error("!! Can't close stmt or conn !! ", e);
			}
		}
	}
	
	/* ���ٻẺ������ */
	private boolean chkFormat(String[] data){ 
		try {
			/*if(!chkDataAmount(data)) {
				this.setMessage("invalid number of input columns");
				return false;
			}*/
			if(!chkMMDDFormat(data)) {
				this.setMessage("invalid date format");
				return false;
			}
			if(!chkEmployeeExist(data)) {
				this.setMessage("invalid employeeid");
				return false;
			}
			// ����ͧ�����ʡ� �ѹ�֡ŧ仡�͹ �����աóշ�����������������ѧ 
			if(!chkTime0Exist(data)) {
				this.setMessage("invalid time0id");
				return false;
			}
			
			if(!chkShiftIndex(data)) {
				this.setMessage("invalid shift index");
				return false;
			}
		}catch(Exception e){
			MyLog.error("!! Error : checking data format !! ", e);
			return false;
		}
		return true;
	}
	
	/* �礨ӹǹ��������ѹ �٧�ش���� 3 �� */
	private boolean chkShiftIndex(String[] data){
		if(Integer.parseInt(data[6]) >= 1 && Integer.parseInt(data[6]) <= 3) {
			return true;
		} else {
			setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[5],MESSAGE[5]);
			return false;
		}
	}
	
	/* �����ʡ� ��ͧ������� MTIME0 ���ʶҹе�ͧ�� ���ҧ��÷ӧҹ (status = 0) */
	private boolean chkTime0Exist(String[] data){
		try {
			if(!hmTime1.containsKey(data[5])) {
				setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[5],MESSAGE[3]);
				return false;
			}
			
			// ǹ�ٻ�����ʡз��������ҷ����� ����������� ��������� 6 �֧ 36 ��ͧ��� MTime0 ���ʶҹе�ͧ�繵��ҧ�����繪�ͧ��ҧ������Ҽ�ҹ
			for(int i=7;i < COLUMNS; i++){
				if(data[i].trim().length() > 0){
					if(!hmTime0.containsKey(data[i])) {
						setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[i],MESSAGE[4]);
						return false;
					}
				}
			}
		}catch(Exception e){
			MyLog.error("!! Error : !!",e);
		}
		return true;
	}
	
	/* �����ʾ�ѡ�ҹ ��ͧ������㹰ҹ������ */
	private boolean chkEmployeeExist(String[] data){
		try {
        	DbRecord emp = new InitialRecord(this.upf).getDbRecord("MEMPLOYEE");
        	emp.set("employeeid", data[2]);
        	emp.setColumn("employeeid");
        	if(emp.search() == 0){ // ���Ҿ�ѡ�ҹ������� ��������㹰ҹ������ ����� Error ���
        		setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[5],MESSAGE[2]);
    			return false;
        	}
		} catch(Exception e){
			MyLog.error("!! Error : ", e);
			return false;
		}
		return true;
	}
	
	/* �礨ӹǹ������� ��ͧ�� 37 ��ҹ��*/
	private boolean chkDataAmount(String[] data){
		if(data.length != COLUMNS) {
			// ����������ú �Ҵ�����Թ ���红�ͤ��� Error ������� ��͹��:���ʾ�ѡ�ҹ:�ӴѺ�� �繤��� 
			setError(data[0]+":"+data[1]+":"+data[4],MESSAGE[0]);
			return false;
		} else {
			return true;
		}
	}
	
	/* ��Ǩ�ͺ�ٻẺ ��͹��л� ����͹��ͧ�ҡ���� 0 ��й��� 13 (���� 12 ��͹) */
	private boolean chkMMDDFormat(String[] data){
		try {
			//String[] mmyy = data[0].split("\\-");
			if(Integer.parseInt(data[0]) < 1 && Integer.parseInt(data[0]) > 12 ) {
				setError(data[0]+"-"+data[1]+":"+data[2]+":"+data[5],MESSAGE[1]); // �� Error ������ʾ�ѡ�ҹ����ջѭ��
				return false;
			}
		} catch(Exception e) {
			MyLog.error("!! Error : invalid Month & Year format !! ", e);
			return false;
		}
		return true;
	}
	
	private void setError(String key,String msg){
		vError.add(new String[] {key,msg});
		ERROR++;
	}
}