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

package Controller;

import Dao.LoginDao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 1004242
 */
public class resetLoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String modifierLoginId = (String) session.getAttribute("user");
        String pacsId = (String) session.getAttribute("pacsId");
       // String resetableUserId = request.getParameter("userName");
        LoginDao lDao = new LoginDao();
        boolean result = false;
        int counter = 0;
        List<String> userSessionList = new ArrayList<String>();
        try {
            if (!(modifierLoginId.equalsIgnoreCase(resetableUserId))) {
                result = lDao.verifySamPacsForUser(resetableUserId, pacsId);
                if (result) {
                    userSessionList = lDao.deleteFromLoggedInUser(resetableUserId, pacsId);
                    for (String sessionId : userSessionList) {
                        if (LoginServlet.sesssionMap.containsKey(sessionId)) {
                            HttpSession sessionFromMap = LoginServlet.sesssionMap.get(sessionId);
                            sessionFromMap.invalidate();
                            LoginServlet.sesssionMap.remove(sessionId);
                            counter++;
                        }
                    }
                    System.out.println("No of User login entry deleted from sessionMap :" + counter);
                    if (counter == 0) {
                        request.setAttribute("error", "No active session for the user.");
                        request.getRequestDispatcher("/ResetLogin.jsp").forward(request, response);
                    } else {
                        lDao.insertToResetLoginActivity(modifierLoginId, resetableUserId, pacsId);
                        request.setAttribute("error", "User login succesfully reset. Please login again to check. ");
                        request.getRequestDispatcher("/ResetLogin.jsp").forward(request, response);
                    }
                } else {
                    request.setAttribute("error", "Target User for reset doesnot belong to your pacs.Activity is unauthorized");
                    request.getRequestDispatcher("/ResetLogin.jsp").forward(request, response);
                }
            } else {
                request.setAttribute("error", "Unauthorized activity.You can not force logout your own account.");
                request.getRequestDispatcher("/ResetLogin.jsp").forward(request, response);
            }
        } catch (Exception e) {
           // e.printStackTrace();
           System.out.println("Error occurred during processing.");
            request.setAttribute("error", "Soemthing is wrong. PLease try again.");
            request.getRequestDispatcher("/ResetLogin.jsp").forward(request, response);
        
        } finally {
            System.out.println("Processing done.");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
