/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCCListCrateUtil
*   ソースファイル名：JCCListCrateUtil.java
*   作成者          ：富士通
*   日付            ：2011年07月06日
*＜機能概要＞
*   ListCreatorのコネクタ連携機能を用いて、帳票を生成します
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/07/06   EK909308    新規作成
*   v36.00.00   2018/04/25   FJ)河邊     【OM-2017-0000953】帳票同時実行エラー
*
**********************************************************************/
package eo.common.util;

import java.util.HashMap;
import java.io.File;

import com.fujitsu.systemwalker.outputassist.connector.FormBase;
import com.fujitsu.systemwalker.outputassist.connector.FormsFile;
import com.fujitsu.systemwalker.outputassist.connector.PrintProperties;
import com.fujitsu.systemwalker.outputassist.connector.PrintForm;

/**
 * クラス名：帳票生成部品
 *
 * 処理概要：ListCreatorのコネクタ連携機能を用いて、帳票を生成します
 *
 * 前提条件：InterStageListCreatorが提供する以下のjarファイルをクラスパスに設定する必要があります。
 *「fjoaweb.jar」「ArdusRepository.jar」「ardus.jar」「ardusobj.jar」「jomcli.jar」「jomrequester.jar」
 * 
 * @author 富士通
 */
public class JCCListCreateUtil
{

