/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import LoginDb.DbHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 *
 * @author 981898
 */
public class LogOutDao {

    public int removeFromLoggedInUsers(String sessionId, String userId, int temp) {
        Connection con = null;
        StringBuilder userLoginDetailsRemovequery = null;
        PreparedStatement ps = null;
        int res = 0;
        try {
            con = DbHandler.getDBConnection();
            if (temp == 0) {
                userId = "tomcatDownEventUsers";
                sessionId = "ALL";
                userLoginDetailsRemovequery = new StringBuilder("Delete  from logged_in_users");
            } else if (temp == 1 || temp == 2) {
                if (temp == 2) {
                    userId = "tomcatSessionOutUsers";
                }

                userLoginDetailsRemovequery = new StringBuilder("Delete  from logged_in_users where session_id=?");
            }
            ps = con.prepareStatement(userLoginDetailsRemovequery.toString());
            
            if (temp == 1 || temp == 2) {
                ps.setString(1, sessionId);
            }


            res = ps.executeUpdate();
            if (res != 0) {
                System.out.println("User removed from on logout logged_in_users for session Id :" + sessionId + " and user Id :" + userId);
            } else {
                System.out.println("No user login details found in logged_in_users on logout for session Id :" + sessionId + " and user Id :" + userId);
            }

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured removing  details in logged_in_users for session Id :" + sessionId + " and user Id :" + userId);
            System.out.println("Error occurred during processing.");
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                userLoginDetailsRemovequery = null;
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }
        return res;
    }
}
