/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2011 Aimluck,Inc.
 * http://www.aipo.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aimluck.eip.modules.actions;

import javax.servlet.http.Cookie;

import org.apache.jetspeed.om.security.JetspeedUser;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.turbine.TurbineConstants;
import org.apache.turbine.modules.ActionEvent;
import org.apache.turbine.util.RunData;

import com.aimluck.eip.common.ALEipUser;
import com.aimluck.eip.services.config.ALConfigHandler.Property;
import com.aimluck.eip.services.config.ALConfigService;
import com.aimluck.eip.services.eventlog.ALEventlogFactoryService;
import com.aimluck.eip.util.ALEipUtils;

/**
 * ログアウト処理用のクラスです。 <br />
 * 
 */
public class ALJLogoutUser extends ActionEvent {

  private static final JetspeedLogger logger = JetspeedLogFactoryService
    .getLogger(ALJLogoutUser.class.getName());

  @Override
  public void doPerform(RunData data) throws Exception {

    String username = data.getParameters().getString("user");

    if (JetspeedResources.getBoolean("automatic.logon.enable", false)) {
      Cookie userName = new Cookie("username", "");
      Cookie loginCookie = new Cookie("logincookie", "");

      String comment =
        JetspeedResources.getString("automatic.logon.cookie.comment", "");
      String domain =
        JetspeedResources.getString("automatic.logon.cookie.domain");
      String path =
        JetspeedResources.getString("automatic.logon.cookie.path", "/");

      if (domain == null) {
        String server = data.getServerName();
        domain = "." + server;
      }

      userName.setMaxAge(0);
      userName.setComment(comment);
      userName.setDomain(domain);
      userName.setPath(path);

      loginCookie.setMaxAge(0);
      loginCookie.setComment(comment);
      loginCookie.setDomain(domain);
      loginCookie.setPath(path);

      data.getResponse().addCookie(userName);
      data.getResponse().addCookie(loginCookie);

      if (data.getRequest().getCookies() != null) {
        data.getCookies().remove("logincookie");
        data.getCookies().remove("username");
      }
    }

    if (JetspeedResources.getBoolean("automatic.logout.save", false)) {
      JetspeedSecurity.saveUser((JetspeedUser) data.getUserFromSession());
    }

    JetspeedSecurity.logout();

    if (username != null && !"".equals(username)) {
      ALEipUser logoutuser = ALEipUtils.getALEipUser(username);
      if (logoutuser != null) {
        int logoutUserId = (int) logoutuser.getUserId().getValue();
        ALEventlogFactoryService.getInstance().getEventlogHandler().logLogout(
          logoutUserId);
      }
    }

    data.setMessage(JetspeedResources
      .getString(TurbineConstants.LOGOUT_MESSAGE));

    JetspeedLink jsLink = null;

    data.setScreen(JetspeedResources
      .getString(TurbineConstants.SCREEN_HOMEPAGE));

    try {
      jsLink = JetspeedLinkFactory.getInstance(data);
    } catch (Exception e) {
      logger.error("Error getting jsLink", e);
    }

    String externalLoginUrl = ALConfigService.get(Property.EXTERNAL_LOGIN_URL);
    if ("".equals(externalLoginUrl)) {
      if (ALEipUtils.isCellularPhone(data)) {
        data.setRedirectURI(jsLink
          .getHomePage()
          .addQueryData("logout", "T")
          .toString());
      } else {
        data.setRedirectURI(jsLink.getHomePage().toString());
      }
    } else {
      data.setRedirectURI(externalLoginUrl);
    }

    JetspeedLinkFactory.putInstance(jsLink);
    jsLink = null;
    // セッションの削除
    if (data.getSession() != null) {
      try {
        data.getSession().invalidate();
      } catch (IllegalStateException ex) {
        logger.debug("セッションは既に削除されています。");
      }
    }
  }

}
