1    package com.instantbank.lettertemplate.control.ejb;
2    
3    import java.util.Collection;
4    import java.util.HashMap;
5    import java.rmi.RemoteException;
6    import javax.ejb.CreateException;
7    import javax.ejb.EJBException;
8    import javax.ejb.RemoveException;
9    import javax.ejb.SessionBean;
10   import javax.ejb.SessionContext;
11   import javax.ejb.FinderException;
12   import com.instantbank.lettertemplate.control.util.EJBUtil;
13   
14   import com.instantbank.common.utilcomponents.Debug;
15   import com.instantbank.component.lettertemplate.ejb.LetterTemplate;
16   import com.instantbank.component.lettertemplate.ejb.LetterTemplateHome;
17   import com.instantbank.component.job.ejb.Job;
18   import com.instantbank.component.job.ejb.JobHome;
19   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
20   import com.instantbank.lettertemplate.control.event.LetterTemplateEvent;
21   
22   /**
23    *  Session Bean implementation for LetterTemplateController EJB. This is the
24    *  EJB-tier controller of the MVC. It is implemented as a session EJB. It
25    *  controls all the activities that happen in a client session (managing update
26    *  events through handleEvent() method) It also provides mechanisms to access
27    *  other session EJBs and variables: get services for obtaining session
28    *  variables and for contacting EJBs (entity or session) which are instanced
29    *  for the user.
30    *
31    * @author Instant-bank (Consuelo Franky)
32    * @created September 2002
33    */
34   public class LetterTemplateControllerEJB
35       implements SessionBean {
36   
37     // attributes corresponding to the EJB state:
38     private StateMachine sm;
39     private LetterTemplate letterTemplate;
40     private Job job = null;
41   
42     // others attributes : context, debug
43     private SessionContext sc;
44     private Debug debug = null;
45   
46   
47     // CONSTRUCTOR :
48   
49     /**
50      *  Constructor for the LetterTemplateControllerEJB object
51      */
52     public LetterTemplateControllerEJB() { }
53   
54   
55     // METHODS OF CONTEXT:
56   
57     /**
58      *  Sets the sessionContext attribute of the LetterTemplateControllerEJB
59      *  object
60      *
61      * @param sc The new sessionContext value
62      */
63     public void setSessionContext(SessionContext sc) {
64       this.sc = sc;
65       debug = new Debug();
66       debug.setDebugginOn(true);
67       debug.setPreMessage("** LetterTemplateControllerEJB");
68     }
69   
70   
71     /**
72      *  activation after swapping
73      */
74     public void ejbActivate() { }
75   
76   
77     /**
78      *  before swappping
79      */
80     public void ejbPassivate() { }
81   
82   
83     // EJB Methods
84   
85     /**
86      *  after creation of ejb instance
87      */
88     public void ejbCreate() {
89       sm = new StateMachine(this, sc);
90     }
91   
92   
93     /**
94      *  before remove ejb instance
95      */
96     public void ejbRemove() {
97       debug.println("ejbRemove method");
98       sm = null;
99       // this method will be called at the time of sign off.
100      // destroy all the session EJB's created by the EJB controller
101  
102      // remove Session EJB
103      if(letterTemplate != null) {
104        try {
105          letterTemplate.remove();
106        }
107        catch(RemoteException re) {
108          throw new EJBException(re);
109        }
110        catch(RemoveException re) {
111          throw new EJBException(re);
112        }
113      }
114      // assign null to entity EJB of user
115      job = null;
116  
117    }
118  
119  
120    // METHODS OF BUSINESS:
121  
122    /**
123     *  Principal service: Feeds the specified event to the state machine of the
124     *  business logic.
125     *
126     * @param ese event corresponding to user request
127     * @return a list of models (EJBs names)
128     *      that got updated because of the processing of this event. In
129     *      addition, the first element of the list contains the service answer
130     *      (or the "" string when this answer is not required)
131     * @exception LetterTemplateEventException
132     */
133    public Collection handleEvent(LetterTemplateEvent ese)
134       throws LetterTemplateEventException {
135      debug.println("handleEvent :" + ese);
136      try {
137        return (sm.handleEvent(ese));
138      }
139      catch(RemoteException re) {
140        throw new EJBException(re);
141      }
142    }
143  
144  
145    /**
146     * Sets null to job attribute:
147     */
148    public void setJobNull() {
149      this.job = null;
150      debug.println("job has been set to null");
151    }
152  
153  
154    /**
155     *Get reference to LetterTemplate EJB
156     *
157     * @param companyId current company
158     * @param userId current user
159     * @return the LetterTemplate session EJB associated
160     *      with this session.
161     */
162    public LetterTemplate getLetterTemplate(String companyId, Long userId) {
163      if(letterTemplate == null) {
164        try {
165          LetterTemplateHome home = EJBUtil.getLetterTemplateHome();
166          letterTemplate = home.create(companyId, userId);
167        }
168        catch(CreateException ce) {
169          throw new EJBException(ce);
170        }
171        catch(RemoteException re) {
172          throw new EJBException(re);
173        }
174        catch(javax.naming.NamingException ne) {
175          throw new EJBException(ne);
176        }
177        catch(Exception e) {
178          throw new EJBException(e);
179        }
180      }
181      return letterTemplate;
182    }
183  
184  
185    /**
186     *Get reference to an existent Job EJB
187     *
188     * @param jobId primary key of the Job
189     * @return the Job entity EJB associated.
190     */
191    public Job getOldJob(Long jobId) {
192      debug.println("getOldJob method");
193      debug.println("--jobId=" + jobId);
194  
195      if(jobId == null) {
196        // Job recently created: web layer does not know jobId;
197        // it is possible that job is null if it has been failed to be created
198        if(job == null) {
199          debug.println("job is null");
200        }
201        return job;
202      }
203  
204      try {
205        if(job == null
206          || job.getState().getJobId().longValue() != jobId.longValue()) {
207          // old job requested
208          JobHome home = EJBUtil.getJobHome();
209          job = home.findByPrimaryKey(jobId);
210        }
211      }
212      catch(FinderException ce) {
213        debug.println("FinderException: " + ce);
214        throw new EJBException(ce);
215      }
216      catch(RemoteException re) {
217        debug.println("RemoteException: " + re);
218        throw new EJBException(re);
219      }
220      catch(javax.naming.NamingException ne) {
221        debug.println("NamingException: " + ne);
222        throw new EJBException(ne);
223      }
224      catch(Exception e) {
225        debug.println("Exception: " + e);
226        throw new EJBException(e);
227      }
228  
229      return job;
230    }
231  
232  
233    /**
234     *Get reference to a new Job EJB
235     *
236     * @param companyId current company
237     * @param userId current user
238     * @param name name of Job
239     * @param ftpPrimaryId primary ftp associated to the Job
240     * @param ftpAlternateId alternate ftp associated to the Job
241     * @return a new Job entity EJB associated
242     *          with this session.
243     */
244    public Job getNewJob(String companyId, Long userId, String name,
245                         long ftpPrimaryId, long ftpAlternateId) {
246      debug.println("getNewJob");
247      try {
248        JobHome home = EJBUtil.getJobHome();
249        debug.println("invokes create");
250        job = home.create
251          (companyId, userId, name, ftpPrimaryId, ftpAlternateId);
252      }
253      catch(CreateException ce) {
254        debug.println("CreateException: " + ce);
255        throw new EJBException(ce);
256      }
257      catch(RemoteException re) {
258        debug.println("RemoteException: " + re);
259        throw new EJBException(re);
260      }
261      catch(javax.naming.NamingException ne) {
262        debug.println("NamingException: " + ne);
263        throw new EJBException(ne);
264      }
265      catch(Exception e) {
266        debug.println("Exception: " + e);
267        throw new EJBException(e);
268      }
269      return job;
270    }
271  
272  
273    /**
274     * @return the w variable for this session: these variables are maintained
275     *      by StateMachine javaBean
276     */
277    public String getW() {
278      String w = (String)sm.getAttribute("w");
279      if(w != null) {
280        return w;
281      }
282      else {
283        throw new EJBException("session w variable doesn't exist");
284      }
285    }
286  
287  }
288  
289  
290