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

import java.util.StringTokenizer;
import java.util.Vector;

import com.csc.library.utilities.MyLog;
import com.csc.library.cscimport.MyImport;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialEnvironment;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.InitialRecord;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyTimerTask;
import com.csc.library.utilities.UProfile;

/**
 * @author SassyBoy100
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/*
 * class �����㹡�ô֧�����š��ŧ����Ẻ�ѵ���ѵ�
 * �´����͹䢡�ô֧�����Ũҡ CSC_Quque
 */
public class ImportSwipeCardAuto extends MyProcess {
	
	public Vector getProcessList() {
		Vector v = new Vector();
		v.addElement("All");
		return v;
	}
	
	/*
	 * ���� check ���͹䢷���Ҩҡ CSC_Queue ��Ҥú�������
	 */
	private String checkConditionData(String dataName) {
		String result = "";
		if (!this.data.containsKey(dataName)) {
			MyLog.error("Can't get "+dataName+" in CSC_QUQUE !!!");
		} else {
			result = (String) this.data.get(dataName);
			if (result.equals("")) {
				MyLog.error(dataName+" in CSC_QUQUE !!! is blank");
			}
		}
		return result;
	}
	
	public void process() throws Exception {
		try {
			Vector vt = this.processList;
			
			/*
			 * Example ������� CSC_Quque field condition
			 * employeeid=999999,companyid=100,processlist=1,filenameformat=#DDMMYY#,
			 * filetype=txt,xmlname=TTIMETEMP_NSTDA,tablename=ttimetemp
			 * ���� schemaname=dbo,lang=THA,appname=TA,dbname=KKBDB
			 */
			
			String processList = this.checkConditionData("processlist"); 
			String fileNameFormat = this.checkConditionData("filenameformat"); //�ٻẺ�ͧ�������
			String fileType = this.checkConditionData("filetype"); //��Դ�ͧ��� �� txt, csv
			String xmlName = this.checkConditionData("xmlname"); //���� xml ����˹��ٻẺ�ͧ��� import
			String tableName = this.checkConditionData("tablename"); //���� table �����㹡�èѴ�红����š��ŧ����
				
			System.out.println("ProcessList : " + processList);
			System.out.println("fileNameFormat : " + fileNameFormat);
			System.out.println("fileType : " + fileType);
			System.out.println("xmlName : " + xmlName);
			System.out.println("tableName : " + tableName);
			
			if (vt==null) {
				vt = new Vector();
				StringTokenizer str = new StringTokenizer(processList,"#");
				while (str.hasMoreTokens()) {
					vt.add(str.nextToken());
				}			
			}
			
			for (int i=0;i<vt.size() && !this.isCancle();i++) {
				String id = (String) vt.elementAt(i);
				UProfile up = this.getUProfile();
		        	        			
				try {
					InitialEnvironment en = new InitialEnvironment("GLOBAL");
					String xmlPath = en.getValue("import-dir"); //path ����� xml ������˹� format 㹡�� import
					String importPath = en.getValue("dirimport-dir"); //path �������������� import
					
					System.out.println("xmlPath : " + xmlPath);
					System.out.println("importPath : " + importPath);
					/**
					 * Author by aGazin
					 * ������Ѻ�ó������� Import ������������ root path ��� tag <dirimport> ��˹����
					 * require TAG <DIRIMPORT_SWIPEAUTO dir="?"> (service.xml)
					 */
					if(!en.getValue("DIRIMPORT_SWIPEAUTO-dir").equals("")){
						importPath = en.getValue("DIRIMPORT_SWIPEAUTO-dir");
					}
					String errorPath = en.getValue("dirimport_error-dir"); 
			        String fileName = this.getFileFormat(fileNameFormat)+"."+fileType; //���� file ����红����ŷ���ͧ��� import
			        		
			        System.out.println("fileName : " + fileName);
			        
			        //�ӡ�� new table ����ͧ��� save ŧ� ����ҹ���� table �ҡ param
					DbRecord rec = new InitialRecord(up).getDbRecord(tableName);
					rec.setColumn("*");
					
					MyImport myImp = new MyImport(rec, importPath + fileName, xmlPath +"XML/"+ xmlName);			
					myImp.process(); //�ӡ�� import
					StaticProperties.serverManager.writeFileResultProcess(this.processId, myImp.getSummaryResult());
					MyLog.debug(this, ">>>>>>>>>>>>>>> Import SwipeCard Result : Successful");
					this.alive("Process Data: "+id+" ["+i+" of "+vt.size()+"]");
				} catch (Exception e) {
					MyLog.error(this, e);
					this.setMessage("Error of ImportSwipeCard : "+vt.size()+"\n"+e);
					this.setFullMessage("Error of ImportSwipeCard : "+vt.size()+"\n"+e);		
				}				
			}
			this.setMessage("Successful of ImportSwipeCard : "+vt.size());
			this.setFullMessage("Successful of ImportSwipeCard : "+vt.size()+"\n");		
		} catch (Exception e) {
			MyLog.error(this, e);			
			MyLog.debug(this, ">>>>>>>>>>>>>>> Import SwipeCard Result : ERROR Not Successful !!!!");
		}		
	}	
	
