package kcg.faics.sec;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kcg.faics.cmmn.service.impl.LogMapper;
import kcg.faics.sec.service.impl.SecurityMapper;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

import egovframework.rte.fdl.security.userdetails.EgovUserDetails;

/** 
 * LogoutSuccessHandler.java
 * @author 임새미 
 * @since 2016. 11. 11.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 11. 11. 	    임새미				       최초생성
 * 
 */
public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
	
	private String successUrl = "/loginView.do";
	
	public void setSuccessUrl(String successUrl){
		this.successUrl = successUrl;
	}
	
	@Resource(name = "logMapper")
	LogMapper logMapper;
	
	@Resource(name = "securityMapper")
	SecurityMapper securityMapper;
	
	 @Override
	    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

	        if (authentication != null) {
	        	Object principal = authentication.getPrincipal();
	            if (principal instanceof EgovUserDetails) {
	            	EgovUserDetails details = (EgovUserDetails) principal;
	            	LoginUserVO user = (LoginUserVO) details.getEgovUserVO();
	            	logMapper.insertLoginoutLog(user, 2);
	            	
	            	user.setLoginchk("N");
	    			securityMapper.updateLoginInfo(user);
	            }
	        }

	        setDefaultTargetUrl(successUrl);
	        super.onLogoutSuccess(request, response, authentication);       
	    }
}
