/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCRejbQueryConditionEdit
*   ソースファイル名：JCRejbQueryConditionEdit.java
*   作成者          ：富士通
*   日付            ：2011年10月05日
*＜機能概要＞
*   問合せクエリ条件作成部品です。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/10/05   FJ          新規作成
*
**********************************************************************/

package eo.ejb.common.edit;

/**
 * 問合せクエリ条件を作成するために指定した、スキーマID、クエリ項目名、
 * フィールド名、フィールドタイプ、クエリ値、クエリ条件コード、OR条件有無の
 * 編集を行います。
 * <BR>
 * @author FJ
 */
public class JCRejbQueryConditionEdit {
	/**
	 * 引数不正 指定された文字列のサイズに対して指定した1項目あたりのサイズが不正
	 */
	private final static String ERR_MSG_ILLEGAL_ITEM_SIZE = "指定された文字列のサイズに対して指定した1項目あたりのサイズが不正です。";
	/**
	 * 引数不正 引数にNULLがセットされている
	 */
	private static final String ERR_MSG_NULL_ARGUMENT = "引数にNULLが指定されています。";
	/**
	 * 引数不正 引数に空文字（""）がセットされている
	 */
	private static final String ERR_MSG_0_SIZE_STRING = "引数に空文字がセットされています。";
	/**
	 * 引数不正 桁数指定で、0が指定されている
	 */
	private static final String ERR_MSG_0_LENGTH = "桁数指定で、0以下が指定されています。";

	/**
	 * 指定されたクエリ条件項目を指定された文字数で区切り、配列にして返します。
	 * <BR>
	 * @param edit
	 * @param length
	 * @return
	 * @throws IllegalArgumentException
	 */
	public static String[] splitQueryCondition( String edit , int length) throws IllegalArgumentException
	{
		
		//----------------------------------------------------------------------------------------------------
		// (1)引数チェックを行う。
		//----------------------------------------------------------------------------------------------------
		if(edit == null)
		{
			//引数がnullの場合
			throw new IllegalArgumentException(ERR_MSG_NULL_ARGUMENT);
		}
		if("".equals(edit))
		{
			//引数が空文字の場合
			throw new IllegalArgumentException(ERR_MSG_0_SIZE_STRING);
		}
		
		if(length <= 0)
		{
			throw new IllegalArgumentException(ERR_MSG_0_LENGTH);
		}
		
		int strLength = edit.length();
		if( strLength % length != 0 )
		{
			//指定した文字列の桁数が、指定された桁数で割り切れなかった場合、引数が不正なので
			//例外をスローする
			throw new IllegalArgumentException(ERR_MSG_ILLEGAL_ITEM_SIZE);
		}
		
		int arraySize = strLength / length;
		int startIndex = 0;
		
		String[] splitArray = new String[arraySize];
		for(int iCnt = 0 ; iCnt < splitArray.length ; iCnt++)
		{
			//------------------------------------------------------------------------------------------------
			// (2)指定された桁数で編集対象文字列を区切り配列へ格納する。
			//------------------------------------------------------------------------------------------------
			splitArray[iCnt] = edit.substring(startIndex , startIndex + length).trim();
			startIndex = startIndex + length;
		}
		
		return splitArray;
	}
}
