package com.fujitsu.futurity.bp.custom.common;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fujitsu.futurity.bp.custom.mapping.DKSV0074_DKSV0074OP_EKKA0020005BSMapper;
import com.fujitsu.futurity.bp.x21.bpm.ServiceComponentRequestInvoker;
import com.fujitsu.futurity.bp.x21.bpm.db.SessionHandle;
import com.fujitsu.futurity.bp.x21.bpm.exception.RequestParameterException;
import com.fujitsu.futurity.bp.x21.bpm.parameter.IRequestParameterReadOnly;
import com.fujitsu.futurity.bp.x21.bpm.parameter.IRequestParameterReadWrite;
import com.fujitsu.futurity.bp.x21.cc.exception.SCCallException;
import com.fujitsu.futurity.bp.x21.sc.exception.SCException;
import com.fujitsu.futurity.common.JCMConstants;
import com.fujitsu.futurity.common.x01.sc.ErrorInfoMapKeys;
import com.fujitsu.futurity.common.x01.sc.SCControlMapKeys;
import com.fujitsu.futurity.mapping.bp.common.TemplateErrorUtil;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.base.CAANSchemaInfo;
import com.fujitsu.futurity.model.common.JCMAPLConstMgr;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;
import com.fujitsu.futurity.model.ejb.common.StatusCodes;

import eo.common.util.JPCUtilCommon;
import eo.ejb.cbs.cbsmsg.EDKA0010008CBSMsg;
import eo.ejb.cbs.cbsmsg.EDKA0010008ConditionList;
import eo.ejb.cbs.cbsmsg.EKKA0020005CBSMsg2List;

public class JDKBPCommon extends JPCUtilCommon {
	/**
	 * CCで検知したエラー情報を呼び出し元に通知します。
	 * @param param パラメータ
	 * @param itemName エラーの起こった項目名
	 * @param errorLevel Eから始まるエラーレベル
	 * @param ifId エラーが起こったことにするインターフェイスＩＤ
	 * @throws RequestParameterException 例外発生時
	 */
	@SuppressWarnings("unchecked")
	public static void addError(IRequestParameterReadWrite param, String itemName, String errorLevel, String ifId, int status, int returnCode) throws RequestParameterException
	{
		List<Map<String, Object>> errors = null;
		Object errorInfos = param.getControlMapData(SCControlMapKeys.ERROR_INFO);
		if (errorInfos == null)
		{
			errors = new ArrayList<Map<String, Object>>();
			param.setControlMapData(SCControlMapKeys.ERROR_INFO, errors);
		}
		else
		{
			errors = (List<Map<String, Object>>)errorInfos;
		}
		
		String recentryErrorStatusString = (String)param.getControlMapData(SCControlMapKeys.RETURN_CODE);
		Integer recentryErrorStatus = 0;
		try
		{
			if (recentryErrorStatusString != null)
			{
				recentryErrorStatus = Integer.parseInt(recentryErrorStatusString);
			}
		} catch (NumberFormatException e)
		{
		}
		if (status > recentryErrorStatus)
		{
			String formatStatus = String.format("%1$04d", status);
			String message = JCMAPLConstMgr.getString("RETURN_MESSAGE_" + formatStatus);
			param.setControlMapData(SCControlMapKeys.RETURN_CODE, formatStatus);
			param.setControlMapData(SCControlMapKeys.RETURN_MESSAGE, message);	
		}

		Map<String, String> itemCheckErrorMap = new HashMap<String, String>();
		itemCheckErrorMap.put(itemName, errorLevel);
		
		Map<String, Object> errorInfoMap = new HashMap<String, Object>();
		errorInfoMap.put(ErrorInfoMapKeys.ITEM_CHECK_ERRORS, itemCheckErrorMap);
		errorInfoMap.put(ErrorInfoMapKeys.TEMPLATE_ID, ifId);
		errorInfoMap.put(ErrorInfoMapKeys.STATUS, status);
		errorInfoMap.put(ErrorInfoMapKeys.RETURN_CODE, returnCode);
		
		errors.add(errorInfoMap);
	}
	
