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

package org.common.filter;

import Dao.LoginDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 *
 * @author 1004242
 */
public class LoginFilter implements Filter {

    public void init(FilterConfig fc) throws ServletException {
        
    }

    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {

        HttpServletRequest httpReq = (HttpServletRequest) sr;
        HttpServletResponse httpResp = (HttpServletResponse) sr1;
        HttpSession session = httpReq.getSession(false);
        String sessionId = session.getId();
       // String pacsId = httpReq.getParameter("PACSID");
        PrintWriter out=sr1.getWriter();
        LoginDao ldao = new LoginDao();
        if(ldao.checkIfUserLoggedIn(sessionId, pacsId, 0))
        {
            out.print("Please close the browser and open a new one to proceed");
          
        }
        else
        {
            fc.doFilter(sr, sr1);
        }

       
    }

    public void destroy() {
        
    }

}
