SqlldrHandler.java 5.26 KB
package com.sitech.ismp.coll.busi.e2e.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;

import com.sitech.base.AgentProperties;
import com.sitech.ismp.coll.busi.e2e.E2EConstant;
import com.sitech.util.DES3;
import com.sitech.util.Formater;

/**
 * SqlldrHandler
 *
 * @author   linxc
 * @version  
 * @since    Ver 1.1
 * @Date	 2012	May 18, 2012		10:08:01 AM
 */
public class SqlldrHandler {
	private Logger logger = Logger.getLogger("BUSI_COLL");
	
	public static String templatePath = AgentProperties.AGENT_HOME + "/config/velocity/";
	public static String sqlldrCtlPath = AgentProperties.AGENT_HOME + "/data/";
	public static String logPath = AgentProperties.AGENT_HOME + "/logs/";
	public static String shellPath = AgentProperties.AGENT_HOME + "/script/busi/";
	public static String noticePath = AgentProperties.AGENT_HOME + "/notice/busi/";
	
	private int dataFileType;
	private String dataFileTime;
	private String dataFileName;	
	private String badFileName;
	private String logFileName;	
	private String sqlldrCtlFileName;
	private String sqlldrExecShellName;
	private String noticeFileName;
	
	private String username;
	private String password;
	
	public SqlldrHandler(String sFileType, String dataFileName,String username,String password) {
		try{
			this.username = username;
			this.password = DES3.decrypt(password);
			
			this.dataFileType = Integer.parseInt(sFileType);
			this.dataFileTime = getDataFileTime(dataFileName);			
			this.dataFileName = sqlldrCtlPath + dataFileName;
			this.badFileName = sqlldrCtlPath + getBadFileName(dataFileName);
			this.logFileName = logPath + getLogFileName(dataFileName);
			this.sqlldrCtlFileName = sqlldrCtlPath + getSqlldrCtlFileName(dataFileName);
			this.sqlldrExecShellName = shellPath + getqlldrExecShellName(dataFileName);
			this.noticeFileName = noticePath + getqlldrExecShellName(dataFileName);
		}catch (Exception e) {
			logger.error("Exception while create SqlldrHandler.", e);
		}
	}
	
	/**
	 * 创建文件入库的控制文件
	 */
	public void createDataCtl() throws Exception{
		String templateName = "";
		switch (dataFileType) {
		case E2EConstant.FILE_TYPE_CRM_ORDER:
			templateName = "crm_order_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_CRM_ORDER_PROP:
			templateName = "crm_prop_ctl.vm";			
			break;
		case E2EConstant.FILE_TYPE_IBP_FINISH_ORDER:
			templateName = "ibp_finish_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_IBP_ORDER:
			templateName = "ibp_order_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_IBP_RECEIVE_ORDER:
			templateName = "ibp_receive_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_CA_OUTSIDE_ORDER:
			templateName = "ca_order_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_KX_ORDER:	
			templateName = "onestop_kx_order_ctl.vm";
			break;
		case E2EConstant.FILE_TYPE_ONESTOP_CRM_ORDER:
			templateName = "onestop_crm_order_ctl.vm";
			break;
		default:
			break;
		}

		VelocityContext vc = new VelocityContext();
		String context = null;
		vc.put("INFILE", dataFileName);
		vc.put("BADFILE", badFileName);
		vc.put("FILE_TIME", dataFileTime);

		StringWriter writer = new StringWriter();
		FileTemplate.getRuleTemplate(templatePath, templateName).merge(vc, writer);
		context = writer.toString();

		createFiles(sqlldrCtlFileName, context);
	}


	/**
	 * 创建执行SQL Loader的shell脚本
	 */
	public void createSqlldrExecShell() throws Exception {
		VelocityContext vc = new VelocityContext();
		String context = null;
		vc.put("USERNAME", username);
		vc.put("PASSWORD", password);
		vc.put("DATA_CTL", sqlldrCtlFileName);
		vc.put("LOG_FILE", logFileName);
		vc.put("DATA_FILE", dataFileName);

		StringWriter writer = new StringWriter();
		FileTemplate.getRuleTemplate(templatePath, "sqlldrExecShell.vm").merge(vc, writer);
		context = writer.toString();

		createFiles(sqlldrExecShellName, context);
		
		createFiles(noticeFileName, noticeFileName);
	}
	
	private String getqlldrExecShellName(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf(".")) + ".sh";
	}
	
	private String getSqlldrCtlFileName(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf(".")) + ".ctl";
	}

	private String getBadFileName(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf(".")) + ".bad";
	}
	
	private String getLogFileName(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf(".")) + ".log";
	}
	
	private String getDataFileTime(String fileName) throws Exception {
		String subFileName = fileName.substring(fileName.lastIndexOf("_") + 1,
				fileName.lastIndexOf("."));
		Date fileTime = Formater.stringToDate(subFileName, "yyyyMMddHH");
		return Formater.datetimeToString(fileTime);
	}	
	
	private File createFiles(String filename, String context)
			throws Exception {
		File redirect = null;
		if (filename.lastIndexOf("/") != -1) {
			String dir = filename.substring(0, filename.lastIndexOf("/"));
			redirect = new File(dir);
			if (!redirect.exists()) {
				redirect.mkdirs();
			}
		}
		File file = new File(filename);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(context);
		bw.flush();
		bw.close();
		return file;
	}
}