/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom						 *
**********************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JCKSV905301CC
*	ソースファイル名：JCKSV905301CC.java
*	作成者			：FJ)宇野
*	日付			：2015年01月09日
*＜機能概要＞
*	本登録移行（MVNO用SOAP）実行CCの共通コンポーネント
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v12.00.00	2015/01/09	FJ)宇野		【ANK-2381-00-00】eoIDなしの新規申込対応
*　 v26.00.00   2016/09/29  FJ)森脇     OM-2016-0002054対応
*   v28.00.00   2017/01/18  FJ)森脇     OM-2016-0001646対応
*
**********************************************************************/
package com.fujitsu.futurity.bp.custom.common;

import static com.fujitsu.futurity.bp.custom.common.JCKPmpCommonUtil.editResultErrorInfo;
import static com.fujitsu.futurity.bp.custom.common.JCKPmpCommonUtil.getLastResult;

import java.util.HashMap;
import java.util.Map;

import com.fujitsu.futurity.bp.custom.constant.CKSV9001_CKSV9001OP_CKSV900101CC;
import com.fujitsu.futurity.bp.custom.constant.CKSV9024_CKSV9024OP_CKSV902401CC;
import com.fujitsu.futurity.bp.x21.bpm.ServiceComponentRequestInvoker;
import com.fujitsu.futurity.bp.x21.bpm.db.SessionHandle;
import com.fujitsu.futurity.bp.x21.bpm.parameter.IRequestParameterReadWrite;
import com.fujitsu.futurity.bp.x21.cc.AbstractCommonComponent;
import com.fujitsu.futurity.bp.x21.cc.exception.CCException;

import eo.common.constant.JCKStrConst;


/**
 * CCの呼び出しを行います。<p>
 * <BR>
 * @author 富士通
 */
public class JCKSV905301CC extends AbstractCommonComponent
{
	
	
	/**
	 * 
	 * @param handle
	 * @param param
	 * @param fixedText
	 * @return IRequestParameterReadWrite
	 * @throws Throwable
	 */
	@SuppressWarnings("unchecked")
	public IRequestParameterReadWrite executeMain(SessionHandle handle, IRequestParameterReadWrite param, String fixedText) throws Throwable
	{
		String dataMapKey = fixedText;
		
		HashMap<String, Object> ccMsg = (HashMap<String, Object>)param.getData(dataMapKey);
		
		// リターンコードを設定
		ccMsg.put("return_code", JCKStrConst.PMP_NORMAL_END);
		
		// SC呼び出し部品のインスタンス生成（引数にはログに出力するクラス名を渡す。空文字を設定した場合はログに出力されない）
		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
		
		// お客様情報本登録移行CC呼び出し処理
		if (execJcksv902401Call(handle, scCall, param, dataMapKey, ccMsg))
		{
			// AxM会員情報更新依頼CC呼び出し処理
			execjcksv900101Call(handle, scCall, param, dataMapKey, ccMsg);
		}
		
		return param;
	}
	
	/**
	 * お客様情報本登録移行CC呼び出し処理<br>
	 * 
	 * 
	 * @param handle
	 * @param scCall
	 * @param param
	 * @param dataMapKey
	 * @param ccMsg
	 * @return boolean 正常終了：true　異常終了：false
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	private boolean execJcksv902401Call(SessionHandle handle, 
										ServiceComponentRequestInvoker scCall, 
										IRequestParameterReadWrite param,
										String dataMapKey,
										HashMap<String, Object> ccMsg) throws Throwable
	{
		
		String fixedText = "CKSV902401CC";
		param.setData(fixedText, new HashMap<String, Object>());
		
		
		HashMap<String, Object> inMap = (HashMap<String, Object>)param.getData(fixedText);
		
		// SYSID
		inMap.put(CKSV9024_CKSV9024OP_CKSV902401CC.SYSID, ccMsg.get("sysid"));
		
		// OM-2016-0002054 DEL START
//		// お客様情報本登録移行CCを実行
//		JCKSV902401CC jcksv902401CC = new JCKSV902401CC();
//		jcksv902401CC.executeMain(handle, param, fixedText);
		// OM-2016-0002054 DEL END
		
		HashMap<String, Object> outMsg = (HashMap<String, Object>)param.getData(fixedText);
		// OM-2016-0002054 ADD START
		try
		{
			// お客様情報本登録移行CCを実行
			JCKSV902401CC jcksv902401CC = new JCKSV902401CC();
			jcksv902401CC.executeMain(handle, param, fixedText);
		}
		catch (CCException ex)
		{
			// OM-2016-0001646 ADD START
			if (JCKStrConst.PMP_ERR_PARAM.equals(outMsg.get(CKSV9024_CKSV9024OP_CKSV902401CC.RETURN_CD)))
			{
					// 最後に実行したSCの実行結果を取得
					Map result = getLastResult(param);
					// SCの実行結果をユーザー情報に設定する。(CKSV902401CC)
					editResultErrorInfo(result, (HashMap<String, Object>)ccMsg, new Object[][] {}, false, "1");
			}
			// OM-2016-0001646 ADD END
			// エラー時リターンコードを設定
			ccMsg.put("return_code", outMsg.get(CKSV9024_CKSV9024OP_CKSV902401CC.RETURN_CD));
			throw ex;
		}

		// OM-2016-0002054 ADD END
		if (!JCKStrConst.PMP_NORMAL_END.equals(outMsg.get(CKSV9024_CKSV9024OP_CKSV902401CC.RETURN_CD)))
		{
			// エラー時リターンコードを設定
			ccMsg.put("return_code", outMsg.get(CKSV9024_CKSV9024OP_CKSV902401CC.RETURN_CD));
			return false;
		}
		
		return true;
	}
	
	/**
	 * AxM会員情報更新依頼CC呼び出し処理<br>
	 * 
	 * 
	 * @param handle
	 * @param scCall
	 * @param param
	 * @param dataMapKey
	 * @param ccMsg
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	private void execjcksv900101Call(SessionHandle handle, 
										ServiceComponentRequestInvoker scCall, 
										IRequestParameterReadWrite param,
										String dataMapKey,
										HashMap<String, Object> ccMsg) throws Throwable
	{
		String fixedText = "CKSV900101CC";
		param.setData(fixedText, new HashMap<String, Object>());
		
		
		HashMap<String, Object> inMap = (HashMap<String, Object>)param.getData(fixedText);
		
		// UPD_TRGT_SYSID
		inMap.put(CKSV9001_CKSV9001OP_CKSV900101CC.UPD_TRGT_SYSID, ccMsg.get("sysid"));
		
		// AxM会員情報更新依頼CCを実行
		JCKSV900101CC jcksv900101CC = new JCKSV900101CC();
		jcksv900101CC.executeMain(handle, param, fixedText);
		
		// エラー時リターンコードを設定
		if ("1".equals(inMap.get("err_flg")))
		{
			// エラー時リターンコードを設定
			ccMsg.put("return_code", JCKStrConst.PMP_ERR_AXM);
		}
		
	}
}
