/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
package com.aimluck.eip.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.SelectQuery;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;

import com.aimluck.commons.field.ALIllegalDateException;
import com.aimluck.eip.cayenne.om.portlet.EipTExtTimecard;
import com.aimluck.eip.cayenne.om.portlet.ImwTOffice;
import com.aimluck.eip.cayenne.om.portlet.ImwTOfficeCalendar;
import com.aimluck.eip.cayenne.om.portlet.ImwTOfficeCalendarMap;
import com.aimluck.eip.orm.DatabaseOrmService;

/**
 * 祝日を保持するシングルトンクラスです。 <br />
 * この実装は同期化されない点に注意してください。
 * 
 */
public class ALEipHolidaysManager {

  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(ALEipHolidaysManager.class.getName());

  /** デフォルトエンコーディングを表わすシステムプロパティのキー */
  protected static final String KEY_ENCODING = "content.defaultencoding";

  private static ALEipHolidaysManager manager = new ALEipHolidaysManager();

  /** デフォルトの祝日が書かれたファイルへのパス */
  private String FILE_HOLI_DAYS_DEFAULT = (JetspeedResources.getString(
      "aipo.home", "").equals("")) ? "" : JetspeedResources.getString(
      "aipo.home", "")
      + File.separator
      + "conf"
      + File.separator
      + "holidays_default.properties";

  /** ユーザ定義の祝日が書かれたファイルへのパス */
  private String FILE_HOLI_DAYS_USER = (JetspeedResources.getString(
      "aipo.home", "").equals("")) ? "" : JetspeedResources.getString(
      "aipo.home", "")
      + File.separator + "conf" + File.separator + "holidays_user.properties";

  /** デフォルトの祝日一覧 */
  private List defaultHolidays = null;

  /** ユーザ定義の祝日一覧 */
  private List userHolidays = null;

  /**
   * コンストラクタ
   */
  private ALEipHolidaysManager() {
    defaultHolidays = new ArrayList();
    userHolidays = new ArrayList();

    loadHolidays();
  }

  /**
   * クラス ALEipHolidaysManager のインスタンスを取得する．
   * 
   * @return クラス ALEipHolidaysManager のインスタンス
   */
  public static ALEipHolidaysManager getInstance() {
    return manager;
  }

  /**
   * 指定した日付が祝日であるかを検証する．
   * 
   * @param date
   *            検証する日付
   * @return 指定した日付の祝日情報．指定した日付が祝日ではない場合は，null．
   */
  public ALHoliday isHoliday(Date date) {
    if (date == null) {
      return null;
    }

    ALHoliday holiDay = null;
    int year = -1;
    int month = -1;
    int day = -1;

    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    try {
      year = calendar.get(Calendar.YEAR);
      month = calendar.get(Calendar.MONTH) + 1;
      day = calendar.get(Calendar.DATE);
    } catch (Throwable ex) {
      return null;
    }

    holiDay = isHoliday(userHolidays, year, month, day);
    if (holiDay == null) {
      holiDay = isHoliday(defaultHolidays, year, month, day);
    }
    return holiDay;
  }

  /**
   * 指定した日付が祝日であるかを検証する．
   * 
   * @param list
   * @param year
   * @param month
   * @param day
   * @return
   */
  private ALHoliday isHoliday(List list, int year, int month, int day) {
    ALHoliday holiDay = null;
    Calendar hCalendar = null;
    int hYear = -1;
    int hMonth = -1;
    int hDay = -1;
    int length = list.size();

    try {
      for (int i = 0; i < length; i++) {
        holiDay = (ALHoliday) list.get(i);
        hCalendar = new GregorianCalendar();
        hCalendar.setTime(holiDay.getDay().getValue().getDate());
        hCalendar.set(Calendar.SECOND, 0);
        hCalendar.set(Calendar.MILLISECOND, 0);
        hYear = hCalendar.get(Calendar.YEAR);
        hMonth = hCalendar.get(Calendar.MONTH) + 1;
        hDay = hCalendar.get(Calendar.DATE);
        if (hYear == year && hMonth == month && hDay == day) {
          return holiDay;
        }
      }
    } catch (ALIllegalDateException ex) {
      logger.error("Exception", ex);
    }
    return null;
  }

  /**
   * 祝日情報をテキストファイルから読み込む． <br>
   * 祝日情報のフォーマット：祝日名,祝日日時 <br>
   * 例）文化の日,2004-11-03
   */
  private void loadHolidays() {
    File defaultFile = new File(FILE_HOLI_DAYS_DEFAULT);
    File userFile = new File(FILE_HOLI_DAYS_USER);

    defaultHolidays.clear();
    userHolidays.clear();

    BufferedReader reader = null;

    try {
      if (defaultFile.exists()) {
        // デフォルトの祝日を読み込む．
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(
            defaultFile), ALEipConstants.DEF_CONTENT_ENCODING));
        loadHoliday(reader, defaultHolidays);
      }

