/*
 *  Copyright (c) 2009-2014, United States Government, as represented by the Secretary of Health and Human Services.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *      * Neither the name of the United States Government nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE UNITED STATES GOVERNMENT BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package gov.hhs.fha.nhinc.admingui.managed;

import gov.hhs.fha.nhinc.admingui.constant.NavigationConstant;
import gov.hhs.fha.nhinc.admingui.display.DirectDisplayController;
import gov.hhs.fha.nhinc.admingui.jee.jsf.UserAuthorizationListener;
import gov.hhs.fha.nhinc.admingui.model.Login;
import gov.hhs.fha.nhinc.admingui.services.LoginService;
import gov.hhs.fha.nhinc.admingui.services.exception.UserLoginException;
import gov.hhs.fha.nhinc.admingui.services.persistence.jpa.entity.UserLogin;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * The Class LoginBean.
 * 
 * @author sadusumilli
 */
@ManagedBean
@SessionScoped
@Component
public class LoginBean {

    private static final Logger LOG = Logger.getLogger(LoginBean.class);

    /** The user name. */
    private String userName;

    /** The password. */
    private String password;

    /** The is correct. */
    public Boolean isCorrect = false;
    
    private static boolean firstTimeLogged = true; 

    /** The login service. */
    @Autowired
    private LoginService loginService;

    /**
     * Gets the user name.
     * 
     * @return the user name
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Sets the user name.
     * 
     * @param userName the new user name
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * Gets the password.
     * 
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Sets the password.
     * 
     * @param password the new password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Instantiates a new login bean.
     */
    public LoginBean() {

    }

    /**
     * Invoke StatusPrime page upon success login.
     * 
     * @return the string
     */
    public String loginAndNavigate() {
        if (!login()) {
            FacesContext.getCurrentInstance().validationFailed();
            FacesContext.getCurrentInstance().addMessage("loginErrors",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "The user name or password entered is incorrect.", ""));
            return null;
        }
        return NavigationConstant.STATUS_PAGE;

    }

    /**
     * Logout.
     * 
     * @return the string
     */
    public String logout() {
        userName = null;
        password = null;

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return NavigationConstant.LOGIN_PAGE;
    }

    /**
     * Login.
     * 
     * @return true, if successful
     */
    private boolean login() {
        boolean loggedIn = false;
        Login login = new Login(userName, password);
        try {
            UserLogin user = loginService.login(login);

            if (user != null) {
                loggedIn = true;
                FacesContext facesContext = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
                session.setAttribute(UserAuthorizationListener.USER_INFO_SESSION_ATTRIBUTE, user);
                checkDisplays();
            }
        } catch (UserLoginException e) {
            LOG.error(e, e);
        }
        return loggedIn;
    }
    
    private void checkDisplays() {
        if(firstTimeLogged){
            new DirectDisplayController().checkDirectDisplay();
            // can add additional checks for enable / disable other displays in the future
            firstTimeLogged = false;
        }
    }
    
}
@@ -2189,17 +2189,16 @@
 rLogin;%0A
-%0A
 import j
@@ -2295,29 +2295,70 @@
 es.bean.
-SessionScoped
+ViewScoped;%0Aimport javax.faces.context.ExternalContext
 ;%0Aimport
@@ -2431,17 +2431,16 @@
 ession;%0A
-%0A
 import o
@@ -2649,23 +2649,20 @@
 edBean%0A@
-Session
+View
 Scoped%0A@
@@ -5178,32 +5178,170 @@
 rentInstance();%0A
+                ExternalContext externalContext = facesContext.getExternalContext();%0A                externalContext.invalidateSession();%0A
                 
@@ -5376,33 +5376,17 @@
 ession) 
-facesContext.getE
+e
 xternalC
@@ -5383,34 +5383,32 @@
  externalContext
-()
 .getSession(fals
@@ -5395,36 +5395,35 @@
 text.getSession(
-fals
+tru
 e);%0A            
@@ -5622,24 +5622,24 @@
 rror(e, e);%0A
-
         %7D%0A  
@@ -5628,32 +5628,100 @@
 , e);%0A        %7D%0A
+        %0A        userName = null;%0A        password = null;%0A        %0A
         return l

Fix for user name caching in AdminGUI