1    package com.instantbank.component.lettertemplate.ejb;
2    
3    import java.util.ArrayList;
4    import java.util.Hashtable;
5    import java.sql.Date;
6    import java.rmi.RemoteException;
7    import java.sql.Connection;
8    
9    import javax.ejb.SessionBean;
10   import javax.ejb.SessionContext;
11   import javax.ejb.EJBException;
12   
13   import com.instantbank.component.lettertemplate.util.*;
14   
15   import com.instantbank.common.utilcomponents.CodeDescription;
16   import com.instantbank.common.utilcomponents.Debug;
17   import com.instantbank.common.utilcomponents.DAOException;
18   
19   
20   /**
21    *  Session Stateful EJB for lettertemplate application
22    *
23    * @author Instant-bank (Consuelo Franky, Roberto Contreras)
24    * @created August 2002
25    */
26   public class LetterTemplateEJB
27      implements SessionBean {
28   
29     // attributes corresponding to EJB state:
30     /**
31      *  current company
32      */
33     String companyId;
34     /**
35      *  current user
36      */
37     Long userId;
38   
39     // others attributes:
40     /**
41      *  session context
42      */
43     private SessionContext context;
44     /**
45      *  LetterTemplateDAO object for interacting with the database
46      */
47   
48     private LetterTemplateDAO lettertemplateDao;
49   
50     /**
51      * Hashtable of system Fields for current company
52      *                        key: fieldId (in Long), value: Field object
53      */
54   
55     private Hashtable systemAlias = null;
56     /**
57      * Hashtable of system Alias
58      *                        key:   String = tableAlias|rootType
59      *                        value: Alias object
60      */
61     private Hashtable systemFields = null;
62   
63     /**
64      * for debugging
65      */
66     private Debug debug = null;
67   
68   
69     // CONSTRUCTOR :
70     /**
71      *  Constructor
72      */
73     public LetterTemplateEJB() { }
74   
75   
76     // CONTEXT METHODS:
77     /**
78      *  Set session context
79      *
80      * @param sc The new sessionContext value
81      */
82     public void setSessionContext(SessionContext sc) {
83       debug = new Debug();
84       debug.setDebugginOn(true);
85       debug.setPreMessage("** LetterTemplateEJB");
86       this.context = sc;
87     }
88   
89   
90     /**
91      *  Actions after swapping
92      */
93     public void ejbActivate() {
94       try {
95         getDao();
96       }
97       catch(DAOException se) {
98         throw new RuntimeException(se.getMessage());
99       }
100      debug = new Debug();
101      debug.setDebugginOn(true);
102      debug.setPreMessage("** LetterTemplateEJB");
103    }
104  
105  
106    /**
107     *  Actions before swapping
108     */
109    public void ejbPassivate() {
110      lettertemplateDao = null;
111      debug = null;
112    }
113  
114  
115    // EJB METHODS:
116  
117    /**
118     *  Get attribute values: companyId, userId and LetterTemplateDAO
119     *  object (lettertemplateDao attribute)
120     *
121     * @param companyId current company
122     * @param userId current user
123     */
124    public void ejbCreate(String companyId, Long userId) {
125      // set the instance data:
126      this.companyId = companyId;
127      this.userId = userId;
128      try {
129        getDao();
130      }
131      catch(DAOException se) {
132        throw new RuntimeException(se.getMessage());
133      }
134    }
135  
136  
137    /**
138     *  Action before removing EJB instance
139     */
140    public void ejbRemove() { }
141  
142  
143    // BUSINESS METHODS FOR LOADING BASIC DATA OF LETTERS:
144  
145    /**
146     *  Loads data of system Fields for current company
147     *
148     * @return Hashtable of system Fields for current company
149     *           key: fieldId (in Long),
150     *           value: com.instantbank.component.job.util.Field object
151     * @exception DAOException
152     */
153    public Hashtable loadFields() throws DAOException {
154      debug.println("loadFields");
155  
156      Hashtable answer = null;
157      try {
158        getDao();
159        answer = lettertemplateDao.loadFields();
160      }
161      catch(DAOException se) {
162        debug.println("DAOException: " + se);
163        throw new DAOException(se.getMessage());
164      }
165      catch(Exception ex) {
166        throw new EJBException(ex.getMessage());
167      }
168      return answer;
169    }
170  
171  
172    /**
173     *  Loads company fields with display information and menu references
174     *  (for current company), ordered by fieldId;
175     *
176     * @return ArrayList of company fields where each element is an
177     *           inner ArrayList composed of: fieldId(Long), displayName(String),
178     *           dataType(String), menuId(Long), menuName (String)
179     * @exception DAOException
180     */
181    public ArrayList loadFieldsDisplay() throws DAOException {
182      debug.println("loadFieldsDisplay");
183  
184      ArrayList answer = null;
185      try {
186        getDao();
187        answer = lettertemplateDao.loadFieldsDisplay();
188      }
189      catch(DAOException se) {
190        debug.println("DAOException: " + se);
191        throw new DAOException(se.getMessage());
192      }
193      catch(Exception ex) {
194        throw new EJBException(ex.getMessage());
195      }
196      return answer;
197    }
198  
199  
200    /**
201     *  Loads system field Menus with its values
202     *  ordered by description (name of menu);
203     *
204     * @return ArrayList of menus where each element is an
205     *           inner ArrayList composed of:
206     *           menuName(String), menuValues(ArrayList);
207     *           each menuValues element is a String[]
208     *           with an internalValue(String) and display(String)
209     * @exception DAOException
210     */
211    public ArrayList loadFieldMenus() throws DAOException {
212      debug.println("loadFieldMenus");
213  
214      ArrayList answer = null;
215      try {
216        getDao();
217        answer = lettertemplateDao.loadFieldMenus();
218      }
219      catch(DAOException se) {
220        debug.println("DAOException: " + se);
221        throw new DAOException(se.getMessage());
222      }
223      catch(Exception ex) {
224        throw new EJBException(ex.getMessage());
225      }
226      return answer;
227    }
228  
229  
230    /**
231     *  Loads data of system Alias
232     *
233     * @return Hashtable of system Alias
234     *           key: String[2] = [tableAlias, rootType],
235     *           value: com.instantbank.component.job.util.Alias object
236     * @exception DAOException
237     */
238    public Hashtable loadAlias() throws DAOException {
239      debug.println("loadAlias");
240  
241      Hashtable answer = null;
242      try {
243        getDao();
244        answer = lettertemplateDao.loadAlias();
245      }
246      catch(DAOException se) {
247        debug.println("DAOException: " + se);
248        throw new DAOException(se.getMessage());
249      }
250      catch(Exception ex) {
251        throw new EJBException(ex.getMessage());
252      }
253      return answer;
254    }
255  
256  
257    /**
258     *  Loads name of letter variables of the current company, ordered by name
259     *
260     * @return ArrayList of variables where each element is an
261     *           inner ArrayList with the following elements of a variable:
262     *             fieldId(Long): code of field (variable) in the system,
263     *             name(String): name of variable to display in user interface,
264     *             type(String): type of variable:
265     *                                numeric: LetterTemplateGlobals.FIELD_NUMERIC,
266     *                                string: LetterTemplateGlobals.FIELD_STRING,
267     *                                date: LetterTemplateGlobals.FIELD_DATE
268     * @exception DAOException
269     */
270    public ArrayList loadVariables() throws DAOException {
271      debug.println("loadVariables service");
272  
273      ArrayList answer = null;
274      try {
275        getDao();
276        answer = lettertemplateDao.loadVariables();
277      }
278      catch(DAOException se) {
279        debug.println("DAOException: " + se);
280        throw new DAOException(se.getMessage());
281      }
282      catch(Exception ex) {
283        throw new EJBException(ex.getMessage());
284      }
285      return answer;
286    }
287  
288  
289    /**
290     *  Loads code and description of format of letter variables;
291     *  ordered by description
292     *
293     * @return CodeDescription[] object with [code, description] of formats
294     * @exception DAOException
295     */
296    public CodeDescription[] loadVariableFormats() throws DAOException {
297      debug.println("loadVariableFormats service");
298  
299      CodeDescription[] answer = null;
300      try {
301        getDao();
302        answer = lettertemplateDao.loadVariableFormats();
303      }
304      catch(DAOException se) {
305        debug.println("DAOException: " + se);
306        throw new DAOException(se.getMessage());
307      }
308      catch(Exception ex) {
309        throw new EJBException(ex.getMessage());
310      }
311      return answer;
312    }
313  
314  
315    /**
316     *  Loads code and description of printTypes; ordered by description
317     *
318     * @return CodeDescription[] object with [code, description] of printTypes
319     * @exception DAOException
320     */
321    public CodeDescription[] loadPrintTypes()
322       throws DAOException {
323      debug.println("loadPrintTypes service");
324  
325      CodeDescription[] answer = null;
326      try {
327        getDao();
328        answer = lettertemplateDao.loadPrintTypes();
329      }
330      catch(DAOException se) {
331        debug.println("DAOException: " + se);
332        throw new DAOException(se.getMessage());
333      }
334      catch(Exception ex) {
335        throw new EJBException(ex.getMessage());
336      }
337      return answer;
338    }
339  
340  
341    // BUSINESS METHODS FOR MANAGEMENT OF LETTERS CATEGORIES:
342  
343    /**
344     *  Loads all [code, description] of the categories of the current company;
345     *  ordered by name
346     *
347     * @return CodeDescription[] object with [code, description] of the categories
348     * @exception DAOException
349     */
350    public CodeDescription[] loadCategories()
351       throws DAOException {
352      debug.println("loadCategories service");
353  
354      CodeDescription[] answer = null;
355      try {
356        getDao();
357        answer = lettertemplateDao.loadCategories();
358      }
359      catch(DAOException se) {
360        debug.println("DAOException: " + se);
361        throw new DAOException(se.getMessage());
362      }
363      catch(Exception ex) {
364        throw new EJBException(ex.getMessage());
365      }
366      return answer;
367    }
368  
369  
370    /**
371     *  Applies a set of changes in the categories of the current company;
372     *
373     * @param items ArrayList with 3 elements of
374     *      type String[] representing the attributes of items to apply:
375     *      code, name and status;
376     *      status indicates if the item is for inserting, deleting or updating
377     * @return ArrayList with 2 elements:
378     *      (0) possible problem (String),
379     *      (1) current categories list (CodeDescription[])
380     * @exception DAOException
381     */
382    public ArrayList applyItemsCategory(ArrayList items)
383       throws DAOException {
384      debug.println("applyItemsCategory service");
385  
386      ArrayList answer = null;
387      try {
388        getDao();
389        answer = lettertemplateDao.applyItemsCategory(items);
390      }
391      catch(DAOException se) {
392        debug.println("DAOException: " + se);
393        throw new DAOException(se.getMessage());
394      }
395      catch(Exception ex) {
396        throw new EJBException(ex.getMessage());
397      }
398      return answer;
399    }
400  
401  
402    // BUSINESS METHODS FOR MANAGEMENT OF LETTERS IMAGES REPOSITORY:
403  
404    /**
405     *  Stores a new image for the current company
406     *
407     * @param nameImage name of image
408     * @param image bytes of image
409     * @return code (long) assigned to the image
410     * @exception DAOException
411     */
412    public long storeImageRepository(String nameImage, byte[] image)
413       throws DAOException {
414      long answer;
415      debug.println("storeImageRepository service");
416      try {
417        getDao();
418        answer = lettertemplateDao.storeImageRepository(nameImage, image);
419      }
420      catch(DAOException se) {
421        debug.println("DAOException: " + se);
422        throw new DAOException(se.getMessage());
423      }
424      catch(Exception ex) {
425        throw new EJBException(ex.getMessage());
426      }
427      return answer;
428    }
429  
430  
431    /**
432     *  Loads all [code, description] of the images of the current company;
433     *  ordered by name
434     *
435     * @return CodeDescription[] object with [code, description] of images
436     * @exception DAOException
437     */
438    public CodeDescription[] loadImagesNamesRepository()
439       throws DAOException {
440      debug.println("loadImagesNamesRepository service");
441  
442      CodeDescription[] answer = null;
443      try {
444        getDao();
445        answer = lettertemplateDao.loadImagesNamesRepository();
446      }
447      catch(DAOException se) {
448        debug.println("DAOException: " + se);
449        throw new DAOException(se.getMessage());
450      }
451      catch(Exception ex) {
452        throw new EJBException(ex.getMessage());
453      }
454      return answer;
455    }
456  
457  
458    /**
459     *  Loads an image of the current company;
460     *
461     * @param nameImage name of image
462     * @return byte[] with bytes of image
463     * @exception DAOException
464     */
465    public byte[] loadImageRepository(String nameImage)
466       throws DAOException {
467      debug.println("loadImageRepository service");
468  
469      byte[] answer = null;
470      try {
471        getDao();
472        answer = lettertemplateDao.loadImageRepository(nameImage);
473      }
474      catch(DAOException se) {
475        debug.println("DAOException: " + se);
476        throw new DAOException(se.getMessage());
477      }
478      catch(Exception ex) {
479        throw new EJBException(ex.getMessage());
480      }
481      return answer;
482    }
483  
484  
485    /**
486     *  Removes an image of the current company
487     *
488     * @param nameImage name of image
489     * @exception DAOException
490     */
491    public void removeImageRepository(String nameImage)
492       throws DAOException {
493      debug.println("removeImageRepository service");
494      try {
495        getDao();
496        lettertemplateDao.removeImageRepository(nameImage);
497      }
498      catch(DAOException se) {
499        debug.println("DAOException: " + se);
500        throw new DAOException(se.getMessage());
501      }
502      catch(Exception ex) {
503        debug.println("Exception: " + ex);
504        throw new EJBException(ex.getMessage());
505      }
506      return;
507    }
508  
509  
510    /**
511     *  Applies a set of changes in the images of the current company;
512     *
513     * @param items ArrayList with 2 elements of
514     *      type String[] representing the attributes of items to apply:
515     *      code, name and status;
516     *      status indicates if the item is for deleting or updating
517     * @return ArrayList with 2 elements
518     *      (0) possible problem (String),
519     *      (1) current images list (CodeDescription[])
520     * @exception DAOException
521     */
522    public ArrayList applyImagesRepository(ArrayList items)
523       throws DAOException {
524      debug.println("applyImagesRepository service");
525  
526      ArrayList answer = null;
527      try {
528        getDao();
529        answer = lettertemplateDao.applyImagesRepository(items);
530      }
531      catch(DAOException se) {
532        debug.println("DAOException: " + se);
533        throw new DAOException(se.getMessage());
534      }
535      catch(Exception ex) {
536        throw new EJBException(ex.getMessage());
537      }
538      return answer;
539    }
540  
541  
542    // BUSINESS METHODS FOR MANAGEMENT OF LETTERS TEMPLATES
543  
544    /**
545     *  Stores a letters template for the current company, generating code for
546     *  the template if it is new, and generating code for each component if it
547     *  is new.
548     *  If the template exists before, its version is validated and the
549     *  old template is replaced.
550     *  The associations between the template and its
551     *  components are established if these associations are new.
552     *
553     * @param template to store
554     * @return CodeDescription[] with 4 [code, stamp] in the following order:
555     *        template, HEADER, BODY, CLOSING
556     * @exception DAOException
557     */
558    public CodeDescription[] storeTemplate(Template template)
559       throws DAOException {
560      debug.println("storeTemplate service");
561  
562      CodeDescription[] answer = null;
563  
564      try {
565        getDao();
566        answer = lettertemplateDao.storeTemplate(template);
567      }
568      catch(DAOException se) {
569        debug.println("DAOException: " + se);
570        throw new DAOException(se.getMessage());
571      }
572      catch(Exception ex) {
573        throw new EJBException(ex.getMessage());
574      }
575      return answer;
576    }
577  
578  
579    /**
580     *  Loads code and description of all templates, for the current company and
581     *  for the specified category; ordered by name.
582     *
583     * @param category specified letter category
584     * @param printType specified print type
585     * @return CodeDescription[] object with [code, description] of templates
586     * @exception DAOException
587     */
588    public CodeDescription[] loadTemplatesDescription
589      (long category, int printType)
590       throws DAOException {
591      debug.println("loadTemplatesDescription service");
592  
593      CodeDescription[] answer = null;
594      try {
595        getDao();
596        answer = lettertemplateDao.loadTemplatesDescription(category, printType);
597      }
598      catch(DAOException se) {
599        debug.println("DAOException: " + se);
600        throw new DAOException(se.getMessage());
601      }
602      catch(Exception ex) {
603        throw new EJBException(ex.getMessage());
604      }
605      return answer;
606    }
607  
608  
609    /**
610     *  Loads code and description of one template and its components for the
611     *  specified template code
612     *
613     * @param templateCode code of template
614     * @return CodeDescription[] object with [code, description] of template
615     * @exception DAOException
616     */
617    public CodeDescription[] loadTemplateCodes(long templateCode)
618       throws DAOException {
619      debug.println("loadTemplateCodes [code, description] service");
620  
621      CodeDescription[] answer = null;
622      try {
623        getDao();
624        answer = lettertemplateDao.loadTemplateCodes(templateCode);
625      }
626      catch(DAOException se) {
627        debug.println("DAOException: " + se);
628        throw new DAOException(se.getMessage());
629      }
630      catch(Exception ex) {
631        throw new EJBException(ex.getMessage());
632      }
633      return answer;
634    }
635  
636  
637    /**
638     *  Loads code and stamp of one template and its components for the
639     *  specified template code
640     *
641     * @param category category of template
642     * @param printType type of print
643     * @param name name of template
644     * @return CodeDescription[] composed
645     *      by [code, stamp] of template and its components
646     *      ordered by template, HEADER, BODY, CLOSING
647     * @exception DAOException
648     */
649    public CodeDescription[] loadTemplateCodes
650      (long category, int printType, String name)
651       throws DAOException {
652  
653      debug.println("loadTemplateCodes [code, stamp] service");
654  
655      CodeDescription[] answer = null;
656      try {
657        getDao();
658        answer = lettertemplateDao.loadTemplateCodes
659          (category, printType, name);
660      }
661      catch(DAOException se) {
662        debug.println("DAOException: " + se);
663        throw new DAOException(se.getMessage());
664      }
665      catch(Exception ex) {
666        throw new EJBException(ex.getMessage());
667      }
668      return answer;
669    }
670  
671  
672    /**
673     *  Loads code and description of one template and its components for the
674     *  specified template code
675     *
676     * @param category category of template
677     * @param printType type of print
678     * @param name name of template
679     * @return CodeDescription[] composed
680     *      by [code, description] of template and its components ordered by
681     *      template, HEADER, BODY, CLOSING
682     * @exception DAOException
683     */
684    public CodeDescription[] loadTemplateCodeDescription
685      (long category, int printType, String name)
686       throws DAOException {
687  
688      debug.println("loadTemplateCodeDescription [code, description] service");
689  
690      CodeDescription[] answer = null;
691      try {
692        getDao();
693        answer = lettertemplateDao.loadTemplateCodeDescription
694          (category, printType, name);
695      }
696      catch(DAOException se) {
697        debug.println("DAOException: " + se);
698        throw new DAOException(se.getMessage());
699      }
700      catch(Exception ex) {
701        throw new EJBException(ex.getMessage());
702      }
703      return answer;
704    }
705  
706  
707    /**
708     *  Loads code and stamp of template and components
709     *
710     * @param category category of template
711     * @param printType type of print
712     * @param name name of template
713     * @param header name of component header
714     * @param body name of component body
715     * @param closing name of component closing
716     * @return CodeDescription[i] for i =  1(HEADER),2(BODY),3(CLOSING) is:
717     *      [code, stamp] of component, if it exists,
718     *      [LetterTemplateGlobals.UNDEF,LetterTemplateGlobals.STR_UNDEF] if component doesn't exist
719     * @exception DAOException
720     */
721    public CodeDescription[] loadCodesStamps
722      (long category, int printType, String name,
723       String header, String body, String closing)
724       throws DAOException {
725  
726      debug.println("loadCodesStamps service");
727  
728      CodeDescription[] answer = null;
729      try {
730        getDao();
731        answer = lettertemplateDao.loadCodesStamps
732          (category, printType, name, header, body, closing);
733      }
734      catch(DAOException se) {
735        debug.println("DAOException: " + se);
736        throw new DAOException(se.getMessage());
737      }
738      catch(Exception ex) {
739        throw new EJBException(ex.getMessage());
740      }
741      return answer;
742    }
743  
744  
745    /**
746     *  Loads a letters template of the current company, corresponding to a
747     *  code.
748     *
749     * @param templateCode code of template
750     * @return Template selected
751     * @exception DAOException
752     */
753    public Template loadTemplate(long templateCode)
754       throws DAOException {
755      debug.println("loadTemplate service");
756      Template answer = null;
757      try {
758        getDao();
759        answer = lettertemplateDao.loadTemplate(templateCode);
760      }
761      catch(DAOException se) {
762        debug.println("DAOException: " + se);
763        throw new DAOException(se.getMessage());
764      }
765      catch(Exception ex) {
766        throw new EJBException(ex.getMessage());
767      }
768      return answer;
769    }
770  
771  
772    /**
773     *  Removes a letters template (not its components) of the current company,
774     *  corresponding to a code.
775     *
776     * @param code code of template
777     * @exception DAOException
778     */
779    public void removeTemplate(long code)
780       throws DAOException {
781      debug.println("removeTemplate service");
782      try {
783        getDao();
784        lettertemplateDao.removeTemplate(code);
785      }
786      catch(DAOException se) {
787        debug.println("DAOException: " + se);
788        throw new DAOException(se.getMessage());
789      }
790      catch(Exception ex) {
791        throw new EJBException(ex.getMessage());
792      }
793    }
794  
795  
796    /**
797     *  Loads code, print type and name of templates and their categories names.
798     *
799     * @return ArrayList of variables where each element is an
800     *          inner ArrayList with the following elements of a variable:
801     *            code(Long): code of template,
802     *            nameCategory(String): name of category,
803     *            type(String): print type,
804     *            nameTemplate (String): name of template.
805     * @exception DAOException
806     */
807    public ArrayList loadAllTemplatesDescription()
808       throws DAOException {
809  
810      debug.println("loadAllTemplatesDescription service");
811  
812      ArrayList answer = null;
813      try {
814        getDao();
815        answer = lettertemplateDao.loadAllTemplatesDescription();
816      }
817      catch(DAOException se) {
818        debug.println("DAOException: " + se);
819        throw new DAOException(se.getMessage());
820      }
821      catch(Exception ex) {
822        throw new EJBException(ex.getMessage());
823      }
824      return answer;
825    }
826  
827  
828    /**
829     *  Applies a set of changes in the templates of the current company;
830     *
831     * @param items ArrayList with 4 elements of type String[]
832     *        representing the attributes of items to apply:
833     *        code of template, code of category, name of template and status;
834     *        status indicates if the item is for deleting or updating
835     * @return ArrayList with 2 elements:
836     *      (0) possible problem (String),
837     *      (1) current Template description list (ArrayList)
838     * @exception DAOException
839     */
840    public ArrayList applyItemsTemplate(ArrayList items)
841       throws DAOException {
842  
843      debug.println("applyItemsTemplate service");
844  
845      ArrayList answer = null;
846      try {
847        getDao();
848        answer = lettertemplateDao.applyItemsTemplate(items);
849      }
850      catch(DAOException se) {
851        debug.println("DAOException: " + se);
852        throw new DAOException(se.getMessage());
853      }
854      catch(Exception ex) {
855        throw new EJBException(ex.getMessage());
856      }
857      return answer;
858    }
859  
860  
861    /**
862     *  Load the date of template.
863     *
864     * @param templateCode Code of template.
865     * @return date date of template.
866     * @exception DAOException
867     */
868    public Date loadTemplateDate(long templateCode) throws DAOException {
869  
870      debug.println("loadTemplateDate service");
871      Date answer = null;
872      try {
873        getDao();
874        answer = lettertemplateDao.loadTemplateDate(templateCode);
875      }
876      catch(DAOException se) {
877        debug.println("DAOException: " + se);
878        throw new DAOException(se.getMessage());
879      }
880      catch(Exception ex) {
881        throw new EJBException(ex.getMessage());
882      }
883      return answer;
884    }
885  
886  
887    // BUSINESS METHODS FOR MANAGEMENT OF LETTERS COMPONENTS
888  
889    /**
890     *  Stores a letters component for the current company, generating code for
891     *  the component if it is new. If the component exists before, its version
892     *  is validated and the old component is replaced.
893     *
894     * @param component is the letter component to store
895     * @return CodeDescription: [code, stamp] of component
896     * @exception DAOException
897     */
898    public CodeDescription storeComponent(LetterComponent component)
899       throws DAOException {
900      debug.println("storeComponent service");
901  
902      CodeDescription answer = null;
903  
904      try {
905        getDao();
906        answer = lettertemplateDao.storeComponent(component);
907      }
908      catch(DAOException se) {
909        debug.println("DAOException: " + se);
910        throw new DAOException(se.getMessage());
911      }
912      catch(Exception ex) {
913        throw new EJBException(ex.getMessage());
914      }
915      return answer;
916    }
917  
918  
919    /**
920     *  Store as a letters component for the current company, generating code
921     *  for the component if it is new. If the component exists before, its
922     *  version is validated and the old component is replaced. The association
923     *  between the template and the component is established.
924     *
925     * @param templateCode Code of template
926     * @param component letters component
927     * @return CodeDescription [code, stamp] of component
928     * @exception DAOException
929     */
930    public CodeDescription storeComponentAs(long templateCode,
931                                            LetterComponent component)
932       throws DAOException {
933      debug.println("storeComponentAs service");
934  
935      CodeDescription answer = null;
936  
937      try {
938        getDao();
939        answer = lettertemplateDao.storeComponentAs(templateCode, component);
940      }
941      catch(DAOException se) {
942        debug.println("DAOException: " + se);
943        throw new DAOException(se.getMessage());
944      }
945      catch(Exception ex) {
946        throw new EJBException(ex.getMessage());
947      }
948      return answer;
949    }
950  
951  
952    /**
953     *  Loads code and description of all components, for the current company,
954     *  for the specified component type and print type; ordered by name
955     *
956     * @param componentType type of components to load
957     * @param printType print type of components to load
958     * @return CodeDescription[] object
959     *                         with [code, description] of components
960     * @exception DAOException
961     */
962    public CodeDescription[] loadComponentsDescription
963      (int componentType, int printType)
964       throws DAOException {
965      debug.println("loadComponentsDescription service");
966  
967      CodeDescription[] answer = null;
968      try {
969        getDao();
970        answer = lettertemplateDao.loadComponentsDescription
971          (componentType, printType);
972      }
973      catch(DAOException se) {
974        debug.println("DAOException: " + se);
975        throw new DAOException(se.getMessage());
976      }
977      catch(Exception ex) {
978        throw new EJBException(ex.getMessage());
979      }
980      return answer;
981    }
982  
983  
984    /**
985     *  Loads a letters component of the current company, corresponding to a
986     *  code.
987     *
988     * @param code code of letters component
989     * @return LetterComponent is the loaded component
990     * @exception DAOException
991     */
992    public LetterComponent loadComponent(long code)
993       throws DAOException {
994      debug.println("loadComponent service");
995      LetterComponent answer = null;
996      try {
997        getDao();
998        answer = lettertemplateDao.loadComponent(code);
999      }
1000     catch(DAOException se) {
1001       debug.println("DAOException: " + se);
1002       throw new DAOException(se.getMessage());
1003     }
1004     catch(Exception ex) {
1005       throw new EJBException(ex.getMessage());
1006     }
1007     return answer;
1008   }
1009 
1010 
1011   /**
1012    *  Removes a letters component of the current company, corresponding to a
1013    *  code.
1014    *
1015    * @param code code of component
1016    * @exception DAOException
1017    */
1018   public void removeComponent(long code)
1019      throws DAOException {
1020     debug.println("removeComponent service");
1021     LetterComponent answer = null;
1022     try {
1023       getDao();
1024       lettertemplateDao.removeComponent(code);
1025     }
1026     catch(DAOException se) {
1027       debug.println("DAOException: " + se);
1028       throw new DAOException(se.getMessage());
1029     }
1030     catch(Exception ex) {
1031       throw new EJBException(ex.getMessage());
1032     }
1033   }
1034 
1035 
1036   /**
1037    *  Load [code, stamp] for specified component
1038    *
1039    * @param componentType type of component (HEADER, BODY or CLOSING)
1040    * @param printType type of print
1041    * @param name name of component
1042    * @return CodeDescription [code, stamp] if the component exists,
1043    *        [LetterTemplateGlobals.UNDEF,LetterTemplateGlobals.STR_UNDEF] if the component doesn't exist
1044    * @exception DAOException
1045    */
1046   public CodeDescription loadComponentCode
1047     (int componentType, int printType, String name)
1048      throws DAOException {
1049 
1050     debug.println("loadComponentCode service");
1051 
1052     CodeDescription answer = null;
1053     try {
1054       getDao();
1055       answer = lettertemplateDao.loadComponentCode
1056         (componentType, printType, name);
1057     }
1058     catch(DAOException se) {
1059       debug.println("DAOException: " + se);
1060       throw new DAOException(se.getMessage());
1061     }
1062     catch(Exception ex) {
1063       throw new EJBException(ex.getMessage());
1064     }
1065     return answer;
1066   }
1067 
1068 
1069   /**
1070    *  If the relationship between Template and Component doesn't exist loads
1071    *  code, name and print type of components and name of component type.
1072    *
1073    * @return ArrayList of variables where each element is an
1074    *             inner ArrayList with the following elements of a  variable:
1075    *               code(Long): code of component,
1076    *               nameComponentType(String):name of component type,
1077    *               type(String): print type,
1078    *               nameComponent (String): name of component.
1079    * @exception DAOException
1080    */
1081   public ArrayList loadUnlikedComponents()
1082      throws DAOException {
1083 
1084     debug.println("loadUnlikedComponents service");
1085 
1086     ArrayList answer = null;
1087     try {
1088       getDao();
1089       answer = lettertemplateDao.loadUnlikedComponents();
1090     }
1091     catch(DAOException se) {
1092       debug.println("DAOException: " + se);
1093       throw new DAOException(se.getMessage());
1094     }
1095     catch(Exception ex) {
1096       throw new EJBException(ex.getMessage());
1097     }
1098     return answer;
1099   }
1100 
1101 
1102   /**
1103    *  Applies a set of changes in the components of the current company where
1104    *  the relationship between Template and Component doesn't exist.
1105    *
1106    * @param items ArrayList with 3 elements of
1107    *      type String[] representing the attributes of items to apply:
1108    *      code and name of component and status;
1109    *      status indicates if the item is for deleting or updating
1110    * @return ArrayList with 2 elements:
1111    *      (0) possible problem (String),
1112    *      (1) current Template description list (ArrayList)
1113    * @exception DAOException
1114    */
1115   public ArrayList applyItemsComponent(ArrayList items)
1116      throws DAOException {
1117     debug.println("applyItemsComponent service");
1118 
1119     ArrayList answer = null;
1120     try {
1121       getDao();
1122       answer = lettertemplateDao.applyItemsComponent(items);
1123     }
1124     catch(DAOException se) {
1125       debug.println("DAOException: " + se);
1126       throw new DAOException(se.getMessage());
1127     }
1128     catch(Exception ex) {
1129       throw new EJBException(ex.getMessage());
1130     }
1131     return answer;
1132   }
1133 
1134 
1135   // BUSINESS METHODS FOR LETTERS JOBS
1136 
1137   /**
1138    *  Gets template variables values for one loan of the current company,
1139    *  which will be used in a preview of the template.
1140    *
1141    * @param templateCode is the code of template to preview
1142    * @param loanCode is the code of the loan (i.e. agreement)
1143    * @param templateVarsCodes template variables codes
1144    * @return ArrayList of one tuple that
1145    *      is an Object[] of Strings; each String is the value of a template
1146    *      variable for the chosen loan
1147    * @exception DAOException
1148    */
1149   public ArrayList generatePreviewTemplate(long templateCode, long loanCode,
1150                                            Long[] templateVarsCodes) throws DAOException {
1151     debug.println("generatePreviewTemplate service");
1152 
1153     ArrayList answer = null;
1154     try {
1155       getSystemAlias();  // sets value to systemAlias attribute
1156       getSystemFields();  // sets value to systemFields attribute
1157       getDao();
1158       answer = lettertemplateDao.generatePreviewTemplate
1159         (templateCode, loanCode, templateVarsCodes,
1160         systemAlias, systemFields);
1161     }
1162     catch(DAOException se) {
1163       debug.println("DAOException: " + se);
1164       throw new DAOException(se.getMessage());
1165     }
1166     catch(Exception ex) {
1167       throw new EJBException(ex.getMessage());
1168     }
1169     return answer;
1170   }
1171 
1172 
1173   /**
1174    *  Gets a result set executing a SQL sentence,
1175    *  associated with a letter job.
1176    *
1177    * @param sentenceSQL SQL sentence for executing
1178    * @param selectFields output fields ids
1179    * @param jobORDERhashtable Hashtable of job ORDER elements:
1180    *                             key: fieldId (in Long), value: JobORDERelement
1181    * @return ArrayList[] as following:
1182    *          ArrayList[0]: first column of the result set (i.e. agreement code)
1183    *                        it is an ArrayList of String elements.
1184    *          ArrayList[1]: the others columns of the result set
1185    *                        it is an ArrayList of String[] elements.
1186    * @exception DAOException
1187    */
1188   public ArrayList[] executeSQL(String sentenceSQL, Long[] selectFields,
1189                                 Hashtable jobORDERhashtable) throws DAOException {
1190     debug.println("executeSQL service");
1191 
1192     ArrayList[] answer = null;
1193     try {
1194       getDao();
1195       getSystemFields();  // sets value to systemFields attribute
1196       answer = lettertemplateDao.executeSQL
1197         (sentenceSQL, selectFields,
1198         jobORDERhashtable, systemFields);
1199     }
1200     catch(DAOException se) {
1201       debug.println("DAOException: " + se);
1202       throw new DAOException(se.getMessage());
1203     }
1204     catch(Exception ex) {
1205       throw new EJBException(ex.getMessage());
1206     }
1207     return answer;
1208   }
1209 
1210 
1211   /**
1212    *  Loads all [code, name] of the ftp's of the current company,
1213    *  ordered by name
1214    *
1215    * @return CodeDescription[] object
1216    *           with [code, name] of the ftp's
1217    * @exception DAOException
1218    */
1219   public CodeDescription[] loadFTPCodes() throws DAOException {
1220     debug.println("loadFTPids service");
1221 
1222     CodeDescription[] answer = null;
1223     try {
1224       getDao();
1225       answer = lettertemplateDao.loadFTPCodes();
1226     }
1227     catch(DAOException se) {
1228       debug.println("DAOException: " + se);
1229       throw new DAOException(se.getMessage());
1230     }
1231     catch(Exception ex) {
1232       throw new EJBException(ex.getMessage());
1233     }
1234     return answer;
1235   }
1236 
1237 
1238   /**
1239    *  Loads all [code, name] of the jobs of the current company;
1240    *  ordered by name
1241    *
1242    * @return CodeDescription[] object
1243    *      with [code, name] of the jobs
1244    * @exception DAOException
1245    */
1246   public CodeDescription[] loadJobsCodes() throws DAOException {
1247     debug.println("loadJobsCodes service");
1248 
1249     CodeDescription[] answer = null;
1250     try {
1251       getDao();
1252       answer = lettertemplateDao.loadJobsCodes();
1253     }
1254     catch(DAOException se) {
1255       debug.println("DAOException: " + se);
1256       throw new DAOException(se.getMessage());
1257     }
1258     catch(Exception ex) {
1259       throw new EJBException(ex.getMessage());
1260     }
1261     return answer;
1262   }
1263 
1264 
1265   /**
1266    *  Loads all the calendars of the current company
1267    *
1268    * @return Hashtable of company calendars:
1269    *           key:   year (Integer),
1270    *           value: String of 365/366 characters representing the year calendar
1271    *                  (each day of year is a character :
1272    *                  'P' is it is workable(processing) day, 'N' otherwise)
1273    * @exception DAOException
1274    */
1275   public Hashtable loadCalendars() throws DAOException {
1276     debug.println("loadCalendars service");
1277 
1278     Hashtable answer = null;
1279     try {
1280       getDao();
1281       answer = lettertemplateDao.loadCalendars();
1282     }
1283     catch(DAOException se) {
1284       debug.println("DAOException: " + se);
1285       throw new DAOException(se.getMessage());
1286     }
1287     catch(Exception ex) {
1288       throw new EJBException(ex.getMessage());
1289     }
1290     return answer;
1291   }
1292 
1293 
1294 
1295   // BUSINESS METHODS TO LAUNCH AND REVIEW BATCH LETTER JOBS
1296 
1297   /**
1298    *  Loads all letters Job information.
1299    *
1300    * @return answer The LetterBatchStartModel object with all letters Job
1301    *         when each atribute represents: Jobs to run, Jobs out of frequency
1302    *         and other Jobs(inactive,no yet active and on request)
1303    * @exception DAOException
1304    */
1305   public LetterBatchStartModel loadLetterJob() throws DAOException {
1306 
1307     debug.println("loadLetterJob service");
1308     LetterBatchStartModel answer = null;
1309 
1310     try {
1311       getDao();
1312       answer = lettertemplateDao.loadLetterJob();
1313     }
1314     catch(DAOException se) {
1315       debug.println("DAOException: " + se);
1316       throw new DAOException(se.getMessage());
1317     }
1318     catch(Exception ex) {
1319       throw new EJBException(ex.getMessage());
1320     }
1321     return answer;
1322   }
1323 
1324 
1325   /**
1326    *  Loads the date (Oracle SYSDATE() function).
1327    *
1328    * @return date The date of Oracle SYSDATE() function.
1329    * @exception DAOException
1330    */
1331   public Date loadSysDate() throws DAOException {
1332 
1333     debug.println("loadSysDate service");
1334     Date answer = null;
1335 
1336     try {
1337       getDao();
1338       answer = lettertemplateDao.loadSysDate();
1339     }
1340     catch(DAOException se) {
1341       debug.println("DAOException: " + se);
1342       throw new DAOException(se.getMessage());
1343     }
1344     catch(Exception ex) {
1345       throw new EJBException(ex.getMessage());
1346     }
1347     return answer;
1348   }
1349 
1350 
1351   /**
1352    *  Loads the temporal directory path of the current company.
1353    *
1354    * @return path  Path for the Downloads generated by the system.
1355    * @exception DAOException
1356    */
1357   public String loadCompanyWorkDir() throws DAOException {
1358 
1359     debug.println("loadCompanyWorkDir service");
1360     String answer = null;
1361 
1362     try {
1363       getDao();
1364       answer = lettertemplateDao.loadCompanyWorkDir();
1365     }
1366     catch(DAOException se) {
1367       debug.println("DAOException: " + se);
1368       throw new DAOException(se.getMessage());
1369     }
1370     catch(Exception ex) {
1371       throw new EJBException(ex.getMessage());
1372     }
1373     return answer;
1374   }
1375 
1376 
1377   /**
1378    *  Store the log of executed letter jobs of the current company.
1379    *
1380    * @param success Indicates if the job was sucessfully executed (yes) or
1381    *       not (no).
1382    * @param resultSetLength Number of records retrieved by the job execution.
1383    * @param loggeableResultSet Result set with the records retrieved by
1384    *       the job execution.
1385    * @param foTemplate Transformed text of template used by the job execution.
1386    * @param jobId Description of the Parameter
1387    * @param executionDate Description of the Parameter
1388    * @return answer  The LettersJobLogBean object with
1389    *        the log code, job id and execution date of executed letter jobs.
1390    * @exception DAOException
1391    * @parm jobId The code of letter job.
1392    * @parm executionDate The date of execution letter job.
1393    */
1394   public LettersJobLogBean storeLetterJobLog(long jobId, Date executionDate,
1395                                              String success, long resultSetLength,
1396                                              String loggeableResultSet,
1397                                              String foTemplate) throws DAOException {
1398 
1399     debug.println("storeLetterJobLog service");
1400     LettersJobLogBean answer = null;
1401 
1402     try {
1403       getDao();
1404       answer = lettertemplateDao.storeLetterJobLog(jobId, executionDate, success,
1405         resultSetLength, loggeableResultSet, foTemplate);
1406     }
1407     catch(DAOException se) {
1408       debug.println("DAOException: " + se);
1409       throw new DAOException(se.getMessage());
1410     }
1411     catch(Exception ex) {
1412       throw new EJBException(ex.getMessage());
1413     }
1414     return answer;
1415   }
1416 
1417 
1418   /**
1419    *  Store the Log of ftps made for executed letter jobs
1420    *  of the current company.
1421    *
1422    * @param succes Indicates if the JOB_FILE was sucessfully sent by ftp (yes)
1423    *        or not (no).
1424    * @param attempts Number of failed attempts in sending by ftp the JOB_FILE.
1425    * @param ip IP address used in last attempt in sending by ftp the JOB_FILE.
1426    * @param path complementary PATH of LAST_FTP_IP_ADDRESS.
1427    * @param failureDescription explanation in failure case.
1428    * @param jobLogId Description of the Parameter
1429    * @param fullFilePath Description of the Parameter
1430    * @exception DAOException
1431    * @parm jobLogId The code of letter job.
1432    */
1433   public void storeLetterFtpJobLog(long jobLogId, String fullFilePath,
1434                                    boolean succes, int attempts,
1435                                    String ip, String path,
1436                                    String failureDescription)
1437      throws DAOException {
1438 
1439     debug.println("storeLetterFtpJobLog service");
1440 
1441     try {
1442       getDao();
1443       lettertemplateDao.storeLetterFtpJobLog(jobLogId, fullFilePath,
1444         succes, attempts, ip, path,
1445         failureDescription);
1446     }
1447     catch(DAOException se) {
1448       debug.println("DAOException: " + se);
1449       throw new DAOException(se.getMessage());
1450     }
1451     catch(Exception ex) {
1452       throw new EJBException(ex.getMessage());
1453     }
1454 
1455   }
1456 
1457 
1458   /**
1459    *  Update the Log of ftps made for executed letter jobs
1460    *  of the current company.
1461    *
1462    * @param succes Indicates if the JOB_FILE was sucessfully sent by ftp (yes)
1463    *        or not (no).
1464    * @param attempts Number of failed attempts in sending by ftp the JOB_FILE.
1465    * @param ip IP address used in last attempt in sending by ftp the JOB_FILE.
1466    * @param path complementary PATH of LAST_FTP_IP_ADDRESS.
1467    * @param failureDescription explanation in failure case.
1468    * @param jobLogId Description of the Parameter
1469    * @param fullFilePath Description of the Parameter
1470    * @exception DAOException
1471    * @parm jobLogId The code of letter job.
1472    */
1473   public void updateLetterFtpJobLog(long jobLogId, String fullFilePath,
1474                                     boolean succes, int attempts,
1475                                     String ip, String path,
1476                                     String failureDescription)
1477      throws DAOException {
1478     debug.println("updateLetterFtpJobLog service");
1479 
1480     try {
1481       getDao();
1482       lettertemplateDao.updateLetterFtpJobLog(jobLogId, fullFilePath,
1483         succes, attempts, ip, path,
1484         failureDescription);
1485     }
1486     catch(DAOException se) {
1487       debug.println("DAOException: " + se);
1488       throw new DAOException(se.getMessage());
1489     }
1490     catch(Exception ex) {
1491       throw new EJBException(ex.getMessage());
1492     }
1493 
1494   }
1495 
1496 
1497   /**
1498    *  Update the  date of execution of the letter job
1499    *  of the current company.
1500    *
1501    * @param date The  date of execution of the letter job.
1502    * @param jobId Description of the Parameter
1503    * @exception DAOException
1504    * @parm jobLogId The code of letter job.
1505    */
1506   public void updateJobExecDate(long jobId, Date date) throws DAOException {
1507 
1508     debug.println("updateJobExecDate service");
1509 
1510     try {
1511       getDao();
1512       lettertemplateDao.updateJobExecDate(jobId, date);
1513     }
1514     catch(DAOException se) {
1515       debug.println("DAOException: " + se);
1516       throw new DAOException(se.getMessage());
1517     }
1518     catch(Exception ex) {
1519       throw new EJBException(ex.getMessage());
1520     }
1521   }
1522 
1523 
1524   /**
1525    *  Load the data of company locations.
1526    *
1527    * @param ftpId The code of ftp.
1528    * @return answer The CompanyLocation object with location id, path
1529    *        ip address, ftp login name and ftp password.
1530    * @exception DAOException
1531    */
1532   public CompanyLocation loadCompanyLocation(long ftpId) throws DAOException {
1533 
1534     debug.println("loadCompanyLocation service");
1535     CompanyLocation answer = null;
1536     try {
1537       getDao();
1538       answer = lettertemplateDao.loadCompanyLocation(ftpId);
1539     }
1540     catch(DAOException se) {
1541       debug.println("DAOException: " + se);
1542       throw new DAOException(se.getMessage());
1543     }
1544     catch(Exception ex) {
1545       throw new EJBException(ex.getMessage());
1546     }
1547     return answer;
1548   }
1549 
1550 
1551   /**
1552    *  Review the batch of letters jobs
1553    *
1554    * @param dayMonthYear The date to ask for letter Job Log information
1555    * @return answer The LetterBatchReviewModel object with Job Log information.
1556    * @exception DAOException
1557    */
1558   public LetterBatchReviewModel loadLetterBatchReview(String dayMonthYear)
1559      throws DAOException {
1560 
1561     debug.println("loadLetterBatchReview service");
1562     LetterBatchReviewModel answer = null;
1563     try {
1564       getDao();
1565       answer = lettertemplateDao.loadLetterBatchReview(dayMonthYear);
1566     }
1567     catch(DAOException se) {
1568       debug.println("DAOException: " + se);
1569       throw new DAOException(se.getMessage());
1570     }
1571     catch(Exception ex) {
1572       throw new EJBException(ex.getMessage());
1573     }
1574     return answer;
1575   }
1576 
1577   // AUXILIARY METHODS
1578 
1579   /**
1580    *  Obtains a DAO instance assigning to lettertemplateDao attribute
1581    *
1582    * @return The dao value
1583    * @exception DAOException
1584    */
1585   private LetterTemplateDAO getDao() throws DAOException {
1586     if(lettertemplateDao == null) {
1587       lettertemplateDao =
1588         new LetterTemplateDAO(companyId, userId);
1589     }
1590     return lettertemplateDao;
1591   }
1592 
1593 
1594   /**
1595    *  Obtains a value for systemAlias attribute
1596    *
1597    * @return The systemAlias value
1598    * @exception DAOException
1599    */
1600   private Hashtable getSystemAlias() throws DAOException {
1601     if(systemAlias == null) {
1602       debug.println("systemAlias must be loaded");
1603       systemAlias = loadAlias();
1604     }
1605     return systemAlias;
1606   }
1607 
1608 
1609   /**
1610    *  Obtains a value for systemFields attribute
1611    *
1612    * @return The systemFields value
1613    * @exception DAOException
1614    */
1615   private Hashtable getSystemFields() throws DAOException {
1616     if(systemFields == null) {
1617       systemFields = loadFields();
1618     }
1619     return systemFields;
1620   }
1621 
1622 
1623   public void insertLetterARHistory(Connection dbConnection, long lCompID, long lAgrmID, Date procDate, long lLettID, long lLettCode, long lActionCode, long lResultCode)
1624      throws RemoteException, DAOException {
1625     debug.println("Calling LetterTemplateDAO for Letters Action Result");
1626 
1627     try {
1628       getDao();
1629       lettertemplateDao.insertLetterARHistory(dbConnection, lCompID, lAgrmID, procDate, lLettID, lLettCode, lActionCode, lResultCode);
1630     }
1631     catch(DAOException se) {
1632       debug.println("DAOException: " + se);
1633       throw new DAOException(se.getMessage());
1634     }
1635     catch(Exception ex) {
1636       throw new EJBException(ex.getMessage());
1637     }
1638   }
1639 
1640 }
1641 
1642