package WEB-INF.classes.wenrgise.common.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.webtier.action.BaseAction;

public class LogOutAction extends BaseAction {
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    Cookie cookieEmpId = null;
    Cookie cookieSiteId = null;
    String target = "success";
    String sEmpId = null;
    String sSiteId = null;
    cookieEmpId = new Cookie("EmpId", sEmpId);
    cookieSiteId = new Cookie("SiteId", sSiteId);
    setCookieAttributes(cookieEmpId, false);
    setCookieAttributes(cookieSiteId, false);
    response.addCookie(cookieEmpId);
    response.addCookie(cookieSiteId);
    System.out.println("The cookie has been created");
    HttpSession session = request.getSession();
    session.setAttribute("status", "LOGOUT");
    return mapping.findForward("success");
  }
  
  private void setCookieAttributes(Cookie c, boolean bValid) {
    c.setPath("/");
    if (bValid) {
      c.setMaxAge(1800);
    } else {
      c.setMaxAge(0);
    } 
  }
}
