1    package com.instantbank.lettertemplate.editor.applet;
2    
3    import java.io.ObjectInputStream;
4    import java.io.Serializable;
5    
6    import java.net.URL;
7    import java.net.MalformedURLException;
8    
9    import java.util.ArrayList;
10   
11   import java.applet.AppletContext;
12   
13   import com.instantbank.common.uiutils.MessageFrame;
14   
15   import com.instantbank.lettertemplate.editor.util.LetterOp;
16   import com.instantbank.component.lettertemplate.util.LetterComponent;
17   import com.instantbank.component.lettertemplate.util.Template;
18   import com.instantbank.common.utilcomponents.CodeDescription;
19   
20   /**
21    *  Proxy for the Template Editor servlet. Sends commands and receives results
22    *  from the servlet.
23    *
24    * @author InstantBank (Rodrigo Lopez)
25    * @created September 2002
26    */
27   public class TemplateEditorProxy {
28     /**
29      *  The fully-qualified URL to the called servlet.
30      */
31     URL servlet;
32   
33     /**
34      *  The complete URL to the servlet web server (for instance,
35      *  'http://www.sun.com:8080').
36      */
37     URL webBase;
38   
39     /**
40      *  The URL of the timeout page
41      */
42     URL timeout;
43   
44     /**
45      *  Input stream from the servlet.
46      */
47     ObjectInputStream in;
48   
49   
50     /**
51      *  TemplateEditorProxy constructor.
52      *
53      * @param web Servlet's Web server URL (excluding
54      *      servlet path).
55      * @throws MalformedURLException Description of the Exception
56      */
57     public TemplateEditorProxy(URL web) throws MalformedURLException {
58       webBase = web;
59       servlet = new URL
60         (webBase,
61         "/lettertemplate/editor/templatecontroller");
62       timeout = new URL
63         (webBase,
64         "/lettertemplate/control_web/TimeOutPage.jsp");
65       //Dbg MessageFrame.getFrame().addText("servlet =" + servlet.toExternalForm());
66     }
67   
68   
69     /**
70      *  Goes to the server and verifies if time out has delayed. If it has,
71      *  control flows to the {@link #timeout} page; otherwise it returns silently.
72      *
73      * @param appContext Context of the applet that invokes the service.
74      * @return boolean: true if it exists timeout, false otherwise
75      */
76     public boolean controlTimeout(AppletContext appContext) {
77       Integer opCode = new Integer(LetterOp.CONTROLTIMEOUT);
78       Serializable objs[] = {opCode};
79       boolean existsTimeout = false;
80       try {
81         in = TemplateEditorSender.postObjects(servlet, objs);
82         existsTimeout = ((Boolean)in.readObject()).booleanValue();
83         in.close();
84         if(existsTimeout) {
85           appContext.showDocument(timeout);
86         }
87       }
88       catch(Throwable ex) {
89         appContext.showDocument(timeout);
90       }
91       return existsTimeout;
92     }
93   
94   
95     /**
96      *  Loads the names of the images from the data base.
97      *
98      * @return An array of images names.
99      * @throws Exception Description of the Exception
100     */
101  
102    public String[] loadImagesNamesRepository() throws Exception {
103      Integer opCode = new Integer(LetterOp.LOADIMGNAMES);
104      Serializable objs[] = {opCode};
105      in = TemplateEditorSender.postObjects(servlet, objs);
106      String[] resultado = (String[])in.readObject();
107      in.close();
108      return resultado;
109    }
110  
111  
112    /**
113     *  Asks for the service of storing a template.
114     *
115     * @param template The full "in memory" representation of a template.
116     * @return An array of < code, stamp> of the stored elements
117     *      in the <a href="./package-summary.html#thbc">THBC order.
118     *      </a>
119     * @throws Exception Description of the Exception
120     */
121    public CodeDescription[] storeTemplate(Template template) throws Exception {
122      Integer opCode = new Integer(LetterOp.STORETEMPLATE);
123      Serializable objs[] = {opCode, template};
124      in = TemplateEditorSender.postObjects(servlet, objs);
125      CodeDescription[] resultado = (CodeDescription[])in.readObject();
126      in.close();
127      return resultado;
128    }
129  
130  
131    /**
132     *  Asks the service of loading an image --of the current company-- from the
133     *  data base.
134     *
135     * @param name The name of the image in the data base.
136     * @return An array containing the raw bytes of the image. see
137     *      {@link com.instantbank.component.lettertemplate.ejb.LetterTemplate#loadImageRepository(String)}
138     *      and {@link com.instantbank.lettertemplate.editor.web.TemplateEditorServlet#doPost(HttpServletRequest,HttpServletResponse)}
139     * @throws Exception Description of the Exception
140     */
141    public byte[] loadImageRepository(String name) throws Exception {
142      Integer opCode = new Integer(LetterOp.LOADIMG);
143      Serializable objs[] = {opCode, name};
144      in = TemplateEditorSender.postObjects(servlet, objs);
145      byte[] res = (byte[])in.readObject();
146      in.close();
147      return res;
148    }
149  
150  
151    /**
152     *  exit
153     *
154     * @param url
155     * @throws Exception Description of the Exception
156     */
157    public void exit(String url) throws Exception {
158      Integer opCode = new Integer(LetterOp.EXIT);
159      Serializable objs[] = {opCode, url};
160      TemplateEditorSender.postObjectsNoAnswer(servlet, objs);
161    }
162  
163  
164    /**
165     *  Asks for code and description of the template categories
166     *
167     * @return An array of < code,name> pairs of categories
168     *      sorted by name.
169     * @throws Exception Description of the Exception
170     */
171    public CodeDescription[] loadCategories() throws Exception {
172      Serializable objs[] = {new Integer(LetterOp.LOADCATEGORIES)};
173      in = TemplateEditorSender.postObjects(servlet, objs);
174      CodeDescription[] res = (CodeDescription[])in.readObject();
175      in.close();
176  
177      return res;
178    }
179  
180  
181    /**
182     *  Asks for code and description of the templates, for a given category and
183     *  print type.
184     *
185     * @param category The category code for the enquired templates.
186     * @param pType Print type.
187     * @return An array of < code,name> pairs of templates sorted
188     *      by name.
189     * @throws Exception Description of the Exception
190     */
191    public CodeDescription[] loadTemplatesDescription(long category, int pType)
192       throws Exception {
193      Serializable objs[] = {new Integer(LetterOp.LOADTEMPLNAMES),
194        new Long(category),
195        new Integer(pType)};
196  
197      in = TemplateEditorSender.postObjects(servlet, objs);
198      CodeDescription[] res = (CodeDescription[])in.readObject();
199      in.close();
200      return res;
201    }
202  
203  
204    /**
205     *  Asks for the service of loading < code, stamp> for a template and its
206     *  components.
207     *
208     * @param code The template's code.
209     * @return A < code, stamp> array of the template in the <a
210     *      href="./package-summary.html#thbc">THBC order.</a>
211     * @throws Exception Description of the Exception
212     */
213    public CodeDescription[] loadTemplateCodes(long code) throws Exception {
214      Serializable objs[] = {new Integer(LetterOp.LOADTEMPLCODES),
215        new Long(code)
216        };
217  
218      in = TemplateEditorSender.postObjects(servlet, objs);
219      CodeDescription[] res = (CodeDescription[])in.readObject();
220      in.close();
221      return res;
222    }
223  
224  
225    /**
226     *  Asks for code and description of the components, for a given component
227     *  type and print type.
228     *
229     * @param cmpType Component type.
230     * @param pType Print type.
231     * @return An array of < code,name> pairs of components
232     *      sorted by name.
233     * @throws Exception Description of the Exception
234     */
235    public CodeDescription[] loadComponentsDescription(int cmpType, int pType)
236       throws Exception {
237      Serializable objs[] = {new Integer(LetterOp.LOADCOMPDESCR),
238        new Integer(cmpType),
239        new Integer(pType)};
240  
241      in = TemplateEditorSender.postObjects(servlet, objs);
242      CodeDescription[] res = (CodeDescription[])in.readObject();
243      in.close();
244      return res;
245    }
246  
247  
248    /**
249     *  Loads a template from the data base.
250     *
251     * @param code The template's code.
252     * @return A pair containing the full template structure extracted
253     *            from the data base and a message containing the names of
254     *            the variables with inconsistent date offsets. Those offsets
255     *            are forced to zero.
256     * @throws Exception Description of the Exception
257     */
258    public ArrayList loadTemplate(long code) throws Exception {
259      Serializable objs[] = {new Integer(LetterOp.LOADTEMPL), new Long(code)};
260      in = TemplateEditorSender.postObjects(servlet, objs);
261      ArrayList res = (ArrayList)in.readObject();
262      in.close();
263      return res;
264    }
265  
266  
267    /**
268     *  Asks the service of loading code and stamp of a template and components.
269     *  The template and components don't have to be related.
270     *
271     * @param category Category of template
272     * @param printType Print type
273     * @param header Name of the header component
274     * @param body Name of the body component
275     * @param closing Name of the closing component
276     * @param template
277     * @return A < code,stamp> array of the template and
278     *      components in the <a href="./package-summary.html#thbc">
279     *      THBC order.</a> In all cases, if the template or component doesn't
280     *      exist the returned value is: <tt> < LetterTemplateGlobals.UNDEF,
281     *      LetterTemplateGlobals.STR_UNDEF></tt> .
282     * @throws Exception Description of the Exception
283     */
284    public CodeDescription[] loadCodesStamps(long category, int printType,
285                                             String template, String header, String body, String closing)
286       throws Exception {
287      Serializable objs[] = {new Integer(LetterOp.LOADCODESTAMP),
288        new Long(category),
289        new Integer(printType),
290        template, header, body, closing
291        };
292      in = TemplateEditorSender.postObjects(servlet, objs);
293      CodeDescription[] res = (CodeDescription[])in.readObject();
294      in.close();
295      return res;
296    }
297  
298  
299    /**
300     *  Loads the < code,stamp> of a component from the database.
301     *
302     * @param compType Type of the component (Header/Body/Closing).
303     * @param printType LASER/TYPEWRITTER
304     * @param name Component's name
305     * @return The < code, stamp> of the component. If the
306     *      component doesn't exists the returned value is <tt> <
307     *      LetterTemplateGlobals.UNDEF, LetterTemplateGlobals.STR_UNDEF></tt> .
308     * @throws Exception Description of the Exception
309     */
310    public CodeDescription loadComponentCode(int compType, int printType, String name)
311       throws Exception {
312      Serializable objs[] = {new Integer(LetterOp.LOADCOMPCODE),
313        new Integer(compType),
314        new Integer(printType),
315        name};
316      in = TemplateEditorSender.postObjects(servlet, objs);
317      CodeDescription res = (CodeDescription)in.readObject();
318      in.close();
319      return res;
320    }
321  
322  
323    /**
324     *  Asks for the service of loading a full {@link LetterComponent} from the
325     *  dadabase.
326     *
327     * @param code The code of the component.
328     * @return A pair containing the full component structure extracted
329     *            from the data base and a message containing the names of
330     *            the variables with inconsistent date offsets. Those offsets
331     *            are forced to zero.
332     * @throws Exception Description of the Exception
333     */
334    public ArrayList loadComponent(long code) throws Exception {
335      Serializable objs[] = {new Integer(LetterOp.LOADCOMPONENT),
336        new Long(code)
337        };
338      in = TemplateEditorSender.postObjects(servlet, objs);
339      ArrayList res = (ArrayList)in.readObject();
340      in.close();
341      return res;
342    }
343  
344  
345    /**
346     *  Asks for the service of storing a letter component without changing its
347     *  links with any component. An old version of the component must exist
348     *  before asking the service.
349     *
350     * @param component The component to be saved.
351     * @return The < code, stamp> of the saved component.
352     * @throws Exception Description of the Exception
353     */
354    public CodeDescription storeComponent(LetterComponent component)
355       throws Exception {
356      Serializable objs[] = {new Integer(LetterOp.STORECOMPONENT),
357        component
358        };
359      in = TemplateEditorSender.postObjects(servlet, objs);
360      CodeDescription res = (CodeDescription)in.readObject();
361      in.close();
362      return res;
363    }
364  
365  
366    /**
367     *  Asks for the service of storing a letter component bound to a template.
368     *  Following its type (header, body, closing) it also replaces the
369     *  corresponding component of the template.
370     *
371     * @param templateCode Code of the template bound to the component.
372     * @param component The component to be saved and linked to the template.
373     * @return The < code,stamp> of the saved component.
374     * @throws Exception Description of the Exception
375     */
376    public CodeDescription storeComponentAs(
377                                            long templateCode, LetterComponent component) throws Exception {
378      Serializable objs[] = {new Integer(LetterOp.STORECOMPONENTAS),
379        new Long(templateCode),
380        component
381        };
382      in = TemplateEditorSender.postObjects(servlet, objs);
383      CodeDescription res = (CodeDescription)in.readObject();
384      in.close();
385      return res;
386    }
387  
388  
389    /**
390     *  Asks for the service of loading loan variables.
391     *
392     * @return An ArrayList of ArrayLists containing the information
393     *      about all loan variables. The internal ArrayList contains [code, name,
394     *      type] of a variable.
395     * @throws Exception Description of the Exception
396     */
397    public ArrayList loadVariables() throws Exception {
398      Serializable objs[] = {new Integer(LetterOp.LOADVARIABLES)};
399      in = TemplateEditorSender.postObjects(servlet, objs);
400      ArrayList res = (ArrayList)in.readObject();
401      in.close();
402      return res;
403    }
404  
405  
406    /**
407     *  Asks for the service of loading variable formats.
408     *
409     * @return An array of CodeDescription containing
410     *      [code,description] of all variable formats.
411     * @throws Exception Description of the Exception
412     */
413    public CodeDescription[] loadVariableFormats() throws Exception {
414      Serializable objs[] = {new Integer(LetterOp.LOADVARIABLEFORMATS)};
415      in = TemplateEditorSender.postObjects(servlet, objs);
416      CodeDescription[] res = (CodeDescription[])in.readObject();
417      in.close();
418      return res;
419    }
420  
421  
422    /**
423     * Asks for an extra "debugging" service.
424     *
425     * @return Description of the Return Value
426     * @throws Exception Description of the Exception
427     */
428    public String extraservice() throws Exception {
429      Serializable objs[] = {new Integer(6210)};
430      in = TemplateEditorSender.postObjects(servlet, objs);
431      String res = (String)in.readObject();
432      in.close();
433      return res;
434    }
435  
436  }
437