Topic Text   Topic Comments (0)   Topic Properties   Topic Information vidhyalakshmi.prasanna@bam...
Topic title: test Tuesday January 7, 2014 12:47:07

Download topic text | View in variable-width font | Tab width set to 4 (change to 8)

Files in topic:  
[Jump to] DUUtil.java   {+780,-0}

[Add General Comment] to topic.

File DUUtil.java (Revision 1.0) [Add File Comment] [Top]
 
1 package com.bofa.fast.du.utilities;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.math.BigDecimal;
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.text.DateFormat;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Random;
28
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import javax.transaction.UserTransaction;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.datatype.DatatypeConfigurationException;
35 import javax.xml.datatype.DatatypeFactory;
36 import javax.xml.datatype.XMLGregorianCalendar;
37 import javax.xml.parsers.DocumentBuilder;
38 import javax.xml.parsers.DocumentBuilderFactory;
39
40 import org.apache.commons.codec.binary.Base64;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.cxf.helpers.IOUtils;
43 import org.apache.log4j.Logger;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48
49 import com.bofa.fast.du.dto.ErrorCodeTO;
50 import com.bofa.fast.du.helper.v1.DocUtilHelper;
51 import com.bofa.fast.du.jaxb.common.v1.AccountDtls;
52 import com.bofa.fast.du.jaxb.common.v1.Accounts;
53 import com.bofa.fast.du.jaxb.common.v1.ClientInfo;
54 import com.bofa.fast.du.jaxb.common.v1.RefAccountDtls;
55 import com.bofa.fast.du.jaxb.common.v1.RequestInfo;
56 import com.bofa.fast.du.jaxb.common.v1.Signer;
57 import com.bofa.fast.du.jaxb.common.v1.SignerDtls;
58 import com.bofa.fast.du.jaxb.common.v1.StmtAddress;
59 import com.bofa.fast.du.services.exception.DUApplicationException;
60 import com.bofa.fast.du.services.exception.DUException;
61 import com.bofa.fast.du.services.exception.DUResourceException;
62 import com.bofa.fast.du.services.exception.DocUtilException;
63 import com.itextpdf.text.DocumentException;
64 import com.itextpdf.text.pdf.AcroFields;
65 import com.itextpdf.text.pdf.PdfReader;
66 import com.itextpdf.text.pdf.PdfStamper;
67 import com.itextpdf.text.pdf.XfaForm;
68
69
70 public class DUUtil {
71
72 private static Logger log = Logger.getLogger(DUUtil.class);
73
74 public static String getSubString(String str, String split, int index) {
75 String subString = null;
76 String segments[] = str.split(split);
77 subString = segments[segments.length - index];
78 return subString;
79 }
80
81 public static UserTransaction getTransaction() {
82 UserTransaction transaction = null;
83 try {
84 transaction = (UserTransaction) new InitialContext()
85 .lookup("javax.transaction.UserTransaction");
86 } catch (NamingException e) {
87
88 log.error(e.getMessage());
89 }
90 return transaction;
91
92 }
93
94 public static String readFromLocal(String path) {
95 String result = "";
96
97 File file = new File(path);
98 try {
99 result = FileUtils.readFileToString(file);
100 } catch (IOException e) {
101 log.error(e.getMessage());
102 }
103 return result;
104 }
105
106 public static void assembleDocument(String src, InputStream dataStream,
107 String dest) throws DocUtilException, DUException {
108
109 try {
110 PdfReader reader = new PdfReader(src);
111 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
112 dest));
113 AcroFields afields = stamper.getAcroFields();
114 XfaForm xfa = afields.getXfa();
115 xfa.fillXfaForm(dataStream);
116 stamper.close();
117 reader.close();
118 log.info("Doc generation successful...");
119 } catch (Exception e) {
120 log.error(e.getMessage());
121
122 HashMap<String, String> errMsgElements = new HashMap<String, String>();
123 errMsgElements.put("%#ERROR#%", "Error assembling the document.");
124 DUResourceException resourceExp = new DUResourceException(
125 "5100000", errMsgElements);
126 throw resourceExp;
127 }
128 }
129
130
131 /**
132 * This method is responsible for populating the PDF based on the template ID
133 * @param src
134 * @param dataStream
135 * @param dest
136 * @param templateId
137 * @throws DocUtilException
138 * @throws DUException
139 */
140 public static void assembleDocumentTesting(String src,
141 InputStream dataStream, String dest, String templateId, boolean ghostTemplate)
142 throws DocUtilException, DUException {
143
144 try {
145
146 //Convert the dataStream to ReqInfo object.
147
148 JAXBContext jaxbContext = JAXBContext
149 .newInstance(com.bofa.fast.du.jaxb.common.v1.RequestInfo.class);
150 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
151
152 RequestInfo reqInfo = (com.bofa.fast.du.jaxb.common.v1.RequestInfo) jaxbUnmarshaller
153 .unmarshal(dataStream);
154
155 PdfReader reader = new PdfReader(src);
156 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
157 dest));
158 AcroFields form = stamper.getAcroFields();
159
160 Accounts accounts = reqInfo.getReqItems().getReqItem().get(0).getAccounts();
161 AccountDtls acctDtls = accounts.getAccount().get(0).getAccountDtls();
162 ClientInfo clientInfo = reqInfo.getClientInfo();
163 SignerDtls signDtls = accounts.getAccount().get(0).getSignerDtls();
164
165 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
166 if (templateId
167 .equalsIgnoreCase(DUConstants.ADDITION_OF_ACCT_TEMPLATE_NEW) || templateId
168 .equalsIgnoreCase(DUConstants.ADDITION_OF_ACCT_GHOST_TEMPLATE)) {
169
170 RefAccountDtls refAccountDtls=reqInfo.getReqItems().getReqItem().get(0).getAccounts().getAccount().get(0).getRefAccountDtls();
171 form.setField("reqDate", sdf.format(new Date()));
172 Date date = toDate(refAccountDtls.getAccOpenedDate());
173 log.info("Orig DepoAccDocuDate:: "+date);
174 if(date != null || "".equals(date)){
175 form.setField("origDepoAccDocuDate",sdf.format(date));
176 }
177 form.setField("orgLegalName", clientInfo.getOrgLegalName());
178 form.setField("accountNum",refAccountDtls.getAccNum());
179 String str = clientInfo.getEIN();
180 if(str != null && str.length() == 9){
181 form.setField("ein0",str.substring(0,1));
182 form.setField("ein1", str.substring(1,2));
183 form.setField("ein2",str.substring(2,3));
184 form.setField("ein3", str.substring(3,4));
185 form.setField("ein4", str.substring(4,5));
186 form.setField("ein5", str.substring(5,6));
187 form.setField("ein6", str.substring(6,7));
188 form.setField("ein7", str.substring(7,8));
189 form.setField("ein8", str.substring(8,9));
190 }
191 for (int i=0; i < accounts.getAccount().size(); i++) {
192 AccountDtls acctDtls1 = accounts.getAccount().get(i).getAccountDtls();
193 form.setField("bankNum"+i, acctDtls1.getBankNum());
194 form.setField("accountNum"+i, acctDtls1.getAccNum());
195 form.setField("accountTitle"+i, acctDtls1.getAccTitle());
196 StmtAddress address = acctDtls1.getStmtAddress();
197
198 if (address != null) {
199 String address1="";
200 String address2="";
201 String city="";
202 String state="";
203 if(address.getAddress1() != null && !address.getAddress1().equals("") && !address.getAddress1().equals("null"))
204 {
205 address1=address.getAddress1() + ",";
206 log.info("address1 : "+address1);
207 }
208 else
209 {
210 address1="";
211 }
212 if(address.getAddress2()!=null && !address.getAddress2().equals("") && !address.getAddress2().equals("null"))
213 {
214 address2=address.getAddress2() + ",";
215 log.info("address2 : "+address1);
216 }
217 else
218 {
219 address2="";
220 }
221 if(address.getCity()!=null && !address.getCity().equals("") && !address.getCity().equals("null"))
222 {
223 city=address.getCity() + ",";
224 log.info("city : "+address1);
225 }
226 else
227 {
228 city="";
229 }
230 if(address.getState()!=null && !address.getState().equals("") && !address.getState().equals("null"))
231 {
232 state=address.getState() + ",";
233 log.info("state : "+address1);
234 }
235 else
236 {
237 state="";
238 }
239 log.info("statementAddress" + address1+address2+city+state+address.getPostalCode());
240 String fullAddress=address1+address2+city+state+address.getPostalCode();
241 form.setField("statementAddress"+i, fullAddress);
242 }
243
244 }
245 if (signDtls != null) {
246 populateSignerDetails(signDtls,form,templateId);
247 }
248
249 if(ghostTemplate){
250 form.setField("agreementAck", "Acknowledged via Electronic Acceptance "+sdf.format(new Date()));
251 }
252 } else if (templateId
253 .equalsIgnoreCase(DUConstants.SIGNATURE_CARD_TEMPLATE_NEW)) {
254 StmtAddress stmtAddress = acctDtls.getStmtAddress();
255
256 String requestType = reqInfo.getReqItems().getReqItem().get(0).getReqItemType();
257 populateRequestType(requestType,form);
258
259 form.setField("txtAccountNumber", acctDtls.getAccNum());
260 form.setField("txtOrgLegalName", clientInfo.getOrgLegalName());
261 form.setField("txtDbaName", acctDtls.getDbaName());
262 form.setField("txtAccountTitle", acctDtls.getAccTitle());
263
264 String address = "";
265 if(stmtAddress.getAddress1()!=null && !stmtAddress.getAddress1().isEmpty()){
266 address = stmtAddress.getAddress1();
267 if(stmtAddress.getAddress2()!=null && !stmtAddress.getAddress2().isEmpty() ){
268 address = address + "," + stmtAddress.getAddress2();
269 }
270 }
271 form.setField("txtStatementAddress", address);
272 form.setField("txtCity", stmtAddress.getCity());
273 form.setField("txtState", stmtAddress.getState());
274 form.setField("txtPostalCode", stmtAddress.getPostalCode());
275
276 form.setField("txtStateOfFormation", clientInfo.getStateofFormation());
277
278 populateBusinessType(clientInfo.getTypeofBusiness(),form,clientInfo.getTypeofBusinessSubType());
279 populateEIN(clientInfo.getEIN(),form);
280
281 if(clientInfo.isExemptPayee()){
282 form.setField("ExemptPayee","On");
283 }
284
285 form.setField("txtEINOwnerLegalName",clientInfo.getLegalNameofEIN());
286
287 populateFederalTaxClass(clientInfo.getFederalTaxClass(),form,clientInfo.getFederalTaxClassSubType());
288 populateLLCTaxClass(clientInfo.getFederalTaxClass(),form);
289 populateSignerDetails(signDtls,form,templateId);
290
291 } else if (templateId.equalsIgnoreCase(DUConstants.SUPP_SIG_PAGE_TEMPLATE_NEW)) {
292
293 form.setField("txtAccountNumber", acctDtls.getAccNum());
294 form.setField("txtOrgLegalName", clientInfo.getOrgLegalName());
295
296 populateSignerDetails(signDtls,form,templateId);
297 }
298 stamper.setFormFlattening(true);
299 stamper.close();
300 reader.close();
301 } catch (Exception e) {
302 e.printStackTrace();
303 log.error(e.getMessage());
304
305 HashMap<String, String> errMsgElements = new HashMap<String, String>();
306 errMsgElements.put("%#ERROR#%", "Error assembling the document.");
307 DUResourceException resourceExp = new DUResourceException(
308 "5100000", errMsgElements);
309 throw resourceExp;
310 }
311
312
313
314 }
315 public static Date toDate(XMLGregorianCalendar calendar){
316
317 if(calendar ==null) {
318
319 return null;
320
321 }
322
323 return calendar.toGregorianCalendar().getTime();
324
325 }
326
327 /**
328 * This method is to populate the request type in the PDF
329 * @param reqType
330 * @param form
331 * @throws IOException
332 * @throws DocumentException
333 */
334 public static void populateRequestType(String reqType,AcroFields form) throws IOException, DocumentException{
335 String type="";
336 if(reqType!=null && !reqType.isEmpty()){
337 reqType = reqType.replace(" ", "").replace("(", "").replace(")", "").replace("/", "").toUpperCase();
338 type = DUConstants.RequestType.valueOf(reqType).getReqType();
339 form.setField("chkReqItemType", type);
340 }
341 }
342
343 /**
344 * This method is to populate the businessType in the PDF
345 * @param businessType
346 * @param form
347 * @param subBusinessType
348 * @throws IOException
349 * @throws DocumentException
350 */
351 public static void populateBusinessType(String businessType,AcroFields form,String subBusinessType) throws IOException, DocumentException{
352
353 String type="";
354 if(businessType!=null && !businessType.isEmpty()){
355 businessType = businessType.replace(" ", "").replace("-", "").replace("/", "").toUpperCase();
356
357 type = DUConstants.BusinessType.valueOf(businessType).getType();
358
359 if(type.equalsIgnoreCase(DUConstants.BusinessType.GOVERNMENTAUTHORITYAGENCY.getType())){
360 form.setField("txtGovtAuthAgencyType",subBusinessType);
361 }else if(type.equalsIgnoreCase(DUConstants.BusinessType.OTHER.getType())){
362 form.setField("txtOtherType",subBusinessType);
363 }
364 form.setField("chkTypeOfBusiness", type);
365 }
366 }
367
368 /**
369 * This method is to populate the ein in the PDF
370 * @param eIn
371 * @param form
372 * @throws IOException
373 * @throws DocumentException
374 */
375 public static void populateEIN(String eIn,AcroFields form) throws IOException, DocumentException{
376 if(eIn!=null && !eIn.isEmpty() && eIn.length()>0){
377 for(int i=0;i<eIn.length();i++){
378 form.setField("txtEIN"+i, String.valueOf(eIn.charAt(i)));
379 }
380 }
381 }
382
383 /**
384 * This method is to populate the federal tax details in the pdf
385 * @param federalTax
386 * @param form
387 * @param subBusinessType
388 * @throws IOException
389 * @throws DocumentException
390 */
391 public static void populateFederalTaxClass(String federalTax,AcroFields form,String subBusinessType) throws IOException, DocumentException{
392 String federalTaxClass="";
393 if(federalTax!=null && !federalTax.isEmpty()){
394 federalTax = federalTax.replace(" ", "").replace("/", "").toUpperCase();
395 federalTaxClass = DUConstants.TaxType.valueOf(federalTax).getTaxType();
396 if(federalTaxClass.equalsIgnoreCase(DUConstants.TaxType.OTHER.getTaxType())){
397 form.setField("txtFedTaxOther",subBusinessType);
398 }
399 form.setField("chkFedTaxClass", federalTaxClass);
400 }
401 }
402
403 /**
404 * This method is to populate the LLC tax details
405 * @param taxLLC
406 * @param form
407 * @throws IOException
408 * @throws DocumentException
409 */
410 public static void populateLLCTaxClass(String taxLLC,AcroFields form) throws IOException, DocumentException{
411 String taxLLCClass="";
412 if(taxLLC!=null && !taxLLC.isEmpty()){
413 taxLLC = taxLLC.replace(" ", "").replace("/", "").toUpperCase();
414 taxLLCClass = DUConstants.TaxType.valueOf(taxLLC).getTaxType();
415 form.setField("chkLLCTax", taxLLCClass);
416 }
417 }
418
419 /**
420 * This method is to populate the signer details
421 * @param signDtls
422 * @param form
423 * @param templateId
424 * @throws IOException
425 * @throws DocumentException
426 * @throws ParseException
427 */
428 public static void populateSignerDetails(SignerDtls signDtls,AcroFields form, String templateId) throws IOException, DocumentException, ParseException{
429 List<Signer> signerList = signDtls.getSigner();
430
431 DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
432 Date date = new Date();
433 if (templateId.equalsIgnoreCase(DUConstants.SIGNATURE_CARD_TEMPLATE_NEW) || templateId.equalsIgnoreCase(DUConstants.SUPP_SIG_PAGE_TEMPLATE_NEW)) {
434 int i=0;
435 int j=0;
436
437 for(Signer signer : signerList){
438 if(signer.getType().equalsIgnoreCase("Authorized Signer")){
439 form.setField("txtASRName"+i,signer.getName());
440 form.setField("txtASRTitle"+i,signer.getTitle());
441 form.setField("chkASRDate"+i,dateFormat.format(date));
442 i++;
443 }else{
444 if(signer.getType().equalsIgnoreCase("Check Only Signer")){
445 form.setField("chkSignCheck"+j,"On");
446 }
447 if(signer.getOperation().equalsIgnoreCase("Add")){
448 form.setField("txtAddDeleteSigner"+j,"A");
449 }else{
450 form.setField("txtAddDeleteSigner"+j,"D");
451 }
452
453 form.setField("txtDesigSignerName"+j,signer.getName());
454 form.setField("txtDesigSignerTitle"+j,signer.getTitle());
455 j++;
456 }
457 }
458 } else if (templateId
459 .equalsIgnoreCase(DUConstants.ADDITION_OF_ACCT_TEMPLATE_NEW) || templateId
460 .equalsIgnoreCase(DUConstants.ADDITION_OF_ACCT_GHOST_TEMPLATE)) {
461 int k=0;
462
463 for(Signer signer : signerList){
464 if(signer.getType().equalsIgnoreCase("Authorized Signer") && k==0){
465 form.setField("authSigner",signer.getName()+"/"+signer.getTitle());
466 break;
467 }
468 }
469
470 }
471 }
472
473 public static void deleteTempFiles(File templateFile, File generatedDocFile) {
474 templateFile.delete();
475 generatedDocFile.delete();
476 }
477
478 public static Document loadXMLFromString(String xml) throws Exception {
479 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
480 factory.setNamespaceAware(true);
481 DocumentBuilder builder = factory.newDocumentBuilder();
482 return builder.parse(new ByteArrayInputStream(xml.getBytes()));
483 }
484
485 public static byte[] getBase64String(String path) throws IOException {
486
487 File file = new File(path);
488 InputStream is = new FileInputStream(file);
489 byte[] byteArray = IOUtils.readBytesFromStream(is);
490
491 byte[] bytearray = Base64.decodeBase64(byteArray);
492
493 return bytearray;
494 }
495
496 public static String getTagValue(String sTag, Element eElement) {
497 if (eElement.getElementsByTagName(sTag).item(0) != null) {
498 NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
499 .getChildNodes();
500 Node nValue = (Node) nlList.item(0);
501 if (nValue != null) {
502 return nValue.getNodeValue();
503 }
504 }
505 return "";
506 }
507
508 public static HashMap<String, List<String>> getRequestItemInfo(
509 NodeList requestItems, int index) {
510 HashMap<String, List<String>> reqTypeMap = new HashMap<String, List<String>>();
511
512 String requestType = null;
513 String requestSubType = null;
514 List<String> requestSubTypeList = new ArrayList<String>();
515
516 Node reqItemsNode = requestItems.item(index);
517 if (reqItemsNode.getNodeType() == Node.ELEMENT_NODE) {
518 Element reqItemElement = (Element) reqItemsNode;
519 requestType = DUUtil.getTagValue(DUConstants.REQUEST_ITEM_TYPE,
520 reqItemElement);
521 log.info("request Type is .. " + requestType);
522
523 NodeList reqSubTypes = reqItemElement
524 .getElementsByTagName(DUConstants.REQUEST_ITEM_SUBTYPE);
525 if (reqSubTypes.getLength() > 0) {
526 for (int j = 0; j < reqSubTypes.getLength(); j++) {
527
528 Node reqSubTypesNode = reqSubTypes.item(j);
529
530 if (reqSubTypesNode.getNodeType() == Node.ELEMENT_NODE) {
531 Element reqSubTypeElement = (Element) reqSubTypesNode;
532 requestSubType = reqSubTypeElement.getTextContent();
533 log.info("request sub type is ... " + requestSubType);
534 requestSubTypeList.add(requestSubType);
535 }
536
537 }
538 }
539
540 }
541
542 reqTypeMap.put(requestType, requestSubTypeList);
543 return reqTypeMap;
544 }
545
546 public static <T> List<HashSet<T>> split(HashSet<T> original, int count) {
547 // Create a list of sets to return.
548 ArrayList<HashSet<T>> l = new ArrayList<HashSet<T>>(count);
549
550 // Create an iterator for the original set.
551 Iterator<T> it = original.iterator();
552
553 // Calculate required number for each set.
554 int each = original.size() / count;
555
556 // Loop for each new set.
557 for (int i = 0; i < count; i++) {
558 // Create the set.
559 HashSet<T> s = new HashSet<T>(original.size() / count + 1);
560
561 // Add to the list.
562 l.add(s);
563
564 // Loop over each element.
565 for (int j = 0; j < each && it.hasNext(); i++) {
566 // Add the element to the set.
567 s.add(it.next());
568 }
569 }
570 return l;
571 }
572
573 public static <T> List<HashSet<T>> split(HashSet<T> original,
574 int howManySets, int howManyInEachSet) {
575 // Create a list of sets to return.
576 ArrayList<HashSet<T>> l = new ArrayList<HashSet<T>>(howManySets);
577
578 // Create an iterator for the original set.
579 Iterator<T> it = original.iterator();
580
581 // Calculate required number for each set.
582 int each = howManyInEachSet;
583
584 // Loop for each new set.
585 for (int i = 0; i < howManySets; i++) {
586 // Create the set.
587 HashSet<T> s = new HashSet<T>(original.size() / howManySets + 1);
588
589 // Add to the list.
590 l.add(s);
591
592 // Loop over each element.
593 for (int j = 0; j < each && it.hasNext(); j++) {
594 // Add the element to the set.
595 s.add(it.next());
596 }
597 }
598 return l;
599 }
600
601 public static <T> int getRequiredSetCount(HashSet<T> original,
602 int howManyInEachSet, int originalSetCount) {
603
604 int val = (int) Math.ceil(original.size() / howManyInEachSet);
605
606 if (val == originalSetCount) {
607 val = val + 1;
608 }
609
610 return val;
611 }
612
613 public static double round(double value, int places) {
614 if (places < 0) {
615 throw new IllegalArgumentException();
616 }
617
618 BigDecimal bd = new BigDecimal(value);
619 bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
620 return bd.doubleValue();
621 }
622
623 public static boolean isNullOrBlank(Object obj) throws Exception {
624 if (null == obj) {
625 return true;
626 }
627 if (obj instanceof String && obj.toString().trim().length() < 1) {
628 return true;
629 }
630 return false;
631 }
632
633 public static String buildFieldName(String[] fields) throws Exception {
634 String multiLevelFieldName = null;
635 if (fields.length > 1) {
636 StringBuilder fieldName = new StringBuilder();
637 for (int i = 0; i < fields.length; i++) {
638 fieldName.append(fields[i]);
639 if (i != fields.length - 1) {
640 fieldName.append(DUConstants.DOT);
641 }
642 }
643 multiLevelFieldName = fieldName.toString();
644 } else if (fields.length == 1) {
645 multiLevelFieldName = fields[0];
646 }
647 return multiLevelFieldName;
648
649 }
650
651 public static XMLGregorianCalendar getCurrentTimestamp() {
652
653 GregorianCalendar c = new GregorianCalendar();
654 c.setTime(new Date());
655 XMLGregorianCalendar timestamp = null;
656 try {
657 timestamp = DatatypeFactory.newInstance()
658 .newXMLGregorianCalendar(c);
659 } catch (DatatypeConfigurationException e) {
660 // TODO Auto-generated catch block
661 log.error(e.getMessage());
662 }
663
664 return timestamp;
665 }
666
667 public static String buildErrorMessage(String errorcode,
668 HashMap<String, String> errMsgElements) {
669
670 String errorMessage = null;
671 try {
672 errorMessage = DocUtilHelper.getFaultCodeMap().get(errorcode)
673 .getExceptionMsgTemplate();
674
675 Iterator<?> it = errMsgElements.entrySet().iterator();
676
677 while (it.hasNext()) {
678 @SuppressWarnings("rawtypes")
679 Map.Entry pairs = (Map.Entry) it.next();
680 errorMessage = errorMessage.replaceAll((String) pairs.getKey(),
681 (String) pairs.getValue());
682
683 }
684
685 } catch (Exception e) {
686 log.error(e.getMessage(), e);
687 }
688
689 return errorMessage;
690 }
691
692 public static HashMap<String, ErrorCodeTO> getFaultCodes()
693 throws DUException {
694 Connection conn = null;
695 PreparedStatement ps = null;
696 ResultSet rs = null;
697 HashMap<String, ErrorCodeTO> faultDetailsMap = null;
698 ErrorCodeTO errorCodeTO = null;
699 try {
700 log.debug("Entering method "
701 + Thread.currentThread().getStackTrace()[0].getMethodName());
702 log.info("Fault Code Code Caching Started");
703 conn = WebLogicDataSource.getInstance().getConnection();
704 //conn = SingletonDatabaseConn.getInstance();
705 ps = conn
706 .prepareStatement("SELECT EPWS_ERROR_CD, EPWS_ERROR_NAME, EPWS_ERROR_MESSAGE from DOCUTIL.DU_ERROR_MESSAGES");
707 rs = ps.executeQuery();
708
709 if (rs != null) {
710 faultDetailsMap = new HashMap<String, ErrorCodeTO>();
711 while (rs.next()) {
712 errorCodeTO = new ErrorCodeTO(
713 rs.getString("EPWS_ERROR_NAME"),
714 rs.getString("EPWS_ERROR_MESSAGE"),
715 rs.getString("EPWS_ERROR_CD"));
716 faultDetailsMap.put(rs.getString("EPWS_ERROR_CD"),
717 errorCodeTO);
718 }
719 }
720 log.info("Fault Code Caching Completed ");
721 log.debug("Exiting method "
722 + Thread.currentThread().getStackTrace()[0].getMethodName());
723 } catch (SQLException sqlEx) {
724 log.error(sqlEx.getMessage(), sqlEx);
725 throw new DUResourceException("5100003", sqlEx.getMessage());
726 } catch (Exception exception) {
727 log.error(exception.getMessage(), exception);
728 throw new DUApplicationException("5000015", exception.getMessage());
729 } finally {
730 try {
731 if (ps != null) {
732 ps.close();
733 }
734 } catch (SQLException e) {
735 throw new DUResourceException("5100003", e.getMessage());
736 }
737 try {
738 if (rs != null) {
739 rs.close();
740 }
741 } catch (SQLException e) {
742 throw new DUResourceException("5100003", e.getMessage());
743 }
744 try {
745 if (conn != null) {
746 conn.close();
747 }
748 } catch (Exception e) {
749 log.error(e.getMessage());
750 }
751 }
752
753 return faultDetailsMap;
754 }
755
756 public static String getNonce() {
757 Random generator = new Random();
758 String nonceString = String.valueOf(generator.nextInt(999999999));
759 return new String(
760 Base64.encodeBase64(hexEncode(nonceString).getBytes()));
761 }
762
763 private static String hexEncode(String in) {
764 StringBuilder sb = new StringBuilder("");
765 for (int i = 0; i < (in.length() - 2) + 1; i = i + 2) {
766 int c = Integer.parseInt(in.substring(i, i + 2), 16);
767 char chr = (char) c;
768 sb.append(chr);
769 }
770 return sb.toString();
771 }
772
773 public static String getStackTrace(Exception ex) {
774 StringWriter stack = new StringWriter();
775 ex.printStackTrace(new PrintWriter(stack));
776 return stack.toString();
777
778 }
779
780 }
 
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.