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
34 public class LetterTemplateControllerEJB
35 implements SessionBean {
36
37
38 private StateMachine sm;
39 private LetterTemplate letterTemplate;
40 private Job job = null;
41
42
43 private SessionContext sc;
44 private Debug debug = null;
45
46
47
48
49
52 public LetterTemplateControllerEJB() { }
53
54
55
56
57
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
74 public void ejbActivate() { }
75
76
77
80 public void ejbPassivate() { }
81
82
83
84
85
88 public void ejbCreate() {
89 sm = new StateMachine(this, sc);
90 }
91
92
93
96 public void ejbRemove() {
97 debug.println("ejbRemove method");
98 sm = null;
99
100
101
102
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
115 job = null;
116
117 }
118
119
120
121
122
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
148 public void setJobNull() {
149 this.job = null;
150 debug.println("job has been set to null");
151 }
152
153
154
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
191 public Job getOldJob(Long jobId) {
192 debug.println("getOldJob method");
193 debug.println("--jobId=" + jobId);
194
195 if(jobId == null) {
196
197
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
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
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
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