	/**
	 * エラーがあるかどうかをチェックします。
	 * @param param パラメータ
	 * @return true:エラーあり false:エラーなし
	 * @throws RequestParameterException 例外発生時
	 */
	@SuppressWarnings("unchecked")
	public static boolean hasError(IRequestParameterReadOnly param) throws RequestParameterException
	{
		List<Map<String, Object>> errors = null;
		Object errorInfos = param.getControlMapData(SCControlMapKeys.ERROR_INFO);
		if (errorInfos == null)
		{
			return false;
		}
		errors = (List<Map<String, Object>>)errorInfos;
		
		return !errors.isEmpty();
	}
	
	
	/**
	 * 何らかのエラーがある場合、例外を送出します
	 */
	public static void throwScExceptionIfHasError(String message, IRequestParameterReadOnly param) throws RequestParameterException, SCCallException
	{
		int bpStatus = Integer.parseInt((String)param.getControlMapData(SCControlMapKeys.RETURN_CODE));
		if (bpStatus == 0)
		{
			return;
		}
		throw new SCCallException(message, "0", bpStatus);
	}
	
	
//	/**
//	 * 製造番号を指定してSTBIDを取得します。
//	 */
//	@SuppressWarnings("unchecked")
//	public static String getSTBID(String seizoNo, String modelCd, SessionHandle handle, IRequestParameterReadWrite param)
//	throws SCException, RequestParameterException, Exception {
//		// 製造番号なしの場合は、以下の処理を行わない
//		if (seizoNo == null || seizoNo.length() <= 0) {
//			return null;
//		}
//
//		HashMap<String, Object> paramMap = setServiceInterfaceMetaData(param);
//		
//		CAANMsg parentTemplate = new CAANMsg(EDKA0010008CBSMsg.class.getName());
//		CAANMsg[] condition = {new CAANMsg(EDKA0010008ConditionList.class.getName())};
//		parentTemplate.set(EDKA0010008CBSMsg.EDKA0010008CONDITIONLIST, condition);
//
//		parentTemplate.set(JCMConstants.OPERATE_DATE_KEY, com.fujitsu.futurity.bp.custom.common.JCCBPCommon.getOpeDate(null));
//		parentTemplate.set(JCMConstants.OPERATE_DATETIME_KEY, com.fujitsu.futurity.bp.custom.common.JCCBPCommon.getOpeDateTimeStamp(null));
//		parentTemplate.set(EDKA0010008CBSMsg.FUNC_CODE, "1");
//		condition[0].set(EDKA0010008ConditionList.KEY_KK_SEIZO_NO, seizoNo);
//		condition[0].set(EDKA0010008ConditionList.KEY_TK_MDL_CD, modelCd);
//		condition[0].set(EDKA0010008ConditionList.KEY_TRN_DIV, "1");
//		condition[0].setNull(EDKA0010008ConditionList.KEY_TAKNKIKI_SETHIN_MODEL_CD);
//		condition[0].setNull(EDKA0010008ConditionList.KEY_TAKNKIKI_SETHIN_NO);
//
//		CAANMsg[] ts = {parentTemplate};
//		paramMap.put(JCMConstants.TEMPLATE_LIST_KEY, ts);
//
//		JSYejbLog.println(JSYejbLog.DEBUG, JDKBPCommon.class, "宅内機器一覧照会(5)処理の実行");
//		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
//		Map takunaiDatas = scCall.run(paramMap, handle);
//		
//		
//		// SCからの戻り値からCAANMsgを取得する。
//		CAANMsg[] templates = (CAANMsg[]) takunaiDatas.get(JCMConstants.TEMPLATE_LIST_KEY);
//		// リターンコード取得
//		Object return_code = takunaiDatas.get(JCMConstants.RET_CD_INT_KEY);
//		// エラー情報の設定
//        param = editErrorInfo(param, templates, (Integer)return_code);
//		//エラー情報のマップを取得
//		ArrayList<Object> errList = (ArrayList<Object>)param.getControlMapData(SCControlMapKeys.ERROR_INFO);
//		if(errList == null){
//			errList = new ArrayList<Object>();
//		}
//		// コントロールマップに設定
//		param.setControlMapData(SCControlMapKeys.ERROR_INFO, TemplateErrorUtil.getErrorInfo(takunaiDatas, errList));
//		
//		
//		if (JDKBPCommon.hasError(param))
//		{
//			throw new SCCallException("宅内機器一覧照会(5)処理失敗", "0", 
//										Integer.parseInt(param.getControlMapData("returnCode").toString()));
//		}
//
//		JDKBPCommon.throwScExceptionIfHasError("宅内機器一覧照会(5)処理リターンコード不正", param);
//
//		if (templates == null || templates.length == 0)
//		{
//			return null;
//		}
//		CAANMsg parentTemplateResult = templates[0];
//		CAANMsg[] templateArray = parentTemplateResult.getCAANMsgList(EDKA0010008CBSMsg.EDKA0010008CBSMSG1LIST);
//		if (templateArray == null || templateArray.length == 0)
//		{
//			return null;
//		}
//		CAANMsg templateResult = templateArray[0];
//		
//		return templateResult.getString(EDKA0010008CBSMsg1List.STB_ID);
//	}

	
	/**
	 * 指定キーの在庫数量を取得します
	 * @param warehouse 倉庫
	 * @param shelfCode 棚
	 * @param modelCode 型番
	 * @param idomotoKriplaceSkcd 移動元管理場所識別コード
	 * @param handle なにか
	 * @param param わからん
	 * @return
	 * @throws RequestParameterException 例外
	 * @throws SCException 例外
	 * @throws Exception 例外
	 */
	@SuppressWarnings("unchecked")
	public static Integer getStockAmount(String warehouse, String shelfCode, String modelCode, String idomotoKriplaceSkcd, SessionHandle handle, IRequestParameterReadWrite param)
	throws RequestParameterException, SCException, Exception {
		Map map = new HashMap();
		map.put("sokoCd", warehouse);
		map.put("tanaCd", shelfCode);
		map.put("modelCd", modelCode);
		map.put("idomotoKriplaceSkcd", idomotoKriplaceSkcd);
		
		List<Map> maps = new ArrayList<Map>();
		maps.add(map);
		
		return getStockAmount(maps, "sokoCd", "tanaCd", "modelCd", "idomotoKriplaceSkcd", handle, param).getValue(warehouse, shelfCode, modelCode, idomotoKriplaceSkcd);
	}

