Rev 22202 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.dao.util;import java.io.ByteArrayOutputStream;import java.lang.reflect.Type;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.itextpdf.text.Document;import com.itextpdf.text.Element;import com.itextpdf.text.Font;import com.itextpdf.text.Image;import com.itextpdf.text.Paragraph;import com.itextpdf.text.pdf.PdfContentByte;import com.itextpdf.text.pdf.PdfImportedPage;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfReader;import com.itextpdf.text.pdf.PdfWriter;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.Mongo;@Componentpublic class FofoDocumentsGenerator {private static final Map<String, String> titles = new HashMap<String, String>();@Autowiredprivate Mongo mongoClient;@Autowiredprivate DocumentRepository documentRepository;static {titles.put("bEntityDoc", "Business Document");titles.put("gstDoc", "GST Doc");titles.put("panDoc", "Pan Doc");titles.put("itrDoc", "ITR DOC");titles.put("angleDoc1", "Angle Doc1");titles.put("angleDoc2", "Angle Doc2");titles.put("angleDoc3", "Angle Doc3");titles.put("angleDoc4", "Angle Doc4");titles.put("angleDoc5", "Angle Doc5");titles.put("ownershipDoc", "OwnerShip Doc");titles.put("chequeCopy", "Cheque");titles.put("insuranceDoc", "Insurance Doc");titles.put("loanDoc", "Loan Doc");titles.put("sanctionDoc", "Sanction Doc");}private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);public byte[] getDocumentStream(int fofoId) throws Exception {// String responseString = HttpClientUtil.doGet("localhost:8080",// fofoId);ByteArrayOutputStream outputStream = new ByteArrayOutputStream();Document document = new Document();PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);document.open();PdfContentByte cb = pdfWriter.getDirectContent();String jsonString = mongoClient.getFofoFormJsonStringByFofoId(fofoId);Map<String, String> titlePathMap = new HashMap<String, String>();Type stringObjectMapType = new TypeToken<Map<String, Object>>() {}.getType();Gson gson = new Gson();Map<String, Object> map = gson.fromJson(jsonString, stringObjectMapType);Paragraph paragraphTitle = new Paragraph((String)map.get("registeredBusinessName"), FONT_TITLE);paragraphTitle.setAlignment(Element.ALIGN_CENTER);document.add(paragraphTitle);System.out.println(map);for (String key : map.keySet()) {if (titles.containsKey(key)) {if(map.get(key)==null){titlePathMap.put(titles.get(key), null);} else {try {com.spice.profitmandi.dao.entity.dtr.Document doc = documentRepository.selectById(((Double)map.get(key)).intValue());titlePathMap.put(key, doc.getPath() + doc.getName());} catch(ProfitMandiBusinessException e) {titlePathMap.put(titles.get(key), null);}}}}PdfPTable table = new PdfPTable(1);PdfPCell title;PdfPTable innerTable;for (Map.Entry<String, String> entry : titlePathMap.entrySet()) {Image image = null;try {if(entry.getValue() != null) {image = Image.getInstance(entry.getValue());} else {document.add(new Paragraph(entry.getKey() + " is missing."));continue;}} catch (Exception e) {e.printStackTrace();try {PdfReader reader = new PdfReader(entry.getValue());for (int i = 1; i <= reader.getNumberOfPages(); i++) {PdfImportedPage page = pdfWriter.getImportedPage(reader, i);cb.addTemplate(page, 0, 0);document.newPage();}} catch (Exception ex) {document.add(new Paragraph(entry.getKey() + " is password encrypted."));}continue;}innerTable = new PdfPTable(1);float scaler = (Math.min((document.getPageSize().getWidth()) / image.getWidth(), 1)) * 100;title = new PdfPCell(new Paragraph(entry.getKey(), FONT_BOLD));title.setHorizontalAlignment(Element.ALIGN_CENTER);innerTable.addCell(title);image.scalePercent(scaler);innerTable.addCell(image);table.addCell(innerTable);}document.add(table);document.close();return outputStream.toByteArray();}}