/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCRUtilCommon
*   ソースファイル名：JCRUtilCommon.java
*   作成者          ：富士通
*   日付            ：2011年04月27日
*＜機能概要＞
*   対応履歴共通文字列関連部品
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/04/27   FJ) 富士通  新規作成
*	v6.00.00	2013/08/30	FJ)斉藤諭	OM-2013-0001243対応 改行コード付加処理追加
*
**********************************************************************/
package eo.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import eo.common.constant.JCRStrConst;

/**
 * 対応履歴共通文字列関連部品<p>
 * <br>
 * @author 富士通
 */
public class JCRStringUtil
{

	/**
	 * 文字列のバイト数を取得する。
	 * @param str 対象文字列
	 * @return バイト数
	 * @throws Exception 例外
	 */
	public static int getByteLength(String str) throws Exception
	{
		int length = 0;

		if (str != null)
		{
			byte[] bytes = str.getBytes(JCRStrConst.DEFAULT_ENCODE);
			length = bytes.length;
		}

		return length;
	}

	/**
	 * 日付オブジェクトを指定された形式の文字列に編集します。
	 * <br>
	 * @param date 編集する日付オブジェクト
	 * @param parseformat java.util.Date型の日付書式
	 * @param dateformat 編集する文字列の日付書式
	 * @return String 指定された形式に編集された文字列
	 */
	protected static String formatDate(String date, String parseformat, String dateformat)
	{

		SimpleDateFormat instance = new SimpleDateFormat(parseformat);

		try
		{
			return new SimpleDateFormat(dateformat).format(instance.parse(date));
		}
		catch (ParseException e)
		{
			return date;
		}

	}

	/**
	 * <p>
	 * 年(yyyy)、月(MM)、日(dd)、時(HH)、分(mm)の文字列を結合します。
	 * </p>
	 * @param year 年(yyyy)
	 * @param month 月(MM)
	 * @param day 日(dd)
	 * @param hour 時(HH)
	 * @param minute 分(mm)
	 * @param second 分(ss)
	 * @return 年(yyyy)、月(MM)、日(dd)、時(HH)、分(mm)を結合した文字列
	 */
	protected static String concatYmdhms(String year, String month, String day, String hour, String minute, String second)
	{
		StringBuffer ymdhm = new StringBuffer();

		if (!JCRUtilCommon.isNull(year))
		{
			ymdhm.append(year);
		}

		if (!JCRUtilCommon.isNull(month))
		{
			ymdhm.append(month);
		}

		if (!JCRUtilCommon.isNull(day))
		{
			ymdhm.append(day);
		}

		if (!JCRUtilCommon.isNull(hour))
		{
			ymdhm.append(hour);
		}

		if (!JCRUtilCommon.isNull(minute))
		{
			ymdhm.append(minute);
		}

		if (!JCRUtilCommon.isNull(second))
		{
			ymdhm.append(second);
		}

		String ret = ymdhm.toString();

		if (0 == ret.length())
		{
			return null;
		}

		return ret;
	}

	/**
	 * <p>
	 * 時(HH)、分(mm)の文字列を結合します。
	 * </p>
	 * @param hour 時(HH)
	 * @param minute 分(mm)
	 * @return 時(HH)、分(mm)を結合した文字列
	 */
	protected static String concatHm(String hour, String minute)
	{
		StringBuffer hm = new StringBuffer();

		if (!JCRUtilCommon.isNull(hour))
		{
			hm.append(hour);
		}

		if (!JCRUtilCommon.isNull(minute))
		{
			hm.append(minute);
		}

		String ret = hm.toString();

		if (0 == ret.length())
		{
			return null;
		}

		return ret;
	}