	/**
	 * コネクタ連携機能を用いて帳票を生成する
	 *  
	 * @param mapArgs 帳票生成情報格納したHashMap。
	 * @return outPath 生成した帳票のフルパス
	 * @throws Exception
	 */
	public static String callListCreator(HashMap<String, Object> mapArgs) throws Exception
	{
		// 生成した帳票のフルパス
		String outPath = null;
		
		// 文字コード判別用の変数
		int cd = 0;
		
		// マッピングされた帳票生成情報をローカル変数にコピー
		String chohyoServerNm    = (String)mapArgs.get("CHOHYO_SERVER_NM");		// 帳票サーバー名
		String[] chohyoNm        = (String[])mapArgs.get("CHOHYO_NM");			// 帳票名
		String chohyoKind        = (String)mapArgs.get("CHOHYO_KIND");			// 帳票種別
		String chohyoTeigiPath   = (String)mapArgs.get("CHOHYO_TEIGI_PATH");	// 帳票定義体格納先パス
		String chohyoDataPath    = (String)mapArgs.get("CHOHYO_DATA_PATH");		// 帳票入力データのパス
		String[] chohyoMediaPath = (String[])mapArgs.get("CHOHYO_MEDIA_PATH");	// 帳票組込みメディアファイルのパス
		String chohyoMojiCd      = (String)mapArgs.get("CHOHYO_MOJI_CD");		// 帳票入力データの文字コード
		String pdfInfoFilePath   = (String)mapArgs.get("PDF_INFO_FILE_PATH");   // pdf文書情報ファイルパス

		//------------------------------------------------------------------
		// 入力パラメータ検査
		//------------------------------------------------------------------
		// 帳票サーバー名チェック
		if(chkNull(chohyoServerNm))
		{
			throw new Exception("帳票サーバー名が指定されていません");
		}
		
		// 帳票定義体格納先パス
		if(chkNull(chohyoTeigiPath))
		{
			throw new Exception("帳票定義体(群)を格納したディレクトリのパスが指定されていません");
		}		
		
		// 出力帳票名チェック
		if(chkNull(chohyoNm))
		{
			throw new Exception("出力する帳票の名前が指定されていません");
		}
		// 拡張子が指定されている場合は除去
		for(int i = 0; i < chohyoNm.length; i++)
		{
			if(chkNull(chohyoNm[i]))
			{
				throw new Exception("出力する帳票の名前がnullまたは空文字です");
			}
			if(chohyoNm[i].lastIndexOf(".") > 0)
			{
				chohyoNm[i] = chohyoNm[i].substring(0, chohyoNm[i].lastIndexOf("."));
			}
		}
		
		// 出力ファイル形式チェック
		if(chkNull(chohyoKind))
		{
			throw new Exception("出力する帳票の形式情報が指定されていません");
		}
		if("0".equals(chohyoKind) == false && "1".equals(chohyoKind) == false)
		{
			throw new Exception("出力する帳票の形式情報は0(エクセル)または1(PDF)で指定して下さい");
		}
		
		// 入力データファイルパス
		if(chkNull(chohyoDataPath))
		{
			throw new Exception("帳票入力データのパスが指定されていません");
		}
		
		// 文字コード
		if(chkNull(chohyoMojiCd))
		{
			// 指定無しの場合はUTF-8とする
			chohyoMojiCd = "UTF-8";
		
		}
		if("Shift_JIS".equals(chohyoMojiCd))
		{
			cd = FormBase.CODE_SJIS;
		}
		else if("EUC".equals(chohyoMojiCd))
		{
			cd = FormBase.CODE_EUC;
		}
		else if("UTF-16BE".equals(chohyoMojiCd))
		{
			cd = FormBase.CODE_UCS2BE;
		}
		else if("UTF-16LE".equals(chohyoMojiCd))
		{
			cd = FormBase.CODE_UCS2LE;
		}
		else
		{
			cd = FormBase.CODE_UTF8;
		}
		
		//------------------------------------------------------------------
		// 帳票生成情報組み立て
		//------------------------------------------------------------------
		try
		{
			// 帳票格納ディレクトリを指定して、帳票と入力データファイルを設定するクラスのインスタンス化
			FormsFile form = new FormsFile(chohyoTeigiPath);
			
			// シングルフォーム定義体の場合
			if(chohyoNm.length == 1)
			{
				// 帳票名を設定
				form.setScriptFile(chohyoNm[0]);
			}
			// マルチフォーム定義体の場合
			else
			{
				// 出力形式がマルチフォームで指定
				form.setGrpOut(FormsFile.GRPOUT_GRP);
				
				// 帳票名を";"区切りで連結する
				StringBuffer chohyoNmBuf = new StringBuffer();
				for (int i = 0 ; i < chohyoNm.length; i++)
				{
					if(i > 0)
					{
						chohyoNmBuf.append(";");
					}
					chohyoNmBuf.append(chohyoNm[i]);
				}
				
				// 連結した帳票名を設定
				form.setLcForm(chohyoNmBuf.toString());
				
				// マルチフォーム用帳票名データ区切り文字種別を指定
				form.setGrpDelimitMode(FormsFile.DELIMIT_ANY);
				
				// マルチフォーム用帳票名データ区切り文字を指定
				form.setGrpDelimit(",");
			}
			// 帳票入力データとその文字コードを設定
			form.setDataFile(chohyoDataPath, cd);
			
			// 帳票の出力方法やその他属性情報を設定するクラスのインスタンス化
			PrintProperties prop = new PrintProperties();
			
			// コネクタ連携対象サーバの決定
			prop.setProperty(PrintProperties.ID_HOST, chohyoServerNm);
			
// OM-2017-0000953 2018/04/25 ADD START
			// システム日付を取得
			String sysDate = JCCGetSystemDateUtil.getSystemDateTime().get(JCCGetSystemDateUtil.KEY_YYYYMMDDHHMMSS_SSS);
// OM-2017-0000953 2018/04/25 ADD END
			
			// 出力ファイルの形式と出力先のフルパスを決定
			if("0".equals(chohyoKind) == true)
			{
				// エクセル形式
// OM-2017-0000953 2018/04/25 MOD START
//				outPath = chohyoTeigiPath + File.separator + chohyoNm[0] + ".xlsx";
				outPath = chohyoTeigiPath + File.separator + chohyoNm[0] + sysDate + ".xlsx";
// OM-2017-0000953 2018/04/25 MOD END
				prop.setProperty(PrintProperties.ID_DIRECTMETHOD, "EXCEL");
				prop.setProperty(PrintProperties.ID_RETURNXLSXPATH, outPath);
			}
			else
			{
				// PDF形式
// OM-2017-0000953 2018/04/25 MOD START
//				outPath = chohyoTeigiPath + File.separator + chohyoNm[0] + ".pdf";
				outPath = chohyoTeigiPath + File.separator + chohyoNm[0] + sysDate + ".pdf";
// OM-2017-0000953 2018/04/25 MOD END
				prop.setProperty(PrintProperties.ID_DIRECTMETHOD, "PDF");
				prop.setProperty(PrintProperties.ID_RETURNPDFPATH, outPath);
				prop.setProperty(PrintProperties.ID_PDF_DOCENVFILE, "pdfDocInfo.properties");
				prop.setProperty(PrintProperties.ID_LCFILE, pdfInfoFilePath);
				
			}
			
			// 組込みメディアファイルの設定
			if(chkNull(chohyoMediaPath) == false)
			{
				// 組込みメディア格納ディレクトリの設定
				prop.setProperty(PrintProperties.ID_VISUALDIR, chohyoTeigiPath);
				
				// メディアファイルの絶対パスを";"区切りで連結する
				StringBuffer chohyoMediaBuf = new StringBuffer();
				for(int i = 0; i < chohyoMediaPath.length; i++)
				{
					if(i > 0)
					{
						chohyoMediaBuf.append(";");
					}
					chohyoMediaBuf.append(chohyoMediaPath[i]);
				}
				
				// メディアファイルの絶対パスを設定
				prop.setProperty(PrintProperties.ID_LCVISUALFILE, chohyoMediaBuf.toString());
			}
			// 帳票生成
			PrintForm print = new PrintForm();
			print.PrintOut(form, prop);
		}
		catch(Throwable th)
		{
			throw new Exception(th);
		}
		return outPath;
	}
	
	/**
	 * Stringクラスが設定されているか判定する
	 * <br>
	 * @param arg0 判定する文字列
	 * @return オブジェクトに値が設定されていない場合はtrue
	 */
	private static boolean chkNull(Object arg0) 
	{
		if (null == arg0) 
		{
			return true;
		}
		if(arg0 instanceof String)
		{
			if(arg0.toString().length() == 0)
			{
				return true;
			}
		}
		else if(arg0 instanceof String[])
		{
			String[] arg1 = (String[])arg0;
			if(arg1.length == 0)
			{
				return true;
			}
		}
		return false;
	}
}