/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：物流管理システム
*   モジュール名    ：JDKCommon12CC
*   ソースファイル名：JDKCommon12CC.java
*   作成者          ：富士通
*   日付            ：2011年07月05日
*＜機能概要＞
*   棚移動入荷一覧照会画面です。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/07/05   FJ          新規作成
*
**********************************************************************/
package com.fujitsu.futurity.bp.custom.common;

import java.util.HashMap;
import java.util.Map;

import com.fujitsu.futurity.bp.custom.mapping.DKSV0023_DKSV0023OP_EDK0061B021BSMapper;
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.IRequestParameterReadWrite;
import com.fujitsu.futurity.bp.x21.cc.AbstractCommonComponent;
import com.fujitsu.futurity.bp.x21.cc.exception.SCCallException;
import com.fujitsu.futurity.common.JCMConstants;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;

import eo.common.util.JDKCommonUtil;
import eo.ejb.cbs.cbsmsg.EDK0061B021CBSMsg;
import eo.ejb.cbs.cbsmsg.EDK0061B021CBSMsg1List;

/**
 * 棚移動入荷一覧情報取得のページング用CC処理を行います。
 * @author user
 */
public class JDKCommon12CC extends AbstractCommonComponent
{
	/**
	 * テンプレートID
	 */
	public static final String TEMPLATE_ID = "EDK0061B021";

	/**
	 * マッパー
	 */
	private static final DKSV0023_DKSV0023OP_EDK0061B021BSMapper MAPPER = new DKSV0023_DKSV0023OP_EDK0061B021BSMapper();

	/**
	 * 棚移動入荷一覧取得処理を行います。
	 * @param handle セッションハンドル
	 * @param param パラメータ
	 * @param fixedText 定型文字
	 * @return パラメータ
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public IRequestParameterReadWrite getListReSearch(SessionHandle handle, 
			IRequestParameterReadWrite param, String fixedText) throws Exception
	{
		// ◇ 上りマッピング処理
		// 上りマッピングの処理結果を取得するためのマップを生成
		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		// リクエストパラメータを引数に上りマッピング処理メソッドを実行
		paramMap = editInMsg(param);

		// ◇ SC呼出実行
		// SC呼び出し部品のインスタンス生成（引数にはログに出力するクラス名を渡す。空文字を設定した場合はログに出力されない）
		ServiceComponentRequestInvoker scCall = new ServiceComponentRequestInvoker();
		JSYejbLog.println(JSYejbLog.DEBUG, this.getClass(), "棚移動入荷一覧照会処理の実行");
		Map<?, ?> result = scCall.run(paramMap, handle);

		CAANMsg[] templates = (CAANMsg[])result.get(JCMConstants.TEMPLATE_LIST_KEY);
		CAANMsg msg = templates[0];

		// 検索リストを取得する
		CAANMsg[] serchList = msg.getCAANMsgList(EDK0061B021CBSMsg.EDK0061B021CBSMSG1LIST);

		// 画面で選択していたデータが検索結果に存在するかチェックを行う
		boolean flg = false;

		// ユーザデータ情報
		// 画面で選択されていたデータを取得する
		HashMap<String, String> inParamMap = (HashMap<String, String>)param.getData("DKSV002301SC");
		String key = (String)inParamMap.get("key_paging");

		// キー情報が存在するか確認する
		if (!JDKCommonUtil.isNull(key))
		{
			if (serchList != null && 0 < serchList.length)
			{
				for (int i = 0; i < serchList.length; i++)
				{
					CAANMsg childTemplate = serchList[i];
					String lotNo = childTemplate.getString(EDK0061B021CBSMsg1List.SHELF_MOVE_LOT_NO);

					// 選択データが検索結果にある場合はチェック終了
					if(key.equals(lotNo))
					{
						flg = true;
						HashMap<String, String> inMap = (HashMap<String, String>)param.getData("DKSV002301SC");
						inMap.put("display_no", String.valueOf(i));
						break;
					}
				}
			}
		}
		else if (serchList != null && 0 < serchList.length)
		{
			// データが検索結果にある場合はチェック終了
			flg = true;
		}

		// 画面で選択していたデータがない場合は1ページ目を再検索する
		if (!flg)
		{
			HashMap<String, String> inMap = (HashMap<String, String>)param.getData("DKSV002301SC");

			// 表示ページ番号を1ページに設定する
			inMap.put("display_page_num", "1");

			// 画面制御のため検索エラーフラグを3に設定する
			inMap.put("search_err_flg", "3");

			// 表示行番号を0に設定する
			inMap.put("display_no", "0");

			// リクエストパラメータを引数に上りマッピング処理メソッドを実行
			paramMap = editInMsg(param);

			// ◇ SC呼出実行
			// SC呼び出し部品のインスタンス生成（引数にはログに出力するクラス名を渡す。空文字を設定した場合はログに出力されない）
			scCall = new ServiceComponentRequestInvoker();
			JSYejbLog.println(JSYejbLog.DEBUG, this.getClass(), "棚移動入荷一覧照会処理の実行");
			result = scCall.run(paramMap, handle);
		}
		// ◇ 下りマッピング処理
		editOutMsg(result, param);
		
		if (JDKBPCommon.hasError(param))
		{
			throw new SCCallException("棚移動入荷一覧照会処理失敗", "0", 
					Integer.parseInt(param.getControlMapData("returnCode").toString()));
		}
		
		return param;
	}

	/**
	 * 棚移動入荷一覧取得処理チェックを行います。
	 * @param handle セッションハンドル
	 * @param param パラメータ
	 * @param fixedText 定型文字
	 * @return マッピングパラメータ
	 * @throws RequestParameterException
	 */
	public  HashMap<String, Object> getInvokeCBS(SessionHandle handle
			, IRequestParameterReadWrite param, String fixedText) throws RequestParameterException 
	{
		// 棚移動入荷一覧取得処理用メッセージ設定処理
		return editInMsg(param);
	}

	/**
	 * 棚移動入荷一覧取得処理用メッセージ設定処理を行います。
	 * @param param パラメータ
	 * @return マッピングパラメータ
	 * @throws RequestParameterException
	 */
	private HashMap<String, Object> editInMsg(IRequestParameterReadWrite param) throws RequestParameterException
	{
		try
		{
			return MAPPER.editInMsg(param);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}

	/**
	 * 棚移動入荷一覧取得処理用メッセージ取得処理を行います。
	 * @param msgList メッセージリスト
	 * @param param パラメータ
	 * @return パラメータ
	 * @throws Exception
	 */
	private IRequestParameterReadWrite editOutMsg(Map<?, ?> msgList,
			IRequestParameterReadWrite param) throws Exception
	{
		try
		{
			return MAPPER.editResultRP(msgList, param);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}

	/**
	 * サービスコンポーネント実行後に、IRequestParameterReadWriteにエラー情報をマッピングします。
	 * @param param パラメータ
	 * @param templates テンプレート
	 * @param returnCode リターンコード
	 * @return パラメータ
	 * @throws RequestParameterException
	 */
	public IRequestParameterReadWrite editErrorInfo(IRequestParameterReadWrite param
			, CAANMsg[] templates, int returnCode) throws RequestParameterException
	{
		try
		{
			return MAPPER.editErrorInfo(param, templates, returnCode);
		}
		catch (Throwable th)
		{
			throw new RequestParameterException();
		}
	}
}