	/*
	 * ���ô� format �ͧ���������зӡ�ô֧������
	 */
	public String getFileFormat(String fileNameFormat) {
		/* FILENAME ===> TEXT#FORMAT#TEXT
		 * FORMAT LIST
		 * =========================
		 * DD-MM-YYYY			01-01-2007
		 * DD-MM-YY			01-01-07
		 * DD-MONTH-YYYY	01-JAN-2007
		 * DD-MONTH-YY		01-JAN-07
		 * DDMMYYYY			01012007
		 * DDMMYY				010107
		 * DDMONTHYYYY		01JAN2007
		 * DDMONTHYY			01JAN07
		 * 
		 * YYYY-MM-DD			2007-01-01
		 * YY-MM-DD			07-01-01
		 * YYYY-MONTH-DD	2007-JAN-01 
		 * YY-MONTH-DD		07-JAN-01
		 * YYYYMMDD			20070101
		 * YYMMDD				070101
		 * YYYYMONTHDD		2007JAN01 
		 * YYMONTHDD			07JAN01
		 * */
		String result="";
		String fixStartName = "";
		String fixEndName = "";  
		
		try {
			fixStartName = fileNameFormat.substring(0, fileNameFormat.indexOf("#"));
			String tmp = fileNameFormat.substring(fileNameFormat.indexOf("#")+1, fileNameFormat.length());
			String format = tmp.substring(0, tmp.indexOf("#"));
			fixEndName = tmp.substring(tmp.indexOf("#")+1, tmp.length()); 
			CscCalendar csc = new CscCalendar();
		
			/*
			 * ��������͹����֧��������͹��ѧ ���ӡ��ź�ѹ�͡� ������͹䢷���������
			 */
			if (!this.checkConditionData("beforeday").equals("")) { 
				int day = Integer.parseInt(this.checkConditionData("beforeday"));
				csc.decDate(day);
			}
			// ============= ENG YEAR ===============
			if (format.equalsIgnoreCase("DD-MM-YYYY")) {
				result = csc.getDD()+"-"+csc.getMM()+"-"+csc.getEngYYYY();
			} else if (format.equalsIgnoreCase("DD-MM-YY")) {
				result = csc.getDD()+"-"+csc.getMM()+"-"+csc.getEngYY();
			} else if (format.equalsIgnoreCase("DD-MONTH-YYYY")) {
				result = csc.getDD()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getEngYYYY();
			} else if (format.equalsIgnoreCase("DD-MONTH-YY")) {
				result = csc.getDD()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getEngYY();
			} else if (format.equalsIgnoreCase("DDMMYYYY")) {
				result = csc.getDD()+csc.getMM()+csc.getEngYYYY();
			} else if (format.equalsIgnoreCase("DDMMYY")) {
				result = csc.getDD()+csc.getMM()+csc.getEngYY();
			} else if (format.equalsIgnoreCase("DDMONTHYYYY")) {
				result = csc.getDD()+csc.getEngShortMonth().toUpperCase()+csc.getEngYYYY();
			} else if (format.equalsIgnoreCase("DDMONTHYY")) {
				result = csc.getDD()+csc.getEngShortMonth().toUpperCase()+csc.getEngYY();
			} else if (format.equalsIgnoreCase("YYYY-MM-DD")) {
				result = csc.getEngYYYY()+"-"+csc.getMM()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("YY-MM-DD")) {
				result = csc.getEngYY()+"-"+csc.getMM()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("YYYY-MONTH-DD")) {
				result = csc.getEngYYYY()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("YY-MONTH-DD")) {
				result = csc.getEngYY()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("YYYYMMDD")) {
				result = csc.getEngYYYY()+csc.getMM()+csc.getDD();
			} else if (format.equalsIgnoreCase("YYMMDD")) {
				result = csc.getEngYY()+csc.getMM()+csc.getDD();
			} else if (format.equalsIgnoreCase("YYYYMONTHDD")) {
				result = csc.getEngYYYY()+csc.getEngShortMonth().toUpperCase()+csc.getDD();
			} else if (format.equalsIgnoreCase("YYMONTHDD")) {
				result = csc.getEngYY()+csc.getEngShortMonth().toUpperCase()+csc.getDD();
			} 
			/**
			 * @author aGaziin
			 * on 04/06/2010
			 * ����� ONESQA ����Ҫ���������ѹ��͹��ѧ 1 �ѹ
			 */
			else if (format.equalsIgnoreCase("YYYYMMDD-decDate")){
				csc.decDate();
				result = csc.getEngYYYY()+csc.getMM()+csc.getDD();
			}
			// ============= THAI YEAR ===============
			else if (format.equalsIgnoreCase("DD-MM-TTTT")) {
				result = csc.getDD()+"-"+csc.getMM()+"-"+csc.getThaiYYYY();
			} else if (format.equalsIgnoreCase("DD-MM-TT")) {
				result = csc.getDD()+"-"+csc.getMM()+"-"+csc.getThaiYY();
			} else if (format.equalsIgnoreCase("DD-MONTH-TTTT")) {
				result = csc.getDD()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getThaiYYYY();
			} else if (format.equalsIgnoreCase("DD-MONTH-TT")) {
				result = csc.getDD()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getThaiYY();
			} else if (format.equalsIgnoreCase("DDMMTTTT")) {
				result = csc.getDD()+csc.getMM()+csc.getThaiYYYY();
			} else if (format.equalsIgnoreCase("DDMMTT")) {
				result = csc.getDD()+csc.getMM()+csc.getThaiYY();
			} else if (format.equalsIgnoreCase("DDMONTHTTTT")) {
				result = csc.getDD()+csc.getEngShortMonth().toUpperCase()+csc.getThaiYYYY();
			} else if (format.equalsIgnoreCase("DDMONTHTT")) {
				result = csc.getDD()+csc.getEngShortMonth().toUpperCase()+csc.getThaiYY();
			} else if (format.equalsIgnoreCase("TTTT-MM-DD")) {
				result = csc.getThaiYYYY()+"-"+csc.getMM()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("TT-MM-DD")) {
				result = csc.getThaiYY()+"-"+csc.getMM()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("TTTT-MONTH-DD")) {
				result = csc.getThaiYYYY()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("TT-MONTH-DD")) {
				result = csc.getThaiYY()+"-"+csc.getEngShortMonth().toUpperCase()+"-"+csc.getDD();
			} else if (format.equalsIgnoreCase("TTTTMMDD")) {
				result = csc.getThaiYYYY()+csc.getMM()+csc.getDD();
			} else if (format.equalsIgnoreCase("TTMMDD")) {
				result = csc.getThaiYY()+csc.getMM()+csc.getDD();
			} else if (format.equalsIgnoreCase("TTTTMONTHDD")) {
				result = csc.getThaiYYYY()+csc.getEngShortMonth().toUpperCase()+csc.getDD();
			} else if (format.equalsIgnoreCase("TTMONTHDD")) {
				result = csc.getThaiYY()+csc.getEngShortMonth().toUpperCase()+csc.getDD();
			}
				
			else {
				MyLog.error("Can't get FileNameFormat ["+format+"] in System !!! ");
				result = format;
			}
		} catch (Exception e) {
			MyLog.error("FileNameFormat in CSC_QUQUE is wrong !!! [#FORMAT#] : "+fileNameFormat+"\n"+e);
		}
		return fixStartName+result+fixEndName; 
	}
}