diff --git a/src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java b/src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java index afdb8d3..66ce7eb 100644 --- a/src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java +++ b/src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java @@ -5,7 +5,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; - +import java.math.BigDecimal; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; @@ -195,7 +195,7 @@ public class PdfService extends AbstractBaseEntityService getTemplate(Long templateId, Long clientId) { + public Map getTemplate(Long templateId, Long clientId) throws IOException{ Map template = templateService.loadTemplate(templateId); byte[] pdfBytes = (byte[])template.get("blobValue"); @@ -204,8 +204,28 @@ public class PdfService extends AbstractBaseEntityService template = templateService.loadTemplate(templateId); + + //Search if there have an old form having the form code + Map sourcePdf = templateService.getLastPdfByFormCode((String)template.get("remarks"), clientId); + byte[] sourcePdfBytes = (byte[])sourcePdf.get("blobValue"); + + //byte[] modifiedPdfBytes = pdfBytes; + + PdfAcroForm sourceForm = null; + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(sourcePdfBytes); + ByteArrayOutputStream sourceOutputStream = new ByteArrayOutputStream()) { + + PdfReader sourceReader = new PdfReader(inputStream); + PdfWriter sourceWriter = new PdfWriter(sourceOutputStream); + PdfDocument sourcePdfDoc = new PdfDocument(sourceReader, sourceWriter); + sourceForm = PdfAcroForm.getAcroForm(sourcePdfDoc, true); + } + + byte[] modifiedPdfBytes = pdfBytes; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(pdfBytes); @@ -217,6 +237,30 @@ public class PdfService extends AbstractBaseEntityService entry : sourceForm.getFormFields().entrySet()) { + PdfFormField sourceField = entry.getValue(); + + if(sourceField.getFieldName() != null){ + String sourceFieldName = sourceField.getFieldName().toUnicodeString(); + if(sourceField.getValue() != null){ + String sourceFieldValue = sourceField.getValue().toString(); + PdfFormField targetField = form.getField(sourceFieldName); + + if (targetField != null) { + targetField.setValue(sourceFieldValue); // Set the value in the target field + } else { + //System.out.println("Field '" + sourceFieldName + "' from source not found in target PDF."); + } + } + } + } + } + /* END of copying the source fields */ + + // --- START DEBUGGING CODE TO LIST ALL PDF FORM FIELDS --- // logger.info("--- Listing all form fields in the PDF (for ID: {}) ---", id); // if (form.getFormFields().isEmpty()) { @@ -234,16 +278,14 @@ public class PdfService extends AbstractBaseEntityService 0){ commonField = commonFieldService.find(id).orElse(new CommonField()); @@ -393,7 +625,7 @@ public class PdfService extends AbstractBaseEntityService commonField.setName(fieldValue); case "fill_7" -> commonField.setNameChi(fieldValue); case "fill_11" -> commonField.setIdCard(fieldValue); - + case "gender_m" -> { commonField.setGenderMale(fieldValue); if("on".equals(fieldValue)) { @@ -431,33 +664,47 @@ public class PdfService extends AbstractBaseEntityService commonField.setEduPri(fieldValue); case "edu_sec" -> commonField.setEduSec(fieldValue); case "edu_post_sec" -> commonField.setEduPostSec(fieldValue); - + /* Page1 End */ + /* Page2 Start */ case "own_income" -> commonField.setOwn_income(fieldValue); case "savings" -> commonField.setSavings(fieldValue); case "premium_financing" -> commonField.setPremium_financing(fieldValue); case "others_funds" -> commonField.setOthers_funds(fieldValue); case "others_funds_desc" -> commonField.setOthers_funds_desc(fieldValue); - + /* Page2 End */ + /* Page3 Start */ + case "fill_26_4" -> commonField.setFna_c1a_amount(fieldValue); + /* Page3 End */ + /* Page4 Start */ + case "total_asset" -> commonField.setFna_c2a_amount(fieldValue); + case "fill_56" -> commonField.setFna_c2b_amount(fieldValue); + /* Page4 End */ } + /* Special logic Start */ //test the concat if( ("date_dd".equals(fieldName) || "date_mm".equals(fieldName) || "date_yyyy".equals(fieldName) ) && commonField.getDdDateOfBirth() != null && commonField.getMmDateOfBirth() != null && commonField.getYyyyDateOfBirth() != null ){ commonField.setDateOfBirth(commonField.getDdDateOfBirth() + "/" + commonField.getMmDateOfBirth() + "/" + commonField.getYyyyDateOfBirth()); } + /* Special logic End */ } if("FNA".equals(formCode)){ - logger.info("FNA "); + //logger.info("FNA "); switch (fieldName) { + /* Page1 Start */ case "fna_a_name" -> commonField.setName(fieldValue); case "fna_a_birth" -> commonField.setDateOfBirth(fieldValue); case "fna_a_occupation" -> commonField.setOccupation(fieldValue); + case "fna_a_primary" -> commonField.setEduPri(fieldValue); + case "fna_a_secondary" -> commonField.setEduSec(fieldValue); case "fna_a_tertiary" -> commonField.setEduTer(fieldValue); case "fna_a_marital" -> commonField.setMaritalStatus(fieldValue); + case "fna_a_contact" -> commonField.setContactNo(fieldValue); case "fna_a_dependents" -> commonField.setNoOfDependents(fieldValue); case "fna_c1a_amount" -> commonField.setFna_c1a_amount(fieldValue); @@ -467,6 +714,7 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_c1c_3_amount(fieldValue); case "fna_c1d_amount" -> commonField.setFna_c1d_amount(fieldValue); case "fna_c2a_amount" -> commonField.setFna_c2a_amount(fieldValue); + case "fna_c2b_amount" -> commonField.setFna_c2b_amount(fieldValue); case "fna_c2c_amount" -> commonField.setFna_c2c_amount(fieldValue); case "fna_c2d_amount" -> commonField.setFna_c2d_amount(fieldValue); @@ -479,6 +727,8 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_b1_d(fieldValue); case "fna_b1_d_year" -> commonField.setFna_b1_d_year(fieldValue); case "fna_b1_d_amount" -> commonField.setFna_b1_d_amount(fieldValue); + /* Page1 End */ + /* Page2 Start */ case "fna_b1_e" -> commonField.setFna_b1_e(fieldValue); case "fna_b1_f" -> commonField.setFna_b1_f(fieldValue); case "fna_b1_f_other" -> commonField.setFna_b1_f_other(fieldValue); @@ -490,6 +740,24 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_b2_e(fieldValue); case "fna_b2_f" -> commonField.setFna_b2_f(fieldValue); case "fna_b2_g" -> commonField.setFna_b2_g(fieldValue); + /* Page2 End */ + /* Page3 Start */ + case "fna_c1e_10" -> commonField.setFna_c1e_10(fieldValue); + case "fna_c1e_10_20" -> commonField.setFna_c1e_10_20(fieldValue); + case "fna_c1e_21_30" -> commonField.setFna_c1e_21_30(fieldValue); + case "fna_c1e_31_40" -> commonField.setFna_c1e_31_40(fieldValue); + case "fna_c1e_41_50" -> commonField.setFna_c1e_41_50(fieldValue); + case "fna_c1e_50" -> commonField.setFna_c1e_50(fieldValue); + /* Page3 End */ + /* Page4 Start */ + case "fna_c2f_10" -> commonField.setFna_c2f_10(fieldValue); + case "fna_c2f_10_20" -> commonField.setFna_c2f_10_20(fieldValue); + case "fna_c2f_21_30" -> commonField.setFna_c2f_21_30(fieldValue); + case "fna_c2f_31_40" -> commonField.setFna_c2f_31_40(fieldValue); + case "fna_c2f_41_50" -> commonField.setFna_c2f_41_50(fieldValue); + case "fna_c2f_50" -> commonField.setFna_c2f_50(fieldValue); + /* Page4 End */ + /* Page5 End */ case "fna_c3a_2_5" -> commonField.setFna_c3a_2_5(fieldValue); case "fna_c3a_6_10" -> commonField.setFna_c3a_6_10(fieldValue); case "fna_c3a_11_15" -> commonField.setFna_c3a_11_15(fieldValue); @@ -505,27 +773,16 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_c3b_policy(fieldValue); case "fna_c3b_others" -> commonField.setFna_c3b_others(fieldValue); case "fna_c3b_desc" -> commonField.setFna_c3b_desc(fieldValue); - case "fna_c1e_10" -> commonField.setFna_c1e_10(fieldValue); - case "fna_c1e_10_20" -> commonField.setFna_c1e_10_20(fieldValue); - case "fna_c1e_21_30" -> commonField.setFna_c1e_21_30(fieldValue); - case "fna_c1e_31_40" -> commonField.setFna_c1e_31_40(fieldValue); - case "fna_c1e_41_50" -> commonField.setFna_c1e_41_50(fieldValue); - case "fna_c1e_50" -> commonField.setFna_c1e_50(fieldValue); - case "fna_c2f_10" -> commonField.setFna_c2f_10(fieldValue); - case "fna_c2f_10_20" -> commonField.setFna_c2f_10_20(fieldValue); - case "fna_c2f_21_30" -> commonField.setFna_c2f_21_30(fieldValue); - case "fna_c2f_31_40" -> commonField.setFna_c2f_31_40(fieldValue); - case "fna_c2f_41_50" -> commonField.setFna_c2f_41_50(fieldValue); - case "fna_c2f_50" -> commonField.setFna_c2f_50(fieldValue); - - + /* Page5 End */ + } } if("HSBCFIN".equals(formCode)){ - logger.info("HSBCFIN "); + //logger.info("HSBCFIN "); switch (fieldName) { + /* Page1 Start */ case "fill_3" -> commonField.setName(fieldValue); case "fill_4" -> commonField.setNameChi(fieldValue); case "fill_5" -> commonField.setGender(fieldValue); @@ -554,19 +811,19 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_c1c_3_amount(fieldValue); case "fill_13" -> commonField.setFna_c1c_1_amount(fieldValue); case "fill_14" -> commonField.setFna_c1c_2_amount(fieldValue); - case "fill_16" -> commonField.setFna_c1d_amount(fieldValue); - + /* Page1 End */ + /* Page2 Start */ case "fill_01" -> commonField.setFna_c2a_amount(fieldValue); case "fill_02" -> commonField.setFna_c2b_amount(fieldValue); case "fill_03" -> commonField.setFna_c2c_amount(fieldValue); case "fill_04" -> commonField.setFna_c2d_amount(fieldValue); case "fill_05" -> commonField.setFna_c2e_amount(fieldValue); - + case "Financial Protection" -> commonField.setFna_b1_a(fieldValue); case "Preparation" -> commonField.setFna_b1_b(fieldValue); case "Providing" -> commonField.setFna_b1_c(fieldValue); @@ -575,21 +832,70 @@ public class PdfService extends AbstractBaseEntityService commonField.setFna_b1_f(fieldValue); case "fill_8_2" -> commonField.setFna_b1_f_other(fieldValue); case "fill_8_21" -> commonField.setFna_b1_f_desc(fieldValue); + /* Page2 End */ + /* Page3 Start */ + case "toggle_91" -> commonField.setFna_b1_a(formCode); + case "fill_1_4" -> commonField.setFna_b1_a_amount(formCode); + case "fill_1_4b" -> commonField.setFna_b1_b(formCode); + case "toggle_93" -> commonField.setFna_b1_d(formCode); + case "fill_1_4c" -> commonField.setFna_b1_d_year(formCode); + case "fill_1_4d" -> commonField.setFna_b1_d_year(formCode); + case "fill_1_4e" -> commonField.setFna_b1_d_amount(formCode); + + case "toggle_4_2" -> commonField.setFna_b2_a(formCode); + case "toggle_5" -> commonField.setFna_b2_b(formCode); + case "toggle_6" -> commonField.setFna_b2_c(formCode); + case "toggle_7" -> commonField.setFna_b2_d(formCode); + case "toggle_8" -> commonField.setFna_b2_e(formCode); + case "toggle_9" -> commonField.setFna_b2_f(formCode); + case "toggle_10" -> commonField.setFna_b2_g(formCode); + /* Page3 End */ + /* Page4 Start */ + case "toggle_1_2" -> commonField.setFna_c3a_2_5(formCode); + case "toggle_2_2" -> commonField.setFna_c3a_6_10(formCode); + case "toggle_3_2" -> commonField.setFna_c3a_11_15(formCode); + case "toggle_4_3" -> commonField.setFna_c3a_16_20(formCode); + case "toggle_5_2" -> commonField.setFna_c3a_20(formCode); + case "toggle_6_2" -> commonField.setFna_c3a_life(fieldValue); + case "toggle_6_2_sum" -> commonField.setFna_c3a_2_5(formCode); + /* Page4 End */ + /* Page5 Start */ + case "toggle_1f" -> commonField.setFna_c3b_income(formCode); + case "toggle_1g" -> commonField.setFna_c3b_saving(formCode); + case "toggle_1h" -> commonField.setFna_c3b_policy(formCode); + case "toggle_1i56" -> commonField.setFna_c3b_others(formCode); + case "Text3" -> commonField.setFna_c3b_desc(formCode); + + case "Check Box6" -> commonField.setFna_c1e_10(formCode); + case "Check Box7" -> commonField.setFna_c1e_10_20(formCode); + case "Check Box8" -> commonField.setFna_c1e_21_30(formCode); + case "Check Box9" -> commonField.setFna_c1e_31_40(formCode); + case "Check Box10" -> commonField.setFna_c1e_41_50(formCode); + case "Check Box11" -> commonField.setFna_c1e_50(formCode); + + case "Check Box12" -> commonField.setFna_c2f_10(formCode); + case "Check Box13" -> commonField.setFna_c2f_10_20(formCode); + case "Check Box14" -> commonField.setFna_c2f_21_30(formCode); + case "Check Box15" -> commonField.setFna_c2f_31_40(formCode); + case "Check Box16" -> commonField.setFna_c2f_41_50(formCode); + case "Check Box17" -> commonField.setFna_c2f_50(formCode); + /* Page5 End */ + } } - logger.info("end Field: " + field.getPartialName() + ", Value: " + field.getValueAsString()); + //logger.info("end Field: " + field.getPartialName() + ", Value: " + field.getValueAsString()); // You can save this data to a database, or perform other operations } commonFieldService.save(commonField); } - logger.info("ended"); + //logger.info("ended"); document.close(); - logger.info("closed"); + //logger.info("closed"); } private void setValueIfPresent(PdfAcroForm form, String fieldName, String value, String valueSource) { diff --git a/src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java b/src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java index 9dcae04..aba666b 100644 --- a/src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java +++ b/src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java @@ -185,6 +185,24 @@ public class TemplateService extends AbstractBaseEntityService getLastPdfByFormCode(String formCode, Long clientId) { + StringBuilder sql = new StringBuilder("SELECT" + + " t.id, " + + " t.name, " + + " t.remarks, " + + " f.filename, " + + " fb.bytes AS blobValue " + + " FROM filled_form ff " + + " LEFT JOIN template t ON ff.templateId = t.id " + + " LEFT JOIN file f ON f.id = t.fileId " + + " LEFT JOIN file_blob fb ON f.id = fb.fileId " + + " WHERE t.deleted = FALSE " + + " AND t.remarks = :formCode and ff.clientId = :clientId " + + " ORDER BY ff.id DESC LIMIT 1 " + ); + return jdbcDao.queryForMap(sql.toString(), Map.of("clientId", clientId, "formCode", formCode)).orElseThrow(NotFoundException::new); + } + public Map loadPDF(Long id) { // Pdf pdfInstance = find(id).orElseThrow(NotFoundException::new); // byte[] blobBytes = fileService.getfileBlob(pdfInstance.getFileId());