/*
 * 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.exttimecard;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;

import jp.co.imtt.businesssystem.util.BusinessSystemUtils;

import org.apache.cayenne.DataRow;
import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.SQLTemplate;
import org.apache.cayenne.query.SelectQuery;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALDateTimeField;
import com.aimluck.commons.field.ALNumberField;
import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.cayenne.om.portlet.EipTExtTimecard;
import com.aimluck.eip.cayenne.om.portlet.EipTExtTimecardSystemMap;
import com.aimluck.eip.cayenne.om.portlet.ImwTExtTimecardDetail;
import com.aimluck.eip.cayenne.om.portlet.ImwMHanyo;
import com.aimluck.eip.cayenne.om.portlet.ImwTExtTimecardSystemDetail;
import com.aimluck.eip.cayenne.om.portlet.ImwTJobOrder;
import com.aimluck.eip.cayenne.om.portlet.ImwTDutyStatus;
import com.aimluck.eip.cayenne.om.portlet.EipTExtTimecardSystem;
import com.aimluck.eip.common.ALAbstractSelectData;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALData;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALEipManager;
import com.aimluck.eip.common.ALEipUser;
import com.aimluck.eip.common.ALPageNotFoundException;
import com.aimluck.eip.common.ALPermissionException;
import com.aimluck.eip.exttimecard.util.ExtTimecardUtils;
import com.aimluck.eip.modules.actions.common.ALAction;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.services.accessctl.ALAccessControlConstants;
import com.aimluck.eip.services.accessctl.ALAccessControlFactoryService;
import com.aimluck.eip.services.accessctl.ALAccessControlHandler;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 作業の一覧を処理するクラスです。 <br />
 *
 *
 */

