/*
 * 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.modules.actions.account;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.account.CellAccountEasyLoginFormData;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALEipUser;
import com.aimluck.eip.modules.actions.common.ALBaseAction;
import com.aimluck.eip.util.ALCellularUtils;
import com.aimluck.eip.util.ALEipUtils;

/**
 * 個人設定用-ユーザ情報用アクションクラス
 * 
 */
public class CellAccountEditAction extends ALBaseAction {

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

  public static final String MODE_CONFIG = "config";

  /**
   * @param portlet
   * @param context
   * @param rundata
   * @throws java.lang.Exception
   * @see org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction#buildNormalContext(org.apache.jetspeed.portal.portlets.VelocityPortlet,
   *      org.apache.velocity.context.Context, org.apache.turbine.util.RunData)
   */
  protected void buildNormalContext(VelocityPortlet portlet, Context context,
      RunData rundata) throws Exception {
    // if (getMode() == null) {
    // doAccountedit_configlogin(rundata, context);
    // }
  }

  /**
   * 最大化表示の際の処理を記述します。 <BR>
   * 
   * @param portlet
   * @param context
   * @param rundata
   */
  protected void buildMaximizedContext(VelocityPortlet portlet,
      Context context, RunData rundata) {

    // MODEを取得
    String mode = rundata.getParameters().getString(ALEipConstants.MODE);
    try {
      String username = null;
      String key = rundata.getParameters().getString("key", "").trim();
      StringTokenizer st = new StringTokenizer(key, "_");
      if (st.countTokens() == 2) {
        username = st.nextToken();
        String base64value = st.nextToken();

        // 入力されたユーザ名を検証する．
        ALStringField tmpname = new ALStringField();
        tmpname.setTrim(true);
        tmpname.setNotNull(true);
        tmpname.setCharacterType(ALStringField.TYPE_ALPHABET_NUMBER);
        tmpname.limitMaxLength(16);
        tmpname.setValue(username);
        boolean valid = tmpname.validate(new ArrayList());

        if (valid) {
          ALEipUser eipuser = ALEipUtils.getALEipUser(username);
          if (eipuser != null) {
            if (!(ALCellularUtils.getCheckValueForCellLogin(username, eipuser
                .getUserId().toString())).equals(base64value)) {
              username = "";
            }
          } else {
            username = "";
          }
        } else {
          username = "";
        }
      }
      context.put("username", username);
      context.put("key", key);

      if (MODE_CONFIG.equals(mode)) {
        doAccountedit_configlogin(rundata, context);
      } else if (ALEipConstants.MODE_INSERT.equals(mode)) {
        doAccountedit_insert(rundata, context);
      }

      if (getMode() == null) {
        doAccountedit_configlogin(rundata, context);
      }
    } catch (Exception ex) {
      logger.error("Exception", ex);
    }
  }

  /**
   * 簡易ログイン設定用のフォームを表示する． <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountedit_configlogin(RunData rundata, Context context)
      throws Exception {
    CellAccountEasyLoginFormData formData = new CellAccountEasyLoginFormData();
    formData.initField();
    formData.doViewForm(this, rundata, context);
    setTemplate(rundata, "accountlogin-config");
  }

  /**
   * 簡易ログイン用に携帯電話の固有番号を登録する． <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountedit_insert(RunData rundata, Context context)
      throws Exception {
    CellAccountEasyLoginFormData formData = new CellAccountEasyLoginFormData();
    formData.initField();
    setTemplate(rundata, "accountlogin-config");
    if (formData.doInsert(this, rundata, context)) {
      // データ登録が成功したとき
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getAction("controls.Restore",
          ALEipUtils.getPortlet(rundata, context).getID()).toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    } else {
      setTemplate(rundata, "accountlogin-config");
    }
  }

  /**
   * 簡易ログイン用に携帯電話の固有番号を削除する． <BR>
   * 
   * @param rundata
   * @param context
   * @throws Exception
   */
  public void doAccountedit_delete(RunData rundata, Context context)
      throws Exception {
    CellAccountEasyLoginFormData formData = new CellAccountEasyLoginFormData();
    formData.initField();
    setTemplate(rundata, "accountlogin-config");
    if (formData.doDelete(this, rundata, context)) {
      // データ登録が成功したとき
      JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
      rundata.setRedirectURI(jsLink.getAction("controls.Restore",
          ALEipUtils.getPortlet(rundata, context).getID()).toString());
      rundata.getResponse().sendRedirect(rundata.getRedirectURI());
      jsLink = null;
    } else {
      setTemplate(rundata, "accountlogin-config");
    }
  }
}
