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

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

import org.apache.cayenne.DataObjectUtils;
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.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALNumberField;
import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.accessctl.util.AccessControlUtils;
import com.aimluck.eip.cayenne.om.account.EipTAclPortletFeature;
import com.aimluck.eip.cayenne.om.account.EipTAclRole;
import com.aimluck.eip.cayenne.om.account.EipTAclUserRoleMap;
import com.aimluck.eip.cayenne.om.security.TurbineUser;
import com.aimluck.eip.common.ALAbstractFormData;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALEipUser;
import com.aimluck.eip.common.ALPageNotFoundException;
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.util.ALEipUtils;

/**
 * アクセスコントロールのフォームデータを管理するクラスです。 <BR>
 * 
 */
public class AccessControlFormData extends ALAbstractFormData {

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

  /** ロールID */
  private String acl_role_id;

  /** ロール名 */
  private ALStringField acl_role_name;

  /** 機能 */
  private ALNumberField feature_id;

  /** メモ */
  private ALStringField note;

  private ALNumberField acllist;

  private ALNumberField acldetail;

  private ALNumberField aclinsert;

  private ALNumberField aclupdate;

  private ALNumberField acldelete;

  private ALNumberField aclexport;

  /** <code>memberList</code> メンバーリスト */
  private ArrayList memberList;

  private int defineAclType;

  /** 機能一覧 */
  private List portletFeatureList;

  private DataContext dataContext;

  /**
   * 
   * @param rundata
   * @param context
   */
  public void loadPortletFeatureList(RunData rundata, Context context) {
    portletFeatureList = AccessControlUtils.getPortletFeatureList();
  }

  /**
   * 
   * @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);

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

  /**
   * 各フィールドを初期化します。 <BR>
   * 
   * @see com.aimluck.eip.common.ALData#initField()
   */
  public void initField() {
    // ロール名
    acl_role_name = new ALStringField();
    acl_role_name.setFieldName("ロール名");
    acl_role_name.setTrim(true);

    // 機能
    feature_id = new ALNumberField();
    feature_id.setFieldName("ロール名");

    // メモ
    note = new ALStringField();
    note.setFieldName("メモ");
    note.setTrim(false);

    acllist = new ALNumberField();
    acldetail = new ALNumberField();
    aclinsert = new ALNumberField();
    aclupdate = new ALNumberField();
    acldelete = new ALNumberField();
    aclexport = new ALNumberField();

    // メンバーリスト
    memberList = new ArrayList();
  }

  /**
   * ロールの各フィールドに対する制約条件を設定します。 <BR>
   * 
   * @see com.aimluck.eip.common.ALAbstractFormData#setValidator()
   */
  protected void setValidator() {
    // ロール名必須項目
    acl_role_name.setNotNull(true);
    // ロール名の文字数制限
    acl_role_name.limitMaxLength(50);

    // 機能
    feature_id.setNotNull(true);

    // メモの文字数制限
    note.limitMaxLength(1000);

    acllist.limitValue(0, 1);
    acldetail.limitValue(0, 1);
    aclinsert.limitValue(0, 1);
    aclupdate.limitValue(0, 1);
    acldelete.limitValue(0, 1);
    aclexport.limitValue(0, 1);
  }

