package at.fhj.itm.util;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.faces.context.FacesContext;
import at.fhj.itm.beans.LoginListener;
import at.fhj.itm.model.User;
/**
* This class works as a Facade to access required methods from FacesContext.
* This allows the Beans to be tested without the real FacesContext. It also
* holds globally useful data like the locale-specific date-time formats.
*
* The DefaultServiceAssembler will create for each session one jsfUtil, which
* makes it possible to use this class to handle the Login and Logout
* notifications.
*
* @author Christian Tassler
*
*/
public class JsfUtilImpl implements JsfUtil {
/**
* Redirects to the specified url.
* <code>FacesContext.getCurrentInstance().getExternalContext().redirect(url);</code>
*
* @param url
* String representing a url
* @throws IOException
*/
@Override
public void redirect(String url) throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
}
/**
* Returns a map containing the parameters passed in the GET-url.
* <code>FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap()</code>
*
* @return Map containing string keys with string values
*/
@Override
public Map<String, String> getRequestParameterMap() {
return FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap();
}
@Override
public User getLoggedInUser() {
return (User) FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().get("loggedInUser");
}
/**
* Sets the current User for this session to the passed user. If the passed
* User is null it will call {@link #userLoggedOut()}. It notifies all
* installed LoginListeners about the change.
*
* @param user
* the User who is logged in.
*/
@Override
public void userLoggedIn(User user) {
if (user == null)
userLoggedOut();
else {
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("loggedInUser", user);
for (LoginListener l : loginListeners)
l.userLoggedIn(user);
}
}
/**
* Sets the User for this session to null and notifies all installed
* LoginListeners about the change.
*/
@Override
public void userLoggedOut() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
.remove("loggedInUser");
for (LoginListener l : loginListeners)
l.userLoggedOut();
}
private List<LoginListener> loginListeners = new ArrayList<LoginListener>();
/**
* An added LoginListener will be notified when a User logs in or logs out.
* It immidiatly notifies the new listener if a user is already logged in.
*
* @param listener
* the LoginListener to add
*/
@Override
public void addLoginListener(LoginListener listener) {
loginListeners.add(listener);
User user = getLoggedInUser();
if (user != null)
listener.userLoggedIn(user);
}
// use messagebundle for dateformat?
private static final DateFormat simpleDate_en = new SimpleDateFormat(
"MM/dd/yyyy");
private static final DateFormat simpleTime_en = new SimpleDateFormat(
"HH:mm");
private static final DateFormat simpleDate_de = new SimpleDateFormat(
"dd.MM.yyyy");
// private static final DateFormat simpleTime_de = new SimpleDateFormat(
// "HH:mm");
// how much work in views and validators if changed to 12hour format
// ("hh:mm a") ?
/**
* Returns the dateformat dd.MM.yyyy if the locale is set to german,
* MM/dd/yyyy otherwise.
*
* @return DateFormat day month year in the current locale
*/
@Override
public DateFormat getSimpleDate() {
Locale locale = FacesContext.getCurrentInstance().getApplication()
.getDefaultLocale();
if (Locale.GERMANY.equals(locale))
return simpleDate_de;
return simpleDate_en;
}
/**
* Returns the dateformat HH:mm.
*
* @return DateFormat hours minutes in the current locale
*/
@Override
public DateFormat getSimpleTime() {
// Locale locale = FacesContext.getCurrentInstance().getApplication()
// .getDefaultLocale();
// if (Locale.GERMANY.equals(locale))
// return simpleTime_de;
return simpleTime_en;
}
}