package wenrgise.ejb.common.session;

import java.text.DateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.BaseDetailVO;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.BaseQueryVO;
import wenrgise.ejb.common.helper.ScreenWrapper;

public class UserSessionBean implements SessionBean {
  private SessionContext context;
  
  private UserInfo userInfo;
  
  private HashMap oFormMap = new HashMap();
  
  private Locale oUserLocale;
  
  private Locale oDefaultLocale = Locale.getDefault();
  
  private HashMap moduleFacade = new HashMap();
  
  private String moduleName = null;
  
  private HashMap securityMap = new HashMap();
  
  public void ejbCreate() {}
  
  public void ejbActivate() {}
  
  public void ejbPassivate() {}
  
  public void ejbRemove() {}
  
  public void setSessionContext(SessionContext ctx) {
    this.context = ctx;
  }
  
  public HashMap getAccessInfo() {
    return this.securityMap;
  }
  
  public void setAccessInfo(HashMap oMap) {
    this.securityMap = oMap;
  }
  
  public void removeAllIfExists(String sFormName, String sScreenName) {
    ScreenWrapper oScreenMap = (ScreenWrapper)this.oFormMap.get(sFormName);
    if (oScreenMap == null)
      return; 
    this.oFormMap.remove(sFormName);
  }
  
  private ScreenWrapper getScreenWrapper(String sFormName) {
    ScreenWrapper oScreenWrapper = (ScreenWrapper)this.oFormMap.get(sFormName);
    if (oScreenWrapper == null) {
      oScreenWrapper = new ScreenWrapper();
      this.oFormMap.put(sFormName, oScreenWrapper);
    } 
    return oScreenWrapper;
  }
  
  public void setForwardedPage(String sFormName, String sForwardedPage) {
    ScreenWrapper screenWrapper = getScreenWrapper(sFormName);
    if (null != screenWrapper)
      screenWrapper.setForwardedPage(sForwardedPage); 
  }
  
  public String getForwardedPage(String sFormName) {
    ScreenWrapper screenWrapper = getScreenWrapper(sFormName);
    if (null != screenWrapper)
      return screenWrapper.getForwardedPage(); 
    return null;
  }
  
  private HashMap getScreenMap(String sFormName) {
    ScreenWrapper oScreenWrapper = getScreenWrapper(sFormName);
    HashMap oScreenMap = oScreenWrapper.getOScreenMap();
    if (oScreenMap == null) {
      oScreenMap = new HashMap();
      oScreenWrapper.setOScreenMap(oScreenMap);
    } 
    return oScreenMap;
  }
  
  public void putBaseHeaderVO(String sFormName, BaseHeaderVO oBaseHeaderVO) {
    getScreenWrapper(sFormName).setOBaseHeaderVO(oBaseHeaderVO);
  }
  
  public void putBaseQueryVO(String sFormName, BaseQueryVO oBaseQueryVO) {
    getScreenWrapper(sFormName).setOBaseQueryVO(oBaseQueryVO);
  }
  
  public void putBaseDetailVO(String sFormName, String sScreenName, BaseDetailVO oBaseDetailVO) {
    getScreenMap(sFormName).put(sScreenName, oBaseDetailVO);
  }
  
  public BaseHeaderVO getBaseHeaderVO(String sFormName) {
    ScreenWrapper oScreenWrapper = (ScreenWrapper)this.oFormMap.get(sFormName);
    if (oScreenWrapper == null)
      return null; 
    return oScreenWrapper.getOBaseHeaderVO();
  }
  
  public BaseQueryVO getBaseQueryVO(String sFormName) {
    ScreenWrapper oScreenWrapper = (ScreenWrapper)this.oFormMap.get(sFormName);
    if (oScreenWrapper == null)
      return null; 
    return oScreenWrapper.getOBaseQueryVO();
  }
  
  public BaseDetailVO getBaseDetailVO(String sFormName, String sScreenName) {
    ScreenWrapper oScreenWrapper = (ScreenWrapper)this.oFormMap.get(sFormName);
    if (oScreenWrapper == null)
      return null; 
    return (BaseDetailVO)oScreenWrapper.getOScreenMap().get(sScreenName);
  }
  
  public UserInfo getUserInfo() {
    return this.userInfo;
  }
  
  public void setUserInfo(UserInfo newUserInfo) {
    this.userInfo = newUserInfo;
  }
  
  public Locale getUserLocale() {
    return this.oUserLocale;
  }
  
  public void setUserLocale(Locale oUserLocale) {
    if (oUserLocale == null)
      oUserLocale = this.oDefaultLocale; 
  }
  
  public Locale getDefaultLocale() {
    return this.oDefaultLocale;
  }
  
  public DateFormat getUserDateFormat() {
    if (this.oUserLocale != null)
      return DateFormat.getDateInstance(2, this.oUserLocale); 
    return DateFormat.getDateInstance();
  }
  
  public DateFormat getDefaultDateFormat() {
    if (this.oDefaultLocale != null)
      return DateFormat.getDateInstance(2, this.oDefaultLocale); 
    return DateFormat.getDateInstance();
  }
  
  public HashMap getModuleFacade() {
    return this.moduleFacade;
  }
  
  public void setModuleFacade(HashMap newModuleFacade) {
    this.moduleFacade = newModuleFacade;
  }
  
  public void removeAllDetailVO(String sFormName) {
    ScreenWrapper oScreenWrapper = (ScreenWrapper)this.oFormMap.get(sFormName);
    HashMap oMap = oScreenWrapper.getOScreenMap();
    Collection oCollection = oMap.values();
    oCollection.clear();
  }
  
  public void setModuleName(String moduleName) {
    this.moduleName = moduleName;
  }
  
  public EJBObject getModuleEJBObject() {
    if (null != this.moduleFacade)
      return (EJBObject)this.moduleFacade.get(this.moduleName); 
    return null;
  }
}
