package hirondelle.predict.pub.login;
import hirondelle.predict.main.preferences.Preferences;
import hirondelle.predict.main.preferences.PreferencesDAO;
import hirondelle.web4j.action.ActionImpl;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.model.AppException;
import hirondelle.web4j.security.LoginTasks;
import hirondelle.web4j.security.SafeText;
import hirondelle.web4j.util.Util;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public final class Login implements LoginTasks {
public boolean hasAlreadyReacted(HttpSession aSession) {
return aSession.getAttribute(Preferences.SCREEN_NAME) != null;
}
public void reactToUserLogin(HttpSession aSession, HttpServletRequest aRequest) throws AppException {
SafeText userName = SafeText.from(aRequest.getUserPrincipal().getName());
fLogger.fine("Adding user preferences to session, for principal name:" + userName);
PreferencesDAO dao = new PreferencesDAO();
try {
Preferences prefs = dao.fetch(userName);
fLogger.fine("Adding user id and screen name to session");
aSession.setAttribute(ActionImpl.USER_ID, prefs.getUserId());
aSession.setAttribute(Preferences.SCREEN_NAME, prefs.getScreenName());
}
catch (DAOException ex){
throw new AppException("Cannot fetch Preferences from database.", ex);
}
}
private static final Logger fLogger = Util.getLogger(Login.class);
}