public class ExtTimecardWorkListSelectData extends ALAbstractSelectData
    implements ALData {

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

  /** <code>target_group_name</code> 表示対象の部署名 */
  private String target_group_name;

  /** <code>target_user_id</code> 表示対象のユーザ ID */
  private String target_user_id;

  /** <code>myGroupList</code> グループリスト（My グループと部署） */
  private List myGroupList = null;

  /** <code>userList</code> 表示切り替え用のユーザリスト */
  private List userList = null;

  /** 一覧データ */
  private ArrayList list;

  /** <code>userid</code> ユーザーID */
  private String userid;

  /** <code>TARGET_GROUP_NAME</code> グループによる表示切り替え用変数の識別子 */
  private final String TARGET_GROUP_NAME = "target_group_name";

  /** <code>TARGET_USER_ID</code> ユーザによる表示切り替え用変数の識別子 */
  private final String TARGET_USER_ID = "target_user_id";

  private String nowtime;

  /** ユーザーマップ */
  private Map usermap;

  /** 日付マップ */
  private Map datemap;

  /** アクセス権限の機能名 */
  private String aclPortletFeature = null;

  /** 閲覧権限の有無 */
  private boolean hasAclSummaryOther;

  /** 他ユーザーのxlsエクスポート権限 */
  private boolean hasAclXlsExport;

  /** <code>viewMonth</code> 現在の月 */
  private ALDateTimeField viewMonth;

  /** <code>prevMonth</code> 前の月 */
  private ALDateTimeField prevMonth;

  /** <code>nextMonth</code> 次の月 */
  private ALDateTimeField nextMonth;

  /** <code>currentMonth</code> 今月 */
  private ALDateTimeField currentMonth;

  /** <code>today</code> 今日 */
  private ALDateTimeField today;

  /** <code>tmpViewDate2</code> テンポラリ */
  protected String tmpViewDate2;

  /** <code>viewStart</code> 表示開始日時 */
  private ALDateTimeField viewStart;

  /** <code>viewEnd</code> 表示終了日時 */
  private ALDateTimeField viewEnd;

  /** <code>viewStart</code> 出勤時刻 */
  private ALDateTimeField workStart;

  /** <code>viewEnd</code> 退勤時刻 */
  private ALDateTimeField workEnd;

  //Add 20110623 小濵 Start ---
  /** <code>worktimeIn</code> 所定内時間 */
  private double worktimeIn;

  /** <code>worktimeOut_usually</code> 所定外時間(普通) */
  private double worktimeOut_usually;

  /** <code>worktimeOut_midnight</code> 所定外字間(深夜) */
  private double worktimeOut_midnight;
  //Add 20110623 小濵 End   ---

  /** <code>viewEndCrt</code> 表示終了日時 (Criteria) */
  private ALDateTimeField viewEndCrt;

  /** <code>viewtype</code> 表示タイプ */
  private String viewtype;

  /** <code>viewTodo</code> ToDo 表示設定 */
  protected int viewTodo;

  /** ポートレット ID */
  private String portletId;

  private DataContext dataContext;

  /** <code>facilityList</code> 表示切り替え用の施設リスト */
  private List facilityList;

  /** 閲覧権限の有無 */
  private boolean hasAclviewOther;

  /** <code>hasAuthoritySelfInsert</code> アクセス権限 */
  private boolean hasAuthoritySelfInsert = false;

  /** <code>hasAuthorityFacilityInsert</code> アクセス権限 */
  private boolean hasAuthorityFacilityInsert = false;

  /** <code>target_user_id</code> 表示対象のユーザ ログイン名 */
  private String target_user_name;
  
  /* 20100823 小濵 Start */
  /** <code>record_flag</code> 対象のタイムカードの有無 */
  private boolean record_flag;
  /* 20100823 小濵 End */

  /* 20100824 小濵 Start */
  /** <code>duty_status_date</code> 更新可能なレコードかの判断 */
  private boolean progress;
  /* 20100824 小濵 End */

  /** Add 2010/10/05 金廣 Start--- */
  // 課題58:他人の作業入力修正
  /** 他人の作業入力修正権限の有無 */
  private boolean hasAclOther; 
  /** Add 2010/10/05 金廣 End----- */
  
  /**
   *
   */
  public void initField() {
  }

  /*
   * (非 Javadoc)
   *
   * @see
   * com.aimluck.eip.common.ALAbstractSelectData#init(com.aimluck.eip.modules
   * .actions.common.ALAction, org.apache.turbine.util.RunData,
   * org.apache.velocity.context.Context)
   */
  public void init(ALAction action, RunData rundata, Context context)
      throws ALPageNotFoundException, ALDBErrorException {
    // TODO 自動生成されたメソッド・スタブ
    super.init(action, rundata, context);

    this.initField();

    dataContext = DatabaseOrmService.getInstance().getDataContext();

    // POST/GET から yyyy-MM の形式で受け渡される。
    // 現在の月
    viewMonth = new ALDateTimeField("yyyy-MM-dd");
    viewMonth.setNotNull(true);
    // 前の月
    prevMonth = new ALDateTimeField("yyyy-MM-dd");
    // 次の月
    nextMonth = new ALDateTimeField("yyyy-MM-dd");
    // 今月
    currentMonth = new ALDateTimeField("yyyy-MM-dd");
    // 表示開始日時
    viewStart = new ALDateTimeField("yyyy-MM-dd");
    // 表示終了日時
    viewEnd = new ALDateTimeField("yyyy-MM-dd");
    // 表示終了日時 (Criteria)
    viewEndCrt = new ALDateTimeField("yyyy-MM-dd");
    // 今日
    today = new ALDateTimeField("yyyy-MM-dd");
    // 出勤時刻
    workStart = new ALDateTimeField("HH時mm分");
    // 退勤時刻
    workEnd = new ALDateTimeField("HH時mm分");
    
    /* 20100823 小濵 Start */
    record_flag = true;
    /* 20100823 小濵 End */
    
    /* 20100824 小濵 Start */
    progress = true;
    /* 20100824 小濵 End */
    
    /** Add 2010/10/05 金廣 Start--- */
    // 課題58:他人の作業入力修正
    // 他人のタイムカード編集権限
    ALAccessControlFactoryService aclservice2 = (ALAccessControlFactoryService) ((TurbineServices) TurbineServices
        .getInstance()).getService(ALAccessControlFactoryService.SERVICE_NAME);
    ALAccessControlHandler aclhandler2 = aclservice2.getAccessControlHandler();
    hasAclOther = aclhandler2.hasAuthority(ALEipUtils.getUserId(rundata),
        ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_OTHER,
        ALAccessControlConstants.VALUE_ACL_LIST);
    /** Add 2010/10/05 金廣 End----- */

    Calendar to = Calendar.getInstance();
    to.set(Calendar.HOUR_OF_DAY, 0);
    to.set(Calendar.MINUTE, 0);
    today.setValue(to.getTime());
    currentMonth.setValue(to.getTime());

    // 自ポートレットからのリクエストであれば、パラメータを展開しセッションに保存する。
    if (ALEipUtils.isMatch(rundata, context)) {
      // スケジュールの表示開始日時
      // e.g. 2004-3-14
      if (rundata.getParameters().containsKey("view_date")) {
        ALEipUtils.setTemp(rundata, context, "view_date", rundata
            .getParameters().getString("view_date"));
      }
    }

    // viewDate に値を設定する。
    String tmpViewDate = ALEipUtils.getTemp(rundata, context, "view_date");
    if (tmpViewDate2 != null) {
      tmpViewDate = tmpViewDate2;
    }
    if (tmpViewDate == null || tmpViewDate.equals("")) {
      // セッションに情報がない場合は今日の日付を設定する。
      Calendar cal = Calendar.getInstance();
      //Add 20110613 小濵 Start ---
      /** タイムカード設定を取得 */
      EipTExtTimecardSystem timecard_system = ExtTimecardUtils
          .getEipTExtTimecardSystemByUserId((Integer) ALEipUtils.getUserId(rundata));
      if (timecard_system.getChangeHour() > cal.get(Calendar.HOUR_OF_DAY)) {
        cal.add(Calendar.DATE, -1);
      }
      //Add 20110613 小濵 End ---
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);

      ALEipUtils.setTemp(rundata, context, "view_date", today.toString());
      viewMonth.setValue(cal.getTime());
    } else {
      viewMonth.setValue(tmpViewDate);
      if (!viewMonth.validate(new ArrayList())) {
        ALEipUtils.removeTemp(rundata, context, "view_date");

        throw new ALPageNotFoundException();

      }
    }

    // 表示開始日時
    Calendar cal = Calendar.getInstance();
    Calendar tmpCal = Calendar.getInstance();
    cal.setTime(viewMonth.getValue());
    tmpCal.setTime(viewMonth.getValue());
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    cal.add(Calendar.DATE, -dayofweek + 1);
    viewStart.setValue(cal.getTime());

    Calendar cal4 = Calendar.getInstance();
    cal4.setTime(cal.getTime());
    Calendar tmpCal4 = Calendar.getInstance();
    tmpCal4.setTime(tmpCal.getTime());

    Calendar cal5 = Calendar.getInstance();
    cal5.setTime(cal.getTime());
    Calendar tmpCal5 = Calendar.getInstance();
    tmpCal5.setTime(tmpCal.getTime());

    // 表示終了日時
    viewEndCrt.setValue(cal.getTime());
    cal.add(Calendar.DATE, -1);
    viewEnd.setValue(cal.getTime());
    // 次の月、前の月
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(viewMonth.getValue());
    cal2.add(Calendar.DATE, 1);
    nextMonth.setValue(cal2.getTime());
    cal2.add(Calendar.DATE, -2);
    prevMonth.setValue(cal2.getTime());

    ALEipUtils.setTemp(rundata, context, "tmpStart", viewStart.toString()
        + "-00-00");
    ALEipUtils.setTemp(rundata, context, "tmpEnd", viewStart.toString()
        + "-00-00");
    
    // ログインユーザの ID を設定する．
    userid = Integer.toString(ALEipUtils.getUserId(rundata));

    // My グループの一覧を取得する．
    List myGroups = ALEipUtils.getMyGroups(rundata);
    myGroupList = new ArrayList();
    int length = myGroups.size();
    for (int i = 0; i < length; i++) {
      myGroupList.add(myGroups.get(i));
    }

    try {
      // スケジュールを表示するユーザ ID をセッションに設定する．
      String userFilter = ALEipUtils.getTemp(rundata, context, TARGET_USER_ID);
      if (userFilter == null || userFilter.equals("")) {
        VelocityPortlet portlet = ALEipUtils.getPortlet(rundata, context);
        userFilter = portlet.getPortletConfig().getInitParameter("p3a-user");
      }

      if (userFilter != null && (!userFilter.equals(""))) {
        int paramId = -1;
        try {
          paramId = Integer.parseInt(userFilter);
          if (paramId > 3) {
            ALEipUser user = ALEipUtils.getALEipUser(paramId);
            if (user != null) {
              // 指定したユーザが存在する場合，セッションに保存する．
              ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, userFilter);
            } else {
              ALEipUtils.removeTemp(rundata, context, TARGET_USER_ID);
            }
          }
        } catch (NumberFormatException e) {
        }
      } else {
        ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, userid);
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
    }

    // アクセス権
    if (target_user_id == null || "".equals(target_user_id)
        || userid.equals(target_user_id)) {
      aclPortletFeature = ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_SELF;
    } else {
      aclPortletFeature = ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_OTHER;
    }
    ALAccessControlFactoryService aclservice = (ALAccessControlFactoryService) ((TurbineServices) TurbineServices
        .getInstance()).getService(ALAccessControlFactoryService.SERVICE_NAME);
    ALAccessControlHandler aclhandler = aclservice.getAccessControlHandler();
    hasAclSummaryOther = aclhandler.hasAuthority(ALEipUtils.getUserId(rundata),
        ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_OTHER,
        ALAccessControlConstants.VALUE_ACL_LIST);
    hasAclXlsExport = aclhandler.hasAuthority(ALEipUtils.getUserId(rundata),
        ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_OTHER,
        ALAccessControlConstants.VALUE_ACL_EXPORT);

    if (!hasAclSummaryOther) {
      // 他ユーザーの閲覧権限がないときには、グループを未選択にする。
      target_group_name = "only";
      aclPortletFeature = ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_SELF;
      hasAclXlsExport = aclhandler.hasAuthority(ALEipUtils.getUserId(rundata),
          ALAccessControlConstants.POERTLET_FEATURE_TIMECARD_TIMECARD_SELF,
          ALAccessControlConstants.VALUE_ACL_EXPORT);
    }

    datemap = new LinkedHashMap();

    usermap = new LinkedHashMap();

    setupLists(rundata, context);
    
    StringBuffer statement = new StringBuffer();
    statement.append(" SELECT");
    statement.append("      eip_t_ext_timecard.TIMECARD_ID");
    statement.append("     ,eip_t_ext_timecard.CLOCK_IN_TIME");
    statement.append("     ,eip_t_ext_timecard.CLOCK_OUT_TIME");
    statement.append("     ,eip_t_ext_timecard.system_id");
    statement.append(" FROM eip_t_ext_timecard");
    statement.append(" WHERE");
    statement.append("      eip_t_ext_timecard.user_id ='" + target_user_id + "'");
    statement.append(" AND");
    statement.append("      eip_t_ext_timecard.punch_date ='" + viewMonth + "'");
    String query = statement.toString();
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
      .getDataContext();
      SQLTemplate rawSelect = new SQLTemplate(EipTExtTimecard.class, query,
          true);
      rawSelect.setFetchingDataRows(true);
      List list = dataContext.performQuery(rawSelect);
      /* 20100823 小濵 Start */
      if (list.size() == 0){
        record_flag = false;
        ALEipUtils.setTemp(rundata, context, "timecard_id", null);
      }else{
      /* 20100823 小濵 End */
        for(int i = 0;i < list.size();i++){
          if(list.get(i) != null){
            ALEipUtils.setTemp(rundata, context, "timecard_id", ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.TIMECARD_ID_COLUMN).toString());
            workStart.setValue((Date)ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.CLOCK_IN_TIME_COLUMN));
            if((Date)ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.CLOCK_OUT_TIME_COLUMN) == null
                && today.getValue().getTime() == viewMonth.getValue().getTime()){
              Calendar cal6;
              cal6 = BusinessSystemUtils.getEndTime(Calendar.getInstance().getTime(), Integer.parseInt(target_user_id), (Integer)ALEipUtils.getObjFromDataRow((DataRow)list.get(i),EipTExtTimecard.SYSTEM_ID_COLUMN));
              workEnd.setValue(cal6.getTime());
            }else{
              workEnd.setValue((Date)ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.CLOCK_OUT_TIME_COLUMN));
            }
            //Add 20110624 小濵 Start ---
            double[] worktime = getWorktime(workStart, workEnd, (Integer)ALEipUtils.getObjFromDataRow((DataRow)list.get(i),EipTExtTimecard.SYSTEM_ID_COLUMN));
            worktimeIn = worktime[0];
            worktimeOut_usually = worktime[1];
            worktimeOut_midnight = worktime[2];
            //Add 20110624 小濵 End   ---
          }
        }
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
    }
    setProgress(rundata,context);

  }


  /*
   * (非 Javadoc)
   *
   * @see
   * com.aimluck.eip.common.ALAbstractSelectData#selectList(org.apache.turbine
   * .util.RunData, org.apache.velocity.context.Context)
   */
  public List selectList(RunData rundata, Context context) {
        
    StringBuffer statement = new StringBuffer();
    statement.append(" SELECT");
    statement.append("      imw_t_ext_timecard_detail.detail_id");
    statement.append("     ,imw_t_ext_timecard_detail.job_order_id");
    statement.append("     ,imw_t_ext_timecard_detail.work_class");
    statement.append("     ,imw_t_ext_timecard_detail.work_place");
    statement.append("     ,imw_t_ext_timecard_detail.worktime_in");
    statement.append("     ,imw_t_ext_timecard_detail.worktime_out");
    statement.append("     ,imw_t_ext_timecard_detail.work_detail");
    statement.append(" FROM imw_t_ext_timecard_detail,eip_t_ext_timecard");
    statement.append(" WHERE");
    statement.append("      eip_t_ext_timecard.timecard_id = imw_t_ext_timecard_detail.timecard_id");
    statement.append(" AND");
    statement.append("      eip_t_ext_timecard.user_id ='" + target_user_id + "'");
    statement.append(" AND");
    statement.append("      eip_t_ext_timecard.punch_date ='" + viewMonth + "'");
    statement.append(" ORDER BY");
    statement.append("      imw_t_ext_timecard_detail.detail_id");
    String query = statement.toString();
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
      .getDataContext();
      SQLTemplate rawSelect = new SQLTemplate(ImwTExtTimecardDetail.class, query,
          true);
      rawSelect.setFetchingDataRows(true);
      List list = dataContext.performQuery(rawSelect);
  
      int recordNum = list.size();
      DataRow dataRow;
      List worklist = new ArrayList();
      for (int i = 0; i < recordNum; i++) {
        dataRow = (DataRow) list.get(i);
        ImwTExtTimecardDetail detail = new ImwTExtTimecardDetail();
        detail.setDetailId((Integer) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.DETAIL_ID_PK_COLUMN));
        detail.setJobOrderId((Integer) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.JOB_ORDER_ID_COLUMN));
        detail.setWorkClass((String) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.WORK_CLASS_COLUMN));
        detail.setWorkDetail((String) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.WORK_DETAIL_COLUMN));
        detail.setWorkPlace((String) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.WORK_PLACE_COLUMN));
        detail.setWorktimeIn((Integer) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.WORKTIME_IN_COLUMN));
        detail.setWorktimeOut((Integer) ALEipUtils.getObjFromDataRow(dataRow,ImwTExtTimecardDetail.WORKTIME_OUT_COLUMN));
        worklist.add(detail);
      }
      return buildPaginatedList(worklist);

  } catch (Exception ex) {
    logger.error("Exception", ex);
    return null;
  }

  }

 /*
   * (非 Javadoc)
   *
   * @see
   * com.aimluck.eip.common.ALAbstractSelectData#selectDetail(org.apache.turbine
   * .util.RunData, org.apache.velocity.context.Context)
   */
  protected Object selectDetail(RunData rundata, Context context)
      throws ALPageNotFoundException, ALDBErrorException {
    // TODO 自動生成されたメソッド・スタブ
    return null;
  }

  /*
   * (非 Javadoc)
   *
   * @see
   * com.aimluck.eip.common.ALAbstractSelectData#getResultData(java.lang.Object)
   */

  /**
   * ResultData に値を格納して返します。（一覧データ） <BR>
   *
   * @param obj
   * @return
   * @see com.aimluck.eip.common.ALAbstractSelectData#getListData(java.lang.Object)
   */
  protected Object getResultData(Object obj) throws ALPageNotFoundException,
      ALDBErrorException {
    try {
      DecimalFormat df=new DecimalFormat();

          df.applyPattern("0");
          
          df.setMaximumFractionDigits(2);
          df.setMinimumFractionDigits(2);
      
      ImwTExtTimecardDetail record = (ImwTExtTimecardDetail) obj;

      ExtTimecardWorkListResultData rd = new ExtTimecardWorkListResultData();
      rd.initField();
      rd.setDetail_id(record.getDetailId());
      rd.setWork_class(record.getWorkClass());
      rd.setWork_place(record.getWorkPlace());
      if(record.getWorktimeIn() == null)record.setWorktimeIn(0);
      rd.setWorktime_in(df.format((double)record.getWorktimeIn() / 60));
      if(record.getWorktimeOut() == null)record.setWorktimeOut(0);
      rd.setWorktime_out(df.format((double)record.getWorktimeOut() / 60));
      rd.setWork_detail(record.getWorkDetail());
      rd.setJob_order_id(record.getJobOrderId());
      
      //作業区分
      StringBuffer statement = new StringBuffer();
      statement.append(" SELECT");
      statement.append("      imw_m_hanyo.hanyokubunnm");
      statement.append(" FROM imw_m_hanyo");
      statement.append(" WHERE");
      statement.append("      imw_m_hanyo.parent_hanyo_id = '11'");
      statement.append(" AND");
      statement.append("      imw_m_hanyo.value = '" + record.getWorkClass() + "'");
      String query = statement.toString();
      try {
        DataContext dataContext = DatabaseOrmService.getInstance()
        .getDataContext();
        SQLTemplate rawSelect = new SQLTemplate(ImwTExtTimecardDetail.class, query,
            true);
        rawSelect.setFetchingDataRows(true);
        List list = dataContext.performQuery(rawSelect);
        rd.setWork_class((String) ALEipUtils.getObjFromDataRow((DataRow)list.get(0),ImwMHanyo.HANYOKUBUNNM_COLUMN));
      } catch (Exception ex) {
        logger.error("Exception", ex);
        return null;
      }

      //作業場所
      statement = new StringBuffer();
      statement.append(" SELECT");
      statement.append("      imw_m_hanyo.hanyokubunnm");
      statement.append(" FROM imw_m_hanyo");
      statement.append(" WHERE");
      statement.append("      imw_m_hanyo.parent_hanyo_id = '12'");
      statement.append(" AND");
      statement.append("      imw_m_hanyo.value = '" + record.getWorkPlace() + "'");
      query = statement.toString();
      try {
        DataContext dataContext = DatabaseOrmService.getInstance()
        .getDataContext();
        SQLTemplate rawSelect = new SQLTemplate(ImwTExtTimecardDetail.class, query,
            true);
        rawSelect.setFetchingDataRows(true);
        List list = dataContext.performQuery(rawSelect);
        rd.setWork_place((String) ALEipUtils.getObjFromDataRow((DataRow)list.get(0),ImwMHanyo.HANYOKUBUNNM_COLUMN));
      } catch (Exception ex) {
        logger.error("Exception", ex);
        return null;
      }

      //件名
      statement = new StringBuffer();
      statement.append(" SELECT");
      statement.append("      imw_t_job_order.job_order_name");
      statement.append(" FROM imw_t_job_order");
      statement.append(" WHERE");
      statement.append("      imw_t_job_order.job_order_id = '" + record.getJobOrderId() + "'");
      query = statement.toString();
      try {
        DataContext dataContext = DatabaseOrmService.getInstance()
        .getDataContext();
        SQLTemplate rawSelect = new SQLTemplate(ImwTExtTimecardDetail.class, query,
            true);
        rawSelect.setFetchingDataRows(true);
        List list = dataContext.performQuery(rawSelect);
        rd.setJob_order_name((String) ALEipUtils.getObjFromDataRow((DataRow)list.get(0),ImwTJobOrder.JOB_ORDER_NAME_COLUMN));
      } catch (Exception ex) {
        logger.error("Exception", ex);
        return null;
      }

      return rd;
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return null;
    }
  }

  /*
   * (非 Javadoc)
   *
   * @see
   * com.aimluck.eip.common.ALAbstractSelectData#getResultDataDetail(java.lang
   * .Object)
   */
  protected Object getResultDataDetail(Object obj)
      throws ALPageNotFoundException, ALDBErrorException {
    // TODO 自動生成されたメソッド・スタブ
    return null;
  }

  /*
   * (非 Javadoc)
   *
   * @see com.aimluck.eip.common.ALAbstractSelectData#getColumnMap()
   */
  protected Attributes getColumnMap() {
    // TODO 自動生成されたメソッド・スタブ
    return null;
  }

  /**
   * ログイン中のユーザー情報を取得
   *
   */

  private List getUserList(int userid) {
    List list = new ArrayList();
    ALEipUser user = new ALEipUser();
    try {
      user = ALEipUtils.getALEipUser(userid);
    } catch (NumberFormatException e1) {
      // TODO 自動生成された catch ブロック
      e1.printStackTrace();
    } catch (ALDBErrorException e1) {
      // TODO 自動生成された catch ブロック
      e1.printStackTrace();
    }
    list.add(user);
    return list;
  }

  /**
   * 指定グループや指定ユーザをセッションに設定する．
   *
   * @param rundata
   * @param context
   * @throws ALDBErrorException
   */
  private void setupLists(RunData rundata, Context context) {
/*    target_group_name = getTargetGroupName(rundata, context);
    if ((target_group_name != null) && (!target_group_name.equals(""))
        && (!target_group_name.equals("all"))
        && (!target_group_name.equals("only"))) {
      userList = ALEipUtils.getUsers(target_group_name);
    } else if (target_group_name.equals("all")
        || target_group_name.equals("only")) {

      userList = getUserList(Integer.parseInt(userid));
    } else {
      userList = ALEipUtils.getUsers("LoginUser");
    }

    if (userList == null || userList.size() == 0) {
      target_user_id = "";
      ALEipUtils.removeTemp(rundata, context, TARGET_USER_ID);
      return;
    }

    target_user_id = getTargetUserId(rundata, context);
*/
    target_group_name = getTargetGroupName(rundata, context);
    if ((target_group_name != null) && (!target_group_name.equals(""))
        && (!target_group_name.equals("all"))) {
      userList = ALEipUtils.getUsers(target_group_name);
    } else {
      userList = ALEipUtils.getUsers("LoginUser");
    }

    if (userList == null || userList.size() == 0) {
      target_user_id = "";
      ALEipUtils.removeTemp(rundata, context, ExtTimecardUtils.TARGET_USER_ID);
      return;
    }

    target_user_id = getTargetUserId(rundata, context);

  }

  /**
   * 表示切り替えで指定したグループ ID を取得する．
   *
   * @param rundata
   * @param context
   * @return
   */
  private String getTargetGroupName(RunData rundata, Context context) {
    String target_group_name = null;
    String idParam = null;
    if (ALEipUtils.isMatch(rundata, context)) {
      // 自ポートレットへのリクエストの場合に，グループ名を取得する．
      idParam = rundata.getParameters().getString(TARGET_GROUP_NAME);
    }
    target_group_name = ALEipUtils.getTemp(rundata, context, TARGET_GROUP_NAME);

    if (idParam == null && target_group_name == null) {
      ALEipUtils.setTemp(rundata, context, TARGET_GROUP_NAME, "only");
      target_group_name = "only";
    } else if (idParam != null) {
      ALEipUtils.setTemp(rundata, context, TARGET_GROUP_NAME, idParam);
      target_group_name = idParam;
    }
    return target_group_name;
  }

  /**
   * 表示切り替えで指定したユーザ ID を取得する．
   *
   * @param rundata
   * @param context
   * @return
   */
  private String getTargetUserId(RunData rundata, Context context) {
    String target_user_id = null;
    String idParam = null;
    if (ALEipUtils.isMatch(rundata, context)) {
      // 自ポートレットへのリクエストの場合に，ユーザ ID を取得する．
      idParam = rundata.getParameters().getString(TARGET_USER_ID);
    }
    target_user_id = ALEipUtils.getTemp(rundata, context, TARGET_USER_ID);

    if (idParam == null && (target_user_id == null)) {
      // ログインユーザのスケジュールを表示するため，ログイン ID を設定する．
      ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, userid);
      target_user_id = userid;
    } else if (idParam != null) {
      if (idParam.equals("none")) {
        // グループで表示を切り替えた場合，
        // ログインユーザもしくはユーザリストの一番初めのユーザを
        // 表示するため，ユーザ ID を設定する．
        ALEipUser eipUser = null;
        boolean found = false;
        int length = userList.size();
        for (int i = 0; i < length; i++) {
          eipUser = (ALEipUser) userList.get(i);
          String eipUserId = eipUser.getUserId().getValueAsString();
          if (userid.equals(eipUserId)) {
            ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, userid);
            target_user_id = userid;
            found = true;
            break;
          }
        }
        if (!found) {
          eipUser = (ALEipUser) userList.get(0);
          String userId = eipUser.getUserId().getValueAsString();
          ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, userId);
          target_user_id = userId;
        }
      } else {
        // ユーザで表示を切り替えた場合，指定したユーザの ID を設定する．
        ALEipUtils.setTemp(rundata, context, TARGET_USER_ID, idParam);
        target_user_id = idParam;
      }
    }
    return target_user_id;
  }

  /**
   * 検索条件を設定した SelectQuery を返します。 <BR>
   *
   * @param rundata
   * @param context
   * @return
   */
  private SelectQuery getSelectQuery(RunData rundata, Context context,
      String target_user_id) {
    SelectQuery query = new SelectQuery(EipTExtTimecard.class);

    Expression exp1 = ExpressionFactory.matchExp(
        EipTExtTimecard.USER_ID_PROPERTY, new Integer(target_user_id));
    query.setQualifier(exp1);
    
    Expression exp2 = ExpressionFactory.matchExp(
        EipTExtTimecard.PUNCH_DATE_PROPERTY, viewMonth);
    query.andQualifier(exp2);
    
    SelectQuery query2 = new SelectQuery(ImwTExtTimecardDetail.class);
    
/*
    Calendar cal = Calendar.getInstance();
    cal.setTime(viewMonth.getValue());
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Expression exp11 = ExpressionFactory.greaterOrEqualExp(
        EipTExtTimecard.PUNCH_DATE_PROPERTY, cal.getTime());

    cal.add(Calendar.MONTH, +1);
    cal.add(Calendar.MILLISECOND, -1);
    Expression exp12 = ExpressionFactory.lessOrEqualExp(
        EipTExtTimecard.PUNCH_DATE_PROPERTY, cal.getTime());
    query.andQualifier(exp11.andExp(exp12));
*/
    return buildSelectQueryForFilter(query, rundata, context);
  }

  /**
   * ユーザー毎のタイムカード一覧を取得する。
   *
   * @return
   */
  public List getUserExtTimecards() {
    ArrayList list = new ArrayList();
    Set userset = usermap.keySet();

    /** 以下、ユーザーごとの処理 */
    for (Object user_id : userset) {
      ArrayList userlist = (ArrayList) usermap.get(user_id);
      ExtTimecardSummaryResultData summary_rd = new ExtTimecardSummaryResultData();
      int work_day = 0, overtime_day = 0, off_day = 0;
      /** 就業、残業、休出日数 */
      float work_hour = 0, overtime_hour = 0, off_hour = 0;
      /** 就業、残業、休出時間 */
      int late_coming_day = 0, early_leaving_day = 0, absent_day = 0;
      /** 遅刻、早退、欠勤 */
      int paid_holiday = 0, compensatory_holiday = 0;
      /** 有休、代休 */
      int other_day = 0, no_input = 0;
      /** その他、未入力 */
      summary_rd.initField();

      /** タイムカード設定を取得 */
      EipTExtTimecardSystem timecard_system = ExtTimecardUtils
          .getEipTExtTimecardSystemByUserId((Integer) user_id);

      /**
       * userlistにはユーザー日ごとのタイムカードのResultDataがリストで入っているため、
       * ListResultDataに代入して各日数・時間を計算させる。
       */

      for (Object obj : userlist) {
        ExtTimecardResultData rd = (ExtTimecardResultData) obj;
        ExtTimecardListResultData lrd = new ExtTimecardListResultData();
        lrd.setRd(rd);
        lrd.setTimecardSystem(timecard_system);
        String type = rd.getType().getValue();
        if (type.equals(EipTExtTimecard.TYPE_WORK)) {
          /** 出勤 */
          if (lrd.getWorkHour() != ExtTimecardListResultData.NO_DATA) {
            work_day++;
            work_hour += lrd.getWorkHourWithoutRestHour();
          }
          if (lrd.getOvertimeHour() != ExtTimecardListResultData.NO_DATA) {
            overtime_day++;
            overtime_hour += lrd.getOvertimeHourWithoutRestHour();
          }
          if (lrd.getOffHour() != ExtTimecardListResultData.NO_DATA) {
            off_day++;
            off_hour += lrd.getOffHour();
          }
          if (lrd.isLateComing()) {
            late_coming_day++;
          }
          if (lrd.isEarlyLeaving()) {
            early_leaving_day++;
          }
        } else if (type.equals(EipTExtTimecard.TYPE_ABSENT)) {
          /** 欠勤 */
          absent_day++;
        } else if (type.equals(EipTExtTimecard.TYPE_HOLIDAY)) {
          /** 有休 */
          paid_holiday++;
        } else if (type.equals(EipTExtTimecard.TYPE_COMPENSATORY)) {
          /** 代休 */
          compensatory_holiday++;
        } else if (type.equals(EipTExtTimecard.TYPE_ETC)) {
          /** その他 */
          other_day++;
        }
      }

      /** ユーザーごとの合計をSummaryResultDataに代入する */
      work_hour = ExtTimecardUtils.roundHour(work_hour);
      overtime_hour = ExtTimecardUtils.roundHour(overtime_hour);
      off_hour = ExtTimecardUtils.roundHour(off_hour);

      summary_rd.setWorkDayHour(work_day, work_hour);
      summary_rd.setOvertimeDayHour(overtime_day, overtime_hour);
      summary_rd.setOffDayHour(off_day, off_hour);
      summary_rd.setLateComingDay(late_coming_day);
      summary_rd.setEarlyLeavingDay(early_leaving_day);
      summary_rd.setAbsentDay(absent_day);
      summary_rd.setPaidHoliday(paid_holiday);
      summary_rd.setCompensatoryHoliday(compensatory_holiday);
      summary_rd.setOtherDay(other_day);
      summary_rd.setNoInput(no_input);

      list.add(summary_rd);
    }
    return list;
  }

  /**
   * グループ毎のタイムカード一覧を取得する。
   *
   * @return
   */

  public List getGroupExtTimecards() {
    ArrayList list = new ArrayList();
    /** 以下、ユーザーごとの処理 */
    for (int i = 0; i < userList.size(); i++) {
      ALEipUser eipUser = (ALEipUser) userList.get(i);
      int user_id = Integer.parseInt(eipUser.getUserId().getValueAsString());
      ArrayList userlist = (ArrayList) usermap.get(user_id);
      ExtTimecardSummaryResultData summary_rd = new ExtTimecardSummaryResultData();
      int work_day = 0, overtime_day = 0, off_day = 0;
      /** 就業、残業、休出日数 */
      float work_hour = 0, overtime_hour = 0, off_hour = 0;
      /** 就業、残業、休出時間 */
      int late_coming_day = 0, early_leaving_day = 0, absent_day = 0;
      /** 遅刻、早退、欠勤 */
      int paid_holiday = 0, compensatory_holiday = 0;
      /** 有休、代休 */
      int other_day = 0, no_input = 0;
      /** その他、未入力 */
      summary_rd.initField();

      /** タイムカード設定を取得 */
      EipTExtTimecardSystem timecard_system = ExtTimecardUtils
          .getEipTExtTimecardSystemByUserId((Integer) user_id);

      /**
       * userlistにはユーザー日ごとのタイムカードのResultDataがリストで入っているため、
       * ListResultDataに代入して各日数・時間を計算させる。
       */
      if (userlist != null) {
        for (Object obj : userlist) {
          ExtTimecardResultData rd = (ExtTimecardResultData) obj;
          ExtTimecardListResultData lrd = new ExtTimecardListResultData();
          lrd.initField();
          lrd.setDate(rd.getPunchDate().getValue());
          lrd.setRd(rd);
          lrd.setTimecardSystem(timecard_system);
          String type = rd.getType().getValue();
          if (type.equals(EipTExtTimecard.TYPE_WORK)) {
            /** 出勤 */
            if (lrd.getWorkHour() != ExtTimecardListResultData.NO_DATA) {
              work_day++;
              work_hour += lrd.getWorkHourWithoutRestHour();
            }
            /** 残業 */
            if (lrd.getOvertimeHour() > 0) {
              overtime_day++;
              overtime_hour += lrd.getOvertimeHourWithoutRestHour();
            }
            /** 休出 */
            if (lrd.getOffHour() != ExtTimecardListResultData.NO_DATA) {
              off_day++;
              off_hour += lrd.getOffHour();
            }
            /** 遅刻 */
            if (lrd.isLateComing()) {
              late_coming_day++;
            }
            /** 早退 */
            if (lrd.isEarlyLeaving()) {
              early_leaving_day++;
            }
          } else if (type.equals(EipTExtTimecard.TYPE_ABSENT)) {
            /** 欠勤 */
            absent_day++;
          } else if (type.equals(EipTExtTimecard.TYPE_HOLIDAY)) {
            /** 有休 */
            paid_holiday++;
          } else if (type.equals(EipTExtTimecard.TYPE_COMPENSATORY)) {
            /** 代休 */
            compensatory_holiday++;
          } else if (type.equals(EipTExtTimecard.TYPE_ETC)) {
            /** その他 */
            other_day++;
          }
        }
      } else {
        /** 未入力数の計算のため、長さ0のArrayListをつくる */
        userlist = new ArrayList();
      }

      /** 未入力数を月日数からデータ数を引いて計算する */
      Calendar cal = Calendar.getInstance();
      cal.setTime(getViewMonth().getValue());
      no_input = cal.getActualMaximum(Calendar.DAY_OF_MONTH) - userlist.size();

      /** ユーザーごとの合計をSummaryResultDataに代入する */
      work_hour = ExtTimecardUtils.roundHour(work_hour);
      overtime_hour = ExtTimecardUtils.roundHour(overtime_hour);
      off_hour = ExtTimecardUtils.roundHour(off_hour);

      summary_rd.setUserName(eipUser.getAliasName().getValue());
      summary_rd.setSystemName(timecard_system.getSystemName());
      summary_rd.setWorkDayHour(work_day, work_hour);
      summary_rd.setOvertimeDayHour(overtime_day, overtime_hour);
      summary_rd.setOffDayHour(off_day, off_hour);
      summary_rd.setLateComingDay(late_coming_day);
      summary_rd.setEarlyLeavingDay(early_leaving_day);
      summary_rd.setAbsentDay(absent_day);
      summary_rd.setPaidHoliday(paid_holiday);
      summary_rd.setCompensatoryHoliday(compensatory_holiday);
      summary_rd.setOtherDay(other_day);
      summary_rd.setNoInput(no_input);

      list.add(summary_rd);
    }
    return list;
  }

  /**
   * @return
   */
  public List getDateListKeys() {
    try {
      List list = new ArrayList();
      Set set = datemap.keySet();
      Iterator iter = set.iterator();
      while (iter.hasNext()) {
        list.add(iter.next());
      }

      if (list.size() > 1) {
        for (int i = 0; i < list.size() - 1; i++) {
          ExtTimecardSummaryResultData listrd1 = (ExtTimecardSummaryResultData) datemap
              .get(list.get(i));
          ExtTimecardSummaryResultData listrd2 = (ExtTimecardSummaryResultData) datemap
              .get(list.get(i + 1));
          int listrd1_size = listrd1.getList().size();
          if (listrd1_size > 0) {
            ExtTimecardResultData listrd1_lastrd = (ExtTimecardResultData) listrd1
                .getList().get(listrd1_size - 1);

            ExtTimecardResultData listrd2_firstrd = (ExtTimecardResultData) listrd2
                .getList().get(0);
            if (ExtTimecardUtils.WORK_FLG_OFF.equals(listrd2_firstrd
                .getWorkFlag().getValue())
                && ExtTimecardUtils.WORK_FLG_ON.equals(listrd1_lastrd
                    .getWorkFlag().getValue())
                && !sameDay(listrd1_lastrd.getWorkDate().getValue(),
                    listrd2_firstrd.getWorkDate().getValue())) {

              Date d = listrd2_firstrd.getWorkDate().getValue();
              Calendar cal = Calendar.getInstance();
              cal.setTime(d);
              cal.set(Calendar.HOUR_OF_DAY, 0);
              cal.set(Calendar.MINUTE, 0);

              ExtTimecardResultData dummyrd = new ExtTimecardResultData();
              dummyrd.initField();
              dummyrd.setWorkFlag(ExtTimecardUtils.WORK_FLG_DUMMY);
              dummyrd.setWorkDate(cal.getTime());

              listrd1.addExtTimecardResultData(dummyrd);
            }
          }
        }
      }
      return list;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * @param date_str
   * @return
   */
  public ExtTimecardSummaryResultData getDateListValue(String date_str) {
    return (ExtTimecardSummaryResultData) datemap.get(date_str);
  }

  /**
   * 指定した2つの日付を比較する．
   *
   * @param date1
   * @param date2
   * @param checkTime
   *          時間まで比較する場合，true．
   * @return 等しい場合，0. date1>date2の場合, 1. date1 <date2の場合, 2.
   */
  private boolean sameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);

    int date1Year = cal1.get(Calendar.YEAR);
    int date1Month = cal1.get(Calendar.MONTH) + 1;
    int date1Day = cal1.get(Calendar.DATE);
    int date2Year = cal2.get(Calendar.YEAR);
    int date2Month = cal2.get(Calendar.MONTH) + 1;
    int date2Day = cal2.get(Calendar.DATE);

    if (date1Year == date2Year && date1Month == date2Month
        && date1Day == date2Day) {
      return true;
    }
    return false;
  }

  /**
   * userListを設定する．
   *
   */
  public void setuserList(String target_group_name) {
    this.target_group_name = target_group_name;
    if ((this.target_group_name != null)
        && (!this.target_group_name.equals(""))
        && (!this.target_group_name.equals("all"))
        && (!this.target_group_name.equals("only"))) {
      userList = ALEipUtils.getUsers(target_group_name);
    } else if (this.target_group_name.equals("all")
        || this.target_group_name.equals("only")) {
      userList = getUserList(Integer.parseInt(userid));
    } else {
      userList = ALEipUtils.getUsers("LoginUser");
    }
  }

  /**
   * 表示切り替え時に指定するグループ名
   *
   * @return
   */
  public String getTargetGroupName() {
    return target_group_name;
  }

  /**
   * 表示切り替え時に指定するユーザ ID
   *
   * @return
   */
  public String getTargetUserId() {
    return target_user_id;
  }

  /**
   * 指定グループに属するユーザの一覧を取得する．
   *
   * @param groupname
   * @return
   */
  public List getUsers() {
    if (hasAclSummaryOther) {
      return userList;
    } else {
      try {
        List users = new ArrayList();
        users.add(ALEipUtils.getALEipUser(Integer.parseInt(userid)));
        return users;
      } catch (Exception e) {
        return null;
      }
    }
  }

  /**
   * 部署の一覧を取得する．
   *
   * @return
   */
  public Map getPostMap() {
    if (hasAclSummaryOther) {
      return ALEipManager.getInstance().getPostMap();
    } else {
      return null;
    }
  }

  /**
   * My グループの一覧を取得する．
   *
   * @return
   */
  public List getMyGroupList() {
    if (hasAclSummaryOther) {
      return myGroupList;
    } else {
      return null;
    }
  }

  /**
   * ログインユーザの ID を取得する．
   *
   * @return
   */
  public String getUserId() {
    return userid;
  }

  /**
   *
   * @param id
   * @return
   */
  public boolean isMatch(int id1, long id2) {
    return id1 == (int) id2;
  }

  /**
   * @return
   */

  public String getNowTime() {
    return nowtime;
  }

  /**
   *
   * @param minute
   * @return
   */
  private String minuteToHour(long minute) {
    BigDecimal decimal = new BigDecimal(minute / 60.0);
    DecimalFormat dformat = new DecimalFormat("##.#");
    String str = dformat.format(decimal.setScale(1, BigDecimal.ROUND_FLOOR)
        .doubleValue());
    return str;
  }

  /**
   * アクセス権限チェック用メソッド。<br />
   * アクセス権限の機能名を返します。
   *
   * @return
   */
  public String getAclPortletFeature() {
    return aclPortletFeature;
  }

  /**
   * 表示開始日時を取得します。
   *
   * @return
   */
  public ALDateTimeField getViewStart() {
    return viewStart;
  }

  /**
   * 表示終了日時を取得します。
   *
   * @return
   */
  public ALDateTimeField getViewEnd() {
    return viewEnd;
  }

  /**
   * 表示終了日時 (Criteria) を取得します。
   *
   * @return
   */
  public ALDateTimeField getViewEndCrt() {
    return viewEndCrt;
  }

  /**
   * 前の月を取得します。
   *
   * @return
   */
  public ALDateTimeField getPrevMonth() {
    return prevMonth;
  }

  /**
   * 次の月を取得します。
   *
   * @return
   */
  public ALDateTimeField getNextMonth() {
    return nextMonth;
  }

  /**
   * 今月を取得します。
   *
   * @return
   */
  public ALDateTimeField getCurrentMonth() {
    return currentMonth;
  }

  /**
   * 現在の月を取得します。
   *
   * @return
   */
  public ALDateTimeField getViewMonth() {
    return viewMonth;
  }

  /**
   * 今日を取得します。
   *
   * @return
   */
  public ALDateTimeField getToday() {
    return today;
  }

  public boolean hasAclXlsExport() {
    return hasAclXlsExport;
  }

  /**
   * 出勤時刻を取得します。
   *
   * @return
   */
  public ALDateTimeField getWorkStart() {
    return workStart;
  }

  /**
   * 退勤時刻を取得します。
   *
   * @return
   */
  public ALDateTimeField getWorkEnd() {
    return workEnd;
  }

  //Add 20110624 小濵 Start ---
  /**
   * 所定内時間を取得します。
   *
   * @return
   */
  public double getWorktimeIn() {
    return worktimeIn;
  }

  /**
   * 普通残業時間を取得します。
   *
   * @return
   */
  public double getWorktimeOut_usually() {
    return worktimeOut_usually;
  }

  /**
   * 深夜残業時間を取得します。
   *
   * @return
   */
  public double getWorktimeOut_midnight() {
    return worktimeOut_midnight;
  }
  //Add 20110624 小濵 End -----

  /**
   * 予定を取得します。
   *
   * @return
   */
  public String getPlan() {
    String plan = "";
    StringBuffer statement = new StringBuffer();
    statement.append(" SELECT");
    statement.append("      eip_t_ext_timecard.plan");
    statement.append(" FROM eip_t_ext_timecard");
    statement.append(" WHERE");
    statement.append("      eip_t_ext_timecard.user_id ='" + target_user_id + "'");
    statement.append(" AND");
    statement.append("      eip_t_ext_timecard.punch_date ='" + viewMonth + "'");
    String query = statement.toString();
    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
      .getDataContext();
      SQLTemplate rawSelect = new SQLTemplate(EipTExtTimecard.class, query,
          true);
      rawSelect.setFetchingDataRows(true);
      List list = dataContext.performQuery(rawSelect);
      for(int i = 0;i < list.size();i++){
        if(list.get(i) != null)
          if((String)ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.PLAN_COLUMN) != null)
            plan = (String)ALEipUtils.getObjFromDataRow((DataRow)list.get(0),EipTExtTimecard.PLAN_COLUMN);
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return null;
    }
    return plan;
  }
  /* 20100823 小濵 Start */  
  public boolean getRecord_flag() {
    return record_flag;
  }
  /* 20100823 小濵 End */

  /* 20100824 小濵 Start */
  public void setProgress(RunData rundata, Context context){
    DataContext dataContext = DatabaseOrmService.getInstance().getDataContext();
    SelectQuery query = new SelectQuery(ImwTDutyStatus.class);
    Expression exp1 = ExpressionFactory.matchExp(
        ImwTDutyStatus.DISABLED_PROPERTY, "F");
    Expression exp2 = ExpressionFactory.greaterOrEqualExp(
        ImwTDutyStatus.PROGRESS_PROPERTY, "1");
    query.setQualifier(exp1);
    query.andQualifier(exp2);
    List slist = dataContext.performQuery(query);
    for(int i = 0;i < slist.size();i++){
      ImwTDutyStatus imwTDutyStatus = (ImwTDutyStatus) slist.get(i);
      if(imwTDutyStatus.getDutyStatusDate().getYear() == viewMonth.getValue().getYear()
          && imwTDutyStatus.getDutyStatusDate().getMonth() == viewMonth.getValue().getMonth()){
        progress = false;
        break;
      }
    }
  }
  
  public boolean getProgress() {
    return progress;
  }

  /* 20100824 小濵 End */

  /** Add 2010/10/05 金廣 Start--- */
  // 課題58:他人の作業入力修正
  public boolean hasAclOther(){
    return hasAclOther;
  }
  /** Add 2010/10/05 金廣 End----- */
  /** Add 2011/06/23 小濵 Start--- */
  public double[] getWorktime(ALDateTimeField startTime, ALDateTimeField endTime,Integer systemId){
    //戻り値を初期化する　1つ目:所定内　2つ目:普通残業　3つ目:深夜残業
    double[] retHour = {0,0,0};
    DataContext dataContext = DatabaseOrmService.getInstance().getDataContext();
    SelectQuery query = new SelectQuery(ImwTExtTimecardSystemDetail.class);
    Expression exp1 = ExpressionFactory.matchDbExp(
        ImwTExtTimecardSystemDetail.SYSTEM_ID_COLUMN,systemId);
    query.setQualifier(exp1);
    query.addOrdering(ImwTExtTimecardSystemDetail.START_HOUR_PROPERTY,true);
    query.addOrdering(ImwTExtTimecardSystemDetail.START_MINUTE_PROPERTY,true);
    List list = dataContext.performQuery(query);
    if(list == null ||list.size() == 0){
      return retHour;
    }
    EipTExtTimecardSystem sys = ExtTimecardUtils.getEipTExtTimecardSystemByUserId(
        systemId);
    for(int i = 0;i < list.size();i++){
      ImwTExtTimecardSystemDetail imwTExtTimecardSystemDetail = 
        (ImwTExtTimecardSystemDetail)list.get(0);
      if(!(imwTExtTimecardSystemDetail.getStartHour() >= sys.getChangeHour())){
        list.add(imwTExtTimecardSystemDetail);
        list.remove(0);
      }else{
        break;
      }
    }
    //出勤時刻を分に変換
    int startMinute = (Integer.parseInt(startTime.getHour()) * 60) 
      + Integer.parseInt(startTime.getMinute());
    if(!(Integer.parseInt(startTime.getHour()) >= sys.getChangeHour()))
        startMinute += (24 * 60);
    //出勤時刻を分に変換
    int endMinute = (Integer.parseInt(endTime.getHour()) * 60) 
      + Integer.parseInt(endTime.getMinute());
    if(!(Integer.parseInt(endTime.getHour()) >= sys.getChangeHour()))
      endMinute += (24 * 60);
    for(int i = 0;i < list.size();i++){
      ImwTExtTimecardSystemDetail obj = (ImwTExtTimecardSystemDetail)list.get(i);
      int minute = obj.getWorktime();
      //対象の勤務形態明細の作業開始時刻を分に変換
      int sysStartMinute = (obj.getStartHour() * 60) + obj.getStartMinute();
      if(!(obj.getStartHour() >= sys.getChangeHour()))
        sysStartMinute += (24 * 60);
      //対象の勤務形態明細の作業終了時刻を分に変換
      int sysEndMinute = (obj.getEndHour() * 60) + obj.getEndMinute();
      if(!(obj.getEndHour() >= sys.getChangeHour()))
        sysEndMinute += (24 * 60);
      if(sysEndMinute <= startMinute){
        continue;
      }
      if(sysStartMinute >= endMinute){
        break;
      }
      if(startMinute > sysStartMinute){
        minute -= (startMinute - sysStartMinute);
      }
      if(endMinute < sysEndMinute){
        minute -= (sysEndMinute - endMinute);
      }
      
      switch(Integer.parseInt(obj.getWorktimeStatus())){
        case 0:
        case 1:
          retHour[0] += minute;
          break;
        
        case 2:
          retHour[1] += minute;
          break;
          
        case 3:
          retHour[2] += minute;
          break;
          
        default:
      }
    }
    retHour[0] /= 60;
    retHour[1] /= 60;
    retHour[2] /= 60;
    
    return retHour;
  }
  /** Add 2010/06/23 小濵 End----- */

}
