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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.cayenne.access.DataContext;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.cayenne.om.portlet.ImwTOffice;
import com.aimluck.eip.common.ALAbstractFormData;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALPageNotFoundException;
import com.aimluck.eip.modules.actions.common.ALAction;
import com.aimluck.eip.office.util.OfficeUtils;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.util.ALEipUtils;

public class OfficeDisable extends ALAbstractFormData {

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

  private String mode;

  /**
   * 
   * @param mode
   */
  public void setMode(String mode) {
    this.mode = mode;
  }

  /**
   * 
   * @return
   */
  public String getMode() {
    return mode;
  }
  
  
  private DataContext dataContext;

  /** 勤務先ID */
  private int officeId;

  /** 勤務先名 */
  private ALStringField officeName;

  /** 有効/無効 */
  private ALStringField disabled;

  /** */
  private List officeList;

  /** 所属メンバー */
  private ArrayList memberList;

  private ALStringField group_name;

  /** */
  private boolean is_join_member = true;

  /**
   * 『勤務先』を削除します。 <BR>
   * このとき勤務先に関連づけられているグループも削除します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#deleteFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context, java.util.ArrayList)
   */
  protected boolean deleteFormData(RunData rundata, Context context,
      ArrayList msgList) throws ALPageNotFoundException, ALDBErrorException {
    
    return true;
  }

  /**
   * 『勤務先』を追加します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#insertFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context, java.util.ArrayList)
   */
  protected boolean insertFormData(RunData rundata, Context context,
      ArrayList msgList) throws ALPageNotFoundException, ALDBErrorException {
    
    return true;
  }

  /**
   * 『勤務先』を読み込みます。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#loadFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context, java.util.ArrayList)
   */
  protected boolean loadFormData(RunData rundata, Context context,
      ArrayList msgList) throws ALPageNotFoundException, ALDBErrorException {
    
    return true;
  }


  /**
   * 各フィールドに対する制約条件を設定します。 <BR>
   * 
   * @see com.aimluck.eip.common.ALAbstractFormData#setValidator()
   */
  protected void setValidator() throws ALPageNotFoundException,
      ALDBErrorException {
    
    
  }

  /**
   * 『勤務先』を更新します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#updateFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context, java.util.ArrayList)
   */
  protected boolean updateFormData(RunData rundata, Context context,
      ArrayList msgList) throws ALPageNotFoundException, ALDBErrorException {
    try {
      // オブジェクトモデルを取得
      ImwTOffice record = OfficeUtils.getImwTOffice(rundata, context);
      if (record == null)
        return false;

      DataContext dataContext = DatabaseOrmService.getInstance()
          .getDataContext();
      if (ALEipConstants.MODE_Disable.equals(mode)) {
        
        List office = OfficeUtils.getOfficeUsers(record.getOfficeName()); 
        int officesize = office.size();
        if (officesize > 0) {
          msgList.add("勤務先に所属のユーザーが存在します");
          return false;
        }
        record.setDisabled(ALEipConstants.USER_STAT_NUTRAL);
    
        record.setUpdateDate(new Date());
    
        record.setUpdateUserId(ALEipUtils.getUserId(rundata));
    
      } else if (ALEipConstants.MODE_Enable.equals(mode)) {
        //有効にするボタンを押す場合、有効（"F"）にする
        
        record.setUpdateDate(new Date());
        
        record.setUpdateUserId(ALEipUtils.getUserId(rundata));
        
        record.setDisabled(ALEipConstants.USER_STAT_ENABLED);
      }

      dataContext.commitChanges();

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

  /**
   * フォームに入力されたデータの妥当性検証を行います。 <BR>
   * 
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#validate(java.util.ArrayList)
   */
  protected boolean validate(ArrayList msgList) throws ALPageNotFoundException,
      ALDBErrorException {
    
    return (msgList.size() == 0);
  }

  /**
   * 初期化します。
   * 
   * @param action
   * @param rundata
   * @param context
   * @see com.aimluck.eip.common.ALAbstractFormData#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 {
    super.init(action, rundata, context);
  }
  
  /**
   * 各フィールドを初期化します。 <BR>
   * 
   * @see com.aimluck.eip.common.ALData#initField()
   */
  public void initField() {
    dataContext = DatabaseOrmService.getInstance().getDataContext();
    
  }
  
  /**
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return
   * @see com.aimluck.eip.common.ALAbstractFormData#setFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context, java.util.ArrayList)
   */
  protected boolean setFormData(RunData rundata, Context context,
      ArrayList msgList) throws ALPageNotFoundException, ALDBErrorException {
    boolean res = super.setFormData(rundata, context, msgList);

    return res;
  }
  
  /**
   * 勤務先名を取得します。 <BR>
   * 
   * @return
   */
  public ALStringField getOfficeName() {
    return officeName;
  }

  /**
   * 勤務先IDを取得します。 <BR>
   * 
   * @return
   */
  public int getOfficeId() {
    return officeId;
  }

  public List getOfficeList() {
    return officeList;
  }

  /**
   * 有効/無効フラグを取得します。 <BR>
   * 
   * @return
   */
  public ALStringField getDisabled() {
    return disabled;
  }
  
  public ArrayList getMemberList() {
    return memberList;
  }

  public ALStringField getGroupName() {
    return group_name;
  }

  /**
   * 
   * @param bool
   */
  public void setJoinMember(boolean bool) {
    is_join_member = bool;
  }

  /**
   * 
   * @return
   */
  public boolean isJoinMember() {
    return is_join_member;
  }

}
