1    package com.instantbank.lettertemplate.control.web;
2    
3    import java.io.Serializable;
4    import java.util.HashMap;
5    import java.util.ArrayList;
6    import java.util.Collection;
7    import java.util.Iterator;
8    import java.rmi.RemoteException;
9    import javax.servlet.http.HttpSession;
10   import javax.servlet.ServletContext;
11   import javax.ejb.CreateException;
12   import javax.ejb.FinderException;
13   import com.instantbank.lettertemplate.control.GeneralFailureException;
14   import com.instantbank.lettertemplate.control.util.EJBUtil;
15   import com.instantbank.lettertemplate.control.util.WebKeys;
16   import com.instantbank.lettertemplate.control.web.handlers.JobWebImpl;
17   import com.instantbank.component.job.ejb.Job;
18   
19   import com.instantbank.common.utilcomponents.Debug;
20   import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
21   import com.instantbank.lettertemplate.control.ejb.LetterTemplateController;
22   
23   /**
24    *  This class provides a convenient set of methods for the web tier
25    *  components to access all the model objects. This class also insures that
26    *  only one copy of the model objects are created for web tier access by
27    *  placing a reference to the model objects in the session.
28    *
29    * @author Instant-bank (Consuelo Franky)
30    * @created August 2002
31    */
32   public class ModelManager extends ModelUpdateNotifier
33       implements Serializable {
34   
35     private ServletContext context;
36     private HttpSession session;
37     private LetterTemplateController sccEjb = null;
38     private LetterTemplateControllerProxy scc = null;
39     private Job jobEjb = null;
40   
41     private Debug debug = null;
42   
43   
44     /**
45      *  constructor
46      */
47     public ModelManager() {
48       debug = new Debug();
49       debug.setDebugginOn(true);
50       debug.setPreMessage("** ModelManager");
51     }
52   
53   
54     /**
55      *  Here it should instances all the javabeans that are view of Model.
56      *  a) only one view copy in the user web session for each EJB in the user Service Layer session
57      *  b) only one view copy in the web context for each EJB shared between all users
58      *
59      * @param context HTTP context
60      * @param session HTTP session
61      */
62     public void init(ServletContext context, HttpSession session) {
63       debug.println("init");
64       this.session = session;
65       this.context = context;
66       // instances javabeans that are view of Model:
67       getJobModel();
68   
69     }
70   
71   
72     /**
73      *  sets to scc attribute the web proxy reference
74      *
75      * @param scc web proxy reference
76      */
77     public void setSCC(LetterTemplateControllerProxy scc) {
78       this.scc = scc;
79       debug.println("scc assigned");
80     }
81   
82   
83     /**
84      * gets an instance of JobWebImpl: javabean that is view of Job EJB
85      *
86      * @return The jobModel value
87      */
88     public JobWebImpl getJobModel() {
89       JobWebImpl jobView = (JobWebImpl)
90         session.getAttribute(WebKeys.JobModelKey);
91       if(jobView == null) {
92         jobView = new JobWebImpl(this);
93         session.setAttribute(WebKeys.JobModelKey, jobView);
94       }
95       return jobView;
96     }
97   
98   
99     /**
100     *  sets to sccEjb attribute the controller ejb reference
101     *
102     * @return The sCCEJB value
103     */
104    public LetterTemplateController getSCCEJB() {
105      debug.println("getSCCEJB method");
106  
107      if(sccEjb == null) {
108        try {
109          sccEjb = EJBUtil.getLetterTemplateControllerHome().create();
110        }
111        catch(CreateException ce) {
112          throw new GeneralFailureException(ce.getMessage());
113        }
114        catch(RemoteException re) {
115          throw new GeneralFailureException(re.getMessage());
116        }
117        catch(javax.naming.NamingException ne) {
118          throw new GeneralFailureException(ne.getMessage());
119        }
120        catch(Exception e) {
121          throw new GeneralFailureException(e.getMessage());
122        }
123      }
124      return sccEjb;
125    }
126  
127  
128    /**
129     * get reference to the current Job EJB through the proxy
130     *
131     * @param jobId Description of the Parameter
132     * @return The jobEJB value
133     */
134    public Job getJobEJB(Long jobId) {
135      if(scc == null) {
136        throw new
137          GeneralFailureException
138          ("ModelManager: Can not get Job EJB");
139      }
140      debug.println("scc.getJobEJB(jobId) requested");
141      jobEjb = scc.getJobEJB(jobId);
142      return jobEjb;
143    }
144  
145  }
146  
147