	/**
	 * 文字列を指定されたサイズに調整します。
	 * <br>
	 * @param text 調整前の文字列
	 * @param byteSize 指定サイズ（バイト数）
	 * @param fillLeft 半角スペースを埋める位置 true:左埋め
	 * @return String 調整後の文字列
	 * @throws Exception 例外
	 */
	static String adjustCharSize(String text, int byteSize, boolean fillLeft) throws Exception
	{
		String res = null;

		if (text == null || byteSize <= 0) 
		{
			res = "";
		}

		if (fillLeft == false)
		{
			res = JPCUtilCommon.adjustCharSize(text, byteSize, JCRStrConst.DEFAULT_ENCODE);
		}
		else
		{
			byte[] paraByte = null;

			// 入力文字列のバイト配列を取得する
			paraByte = text.getBytes(JCRStrConst.DEFAULT_ENCODE);

			// 入力文字列のバイトサイズを取得する
			int paraByteSize = paraByte.length;

			// 入力文字列のバイトサイズ ≦ 指定サイズの場合
			if (paraByteSize <= byteSize) 
			{
				StringBuffer buf = new StringBuffer(text);

				// 指定サイズになるまで半角スペースを補充する
				for (int i = paraByteSize; i < byteSize; i++) 
				{
					buf.insert(0, " ");
				}

				res = buf.toString();
			}
			// 入力文字列のバイトサイズ ＞ 指定サイズの場合
			else 
			{ 
				// 指定サイズ分の文字列を生成する
				// 文字コードＯＳ準拠
				res = new String(paraByte, 0, byteSize, JCRStrConst.DEFAULT_ENCODE);
			}
		}

		return res;
	}

	/**
	 * メールヘッダー内の日付をYYYYMMDDHHMMSS形式に変換する。
	 * @param mailDate メールヘッダー内の日付
	 * @return yyyyMMddHHmmss形式の日付文字
	 */
	static String convMailDateToDtmString(String mailDate)
	{
		String res = null;
		try
		{
			SimpleDateFormat mailDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
			SimpleDateFormat dtmFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			res = dtmFormat.format(mailDateFormat.parse(mailDate));
		}
		catch (ParseException pe)
		{
			res = null;
		}

		return res;
	}

	/**
	 * ダブルクォーテーション付加処理<br>
	 * @param targetData 対象文字列
	 * @return String 付加済み文字列
	 * @throws Exception 例外
	 */
	static String addDblQut(String targetData) throws Exception
	{
		String resultStr = null;
		if(targetData == null)
		{
			resultStr = "\"\"";
		}
		else
		{
			resultStr = new StringBuilder().append("\"").append(targetData.trim()).append("\"").toString();
		}
		return resultStr;
	}

	/**
	 * 改行コード「CRLF」付加処理<br>
	 * 文字列の終端が「CRLF」で無い場合「CRLF」を付加する。
	 * 但し、終端が「CR」もしくは「LF」であった場合は「CRLF」に変換する。
	 * ※ 対応記録履歴に追記する場合の判断に使用する想定
	 * @param targetData 対象文字列
	 * @return String 付加済み文字列
	 * @throws Exception 例外
	 */
	static String addCRLF(String targetData) throws Exception
	{
		String resultStr = null;

		if (JCRUtilCommon.isNull(targetData))
		{
			resultStr = "";
		}
		else
		{
			if (targetData.endsWith(JCRStrConst.KAIGYO_CD))
			{
				// 終端が「CRLF」の場合　編集なし
				resultStr = targetData;
			}
			else
			{
				// 終端が「CRLF」でない場合

				if (targetData.endsWith("\r") || targetData.endsWith("\n"))
				{
					// 終端がCRまたはLFの場合
					// 終端を「CRLF」に変換
					resultStr = new StringBuilder(targetData.substring(0, targetData.length() - 1)).append(JCRStrConst.KAIGYO_CD).toString();
				}
				else
				{
					// 終端が上記以外の場合
					// 終端に「CRLF」を追加
					resultStr = new StringBuilder(targetData).append(JCRStrConst.KAIGYO_CD).toString();
				}
			}
		}

		return resultStr;
	}

}
