package com.prg.safety.model.basic.share.session;import java.util.Locale;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import com.prg.safety.model.basic.share.exception.RootException;
import com.prg.safety.model.basic.share.util.SkinUtil;
import com.prg.safety.model.basic.userRole.api.Permission;/**
 * Used to store information about a specific user. This class is used so that
 * the information is not scattered throughout the HttpSession. Only this object
 * is stored in the session for the user. This class implements the
 * HttpSessionBindingListener interface so that it can be notified of session
 * timeout and perform the proper cleanup.
 */
public class SessionContainer implements HttpSessionBindingListener {
    private Locale locale;    //  Data about the user that is cached
    private UserSession userSession = null;    private Permission[] permissions = null;    private StringBuffer styleInfo = null;    private int pageSize = 0;    private String pageColor = "#4D76B3";    /**
     * Default Constructor
     */
    public SessionContainer() {
        super();
        initialize();
    }    /**
     * The container calls this method when it is being unbound from the
     * session.
     */
    public void valueUnbound(HttpSessionBindingEvent event) {
        // Perform resource cleanup
        System.out.println("Being unbound...");
        cleanUp();
    }    /**
     * Set the locale for the user.
     */
    public void setLocale(Locale aLocale) {
        locale = aLocale;
    }    /**
     * Retrieve the locale for the user.
     */
    public Locale getLocale() {
        return locale;
    }    /**
     * The container calls this method when it is being bound to the session.
     */
    public void valueBound(HttpSessionBindingEvent event) {
        // Don't need to do anything, but still have to implement the
        // interface method.
    }    public UserSession getUserSession() {
        return this.userSession;
    }    public void setUserSession(UserSession userSession) {
        this.userSession = userSession;
    }    public int getPageSize() {
        return pageSize;
    }    public void resetPageSize() {
        this.pageSize = 0;
    }    public void setPageSize(String userName) {
        if (pageSize == 0) {
            try {
                pageSize = SkinUtil.getPageSize(userName).intValue();
            } catch (RootException e) {
                e.printStackTrace();
            }
        }
    }    public String getPageColor() {
        return pageColor;
    }    public void resetPageColor() {
        this.pageColor = "#4D76B3";
    }    public void setPageColor(String userName) {
        if (pageColor.equals("#4D76B3")) {
            try {
                pageColor = SkinUtil.getPageColor(userName);
            } catch (RootException e) {
                e.printStackTrace();
            }
        }
    }    public StringBuffer getStyleInfo() {
        return styleInfo;
    }    public void resetStyleInfo() {
        this.styleInfo = null;
    }    public void setStyleInfo(String userName) {        if (styleInfo == null) {
            try {
                styleInfo = SkinUtil.getStyleInfo(userName);
            } catch (RootException e) {
                e.printStackTrace();
            }
        }    }    /**
     * Initialize all of the required resources
     */
    private void initialize() {
        // Create a new Shopping cart for this user
        userSession = new UserSession();
    }    /**
     * Clean up any open resources. The shopping cart is left intact
     * intentionally.
     */
    public void cleanUp() {
        setUserSession(null);
    }    public Permission[] getPermissions() {
        return permissions;
    }    public void setPermissions(Permission[] permissions) {
        this.permissions = permissions;
    }
}