      if (userFile.exists()) {
        // ユーザ定義の祝日を読み込む．
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(
            userFile), ALEipConstants.DEF_CONTENT_ENCODING));
        loadHoliday(reader, userHolidays);
      }

      Comparator comp = getHolidaysComparator();
      if (comp != null) {
        Collections.sort(defaultHolidays, comp);
        Collections.sort(userHolidays, comp);
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return;
    }
  }

  /**
   * 祝日情報をテキストファイルから読み込む．
   * 
   * @param reader
   * @param list
   * @throws Exception
   */
  private void loadHoliday(BufferedReader reader, List list) throws Exception {
    if (reader == null)
      return;

    ArrayList dummyList = new ArrayList();
    StringTokenizer st = null;
    ALHoliday holiDay = null;
    String line = null;
    while ((line = reader.readLine()) != null) {
      if (line.startsWith("#")) {
        continue;
      }
      st = new StringTokenizer(line, ",");
      if (st.countTokens() == 2) {
        String nameStr = st.nextToken();
        String dayStr = st.nextToken();
        holiDay = new ALHoliday(nameStr, dayStr);
        if (holiDay.getDay().validate(dummyList)) {
          list.add(holiDay);
        }
      }
    }
  }

  /**
   * 祝日情報を日付で昇順に並び替える比較関数を取得する．
   * 
   * @return 祝日情報を日付で昇順に並び替える比較関数
   */
  private Comparator getHolidaysComparator() {
    Comparator com = new Comparator() {
      public int compare(Object obj0, Object obj1) {
        int ret = 0;
        try {
          Date day0 = ((ALHoliday) obj0).getDay().getValue().getDate();
          Date day1 = ((ALHoliday) obj1).getDay().getValue().getDate();

          // 日付の昇順
          if ((ret = day0.compareTo(day1)) == 0) {
            // 日付が同じ場合，祝日名の昇順
            String name0 = ((ALHoliday) obj0).getName().getValue();
            String name1 = ((ALHoliday) obj1).getName().getValue();
            ret = name0.compareTo(name1);
          }
        } catch (Exception ex) {
          return -1;
        }
        return ret;
      }
    };
    return com;
  }

  /** Add 20110909 小濵 Start --- */
  /**
   * 指定したユーザーと日付から、勤務先別の休日であるかを検証する．
   * 
   * @param date
   *            検証する日付
   * @param user_id
   *            検証するユーザー
   * @return 指定した日付の勤務先別休日情報．
   */
  public ImwTOfficeCalendar isOfficeHoliday(Date date, String user_id) {
    if (date == null) {
      return null;
    }
    
    if (user_id == null) {
      return null;
    }

    Integer office_id;
    
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    
    office_id = getOfficeIdByEipTExtTimecard(date, user_id);
    
    if(office_id == null) {
      office_id = getOfficeIdByImwTOfficeCalendarMap(user_id);
    }

    return getImwTOfficeCalendar(date,office_id);
  }

  public Integer getOfficeIdByEipTExtTimecard(Date date, String user_id) {
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
          .getDataContext();
      SelectQuery query = new SelectQuery(EipTExtTimecard.class);
      Expression exp1 = ExpressionFactory.matchExp(
          EipTExtTimecard.USER_ID_PROPERTY, Integer.valueOf(user_id).intValue());
      Expression exp2 = ExpressionFactory.matchExp(
          EipTExtTimecard.PUNCH_DATE_PROPERTY, date);
      query.setQualifier(exp1);
      query.andQualifier(exp2);

      List exttimecards = dataContext.performQuery(query);
      if (exttimecards == null || exttimecards.size() == 0) {
        return null;
      }

      EipTExtTimecard extTimecard = (EipTExtTimecard)exttimecards.get(0);
      
      return extTimecard.getOfficeId();
    } catch (Exception ex) {
      logger.error("Exception", ex);
      throw null;
    }
  }

  public Integer getOfficeIdByImwTOfficeCalendarMap(String user_id) {
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
          .getDataContext();
      SelectQuery query = new SelectQuery(ImwTOfficeCalendarMap.class);
      Expression exp1 = ExpressionFactory.matchExp(
          ImwTOfficeCalendarMap.USER_ID_PROPERTY, Integer.valueOf(user_id).intValue());
      query.setQualifier(exp1);

      List officecalendarmaps = dataContext.performQuery(query);
      if (officecalendarmaps == null || officecalendarmaps.size() == 0) {
        return null;
      }
      
      ImwTOfficeCalendarMap officeCalendarMap = 
        (ImwTOfficeCalendarMap)officecalendarmaps.get(0);

      return officeCalendarMap.getImwTOffice().getOfficeId();
    } catch (Exception ex) {
      logger.error("Exception", ex);
      throw null;
    }
  }
  
  public ImwTOfficeCalendar getImwTOfficeCalendar(Date date,Integer office_id) {
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
          .getDataContext();
      SelectQuery query = new SelectQuery(ImwTOfficeCalendar.class);
      Expression exp1 = ExpressionFactory.matchDbExp(
          ImwTOffice.OFFICE_ID_PK_COLUMN, office_id);
      Expression exp2 = ExpressionFactory.matchExp(
          ImwTOfficeCalendar.CALENDAR_DATE_PROPERTY, date);
      Expression exp3 = ExpressionFactory.matchExp(
          ImwTOfficeCalendar.DISABLED_PROPERTY, "F");
      query.setQualifier(exp1);
      query.andQualifier(exp2);
      query.andQualifier(exp3);

      List officecalendars = dataContext.performQuery(query);
      if (officecalendars == null || officecalendars.size() == 0) {
        return null;
      }
      
      return (ImwTOfficeCalendar)officecalendars.get(0);
    } catch (Exception ex) {
      logger.error("Exception", ex);
      throw null;
    }
  }
  /** Add 20110909 小濵 End   --- */

}
