/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 MIND Co., Ltd.  http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.part;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import jp.mosp.common.CommonConst;
import jp.mosp.common.JapanHolidayUtility;
import jp.mosp.common.common.MospUtility;

/**
 * 祝祭日に関するPartクラス
 * @author yoshida
 *	@version	0.0.1
 */
public class HolidayPart extends BasePart {

	/**
	 * コンストラクタ
	 */
	public HolidayPart() {
		super();
	}

	/**
	 * コンストラクタ
	 * @param partInfo
	 */
	public HolidayPart(PartInfo partInfo) {
		super(partInfo);
	}

	/**
	 * 祝日名称取得
	 * <p>	2009/02/25	m.yoshida	祝日メソッド修正 </p>
	 * @param targetDate		対象年月日
	 * @return	対象年月日祝日名称
	 * @throws Exception
	 */
	public String getPublicHolidayName(Date targetDate) throws Exception {
		String holidayName = "";
		// 対象日祝日名称取得
		String targetName = getHolidayMap(MospUtility.getYear(targetDate)).get(targetDate);
		if (targetName != null) {
			holidayName = targetName;
		}
		return holidayName;
	}

	/**
	 * 祝日名称マップ生成
	 * @param year			対象年
	 * @throws Exception
	 */
	private Map<Date, String> getHolidayMap(int year) throws Exception {
		Map<Date, String> holidayMap = new TreeMap<Date, String>();
		// 祝祭日、休日設定
		String[][] aryCodeHoliday = mospcode().getMosPCodeArray(CommonConst.TID_HOLIDAY);
		Date holiday = null;
		for (int k = 0; k < aryCodeHoliday.length; k++) {
			// MM/DDから始まる休日への対応
			if (aryCodeHoliday[k][0].startsWith("0")
					|| aryCodeHoliday[k][0].startsWith("1")
			) {
				StringBuffer date = new StringBuffer();
				date.append(String.valueOf(year));
				date.append(CommonConst.SEPARATOR_DATE);
				date.append(aryCodeHoliday[k][0].split(CommonConst.SEPARATOR_DATE)[0]);
				date.append(CommonConst.SEPARATOR_DATE);
				date.append(aryCodeHoliday[k][0].split(CommonConst.SEPARATOR_DATE)[1]);
				holiday = MospUtility.getDate(date.toString());
			} else {
				holiday = MospUtility.getDate(aryCodeHoliday[k][0]);
			}
			holidayMap.put(holiday, aryCodeHoliday[k][1]);
		}
		// ハッピーマンデー設定
		String[][] aryJapanHoliday = getAryHolidaySingleYear(year);
		for (int i = 0; i < aryJapanHoliday.length; i++) {
			holiday = MospUtility.getDate(aryJapanHoliday[i][0]);
			if (holidayMap.get(holiday) == null) {
				holidayMap.put(holiday, aryJapanHoliday[i][1]);
			}
		}
		return holidayMap;
	}

	/**
	 * 単年祝祭日配列取得
	 * @param year
	 * @return	result
	 */
	private String[][] getAryHolidaySingleYear(int year) {
		String[][] result = new JapanHolidayUtility(year).getArySyuku();
		return result;
	}

}
