1 package com.instantbank.lettertemplate.templatePreview.web;
2
3
4 import java.io.IOException;
5 import java.io.ByteArrayOutputStream;
6 import java.io.File;
7 import java.io.FileOutputStream;
8
9 import java.util.ArrayList;
10 import java.util.Date;
11 import java.util.Set;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletOutputStream;
15
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
20
21
22 import javax.naming.InitialContext;
23 import com.instantbank.lettertemplate.control.util.JSPUtil;
24 import com.instantbank.lettertemplate.control.util.WebKeys;
25 import com.instantbank.lettertemplate.editor.web.TemplateTransformer;
26
27 import com.instantbank.component.lettertemplate.util.LetterViewable;
28 import com.instantbank.component.lettertemplate.util.Template;
29
30 import com.instantbank.component.lettertemplate.ejb.LetterTemplate;
31 import com.instantbank.component.lettertemplate.ejb.LetterTemplateHome;
32
33
34 import com.instantbank.common.utilcomponents.Debug;
35 import com.instantbank.common.utilcomponents.LetterTemplateExceptionMessage;
36 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
37 import com.instantbank.common.utilcomponents.JNDINames;
38 import com.instantbank.common.utilcomponents.CommonUtil;
39
40
41
48 public class PdfShowServlet extends HttpServlet {
49
52 private LetterTemplateHome lethome = null;
53
54
57 private LetterTemplate letejb = null;
58
59
62 private Debug debug = null;
63
64
65
70 public void init() throws ServletException {
71 debug = new Debug();
72 debug.setDebugginOn(true);
73 debug.setPreMessage("** PdfShowServlet: ");
74
75
76 try {
77
78 InitialContext ctx = new InitialContext();
79 lethome = (LetterTemplateHome)ctx.lookup
80 (JNDINames.LETTERTEMPLATE_EJBHOME);
81 }
82 catch(Exception e) {
83 e.printStackTrace();
84 debug.println("Problem instantiating LetterTemplate EJB:"
85 + e.toString());
86 throw new ServletException
87 (LetterTemplateExceptionMessage.PROBLEM_INSTANTIATING_LETTERS_EJB
88 + e.getMessage());
89 }
90 }
91
92
93
102 public void doGet(HttpServletRequest request, HttpServletResponse response)
103 throws ServletException, IOException {
104 HttpSession session = request.getSession();
105 debug.println("new request=========================");
106
107 if(CommonUtil.controlTimeout(request, response,
108 getServletConfig().getServletContext())) {
109 debug.println("invalidate session and return by timeout");
110 session.invalidate();
111 return;
112
113
114 }
115
116 String viewType = request.getParameter("viewableType");
117 String strViewCode = request.getParameter("viewableCode");
118 long viewableCode = Long.parseLong(strViewCode);
119 long loanCode = LetterTemplateGlobals.DUMMY_LOAN;
120 String strLoanCode = request.getParameter("loanCode");
121 if(!strLoanCode.equals("DUMMY")) {
122 loanCode = Long.parseLong(strLoanCode);
123 }
124 TemplateTransformer transf;
125 ServletOutputStream out = null;
126 try {
127 out = response.getOutputStream();
128
129
130 String temporalPath = (String)request.getSession()
131 .getAttribute(WebKeys.ImagesTemporalPath);
132
135
136 letejb = (LetterTemplate)request.getSession()
137 .getAttribute(WebKeys.LetterTemplateEJB);
138
139 if(letejb == null) {
140 debug.println("creating reference to LetterTemplate EJB");
141 String companyId = (String)request.getSession()
142 .getAttribute(WebKeys.CompanyId);
143 Long userId = (Long)request.getSession()
144 .getAttribute(WebKeys.UserId);
145 letejb = lethome.create(companyId, userId);
146 request.getSession().setAttribute
147 (WebKeys.LetterTemplateEJB, letejb);
148 }
149
150
151 LetterViewable viewable;
152
153 if(viewType.equals("TEMPLATE")) {
154 viewable = letejb.loadTemplate(viewableCode);
155 }
156 else {
157 viewable = letejb.loadComponent(viewableCode);
158 }
159
160 if(viewable == null) {
161 throw new Exception
162 (LetterTemplateExceptionMessage.DOCUMENT_NULL);
163 }
164
165 String foTemplate = viewable.toFoTemplate();
166
167 if(foTemplate == null) {
168 throw new Exception(LetterTemplateExceptionMessage.PROBLEMS_CALCULATING_FO_TEMPLATE);
169 }
170
171 ArrayList[] viewImages = viewable.getAllImages();
172 ArrayList[] setOfVars = viewable.setOfVariables();
173 String foFileName = viewable.getName() + new Date().getTime();
174
175
176
177 transf = new TemplateTransformer(temporalPath, foTemplate,
178 viewImages, setOfVars, foFileName, letejb);
179
180 Set inconsistentVars = transf.offsetConsistency();
181 if(!inconsistentVars.isEmpty()) {
182 String type = "template";
183 if(!viewType.equals("TEMPLATE")) {
184 type = "component";
185 }
186 String msg = "There are date offset inconsistencies in this " +
187 type + ". Please, check with the template editor " +
188 "or with the system administrator.";
189 response.setContentType("text/html");
190 out.println("<H3>WARNING: " + msg + "</H3>");
191 return;
192 }
193
194 if(viewable.getPrintType() == Template.TYPEWRITTER) {
195 transf.setRenderMode(TemplateTransformer.RENDER_TXT);
196 }
197
198
199
200
201
202 ArrayList varValues = null;
203 if(viewable.hasVariables()) {
204 varValues =
205 letejb.generatePreviewTemplate
206 (viewType.equals("TEMPLATE") ? viewable.getCode() :
207 LetterTemplateGlobals.UNDEF,
208 loanCode,
209 TemplateTransformer.varCodesFromSet(setOfVars));
210 }
211
212
213 TemplateTransformer.printSetOfVars(setOfVars);
214 if(varValues != null) {
215 TemplateTransformer.printRawVars(
216 TemplateTransformer.varCodesFromSet(setOfVars),
217 (String[])varValues.get(0));
218 }
219
220
221
222 ByteArrayOutputStream letter =
223 transf.transform(varValues == null ?
224 new String[0] :
225 (String[])varValues.get(0));
226
227 if(letter == null) {
228 throw new Exception("It is impossible the preview of Template, "
229 + "because of problems generating the final document");
230 }
231
232
233
234 byte[] letterBytes = letter.toByteArray();
235 if(viewable.getPrintType() == Template.TYPEWRITTER) {
236
237 response.setContentType("text/plain");
238 response.setContentLength(letterBytes.length);
239 out.write(letterBytes, 0, letterBytes.length);
240 out.flush();
241 }
242 else {
243 response.setContentType("application/pdf");
244 response.setContentLength(letterBytes.length);
245 out.write(letterBytes, 0, letterBytes.length);
246 out.flush();
247 }
248
249
250 transf.cleanfiles();
251
252 }
253 catch(Exception ex) {
254 debug.println(CommonUtil.stackTraceToString(ex));
255 response.setContentType("text/html");
256 out.println("<H3>EXCEPTION: " + ex.getMessage() + "</H3>");
257 }
258 }
259
260
261
269 public void doPost(HttpServletRequest request, HttpServletResponse response)
270 throws ServletException, IOException {
271 doGet(request, response);
272 }
273
274 }
275