MyImport_Absent.java 15.2 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
package com.csc.library.process;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

import org.jdom.Element;

import com.csc.library.cscimport.ImportXml;
import com.csc.library.cscimport.ProcessLineList;
import com.csc.library.cscimport.ProcessLineStrip;
import com.csc.library.cscimport.RecordSet;
import com.csc.library.database.StaticProperties;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialInquiry;
import com.csc.library.utilities.DbData;
import com.csc.library.utilities.DynamicJava;
import com.csc.library.utilities.FileString;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.ThaiUtilities;
import com.csc.library.utilities.UProfile;

public class MyImport_Absent {
	private String dataFileName = "";
	private String xmlFileName = "";
	private String tableName = "";
	private ImportXml importxml;
	private DbRecord dbrec;
	private UProfile uprofile;
	private String errorResult = "";
	private int saveResult = 0;
	private HashMap param =new HashMap();
	int imp_total=0;
	int imp_sucess=0;
	int imp_error=0;
	DbInquiry inqEmp=null;
	DbInquiry inqEmpTemp=null;
	private StringBuffer msg_error = new StringBuffer();
	private String[] errorCode = new String[12];

	public MyImport_Absent(DbRecord record, String fName, String xmlName) {
		try {
			this.dataFileName = fName;
			this.xmlFileName = xmlName;
			this.tableName = record.getTableName();
			this.dbrec = record;
			this.uprofile = dbrec.getUProfile();
			this.setUprofile(dbrec.getUProfile());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public MyImport_Absent(DbRecord record, String fName, String xmlName,HashMap param) {
		try {
			this.dataFileName = fName;
			this.xmlFileName = xmlName;
			this.tableName = record.getTableName();
			this.dbrec = record;
			this.uprofile = dbrec.getUProfile();
			this.setUprofile(dbrec.getUProfile());
			this.param=param;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public MyImport_Absent() {
	}

	public String getDataFileName() {
		return this.dataFileName;
	}

	public String getXmlFileName() {
		return this.xmlFileName;
	}

	private boolean chkNullValue() {
		if ((dataFileName.equals("")) || (dataFileName == null)
				|| (xmlFileName.equals("")) || (xmlFileName == null)) {
			MyLog.error(this, getUprofile(), "Data file or XML file is null ");
			return false;
		} else {
			return true;
		}
	}

	private ProcessLineStrip initValueListStrip(ProcessLineStrip procLS) {
		procLS = new ProcessLineStrip(this.dataFileName);
		Vector procSource = new Vector();
		Vector procField = new Vector();
		procSource = importxml.getSource();
		procLS.setObject("source", procSource.clone());
		procLS.set("processtype", importxml.getProcesType());
		if (importxml.getProcesType().equals("fixspace")) {
			Vector procBlank = new Vector();
			procBlank = importxml.getBlank();
			procLS.setObject("blank", procBlank.clone());
			procLS.setInt("recordsize", importxml.getRecSize());
		} else if (importxml.getProcesType().equals("freespace")) {
			procLS.set("worddelimeter", String.valueOf(importxml.getWordDelim()));
		} else {
			MyLog.error(this, getUprofile(),"No such process type");
			return null;
		}
		return procLS;
	}

	private ProcessLineList initValueLineList(ProcessLineList procLL) {
		procLL = new ProcessLineList(this.dataFileName);
		Vector procSource = new Vector();
		Vector procField = new Vector();
		procSource = importxml.getSource();
		procLL.set("processtype", importxml.getProcesType());
		procLL.setObject("source", procSource.clone());
		if (importxml.getProcesType().equals("fixspace")) {
			Vector procBlank = new Vector();
			procBlank = importxml.getBlank();
			procLL.setObject("blank", procBlank);
		} else if (importxml.getProcesType().equals("freespace")) {
			procLL.set("tokendelimeter", String.valueOf(importxml.getWordDelim()));
		} else {
			MyLog.error(this,getUprofile(),"No such process type");
			return null;
		}
		return procLL;
	}

	private RecordSet initValue() {
		if (this.chkNullValue()) {
			RecordSet rs = null;
			importxml = new ImportXml(this.xmlFileName);
			importxml.process();
			ProcessLineStrip procLS = null;
			ProcessLineList procLL = null;
			// init value for each process
			if (importxml.getXmlType().equals("linelist")) {
				rs = this.initValueLineList(procLL);
			} else if (importxml.getXmlType().equals("linestrip")) {
				rs = this.initValueListStrip(procLS);
			} else {
				MyLog.error(this, getUprofile(),"No such process type");
				return null;
			}
			return rs;
		} else {
			return null;
		}
	}

	public boolean process() throws RemoteException {
		this.setErrorCode();
		FileString fs = new FileString();
		DynamicJava dj = new DynamicJava();
		RecordSet rs = this.initValue();
		DbData dbData = new DbData(this.getUprofile());
		dbData.setParam(this.param);
		dbData.setCache(true);
		if (rs == null)
			return false;
		Element tmpElement = null;
		// Begin Define variable use for process
		FileString fsTmp = new FileString();
		fsTmp.setLazy(true);
		Vector elementData = importxml.getDataField();
		Vector processSource = new Vector();
		Vector processBlank = new Vector();
		String textRecord = "", result = "";
		int loop = 0;
		dj.defineVariable("fs", fs);
		dj.defineVariable("result", result);
		dj.defineVariable("dbdata", dbData);
		// End Define variable use for process
		Vector tableData = new Vector();
		String conXml = importxml.getCondition().toString();
		boolean check = false;
		int line = 1;
		int recCount = 0;
		String processID = this.getUprofile().get("processID").toString();
		while (rs.hasMoreElement()   && !StaticProperties.serverManager.isCancle(processID)) {
			textRecord = rs.next().trim();
			//this.logger.info("Record Data is:" + textRecord);
			fs.setData(textRecord);
			System.out.println(line + " : " + textRecord);
			check = checkCon(fs.getData(), conXml);
			if (!importxml.getCondition().equals("")) {
				if (check) {
					continue;
				}
			}
			loop = 0;
			while (loop < elementData.size()) { // return data for String
				try {
					processSource = (Vector) rs.get("source");
					MyLog.info(this, getUprofile(),"processSource:" + processSource);
					String strFormular = "result=String.valueOf("+ processSource.get(loop) + ");";
					MyLog.info(this, getUprofile(),"formular is:" + strFormular);
					dj.interpret(strFormular, "fact1");
					result = (String) dj.getVariable("result");
					if (rs.getString("processtype").equalsIgnoreCase("fixline")
							|| rs.getString("processtype").equalsIgnoreCase("fixspace")) {
						fsTmp.clearData();
						fsTmp.setData(result);
						processBlank = (Vector) rs.get("blank");
						MyLog.info(this, getUprofile(),"Process Blank:" + processBlank);
						fsTmp.delAllString(processBlank.get(loop).toString(),"");
						result = fsTmp.getData();
					}
					tmpElement = (Element) importxml.getDataField().get(loop);
					String xmlData = tmpElement.getText();
					MyLog.info(this, getUprofile(),"Field:" + xmlData + "\tresult:" + result);
					String[] record = { "", "", "" };
					if (xmlData.indexOf("$") > -1) {						
						//this.getDataEmp(result);
						record[0] = fs.getStrBefore(".", xmlData);
						record[1] = fs.getStrAfter("$", xmlData);
						record[2] = this.getDataEmp(result);
					} else {
						record[0] = fs.getStrBefore(".", xmlData);
						record[1] = fs.getStrAfter(".", xmlData);
						record[2] = result;
					}
//					System.out.println("DATA------->>" + record[0] + "<->" + record[1] + "<->" + record[2]);
					tableData.add(record);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				loop++;
			}
			this.writeData(tableData);
			if (this.saveResult == 1) {
				this.imp_error++;
				if (this.imp_error==1) {
//					MyLog.error(this.getUprofile(), "<<<<<<<< List of Error >>>>>>>>");
		        	StaticProperties.serverManager.writeFileResultProcess(processID, "<<<<<<<< List of Error >>>>>>>>");	
				}
				String error_msg = this.param.get("error_msg").toString();
				if(error_msg.length() == 1){
					error_msg = this.getError_code(Integer.parseInt(error_msg));
				}
				StaticProperties.serverManager.writeFileResultProcess(processID, line + " : " + textRecord + " ==> "+error_msg+"\n");
				this.setMsg_error(this.dbrec.getString("employeeid")+" : "+error_msg);
//				this.errorResult += line + " : " + textRecord + "\n";
			} else {
				this.imp_sucess++;
			}
			this.imp_total++;
			line++;
			tableData.clear();
			dbData.clearCacheData();
			//end if
        	recCount++;
        	StaticProperties.serverManager.aliveProcess(processID, "Importing line number : "+recCount);	
		}
		return true;
	}

	private boolean writeData(Vector tableData) {
		try {
			//this.dbrec = new InitialRecord(this.uprofile).getDbRecord(this.tableName);
			this.dbrec.clearField();
			ThaiUtilities thaiUtil = new ThaiUtilities();
			for (int i = 0; i < tableData.size(); i++) {
				String[] record = (String[]) tableData.get(i);
				String data = (record[2] == null) ? "" : record[2].trim();
				//System.out.println("Table:" + record[0] + "	field:" + record[1] + "	Value:" + record[2]);
				//this.dbrec.set(record[0],record[1],thaiUtil.ASCII2Unicode(data));// [0]=table name [1]=field name [2]=data
				boolean check = this.dbrec.set(record[1], thaiUtil.ASCII2Unicode(data));
				if (check == false) {
					this.saveResult=1;
					return false;
				}
			}
			String companyid = this.dbrec.getString("companyid");
			if (companyid == null || companyid.equals("")) {
				this.dbrec.set("companyid", (String) this.uprofile.get("companyid"));
			}
			
			int i=0;
			if (this.chkKeyValue(this.dbrec)) {
				i = this.dbrec.save();
			}
			
			System.out.println(">>>>>>>>> Save Status = "+i);
			if (i==1 || i==2) {
				this.saveResult = 0; 
			} else{
				this.saveResult = 1; //Not Complete			
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}	
	
	/* Edit by pea 2006-05-11
	 * Ҥ key 繤 space 
	 *  record ١ save 
	 *  RecordCmp0 ҨռšѺ Verify
	 */ 
	private boolean chkKeyValue(DbRecord rec) {
		String fieldName = "";
		try {
			StringTokenizer str = new StringTokenizer(rec.getListKey(),",");
			while (str.hasMoreTokens()) {
				fieldName = str.nextToken();
				if (rec.getField(fieldName).getType().equalsIgnoreCase("String")) {
					if (rec.getString(fieldName).trim().equals("")) {
						return false;
					}
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;		
	}
	
	public String getSummaryResult() {
		String errorHeader = "";
		errorHeader += "IMPORT DATA RESULT OF TABLE : " + this.tableName	+ "\n";
		errorHeader += "XML FILE : " + this.getXmlFileName() + "\n";
		errorHeader += "DATA FILE : " + this.getDataFileName() + "\n";
		errorHeader += "----------------------------------------------------------" + "\n";
		errorHeader += "IMPORT TOTAL : "+this.imp_total+" ROWS" + "\n";
		errorHeader += "IMPORT SUCCESS : "+this.imp_sucess+" ROWS" + "\n";
		errorHeader += "IMPORT ERROR : "+this.imp_error+" ROWS" + "\n";
		errorHeader += "----------------------------------------------------------" + "\n";
		return errorHeader;
//		MyLog.warn(this.getUprofile(), errorHeader);
		//System.out.println(errorHeader);
		//System.out.println(this.errorResult);
//		MyLog.debug(errorHeader+"\n"+this.errorResult);
//		WriteFile wf = new WriteFile(path, errorHeader + this.errorResult);
//		wf.closeFile();
	}

	private String getDataEmp(String fieldD) {
		try {
			if (this.inqEmp==null) {
				this.inqEmp = new InitialInquiry(this.uprofile).getDbInquiry();
				this.inqEmp.initMyTable("Memployee", "", "");
				this.inqEmp.setColumn("employeeid, companyid, swipecardid");
			}
			this.inqEmp.setFilter("swipecardid='" + fieldD.trim() + "'");
			this.inqEmp.refresh();
			this.inqEmp.next();
			return this.inqEmp.getString("employeeid");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/*private String getDataEmpTemp(String cardid, String dateid) {
		try {
			if (this.inqEmpTemp==null) {
				this.inqEmpTemp = new InitialInquiry(this.uprofile).getDbInquiry();
				this.inqEmpTemp.initMyTable("Mempl_cardtemp1", "", "");
				this.inqEmpTemp.setColumn("employeeid, companyid, swipecardid, dateid");
			}
			this.inqEmpTemp.setFilter("swipecardid='" + cardid.trim() + "' and dateid='"+dateid+"'");
			this.inqEmpTemp.refresh();
			this.inqEmpTemp.next();
			return inqEmpTemp.getString("employeeid");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}*/

	private boolean checkCon(String data, String con) {
		if (con == null || con.equals("")) {
			MyLog.warn(this, getUprofile(),"!!!!!!!!!!!!!!!!!!! Condition is empty !!!!!!!!!!!!!!!!!!!");
			return true;
		}
		boolean ans = false;
		FileString fs = new FileString();
		fs.setData(data);
		StringTokenizer st = new StringTokenizer(con, "?");
		String data1 = st.nextToken().toString();
		String con1 = st.nextToken().toString().trim();
		String data2 = st.nextToken().toString();
		data1 = this.runDynamic(data1, data);
		if (con1.trim().equals("==")) {
			if (data1.equals(data2)) {
				ans = false;
			} else {
				ans = true;
			}
		}
		if (con1.trim().equals("!=")) {
			if (data1.equals(data2)) {
				ans = false;
			} else {
				ans = true;
			}
		}
		return ans;
	}

	private String runDynamic(String con, String data) {
		String result = "";
		DynamicJava dj = new DynamicJava();
		FileString fs = new FileString();
		fs.setData(data);
		data = "result=" + con + ";";
		dj.defineVariable("result", result);
		dj.defineVariable("fs", fs);
		dj.interpret(data, "fact1");
		String v = dj.getVariable("result").toString();
		return v;
	}

	protected UProfile getUprofile() {
		return uprofile;
	}

	protected void setUprofile(UProfile uprofile) {
		this.uprofile = uprofile;
	}

	public HashMap getParam() {
		return param;
	}

	public void setParam(HashMap param) {
		this.param = param;
	}
	
	public void setMsg_error(String mes){
		this.msg_error.append(mes+"<br/>");
	}
	
	public StringBuffer getMsg_error(){
		return this.msg_error;
	}
	
	public void setErrorCode(){
		this.errorCode[0] = "Not Have Working Time"; 
		this.errorCode[1] = "Already took leave for today.";
		this.errorCode[2] = "Has no right to request.";
		this.errorCode[3] = "Times of leave are excess the limit.";
		//error[4] = "ln form leave less than advance limit days.";
		this.errorCode[4] = "This system not request to be past. Please contact HR";
		this.errorCode[5] = "Please finish all preriquisite leave first.";
		this.errorCode[6] = "Cannot take leave for today.";
		this.errorCode[7] = "Insufficient remaining leave.";
		this.errorCode[8] = "Sex is not match with leave type.";
		this.errorCode[9] = "Please Request Before 13.00";
		this.errorCode[11] = "Employee code is not match";
	}
	
	public String getError_code(int index){
		return this.errorCode[index];
	}
	
	
		
}