1    package com.instantbank.lettertemplate.control.ejb.handlers;
2    
3    import java.util.Hashtable;
4    import java.util.ArrayList;
5    
6    import java.rmi.RemoteException;
7    import javax.naming.InitialContext;
8    import javax.naming.NamingException;
9    import javax.ejb.CreateException;
10   import javax.ejb.DuplicateKeyException;
11   import javax.ejb.FinderException;
12   
13   // streams:
14   import java.io.StringWriter;
15   import java.io.PrintWriter;
16   
17   import com.instantbank.lettertemplate.control.event.SetLetterJobEvent;
18   import com.instantbank.lettertemplate.control.event.LetterTemplateEvent;
19   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
20   
21   import com.instantbank.component.lettertemplate.ejb.LetterTemplate;
22   import com.instantbank.component.job.ejb.Job;
23   
24   import com.instantbank.common.utilcomponents.Debug;
25   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
26   import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
27   import com.instantbank.common.utilcomponents.CodeDescription;
28   import com.instantbank.common.utilcomponents.CommonUtil;
29   
30   /**
31    *  Set Letter Job usecase: handler of user requests in the Services layer
32    *
33    * @author Instant-bank (Consuelo Franky)
34    * @created August 2002
35    */
36   public class SetLetterJobHandler extends StateHandlerSupport {
37   
38     private Debug debug = null;
39   
40   
41     /**
42      *  principal method that process a user request invoking Model EJBs methods
43      *
44      * @param event request data
45      * @return answer to request
46      * @exception LetterTemplateEventException
47      */
48     public Object perform(LetterTemplateEvent event)
49        throws LetterTemplateEventException {
50       debug = new Debug();
51       debug.setDebugginOn(true);
52       debug.setPreMessage("** SetLetterJobHandler-services tier: ");
53   
54       Object answer = "";
55       SetLetterJobEvent ce = (SetLetterJobEvent)event;
56   
57       switch (ce.getActionType()) {
58   
59         case SetLetterJobEvent.LIST_JOBS:
60         {
61           answer = performLIST_JOBS(ce);
62         }
63           break;
64         case SetLetterJobEvent.GET_TEMPLATE_FIELDS:
65         {
66           answer = performGET_TEMPLATE_FIELDS(ce);
67         }
68           break;
69         case SetLetterJobEvent.SAVE_JOB:
70         {
71           answer = performSAVE_JOB(ce);
72         }
73           break;
74         case SetLetterJobEvent.GET_JOB:
75         {
76           answer = performGET_JOB(ce);
77         }
78           break;
79         case SetLetterJobEvent.REMOVE_JOB:
80         {
81           answer = performREMOVE_JOB(ce);
82         }
83           break;
84         default:
85           debug.println("Error: not implemented yet");
86           break;
87       }
88       return answer;
89     }
90   
91   
92     /**
93      *  auxiliary method that processes an SetLetterJobEvent
94      *  with LIST_JOBS action.
95      *
96      * @param ce event of setLetterJob usecase
97      * @return answer to request
98      * @exception LetterTemplateEventException
99      */
100    private ArrayList performLIST_JOBS(SetLetterJobEvent ce)
101       throws LetterTemplateEventException {
102      debug.println("LIST_JOBS action");
103      ArrayList arrayAnswer = null;
104      String problem = LetterTemplateGlobals.STR_UNDEF;
105  
106      try {
107        // step1: getting Model EJB session references:
108        LetterTemplate varLetterTemplate =
109          machine.getLetterTemplateControllerEJB().getLetterTemplate
110          (ce.getCompanyId(), ce.getUserId());
111  
112        // step3: invoking methods of session EJBs
113        CodeDescription[] jobs = varLetterTemplate.loadJobsCodes();
114        CodeDescription[] locs = varLetterTemplate.loadFTPCodes();
115        ArrayList companyFields = varLetterTemplate.loadFieldsDisplay();
116        ArrayList fieldMenus = varLetterTemplate.loadFieldMenus();
117        ArrayList templatesDescriptors
118           = varLetterTemplate.loadAllTemplatesDescription();
119  
120        // step5: building answer:
121        arrayAnswer = new ArrayList(5);
122        arrayAnswer.add(jobs);
123        arrayAnswer.add(locs);
124        arrayAnswer.add(companyFields);
125        arrayAnswer.add(fieldMenus);
126        arrayAnswer.add(templatesDescriptors);
127      }
128      catch(Exception e) {
129        throw new LetterTemplateEventException(e.getMessage());
130      }
131      return arrayAnswer;
132    }
133  
134  
135    /**
136     *  auxiliary method that processes an SetLetterJobEvent
137     *  with GET_TEMPLATE_FIELDS action.
138     *
139     * @param ce event of setLetterJob usecase
140     * @return answer to request
141     * @exception LetterTemplateEventException
142     */
143    private ArrayList performGET_TEMPLATE_FIELDS(SetLetterJobEvent ce)
144       throws LetterTemplateEventException {
145      debug.println("GET_TEMPLATE_FIELDS action");
146      ArrayList arrayAnswer = null;
147      String problem = LetterTemplateGlobals.STR_UNDEF;
148  
149      try {
150        // step1: getting Model EJB session references:
151        LetterTemplate varLetterTemplate =
152          machine.getLetterTemplateControllerEJB().getLetterTemplate
153          (ce.getCompanyId(), ce.getUserId());
154  
155        // step3: invoking methods of session EJBs
156        Object template = null;
157        try {
158          template = varLetterTemplate.loadTemplate
159            ((ce.getTemplateId()).longValue());
160        }
161        catch(Exception a) {
162          problem = processProblem
163            (a, LetterTemplateExceptionMessage.PROBLEM_NO_TEMPLATE);
164        }
165  
166        CodeDescription[] jobs = varLetterTemplate.loadJobsCodes();
167        CodeDescription[] locs = varLetterTemplate.loadFTPCodes();
168        ArrayList templatesDescriptors
169           = varLetterTemplate.loadAllTemplatesDescription();
170  
171        // step5: building answer:
172        arrayAnswer = new ArrayList(5);
173        arrayAnswer.add(problem);
174        arrayAnswer.add(template);
175        arrayAnswer.add(jobs);
176        arrayAnswer.add(locs);
177        arrayAnswer.add(templatesDescriptors);
178      }
179      catch(Exception e) {
180        throw new LetterTemplateEventException(e.getMessage());
181      }
182      return arrayAnswer;
183    }
184  
185  
186    /**
187     *  auxiliary method that processes an SetLetterJobEvent
188     *  with SAVE_JOB action.
189     *
190     * @param ce event of setLetterJob usecase
191     * @return answer to request
192     * @exception LetterTemplateEventException
193     */
194    private ArrayList performSAVE_JOB(SetLetterJobEvent ce)
195       throws LetterTemplateEventException {
196      debug.println("SAVE_JOB action");
197      ArrayList arrayAnswer = null;
198      String problem = LetterTemplateGlobals.STR_UNDEF;
199      Long varJobId = null;
200      boolean newJob = false;
201      Object template = null;
202  
203      try {
204        // step1: getting Model EJB session references:
205        LetterTemplate varLetterTemplate =
206          machine.getLetterTemplateControllerEJB().getLetterTemplate
207          (ce.getCompanyId(), ce.getUserId());
208  
209        // step2: getting Model EJB entity reference:
210        Job varJob = null;
211        if(ce.getJobId() == null) {
212          try {
213            varJob = machine.getLetterTemplateControllerEJB().getNewJob(
214              ce.getCompanyId(), ce.getUserId(),
215              ce.getJobModel().getName(),
216              ce.getJobModel().getFtpPrimaryId(),
217              ce.getJobModel().getFtpAlternateId());
218            varJobId = varJob.getState().getJobId();
219            ce.getJobModel().setJobId(varJobId);
220          }
221          catch(Exception ee) {
222            problem = processProblem
223              (ee, LetterTemplateExceptionMessage.PROBLEM_JOB_DUPLICATED);
224            machine.getLetterTemplateControllerEJB().setJobNull();
225          }
226        }
227        else {
228          try {
229            varJob = machine.getLetterTemplateControllerEJB()
230              .getOldJob(ce.getJobId());
231          }
232          catch(Exception ee) {
233            problem = processProblem
234              (ee, LetterTemplateExceptionMessage.PROBLEM_JOB_WAS_DELETED);
235            machine.getLetterTemplateControllerEJB().setJobNull();
236          }
237        }
238  
239        // step3: invoking methods of entity EJB
240        if(varJob != null) {
241          try {
242            Hashtable systemAlias
243               = (Hashtable)machine.getAttribute("systemAlias");
244            if(systemAlias == null) {
245              systemAlias = varLetterTemplate.loadAlias();
246              machine.setAttribute("systemAlias", systemAlias);
247            }
248            Hashtable systemFields
249               = (Hashtable)machine.getAttribute("systemFields");
250            if(systemFields == null) {
251              systemFields = varLetterTemplate.loadFields();
252              machine.setAttribute("systemFields", systemFields);
253            }
254  
255            varJob.setState(ce.getJobModel());
256            varJob.generateSQLtext(systemFields, systemAlias,
257              LetterTemplateGlobals.ROOT_AGREEMENTS);
258          }
259          catch(Exception ee) {
260            problem = processProblem
261              (ee, LetterTemplateExceptionMessage.PROBLEM_JOB_DUPLICATED_OR_NO_TEMPLATE);
262            if(newJob) {
263              // new job was created without template => remove it
264              machine.getLetterTemplateControllerEJB().setJobNull();
265              varJob = machine.getLetterTemplateControllerEJB()
266                .getOldJob(varJobId);
267              debug.println("before job.remove()");
268              varJob.remove();
269              varJob = null;
270            }
271            machine.getLetterTemplateControllerEJB().setJobNull();
272          }
273  
274          long templateId = varJob.getState().getTemplateCode();
275          template = varLetterTemplate.loadTemplate(templateId);
276        }
277  
278        // step4: invoking methods of session EJBs
279        CodeDescription[] jobs = varLetterTemplate.loadJobsCodes();
280        CodeDescription[] locs = varLetterTemplate.loadFTPCodes();
281        ArrayList templatesDescriptors
282           = varLetterTemplate.loadAllTemplatesDescription();
283  
284        // step5: building answer:
285        arrayAnswer = new ArrayList(5);
286        arrayAnswer.add(problem);
287        arrayAnswer.add(template);
288        arrayAnswer.add(jobs);
289        arrayAnswer.add(locs);
290        arrayAnswer.add(templatesDescriptors);
291      }
292      catch(Exception e) {
293        throw new LetterTemplateEventException(e.getMessage());
294      }
295      return arrayAnswer;
296    }
297  
298  
299    /**
300     *  auxiliary method that processes an SetLetterJobEvent
301     *  with GET_JOB action.
302     *
303     * @param ce event of setLetterJob usecase
304     * @return answer to request
305     * @exception LetterTemplateEventException
306     */
307    private ArrayList performGET_JOB(SetLetterJobEvent ce)
308       throws LetterTemplateEventException {
309      debug.println("GET_JOB action");
310      ArrayList arrayAnswer = null;
311      String problem = LetterTemplateGlobals.STR_UNDEF;
312      Long varJobId = null;
313      Object template = null;
314  
315      try {
316        // step1: getting Model EJB session references:
317        LetterTemplate varLetterTemplate =
318          machine.getLetterTemplateControllerEJB().getLetterTemplate
319          (ce.getCompanyId(), ce.getUserId());
320  
321        // step2: getting Model EJB entity reference:
322        Job varJob = null;
323        try {
324          varJob = machine.getLetterTemplateControllerEJB()
325            .getOldJob(ce.getJobId());
326        }
327        catch(Exception ee) {
328          problem = processProblem
329            (ee, LetterTemplateExceptionMessage.PROBLEM_JOB_WAS_DELETED);
330          machine.getLetterTemplateControllerEJB().setJobNull();
331        }
332  
333        // step3: invoking methods of entity EJB
334        if(varJob != null) {
335          long templateId = varJob.getState().getTemplateCode();
336          template = varLetterTemplate.loadTemplate(templateId);
337        }
338  
339        // step4: invoking methods of session EJBs
340        CodeDescription[] jobs = varLetterTemplate.loadJobsCodes();
341        CodeDescription[] locs = varLetterTemplate.loadFTPCodes();
342        ArrayList templatesDescriptors
343           = varLetterTemplate.loadAllTemplatesDescription();
344  
345        // step5: building answer:
346        arrayAnswer = new ArrayList(5);
347        arrayAnswer.add(problem);
348        arrayAnswer.add(template);
349        arrayAnswer.add(jobs);
350        arrayAnswer.add(locs);
351        arrayAnswer.add(templatesDescriptors);
352      }
353      catch(Exception e) {
354        throw new LetterTemplateEventException(e.getMessage());
355      }
356      return arrayAnswer;
357    }
358  
359  
360    /**
361     *  auxiliary method that processes an SetLetterJobEvent
362     *  with REMOVE_JOB action.
363     *
364     * @param ce event of setLetterJob usecase
365     * @return answer to request
366     * @exception LetterTemplateEventException
367     */
368    private ArrayList performREMOVE_JOB(SetLetterJobEvent ce)
369       throws LetterTemplateEventException {
370      debug.println("REMOVE_JOB action");
371      ArrayList arrayAnswer = null;
372      String problem = LetterTemplateGlobals.STR_UNDEF;
373  
374      try {
375        // step1: getting Model EJB session references:
376        LetterTemplate varLetterTemplate =
377          machine.getLetterTemplateControllerEJB().getLetterTemplate
378          (ce.getCompanyId(), ce.getUserId());
379  
380        // step2: getting Model EJB entity reference:
381        Job varJob = null;
382        try {
383          varJob = machine.getLetterTemplateControllerEJB()
384            .getOldJob(ce.getJobId());
385        }
386        catch(Exception ee) {
387          problem = processProblem
388            (ee, LetterTemplateExceptionMessage.PROBLEM_JOB_WAS_DELETED);
389          machine.getLetterTemplateControllerEJB().setJobNull();
390        }
391  
392        // step3: invoking methods of entity EJB
393        if(varJob != null) {
394          try {
395            varJob.remove();
396            machine.getLetterTemplateControllerEJB().setJobNull();
397          }
398          catch(Exception ee) {
399            problem = processProblem
400              (ee, LetterTemplateExceptionMessage.PROBLEM_DELETE_JOB);
401          }
402        }
403  
404        // step4: invoking methods of session EJBs
405        CodeDescription[] jobs = varLetterTemplate.loadJobsCodes();
406        CodeDescription[] locs = varLetterTemplate.loadFTPCodes();
407        ArrayList templatesDescriptors
408           = varLetterTemplate.loadAllTemplatesDescription();
409  
410        // step5: building answer:
411        arrayAnswer = new ArrayList(4);
412        arrayAnswer.add(problem);
413        arrayAnswer.add(jobs);
414        arrayAnswer.add(locs);
415        arrayAnswer.add(templatesDescriptors);
416      }
417      catch(Exception e) {
418        throw new LetterTemplateEventException(e.getMessage());
419      }
420      return arrayAnswer;
421    }
422  
423  
424    /**
425     *  auxliary method that process a problem
426     *
427     * @param e Exception
428     * @param messageEx application message associated to e
429     * @return String with the complet problem and stacktrace
430     */
431    private String processProblem(Exception e, String messageEx) {
432      debug.println("processProblem: exception: " + e.getMessage());
433      String problem = messageEx + e.getMessage() + "|"
434        + CommonUtil.stackTraceToString(e);
435      return problem;
436    }
437  }
438  
439