1    package com.instantbank.servicing.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.servicing.control.util.EJBUtil;
13   
14   import com.instantbank.common.utilcomponents.Debug;
15   import com.instantbank.component.parameter.ejb.Parameter;
16   import com.instantbank.component.parameter.ejb.ParameterHome;
17   import com.instantbank.servicing.control.ServicingEventException;
18   import com.instantbank.servicing.control.event.ServicingEvent;
19   
20   /**
21    *  Session Bean implementation for ServicingController EJB. This is the
22    *  EJB-tier controller of the MVC. It is implemented as a session EJB. It
23    *  controls all the activities that happen in a client session (managing update
24    *  events through handleEvent() method) It also provides mechanisms to access
25    *  other session EJBs and variables: get services for obtaining session
26    *  variables and for contacting EJBs (entity or session) which are instanced
27    *  for the user.
28    *
29    * @author Instant-bank (Consuelo Franky)
30    * @created September 2002
31    */
32   public class ServicingControllerEJB
33       implements SessionBean {
34   
35     // attributes corresponding to the EJB state:
36     private StateMachine sm;
37     private Parameter parameter;
38   
39     // others attributes : context, debug
40     private SessionContext sc;
41     private Debug debug = null;
42   
43   
44     // CONSTRUCTOR :
45   
46     /**
47      *  Constructor for the ServicingControllerEJB object
48      */
49     public ServicingControllerEJB() { }
50   
51   
52     // METHODS OF CONTEXT:
53   
54     /**
55      *  Sets the sessionContext attribute of the ServicingControllerEJB
56      *  object
57      *
58      * @param sc The new sessionContext value
59      */
60     public void setSessionContext(SessionContext sc) {
61       this.sc = sc;
62       debug = new Debug();
63       debug.setDebugginOn(true);
64       debug.setPreMessage("** ServicingControllerEJB");
65     }
66   
67   
68     /**
69      *  activation after swapping
70      */
71     public void ejbActivate() { }
72   
73   
74     /**
75      *  before swappping
76      */
77     public void ejbPassivate() { }
78   
79   
80     // EJB Methods:
81   
82     /**
83      *  after creation of ejb instance
84      */
85     public void ejbCreate() {
86       sm = new StateMachine(this, sc);
87     }
88   
89   
90     /**
91      *  before remove ejb instance
92      */
93     public void ejbRemove() {
94       sm = null;
95       // this method will be called at the time of sign off.
96       // destroy all the session EJB's created by the EJB controller
97   
98       // remove Session EJB
99       if(parameter != null) {
100        try {
101          parameter.remove();
102        }
103        catch(RemoteException re) {
104          throw new EJBException(re);
105        }
106        catch(RemoveException re) {
107          throw new EJBException(re);
108        }
109      }
110      // assign null to entity EJB of user
111  
112    }
113  
114  
115    // METHODS OF BUSINESS:
116  
117    /**
118     *  Principal service: Feeds the specified event to the state machine of the
119     *  business logic.
120     *
121     * @param ese event corresponding to user request
122     * @return a list of models (EJBs names)
123     *      that got updated because of the processing of this event. In
124     *      addition, the first element of the list contains the service answer
125     *      (or the "" string when this answer is not required)
126     * @exception ServicingEventException
127     */
128    public Collection handleEvent(ServicingEvent ese)
129       throws ServicingEventException {
130      debug.println("handleEvent ");
131      try {
132        return (sm.handleEvent(ese));
133      }
134      catch(RemoteException re) {
135        throw new EJBException(re);
136      }
137    }
138  
139  
140    /**
141     * @param companyId current company
142     * @param userId current user
143     * @return the Parameter session EJB associated
144     *      with this session.
145     */
146    public Parameter getParameter(String companyId, Long userId) {
147      if(parameter == null) {
148        try {
149          ParameterHome home = EJBUtil.getParameterHome();
150          parameter = home.create(companyId, userId);
151        }
152        catch(CreateException ce) {
153          throw new EJBException(ce);
154        }
155        catch(RemoteException re) {
156          throw new EJBException(re);
157        }
158        catch(javax.naming.NamingException ne) {
159          throw new EJBException(ne);
160        }
161        catch(Exception e) {
162          throw new EJBException(e);
163        }
164      }
165      return parameter;
166    }
167  
168  
169  
170    /**
171     * @return the w variable for this session: these variables are maintained
172     *      by StateMachine javaBean
173     */
174    public String getW() {
175      String w = (String)sm.getAttribute("w");
176      if(w != null) {
177        return w;
178      }
179      else {
180        throw new EJBException("session w variable doesn't exist");
181      }
182    }
183  
184  }
185  
186