	/**
	 * 指定キーの在庫数量を取得します
	 * @param warehouse 倉庫
	 * @param shelfCode 棚
	 * @param modelCode 型番
	 * @param idomotoKriplaceSkcd 移動元管理場所識別コード
	 * @param handle なにか
	 * @param param わからん
	 * @return
	 * @throws RequestParameterException 例外
	 * @throws SCException 例外
	 * @throws Exception 例外
	 */
	@SuppressWarnings("unchecked")
	public static JDKStockMap getStockAmount(List<Map> maps, String warehouseKey, String shelfCodeKey, String modelCodeKey, String idomotoKriplaceSkcd, SessionHandle handle, IRequestParameterReadWrite param)
	throws RequestParameterException, SCException, Exception {
		DKSV0074_DKSV0074OP_EKKA0020005BSMapper mapper = new DKSV0074_DKSV0074OP_EKKA0020005BSMapper();
		HashMap<String, Object> paramMap = mapper.editInMsg(maps, warehouseKey, shelfCodeKey, modelCodeKey, idomotoKriplaceSkcd, param);
		JSYejbLog.println(JSYejbLog.DEBUG, JDKBPCommon.class, "宅内機器物流在庫一意照会処理の実行");
		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
		Map<?, ?> scResult = scCall.run(paramMap, handle);
		mapper.editResultRP(scResult, param);

		if (JDKBPCommon.hasError(param))
		{
			throw new SCCallException("宅内機器物流在庫一意照会処理失敗", "0", 
										Integer.parseInt(param.getControlMapData("returnCode").toString()));
		}

		JDKBPCommon.throwScExceptionIfHasError("宅内機器物流在庫一意照会処理リターンコード不正", param);

		JDKStockMap stock = new JDKStockMap();

		Map map = (Map)param.getData("DKSV007402CC");
		List list = (List)map.get("EKKA0020005CBSMsg2List");
		if (list == null || list.isEmpty())
		{
			return stock;
		}

		for (Map data : (List<Map>)list)
		{
			String warehouse = (String)data.get(EKKA0020005CBSMsg2List.KNRI_PLC_CD);
			String shelf = (String)data.get(EKKA0020005CBSMsg2List.KNRI_PLC_SLF_CD);
			String modelNo = (String)data.get(EKKA0020005CBSMsg2List.TK_MDL_CD);
			String kriplace_skcd = (String)data.get(EKKA0020005CBSMsg2List.KNRI_PLC_SKBT_CD);
			String [] keies = {warehouse, shelf, modelNo, kriplace_skcd};
			
			Integer jitsuZaiko = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.JZAIKO_CNT).toString());
			Integer haisoShukkaSiji = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.HAISO_SHKA_SJI_CNT).toString());
			Integer yobikikiHaisoSiji = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.YBKIKI_HAISO_SJI_CNT).toString());
			Integer shelfIdoSiji = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.SHELF_MV_SJI_CNT).toString());
			Integer yobikikiHaiso = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.YBKIKI_HAISO_CHU_CNT).toString());
			Integer yobikikiFin = Integer.parseInt(data.get(EKKA0020005CBSMsg2List.YBKIKI_SHKA_FIN_CNT).toString());
			
			int stockAmount = jitsuZaiko - (haisoShukkaSiji + yobikikiHaisoSiji + shelfIdoSiji + yobikikiHaiso + yobikikiFin);
			stock.setValue(stockAmount, keies);
		}
		
		return stock;
	}

	/**
	 * 宅内から機器情報を取得します
	 * @param list 引数リスト
	 * @param modelNoKey 型番号のキー
	 * @param productNoKey 製造番号のキー
	 * @param handle はんどる
	 * @param param ぱらむ
	 * @return 宅内から取得した機器情報
	 * @throws RequestParameterException 例外
	 * @throws SCException 例外
	 * @throws Exception 例外
	 */
	@SuppressWarnings("unchecked")
	public static List<Map> getLastUpdateDtm(List<Map> list, String modelNoKey, String productNoKey, SessionHandle handle, IRequestParameterReadWrite param)
	throws RequestParameterException, SCException, Exception
	{
		return getLastUpdateDtm(list, modelNoKey, productNoKey, handle, param, true);
	}

	/**
	 * 宅内から機器情報を取得します
	 * @param list 引数リスト
	 * @param modelNoKey 型番号のキー
	 * @param productNoKey 製造番号のキー
	 * @param handle はんどる
	 * @param param ぱらむ
	 * @param reportError 例外を報告するかどうか
	 * @return 宅内から取得した機器情報
	 * @throws RequestParameterException 例外
	 * @throws SCException 例外
	 * @throws Exception 例外
	 */
	@SuppressWarnings("unchecked")
	public static List<Map> getLastUpdateDtm(List<Map> list, String modelNoKey, String productNoKey, SessionHandle handle, IRequestParameterReadWrite param, boolean reportError)
	throws RequestParameterException, SCException, Exception {
		HashMap<String, Object> paramMap = setServiceInterfaceMetaData(param);
		
		CAANMsg template = new CAANMsg(EDKA0010008CBSMsg.class.getName());
		template.set(EDKA0010008CBSMsg.TEMPLATEID, "EDKA0010008");
		template.set(EDKA0010008CBSMsg.FUNC_CODE, "1");
		Object operatorId = param.getControlMapData(SCControlMapKeys.OPERATOR_ID);
		template.set(JCMConstants.OPERATOR_ID_KEY, operatorId);
		Object operateDate = param.getControlMapData(SCControlMapKeys.OPE_DATE);
		template.set(JCMConstants.OPERATE_DATE_KEY, operateDate);
		Object operateDateTime = param.getControlMapData(SCControlMapKeys.OPE_TIME);
		template.set(JCMConstants.OPERATE_DATETIME_KEY, operateDateTime);

		CAANMsg[] templates = new CAANMsg[1];
		templates[0] = template;
		paramMap.put(JCMConstants.TEMPLATE_LIST_KEY, templates);

		List<CAANMsg> msg1List = new ArrayList<CAANMsg>();

		for (int i = 0;i < list.size();i++) {
			Map map = list.get(i);

			// 製造番号なしの場合は、以下の処理を行わない
			if (map.get(productNoKey) == null || map.get(productNoKey).toString().length() <= 0) {
				continue;
			}

			CAANMsg msg1 = new CAANMsg(EDKA0010008ConditionList.class.getName());
			msg1.set(EDKA0010008ConditionList.KEY_TK_MDL_CD, map.get(modelNoKey));
			msg1.set(EDKA0010008ConditionList.KEY_KK_SEIZO_NO, map.get(productNoKey));
			msg1.set(EDKA0010008ConditionList.KEY_TRN_DIV, "1");
			msg1.setNull(EDKA0010008ConditionList.KEY_TAKNKIKI_SETHIN_MODEL_CD);
			msg1.setNull(EDKA0010008ConditionList.KEY_TAKNKIKI_SETHIN_NO);
			msg1List.add(msg1);
		}

		if (msg1List.size() <= 0) {
			return null;
		}

		template.set(EDKA0010008CBSMsg.EDKA0010008CONDITIONLIST, msg1List.toArray(new CAANMsg[msg1List.size()]));

		JSYejbLog.println(JSYejbLog.DEBUG, JDKBPCommon.class, "宅内機器一覧照会(5)処理の実行");
		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
		Map takunaiDatas = scCall.run(paramMap, handle);
		
		
		// リターンコード取得
		Object return_code = takunaiDatas.get(JCMConstants.RET_CD_INT_KEY);
		// エラー情報の設定
        param = editErrorInfo(param, templates, (Integer)return_code);
		//エラー情報のマップを取得
		ArrayList<Object> errList = (ArrayList<Object>)param.getControlMapData(SCControlMapKeys.ERROR_INFO);
		if(errList == null){
			errList = new ArrayList<Object>();
		}
		// コントロールマップに設定
		param.setControlMapData(SCControlMapKeys.ERROR_INFO, TemplateErrorUtil.getErrorInfo(takunaiDatas, errList));
		
		
		if (JDKBPCommon.hasError(param))
		{
			throw new SCCallException("宅内機器一覧照会(5)処理失敗", "0", 
										Integer.parseInt(param.getControlMapData("returnCode").toString()));
		}
		
		JDKBPCommon.throwScExceptionIfHasError("宅内機器一覧照会(5)処理リターンコード不正", param);

		templates = (CAANMsg[]) takunaiDatas.get(JCMConstants.TEMPLATE_LIST_KEY);
		if (reportError && (templates == null || templates.length == 0))
		{
			addError(param, EDKA0010008CBSMsg.EDKA0010008CBSMSG1LIST_ERR, "EA", "EDKA0010008", StatusCodes.RELATION_ERR, 0);
			JDKBPCommon.throwScExceptionIfHasError("宅内連携処理でエラー", param);
			return null;
		}
		CAANMsg parentTemplateResult = templates[0];
		CAANMsg[] templateArray = parentTemplateResult.getCAANMsgList(EDKA0010008CBSMsg.EDKA0010008CBSMSG1LIST);
		if (reportError && (templateArray == null || templateArray.length == 0))
		{
			addError(param, EDKA0010008CBSMsg.EDKA0010008CBSMSG1LIST_ERR, "EA", "EDKA0010008", StatusCodes.RELATION_ERR, 0);
			JDKBPCommon.throwScExceptionIfHasError("宅内連携処理でエラー", param);
			return null;
		}
		
		if (templateArray == null || templateArray.length == 0)
		{
			return null;
		}
		
		List<Map> results = new ArrayList<Map>();
		for (int i = 0;i < templateArray.length;i++)
		{
			results.add(templateArray[i].getHashMap());
		}
		
		return results;
	}
	
	
	/**
	 * エラーのコレクションを取得します
	 */
	@SuppressWarnings("unchecked")
	public static void overwriteErrors(Map bpValue, CAANMsg ifValue, CAANSchemaInfo dataDescriptor)
	{
		Enumeration<String> keies = dataDescriptor.getKeys();
		while(keies.hasMoreElements())
		{
			String key = keies.nextElement();
			String errKey = key + "_err";
			
			if(ifValue.containsKeyOfMsgData(errKey))
			{
				bpValue.put(errKey, ifValue.getString(errKey));
			}
		}
	}
	

	/**
	 * エラーのコレクションを取得します
	 * bpDataMapは対象となるSCもしくはCCのデータマップ
	 * 一般的には param.getData("XXXXXXXXSC")
	 * で取得されるデータ
	 * ifValusはtemplatesの第一要素
	 * 一般的には template[0]で取得されるデータ
	 * ex) JDKBPCommon.overwriteErrorsInList(dataMap, template, EKKA0020005CBSMsg.EKKA0020005CBSMSG1LIST, new EKKA0020005CBSMsg1List());
	 */
	@SuppressWarnings("unchecked")
	public static void overwriteErrorsInList(Map bpDataMap, CAANMsg ifValues, String listName, CAANSchemaInfo dataDescriptor)
	{		
		CAANMsg [] caanList = ifValues.getCAANMsgList(listName);
		if (caanList == null)
		{
			return;
		}
		
		if (!bpDataMap.containsKey(listName))
		{
			bpDataMap.put(listName, new ArrayList<Map>());
		}
		List<Map> mapList = (List<Map>)bpDataMap.get(listName);
		
		
		int index = 0;
		for (CAANMsg ifValue : caanList)
		{
			Map bpValue = null;
			if (mapList.size() <= index)
			{
				mapList.add(new HashMap());
			}
			bpValue = mapList.get(index);
			
			overwriteErrors(bpValue, ifValue, dataDescriptor);
			
			index++;
		}
	}
	
	/**
	 * サービスインターフェイス呼び出しの共通パラメータを設定する
	 */
	private static HashMap<String, Object> setServiceInterfaceMetaData(IRequestParameterReadWrite param) throws RequestParameterException
	{
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put(JCMConstants.TRANZACTION_ID_KEY, param.getTelegramID());
		paramMap.put(JCMConstants.USECASE_ID_KEY, param.getUsecaseID());
		paramMap.put(JCMConstants.OPERATION_ID_KEY, param.getOperationID());
		paramMap.put(JCMConstants.CALL_TYPE_KEY, param.getCallType());
		paramMap.put(JCMConstants.CLIENT_HOST_NAME_KEY, param.getControlMapData(SCControlMapKeys.REQ_HOSTNAME));
		paramMap.put(JCMConstants.CLIENT_IP_ADDRESS_KEY, param.getControlMapData(SCControlMapKeys.REQ_HOSTIP));
		paramMap.put(JCMConstants.INVOKE_GAMEN_ID_KEY, param.getControlMapData(SCControlMapKeys.REQ_VIEWID));
		paramMap.put(JCMConstants.OPERATOR_ID_KEY, param.getControlMapData(SCControlMapKeys.OPERATOR_ID));
		
		return paramMap;
	}


	
	/**
	 * 宅内のエラーレベルを取得します。
	 */
	@SuppressWarnings("unchecked")
	public static String getTakunaiError(Map datas) {
		return getTakunaiResult(datas, "error_level");
	}
	
	/**
	 * 宅内のリターンコードを取得します。
	 */
	@SuppressWarnings("unchecked")
	public static String getTakunaiReturnCode(Map datas) {
		return getTakunaiResult(datas, "return_Cd");
	}

	/**
	 * 宅内のリターンメッセージを取得します。
	 */
	@SuppressWarnings("unchecked")
	public static String getTakunaiReturnMessage(Map datas) {
		return getTakunaiResult(datas, "return_Message");
	}
	
	/**
	 * 宅内呼び出しがエラーであったかどうかを判断します
	 */
	@SuppressWarnings("unchecked")
	public static boolean isTakunaiError(Map datas) {
		String errorLevel = getTakunaiError(datas);
		if (errorLevel == null) {
			return false;
		}
		return "000".equals(errorLevel);
	}
	
	/**
	 * 宅内の特定メッセージを取得します。
	 */
	@SuppressWarnings("unchecked")
	public static String getTakunaiResult(Map datas, String key) {
		if (!datas.containsKey(key)) {
			return null;
		}
		return datas.get(key).toString();
	}
	
	/**
	 * サービスの項目をサービスインターフェイスに移送します
	 * @param bpProp サービスの項目
	 * @param templateProp サービスインターフェイスの項目
	 * @param template テンプレート
	 * @param bpMap サービスの引数
	 */
	@SuppressWarnings("unchecked")
	public static void setProp(String bpProp, String templateProp, CAANMsg template, Map bpMap)
	{
		if (bpMap == null || bpMap.get(bpProp) == null || "".equals(bpMap.get(bpProp))) {
			template.setNull(templateProp);
		} else {
			template.set(templateProp, (String) bpMap.get(bpProp));
		}
	}
	
	public static void setNulls(CAANMsg template, String...keies) 
	{
		for (String key : keies)
		{
			template.setNull(key);
		}
	}
	
	/**
	 * ワークから値をとりだし設定します
	 */
	@SuppressWarnings("unchecked")
	public static void setFromWork(String workName, String workListName, String workPropName, String ifKey, CAANMsg inMsg, IRequestParameterReadWrite param)
	throws RequestParameterException
	{
		List<Map> list = (List<Map>)((Map)param.getMappingWorkArea().get(workName)).get(workListName);
		if (list == null || list.size() == 0)
		{
			inMsg.setNull(ifKey);
		}
		
		Map map = list.get(0);
		if (map.containsKey(workPropName))
		{
			inMsg.set(ifKey, map.get(workPropName).toString());
		}
		else
		{
			inMsg.setNull(ifKey);
		}
	}
	
	
	/**
	 *<DL>
	 *<DT>処理概要:
	 *<DD>サービスコンポーネント実行後に、IRequestParameterReadWriteにエラー情報をマッピングする
	 *<DT>入力パラメータ説明:
	 *<DD>特になし
	 *<DT>入力パラメータ説明：
	 *<DD>特になし
	 *<DT>出力パラメータ説明:
	 *<DD>特になし
	 *</DL>
	 * @param param (I) 業務データ取得・書込用I/F
	 * @param templates (I) CAANMsgクラス配列
	 * @param returnCode (I) リターンコード
	 * @return 業務データ取得・書込用I/F
	 * @since 2013.03.08
	 */
	public static IRequestParameterReadWrite editErrorInfo(IRequestParameterReadWrite param, CAANMsg[] templates, int returnCode)
	throws Exception {
		CAANMsg template = templates[0];
		int templateStatus = template.getInt(EDKA0010008CBSMsg.STATUS);
		if (returnCode != 0) {
			templateStatus = 9000;
		}
		if (JCMAPLConstMgr.getString("RETURN_MESSAGE_" + String.format("%1$04d", templateStatus)) == null) {
			templateStatus = 0;
		}
		
		int bpStatus = 0;
		Object obj = param.getControlMapData(SCControlMapKeys.RETURN_CODE);
		if (obj == null) {
			bpStatus = -1;
		} else {
			bpStatus = Integer.parseInt((String)param.getControlMapData(SCControlMapKeys.RETURN_CODE));
		}

		if (templateStatus > bpStatus) {
			// BPにサービスコンポーネントのステータスを設定する。
			String formatStatus = String.format("%1$04d", templateStatus);
			String message = JCMAPLConstMgr.getString("RETURN_MESSAGE_" + formatStatus);
			param.setControlMapData(SCControlMapKeys.RETURN_CODE, formatStatus);
			param.setControlMapData(SCControlMapKeys.RETURN_MESSAGE, message);
		}

		return param;
	}

}