  /**
   * 
   * @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);
    try {
      if (res) {
        if (ALEipConstants.MODE_UPDATE.equals(getMode())) {
          acl_role_id = ALEipUtils.getTemp(rundata, context,
              ALEipConstants.ENTITY_ID);
        }

        String[] str = rundata.getParameters().getStrings("member_to");
        if (str != null && str.length > 0) {
          SelectQuery query = new SelectQuery(TurbineUser.class);
          Expression exp = ExpressionFactory.inExp(
              TurbineUser.LOGIN_NAME_PROPERTY, str);
          query.setQualifier(exp);
          memberList.addAll(ALEipUtils.getUsersFromSelectQuery(query));
        }
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
      res = false;
    }

    return res;
  }

  /**
   * フォームに入力されたデータの妥当性検証を行います。 <BR>
   * 
   * @param msgList
   * @return TRUE 成功 FALSE 失敗
   * @see com.aimluck.eip.common.ALAbstractFormData#validate(java.util.ArrayList)
   */
  protected boolean validate(ArrayList msgList) {
    String tmp_acl_role_name = acl_role_name.getValue();
    if (tmp_acl_role_name != null && !"".equals(tmp_acl_role_name)) {
      // ロール名の重複をチェックする
      try {
        SelectQuery query = new SelectQuery(EipTAclRole.class);
        if (ALEipConstants.MODE_INSERT.equals(getMode())) {
          Expression exp = ExpressionFactory.matchExp(
              EipTAclRole.ROLE_NAME_PROPERTY, tmp_acl_role_name);
          query.setQualifier(exp);
        } else if (ALEipConstants.MODE_UPDATE.equals(getMode())) {
          Expression exp1 = ExpressionFactory.matchExp(
              EipTAclRole.ROLE_NAME_PROPERTY, tmp_acl_role_name);
          query.setQualifier(exp1);
          Expression exp2 = ExpressionFactory.noMatchDbExp(
              EipTAclRole.ROLE_ID_PK_COLUMN, Integer.valueOf(acl_role_id));
          query.andQualifier(exp2);
        }

        if (dataContext.performQuery(query).size() != 0) {
          msgList.add("ロール名『 <span class='em'>" + acl_role_name.toString()
              + "</span> 』は既に登録されています。");
        }
      } catch (Exception ex) {
        logger.error("Exception", ex);
        return false;
      }
    }

    // ロール名
    acl_role_name.validate(msgList);
    // メモ
    note.validate(msgList);

    acllist.validate(msgList);
    acldetail.validate(msgList);
    aclinsert.validate(msgList);
    aclupdate.validate(msgList);
    acldelete.validate(msgList);
    aclexport.validate(msgList);

    // アクセス権限
    if (acllist.getValue() == 0 && acldetail.getValue() == 0
        && aclinsert.getValue() == 0 && aclupdate.getValue() == 0
        && acldelete.getValue() == 0 && aclexport.getValue() == 0) {
      msgList.add("アクセス権限を選択してください。");
    }

    // 所属メンバー
    if (memberList.size() == 0) {
      msgList.add("所属メンバーを選択してください。");
    }

    try {
      // 同一機能の他ロールには所属できないようにする

      List uids = new ArrayList();
      int msize = memberList.size();
      for (int i = 0; i < msize; i++) {
        ALEipUser user = (ALEipUser) memberList.get(i);
        uids.add(Integer.valueOf((int) user.getUserId().getValue()));
      }

      SelectQuery rolequery = new SelectQuery(EipTAclRole.class);
      Expression exp11 = ExpressionFactory.matchDbExp(
          EipTAclRole.EIP_TACL_PORTLET_FEATURE_PROPERTY + "."
              + EipTAclPortletFeature.FEATURE_ID_PK_COLUMN, Integer
              .valueOf((int) feature_id.getValue()));
      Expression exp12 = ExpressionFactory.inDbExp(
          EipTAclRole.EIP_TACL_USER_ROLE_MAPS_PROPERTY + "."
              + EipTAclUserRoleMap.TURBINE_USER_PROPERTY + "."
              + TurbineUser.USER_ID_PK_COLUMN, uids);
      rolequery.setQualifier(exp11.andExp(exp12));

      if (ALEipConstants.MODE_UPDATE.equals(getMode())) {
        Expression exp13 = ExpressionFactory.noMatchDbExp(
            EipTAclRole.ROLE_ID_PK_COLUMN, Integer.valueOf(acl_role_id));
        rolequery.andQualifier(exp13);
      }

      if (dataContext.performQuery(rolequery).size() != 0) {
        msgList.add("同一機能の他のロールに所属メンバーがすでに登録されています。");
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return false;
    }

    return (msgList.size() == 0);
  }

  /**
   * ロールをデータベースから読み出します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return TRUE 成功 FALSE 失敗
   * @see com.aimluck.eip.common.ALAbstractFormData#loadFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context)
   */
  protected boolean loadFormData(RunData rundata, Context context,
      ArrayList msgList) {
    try {
      // オブジェクトモデルを取得
      EipTAclRole aclrole = AccessControlUtils.getEipTAclRole(rundata, context);
      if (aclrole == null) {
        return false;
      }

      // ロール名
      acl_role_name.setValue(aclrole.getRoleName());

      List aclUserRoleMaps = AccessControlUtils.getEipTAclUserRoleMaps(aclrole
          .getRoleId().intValue());
      if (aclUserRoleMaps != null && aclUserRoleMaps.size() > 0) {
        EipTAclUserRoleMap rolemap = null;
        TurbineUser tuser = null;
        int size = aclUserRoleMaps.size();
        for (int i = 0; i < size; i++) {
          rolemap = (EipTAclUserRoleMap) aclUserRoleMaps.get(i);
          tuser = rolemap.getTurbineUser();
          ALEipUser user = new ALEipUser();
          user.initField();
          user.setUserId(tuser.getUserId().intValue());
          user.setName(tuser.getLoginName());
          user.setAliasName(tuser.getFirstName(), tuser.getLastName());
          memberList.add(user);
        }
      }

      EipTAclPortletFeature feature = aclrole.getEipTAclPortletFeature();
      feature_id.setValue(feature.getFeatureId().intValue());
      defineAclType = feature.getAclType().intValue();

      // メモ
      note.setValue(aclrole.getNote());

      // アクセス権限
      int tmpAclType = aclrole.getAclType();
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_LIST,
          tmpAclType, acllist);
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_DETAIL,
          tmpAclType, acldetail);
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_INSERT,
          tmpAclType, aclinsert);
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_UPDATE,
          tmpAclType, aclupdate);
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_DELETE,
          tmpAclType, acldelete);
      AccessControlUtils.setupAcl(ALAccessControlConstants.VALUE_ACL_EXPORT,
          tmpAclType, aclexport);

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

  /**
   * ロールをデータベースから削除します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return TRUE 成功 FALSE 失敗
   * @see com.aimluck.eip.common.ALAbstractFormData#deleteFormData(org.apache.turbine.util.RunData,
   *      org.apache.velocity.context.Context)
   */
  protected boolean deleteFormData(RunData rundata, Context context,
      ArrayList msgList) {
    try {

      String aclroleid = ALEipUtils.getTemp(rundata, context,
          ALEipConstants.ENTITY_ID);
      if (aclroleid == null || Integer.valueOf(aclroleid) == null) {
        // IDが空の場合
        logger.debug("[AccessControlUtils] Empty ID...");
        return false;
      }

      Expression exp = ExpressionFactory.matchDbExp(
          EipTAclRole.ROLE_ID_PK_COLUMN, aclroleid);
      SelectQuery query = new SelectQuery(EipTAclRole.class, exp);
      List aclroles = dataContext.performQuery(query);
      if (aclroles == null || aclroles.size() == 0) {
        // 指定したIDのレコードが見つからない場合
        logger.debug("[AccessControlUtils] Not found ID...");
        return false;
      }

      // オブジェクトを削除（Cayenneのカスケード設定でEipTAclUserRoleMapも同時に削除）
      dataContext.deleteObject((EipTAclRole) aclroles.get(0));

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

  /**
   * ロールをデータベースに格納します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return TRUE 成功 FALSE 失敗
   * @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) {
    try {
      Date now = Calendar.getInstance().getTime();

      // 新規オブジェクトモデル
      EipTAclRole aclrole = (EipTAclRole) dataContext
          .createAndRegisterNewObject(EipTAclRole.class);
      aclrole.setRoleName(acl_role_name.getValue());
      aclrole.setNote(note.getValue());

      long aclType = getAclTypeValue();
      aclrole.setAclType(Integer.valueOf((int) aclType));

      EipTAclPortletFeature feature = (EipTAclPortletFeature) DataObjectUtils
          .objectForPK(dataContext, EipTAclPortletFeature.class, Integer
              .valueOf((int) feature_id.getValue()));
      aclrole.setEipTAclPortletFeature(feature);

      // 登録日
      aclrole.setCreateDate(now);
      // 更新日
      aclrole.setUpdateDate(now);

      // userMapの登録
      insertEipTAclUserRoleMap(aclrole, (ALEipUser) memberList.get(0));

      // メンバー登録
      int size = memberList.size();
      for (int i = 1; i < size; i++) {
        insertEipTAclUserRoleMap(aclrole, (ALEipUser) memberList.get(i));
      }

      // ロールを登録
      dataContext.commitChanges();
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return false;
    }
    return true;
  }

  /**
   * データベースに格納されているロールを更新します。 <BR>
   * 
   * @param rundata
   * @param context
   * @param msgList
   * @return TRUE 成功 FALSE 失敗
   * @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) {
    try {
      // オブジェクトモデルを取得
      EipTAclRole aclrole = AccessControlUtils.getEipTAclRole(rundata, context);
      if (aclrole == null) {
        return false;
      }

      aclrole.setRoleName(acl_role_name.getValue());
      aclrole.setNote(note.getValue());
      // 更新日
      aclrole.setUpdateDate(Calendar.getInstance().getTime());

      long aclType = getAclTypeValue();
      aclrole.setAclType(Integer.valueOf((int) aclType));

      EipTAclPortletFeature feature = (EipTAclPortletFeature) DataObjectUtils
          .objectForPK(dataContext, EipTAclPortletFeature.class, Integer
              .valueOf((int) feature_id.getValue()));
      aclrole.setEipTAclPortletFeature(feature);

      // userMapの登録
      int size = memberList.size();
      for (int i = 0; i < size; i++) {
        insertEipTAclUserRoleMap(aclrole, (ALEipUser) memberList.get(i));
      }

      // 古いEipTAclUserRoleMapの削除
      deleteEipTAclUserRoleMap(rundata, context);

      // ロールを更新
      dataContext.commitChanges();
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return false;
    }
    return true;
  }

  private void insertEipTAclUserRoleMap(EipTAclRole aclrole, ALEipUser user) {
    EipTAclUserRoleMap map = (EipTAclUserRoleMap) dataContext
        .createAndRegisterNewObject(EipTAclUserRoleMap.class);
    int userid = (int) user.getUserId().getValue();
    // ユーザーID
    TurbineUser tuser = (TurbineUser) DataObjectUtils.objectForPK(dataContext,
        TurbineUser.class, Integer.valueOf(userid));
    map.setEipTAclRole(aclrole);
    map.setTurbineUser(tuser);
  }

  private boolean deleteEipTAclUserRoleMap(RunData rundata, Context context) {
    String aclroleid = ALEipUtils.getTemp(rundata, context,
        ALEipConstants.ENTITY_ID);
    if (aclroleid == null || Integer.valueOf(aclroleid) == null) {
      // IDが空の場合
      logger.debug("[AccessControlFormData] Empty ID...");
      return false;
    }

    SelectQuery query = new SelectQuery(EipTAclUserRoleMap.class);
    Expression exp = ExpressionFactory.matchDbExp(
        EipTAclUserRoleMap.EIP_TACL_ROLE_PROPERTY + "."
            + EipTAclRole.ROLE_ID_PK_COLUMN, aclroleid);
    query.setQualifier(exp);
    List maps = dataContext.performQuery(query);

    if (maps == null || maps.size() == 0) {
      return true;
    }

    dataContext.deleteObjects(maps);
    return true;
  }

  private long getAclTypeValue() {
    long aclType = acllist.getValue() * ALAccessControlConstants.VALUE_ACL_LIST
        + acldetail.getValue() * ALAccessControlConstants.VALUE_ACL_DETAIL
        + aclinsert.getValue() * ALAccessControlConstants.VALUE_ACL_INSERT
        + aclupdate.getValue() * ALAccessControlConstants.VALUE_ACL_UPDATE
        + acldelete.getValue() * ALAccessControlConstants.VALUE_ACL_DELETE
        + aclexport.getValue() * ALAccessControlConstants.VALUE_ACL_EXPORT;
    return aclType;
  }

  private boolean hasAcl(int defineAclType, int aclType) {
    return ((defineAclType & aclType) == aclType);
  }

  /**
   * ロール名を取得します。 <BR>
   * 
   * @return
   */
  public ALStringField getAclRoleName() {
    return acl_role_name;
  }

  public ALNumberField getFeatureId() {
    return feature_id;
  }

  /**
   * メモを取得します。 <BR>
   * 
   * @return
   */
  public ALStringField getNote() {
    return note;
  }

  public ALNumberField getAclList() {
    return acllist;
  }

  public ALNumberField getAclDetail() {
    return acldetail;
  }

  public ALNumberField getAclInsert() {
    return aclinsert;
  }

  public ALNumberField getAclUpdate() {
    return aclupdate;
  }

  public ALNumberField getAclDelete() {
    return acldelete;
  }

  public ALNumberField getAclExport() {
    return aclexport;
  }

  /**
   * グループメンバーを取得します。
   * 
   * @return
   */
  public List getMemberList() {
    return memberList;
  }

  /**
   * 
   * @return
   */
  public List getPortletFeatureList() {
    return portletFeatureList;
  }

  public int getDefineAclType() {
    return defineAclType;
  }

  public boolean hasAclList() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_LIST);
  }

  public boolean hasAclDetail() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_DETAIL);
  }

  public boolean hasAclInsert() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_INSERT);
  }

  public boolean hasAclUpdate() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_UPDATE);
  }

  public boolean hasAclDelete() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_DELETE);
  }

  public boolean hasAclExport() {
    return hasAcl(defineAclType, ALAccessControlConstants.VALUE_ACL_EXPORT);
  }
}
