package com.reallinkbilling.login;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class LogOutAction extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1104285256434721989L;
	private SessionMap<String,Object> sessionMap; 
	
	public String execute()
	{	
		setSession(sessionMap);
	
		 if(sessionMap!=null){  
			 //System.out.println("In Invalidate Session !!!");
		        
		        
		    	Connection connection = null;
				PreparedStatement preparedStatement= null;
				
				String sql = "";
				String userName = (String) sessionMap.get("username");
				
				String schema = "";
				schema = "rlbilling";
				
				try {
					
					connection = new GetDBConnection().getDBConnection(schema);
					if(connection != null)
					{
						if(userName != null)
						{
						 sql= "update users "
							  		+ "set accountstatus='UNLOCKED' where username='"+userName.trim()+"'";
							  preparedStatement = connection.prepareStatement(sql);
							  preparedStatement.executeUpdate();
						}
							 
					}
				} catch (SQLException e) {
					addActionError(e.getErrorCode()+ "| Connection Error !!!");
				
				}catch (IOException e) {
					addActionError(e.getMessage()+ "| Connection Error !!!");
				}
				finally
				{
					if(preparedStatement != null)
					{
						try {
							
							preparedStatement.close();
						} catch (SQLException e) {
							
						}
					}
					if(connection != null)
					{
						try {
							
							connection.close();
						} catch (SQLException e) {
							
							e.printStackTrace();
						}
					}
					
					 sessionMap.invalidate();
				
			}
		    }  
		return SUCCESS;
	}

	public void setSession(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 sessionMap=(SessionMap<String, Object>)map;  
		
	}

}
