| 22197 |
amit.gupta |
1 |
package com.spice.profitmandi.dao.util;
|
|
|
2 |
|
|
|
3 |
import java.io.ByteArrayOutputStream;
|
|
|
4 |
import java.io.OutputStream;
|
|
|
5 |
import java.lang.reflect.Type;
|
|
|
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.Map;
|
|
|
8 |
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.stereotype.Component;
|
|
|
11 |
|
|
|
12 |
import com.google.gson.Gson;
|
|
|
13 |
import com.google.gson.reflect.TypeToken;
|
|
|
14 |
import com.itextpdf.text.Document;
|
|
|
15 |
import com.itextpdf.text.Element;
|
|
|
16 |
import com.itextpdf.text.Font;
|
|
|
17 |
import com.itextpdf.text.Image;
|
|
|
18 |
import com.itextpdf.text.Paragraph;
|
|
|
19 |
import com.itextpdf.text.pdf.PdfContentByte;
|
|
|
20 |
import com.itextpdf.text.pdf.PdfImportedPage;
|
|
|
21 |
import com.itextpdf.text.pdf.PdfPCell;
|
|
|
22 |
import com.itextpdf.text.pdf.PdfPTable;
|
|
|
23 |
import com.itextpdf.text.pdf.PdfReader;
|
|
|
24 |
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
25 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
|
|
28 |
|
|
|
29 |
@Component
|
|
|
30 |
public class FofoDocumentsGenerator {
|
|
|
31 |
private static final Map<String, String> titles = new HashMap<String, String>();
|
|
|
32 |
@Autowired
|
|
|
33 |
Mongo mongoClient;
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
DocumentRepository documentRepository;
|
|
|
37 |
|
|
|
38 |
static {
|
|
|
39 |
titles.put("bEntityDoc", "Business Document");
|
|
|
40 |
titles.put("gstDoc", "GST Doc");
|
|
|
41 |
titles.put("panDoc", "Pan Doc");
|
|
|
42 |
titles.put("itrDoc", "ITR DOC");
|
|
|
43 |
titles.put("angleDoc1", "Angle Doc1");
|
|
|
44 |
titles.put("angleDoc2", "Angle Doc2");
|
|
|
45 |
titles.put("angleDoc3", "Angle Doc3");
|
|
|
46 |
titles.put("angleDoc4", "Angle Doc4");
|
|
|
47 |
titles.put("angleDoc5", "Angle Doc5");
|
|
|
48 |
titles.put("ownershipDoc", "OwnerShip Doc");
|
|
|
49 |
titles.put("chequeCopy", "Cheque");
|
|
|
50 |
titles.put("insuranceDoc", "Insurance Doc");
|
|
|
51 |
titles.put("loanDoc", "Loan Doc");
|
|
|
52 |
titles.put("sanctionDoc", "Sanction Doc");
|
|
|
53 |
}
|
|
|
54 |
private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
|
|
|
55 |
private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
|
|
|
56 |
|
|
|
57 |
public byte[] getDocumentStream(int fofoId) throws Exception {
|
|
|
58 |
// String responseString = HttpClientUtil.doGet("localhost:8080",
|
|
|
59 |
// fofoId);
|
|
|
60 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
61 |
|
|
|
62 |
Document document = new Document();
|
|
|
63 |
|
|
|
64 |
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
|
|
|
65 |
document.open();
|
|
|
66 |
|
|
|
67 |
PdfContentByte cb = pdfWriter.getDirectContent();
|
|
|
68 |
|
|
|
69 |
String jsonString = mongoClient.getFofoFormJsonStringByFofoId(fofoId);
|
|
|
70 |
Map<String, String> titlePathMap = new HashMap<String, String>();
|
|
|
71 |
Type stringObjectMapType = new TypeToken<Map<String, Object>>() {
|
|
|
72 |
}.getType();
|
|
|
73 |
Gson gson = new Gson();
|
|
|
74 |
Map<String, Object> map = gson.fromJson(jsonString, stringObjectMapType);
|
|
|
75 |
|
|
|
76 |
Paragraph paragraphTitle = new Paragraph((String)map.get("registeredBusinessName"), FONT_TITLE);
|
|
|
77 |
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
|
|
|
78 |
document.add(paragraphTitle);
|
|
|
79 |
System.out.println(map);
|
|
|
80 |
for (String key : map.keySet()) {
|
|
|
81 |
if (titles.containsKey(key)) {
|
|
|
82 |
if(map.get(key)==null){
|
|
|
83 |
titlePathMap.put(titles.get(key), null);
|
|
|
84 |
} else {
|
|
|
85 |
try {
|
| 22199 |
amit.gupta |
86 |
com.spice.profitmandi.dao.entity.dtr.Document doc = documentRepository.selectById(((Double)map.get(key)).intValue());
|
| 22197 |
amit.gupta |
87 |
titlePathMap.put(key, doc.getPath() + doc.getName());
|
|
|
88 |
} catch(ProfitMandiBusinessException e) {
|
|
|
89 |
titlePathMap.put(titles.get(key), null);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
PdfPTable table = new PdfPTable(1);
|
|
|
96 |
PdfPCell title;
|
|
|
97 |
PdfPTable innerTable;
|
|
|
98 |
|
|
|
99 |
for (Map.Entry<String, String> entry : titlePathMap.entrySet()) {
|
|
|
100 |
Image image = null;
|
|
|
101 |
try {
|
|
|
102 |
if(entry.getValue() != null) {
|
|
|
103 |
image = Image.getInstance(entry.getValue());
|
|
|
104 |
} else {
|
|
|
105 |
document.add(new Paragraph(entry.getKey() + " is missing."));
|
| 22202 |
amit.gupta |
106 |
continue;
|
| 22197 |
amit.gupta |
107 |
}
|
|
|
108 |
} catch (Exception e) {
|
|
|
109 |
e.printStackTrace();
|
|
|
110 |
try {
|
|
|
111 |
PdfReader reader = new PdfReader(entry.getValue());
|
|
|
112 |
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
|
|
|
113 |
PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
|
|
|
114 |
cb.addTemplate(page, 0, 0);
|
|
|
115 |
document.newPage();
|
|
|
116 |
}
|
|
|
117 |
} catch (Exception ex) {
|
|
|
118 |
document.add(new Paragraph(entry.getKey() + " is password encrypted."));
|
|
|
119 |
}
|
|
|
120 |
continue;
|
|
|
121 |
}
|
|
|
122 |
innerTable = new PdfPTable(1);
|
|
|
123 |
float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;
|
|
|
124 |
title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));
|
|
|
125 |
title.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
126 |
innerTable.addCell(title);
|
|
|
127 |
image.scalePercent(scaler);
|
|
|
128 |
innerTable.addCell(image);
|
|
|
129 |
table.addCell(innerTable);
|
|
|
130 |
}
|
|
|
131 |
document.add(table);
|
|
|
132 |
document.close();
|
|
|
133 |
|
|
|
134 |
return outputStream.toByteArray();
|
|
|
135 |
}
|
|
|
136 |
}
|