/*********************************************************************
*	All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JCRFileUtil
*	ソースファイル名：JCRFileUtil.java
*	作成者			：富士通
*	日付			：2011年05月12日
*＜機能概要＞
*	顧客対応履歴管理システム用ファイル関連共通部品
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/05/12	富士通		新規作成
*
**********************************************************************/
package eo.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 顧客対応履歴管理システム用ファイル関連共通部品<p>
 * <br>
 * @author 富士通
 */
public class JCRFileUtil
{
	
	/**
	 * ファイルを移動する
	 * @param targetFile 対象ファイル
	 * @param idousakiPath 移動先パス
	 * @return 正常に完了した場合true
	 * @throws Exception 例外
	 */
	static boolean moveFile(File targetFile, File idousakiPath) throws Exception
	{
		boolean ret = false;
		
		// 引数チェック
		if(targetFile == null || targetFile.isFile() == false || idousakiPath == null || idousakiPath.isDirectory() == false)
		{
			return ret;
		}
		
		// 移動先ファイルの存在チェック
		File dest = new File(inspection(idousakiPath), targetFile.getName());
		if(dest.exists())
		{
			return ret;
		}
		
		// 移動
		ret = targetFile.renameTo(dest);
		
		return ret;
	}

	/**
	 * ファイルを移動する
	 * @param targetFile 対象ファイル
	 * @param idousakiPath 移動先パス
	 * @param outputFile 移動先ファイル名情報
	 * @return 正常に完了した場合true
	 * @throws Exception 例外
	 */
	static boolean moveFile(File targetFile, File idousakiPath, File outputFile) throws Exception
	{
		boolean ret = false;

		// 引数チェック
		if(targetFile == null || targetFile.isFile() == false || idousakiPath == null || idousakiPath.isDirectory() == false)
		{
			return ret;
		}

		if (outputFile == null)
		{
			ret = moveFile(targetFile, idousakiPath);
		}
		else
		{
			// 移動先ファイルの存在チェック
			File dest = new File(inspection(idousakiPath), outputFile.getName());
			if(dest.exists())
			{
				return ret;
			}

			// 移動
			ret = targetFile.renameTo(dest);
		}

		return ret;
	}

	/**
	 * ディレクトリー存在チェック
	 * @param path パス
	 * @throws Exception 例外
	 * @return ファイルオブジェクト(ディレクトリーでない場合はnullを返す)
	 */
	static File checkDirExist(String path) throws Exception
	{

		File file = new File(inspection(path));
		if (!(file.isDirectory()))
		{
			file = null;
		}

		return file;
	}

	/**
	 * ファイル移動・削除
	 * @param moveFiles 移動ファイルの配列
	 * @param deleteFiles 削除ファイルの配列
	 * @param outputPath 出力先パス
	 * @throws Exception 例外
	 */
	static void moveAndDeleteFile(File[] moveFiles, File[] deleteFiles, File outputPath) throws Exception
	{
		// ファイル存在チェック
		if (moveFiles != null)
		{
			File[] outputFiles = new File[moveFiles.length];
			for (int i = 0; i < outputFiles.length; i++)
			{
				outputFiles[i] = new File(inspection(outputPath), moveFiles[i].getName());
			}

			boolean needCheck = true;				// ファイル名チェックが必要な場合true
			boolean existSameNameFile = false;		// 同名のファイルが存在した場合true
			int fileNameIndex = 0;					// ファイル名変更時に付加する数字

			while (needCheck)
			{
				existSameNameFile = false;

				for (int i = 0; i < outputFiles.length; i++)
				{
					// 移動先ファイルに１つでも同名のファイルがあるかどうか確認
					if (outputFiles[i].exists())
					{
						// 同名のファイルが存在した場合
						existSameNameFile = true;
						break;
					}
				}

				if (existSameNameFile == true)
				{
					++fileNameIndex;

					// 同名のファイルが１つでも存在した場合
					for (int i = 0; i < outputFiles.length; i++)
					{
						// 対象ファイル全てファイル名変更
						String fileName = moveFiles[i].getName();
						int extIndex = fileName.lastIndexOf(".");	// 拡張子開始位置検索
						String renameFileName = null;
						if (extIndex == -1)
						{
							// 拡張子なしの場合
							renameFileName = fileName +  "_" + fileNameIndex;
						}
						else
						{
							// 拡張子前に番号付加
							renameFileName = fileName.substring(0, extIndex) +  "_" + fileNameIndex + fileName.substring(extIndex);
						}
						outputFiles[i] = new File(inspection(outputPath), renameFileName);
					}
				}
				else
				{
					// 同名のファイルが１つも存在しなかった場合
					needCheck = false;
				}
			}

			for (int i = 0; i < moveFiles.length; i++)
			{
				// ファイル移動
				moveFile(moveFiles[i], outputPath, outputFiles[i]);
			}
		}

		// ファイル削除
		if (deleteFiles != null)
		{
			for (int i = 0; i < deleteFiles.length; i++)
			{
				// ファイル削除
				deleteFiles[i].delete();
			}
		}
	}

	/**
	 * ファイルを複写する
	 * @param targetFile 対象ファイル
	 * @param toDirPath 複写先パス
	 * @return 正常に完了した場合true
	 */
	static boolean copyFile(File targetFile, File toDirPath)
	{
		boolean ret = false;

		// 引数チェック
		if(targetFile == null || targetFile.isFile() == false || toDirPath == null || toDirPath.isDirectory() == false)
		{
			return ret;
		}

		// 複写先ファイルの存在チェック
		File dest = new File(inspection(toDirPath), targetFile.getName());
		if(dest.exists())
		{
			// 既に存在する場合
			return ret;
		}
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try
		{
			// 複写
			byte[] fileData = new byte[(int)targetFile.length()];

			bis = new BufferedInputStream(new FileInputStream(targetFile));
			bos = new BufferedOutputStream(new FileOutputStream(dest));
			bis.read(fileData);
			bos.write(fileData);
			bos.flush();
			ret = true;
		}
		catch (IOException ioe)
		{
			return ret;
		}
		finally
		{
			if (bis != null)
			{
				try
				{
					bis.close();
				}
				catch (IOException ioe)
				{
				}
			}
			if (bos != null)
			{
				try
				{
					bos.close();
				}
				catch (IOException ioe)
				{
				}
			}
		}
		return ret;
	}

	/**
	 * 入力文字列に「..」が含まれていないことを検査します。
	 * @param filePath チェック対象パス情報
	 * @return 「..」が含まれていない場合はパス情報
	 * @throws IllegalArgumentException パスに「..」が含まれている場合
	 */
	static File inspection(File filePath) throws IllegalArgumentException
	{
		try
		{
			inspection(filePath.getAbsolutePath());
		}
		catch (IllegalArgumentException iae)
		{
			throw iae;
		}
		return filePath;
	}

	/**
	 * 入力文字列に「..」が含まれていないことを検査します。
	 * @param filePath チェック対象文字列
	 * @return 「..」が含まれていない場合は入力文字列
	 * @throws IllegalArgumentException 入力文字列に「..」が含まれている場合
	 */
	static String inspection(String filePath) throws IllegalArgumentException
	{
		if (filePath != null && filePath.indexOf("..") == -1)
		{
			return filePath;
		}
		throw new IllegalArgumentException(filePath);
	}
}
