package CoreFS.SB03;

import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import CoreFS.SA01.CoreCommunicationService;
import CoreFS.SA01.CoreGlobalConstants;
import CoreFS.SA01.CoreIComponent;
import CoreFS.SA05.CoreFileOperation;
import CoreFS.SA06.CoreParamObject;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SB04.CoreDesignBean;
import CoreFS.SB04.CoreXmlDesignBean;


public class CoreXmlIvokeBean extends CoreIComponent  {//
	public static CoreXmlDesignBean designbean = new CoreXmlDesignBean();
	public static HashMap rowMap = null;

	public static HashMap getRowMap(String path) {
		rowMap = designbean.list2Map((ArrayList) designbean.findAllStr(path));
		return rowMap;
	}

	public CoreReturnObject callActivity1(String path, String id,
			Object params[]) {
		CoreParamObject cpo = new CoreParamObject();
		CoreReturnObject cro = new CoreReturnObject();
		CoreFileOperation cfo = new CoreFileOperation();
		
		if (cfo.isFileExists(path)) {
			cro.setV_errCode(-1);
			cro.setV_errMsg("文件不存在...");
		} else if (cfo.isFileContentNull(path)) {
			cro.setV_errCode(-1);
			cro.setV_errMsg("文件内容为空...");
		}
		if (rowMap == null) {
			getRowMap(path);
		}
		
		CoreDesignBean rowBean = (CoreDesignBean) rowMap.get(id);
		try {
			if (rowBean == null) {
				cro.setV_errCode(-1);
				cro.setV_errMsg("找不到对应的服务id...");
			} else {
				cpo.setV_serviceName(rowBean.getSERVERNAME());
				cpo.setV_method(rowBean.getSERVERMETHODNAME());
				cpo.setV_reqParameters(params);
				cro = runAct(cpo);
			}
		} catch (Exception ex) {
			cro.setV_errCode(-1);
			cro.setV_errMsg(ex.getMessage());
		}
		return cro;
	}
	
	
	public CoreReturnObject callActivity(String id,
			Object params[]) {
		CoreParamObject cpo = new CoreParamObject();
		CoreReturnObject cro = new CoreReturnObject();
		CoreFileOperation cfo = new CoreFileOperation();
		 
		String sql="select A.PARENT_ID slnID, A.ENG_NAME Project_Name, B.ENG_NAME class_Name, C.ENG_NAME function_Name FROM "+
			"(select MID,ENG_NAME,PARENT_ID from PT_DEFIND_MODELS) A right join "+
			"(select MID,ENG_NAME,PARENT_ID from PT_DEFIND_MODELS) B on A.MID=B.PARENT_ID "+
			"right join (select FID,ENG_NAME,MID from PT_DEFIND_FUNS where FID=(select DEMAND_FID from pt_defind_funs where FID=?)) C on B.MID=C.MID";
		List rsls = new ArrayList();
//		try {
//			//rsls=this.getDao(CoreConstantData.sysbeanstr).ExcuteQueryReturnList(sql,new Object[] {id});
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		//CoreDesignBean rowBean = (CoreDesignBean) rowMap.get(id);
		try {
			
			
			if (rsls ==null) {
				cro.setV_errCode(-1);
				cro.setV_errMsg("找不到对应的服务id...");
			} else {		
				
//				String Project=((ListOrderedMap)rsls.get(0)).get("Project_Name").toString()+".";
//				cpo.setV_serviceName(Project+((ListOrderedMap)rsls.get(0)).get("CLASS_NAME").toString());
//				cpo.setV_method(((ListOrderedMap)rsls.get(0)).get("FUNCTION_NAME").toString());
			
				cpo.setV_reqParameters(params);
				cro = runAct(cpo);
			}
		} catch (Exception ex) {
			cro.setV_errCode(-1);
			cro.setV_errMsg(ex.getMessage());
		}
		return cro;
	}

	public CoreReturnObject runAct(CoreParamObject ctx) {
		CoreReturnObject rtnContext = new CoreReturnObject();

		String className = ctx.v_serviceName;
		String methodName = ctx.v_method;
		Object[] strParms = ctx.v_reqParameters;

		Object instance = null;
		Method method = null;
		String errmsg = null;
		try {
			// 创建反射类的一个实例
			instance = Class.forName(className).newInstance();

			Class[] types = null;
			if (!"".equals(strParms) && strParms != null) {
				int len = strParms.length;
				// 获取方法参数类型数组
				types = new Class[len];
				for (int i = 0; i < len; i++) {
					types[i] = strParms[i].getClass();
				}
			}
			method = instance.getClass().getMethod(methodName, types);

			rtnContext = (CoreReturnObject) method.invoke(instance, strParms);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			rtnContext.setV_errCode(-1);
			rtnContext.setV_errMsg(e.getMessage());
		}
		return rtnContext;

	}

	public static void main(String[] args) {
		CoreXmlIvokeBean t = new CoreXmlIvokeBean();
//		CoreReturnObject cpo = t.callActivity(
//				CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH, "TS000001",
//				null);
		;
	}

}
