Sfoglia il codice sorgente

PDF service & controller update & clean-up

master
kelvinsuen 1 mese fa
parent
commit
2d1ebad854
3 ha cambiato i file con 41 aggiunte e 40 eliminazioni
  1. +16
    -23
      src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java
  2. +24
    -16
      src/main/java/com/ffii/lioner/modules/lioner/pdf/web/PdfController.java
  3. +1
    -1
      src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java

+ 16
- 23
src/main/java/com/ffii/lioner/modules/lioner/pdf/service/PdfService.java Vedi File

@@ -32,6 +32,7 @@ import com.ffii.lioner.modules.lioner.commonField.service.CommonFieldService;
import com.ffii.lioner.modules.lioner.entity.ImpEvent; import com.ffii.lioner.modules.lioner.entity.ImpEvent;
import com.ffii.lioner.modules.lioner.pdf.req.UpdatePdfReq; import com.ffii.lioner.modules.lioner.pdf.req.UpdatePdfReq;
import com.ffii.lioner.modules.lioner.service.FileService; import com.ffii.lioner.modules.lioner.service.FileService;
import com.ffii.lioner.modules.lioner.template.entity.Template;
import com.ffii.lioner.modules.lioner.template.service.TemplateService; import com.ffii.lioner.modules.lioner.template.service.TemplateService;
import com.ffii.lioner.modules.common.SecurityUtils; import com.ffii.lioner.modules.common.SecurityUtils;
import com.ffii.lioner.modules.common.service.AuditLogService; import com.ffii.lioner.modules.common.service.AuditLogService;
@@ -194,28 +195,16 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito
return file; return file;
} }


//This included the common fields getting
public byte[] getTemplateForm(Long templateId, Long clientId) {
// String filename;
// switch (templateId.intValue()) {
// case 1:
// filename = "IDA.pdf";
// break;
// case 2:
// filename = "FNA.pdf";
// break;
// case 3:
// filename = "HSBCFIN.pdf";
// break;
// default:
// filename = "HSBCFIN.pdf";
// };
// ClassPathResource pdfFile = new ClassPathResource(pdf_path + filename);
public Map<String, Object> getTemplate(Long templateId, Long clientId) {
Map<String, Object> template = templateService.loadTemplate(templateId); Map<String, Object> template = templateService.loadTemplate(templateId);
byte[] pdfBytes = (byte[])template.get("blobValue");;
// byte[] pdfBytes;
byte[] pdfBytes = (byte[])template.get("blobValue");
template.put("blobValue", getTemplateForm(pdfBytes, clientId, templateId));
return template;
}

public byte[] getTemplateForm(byte[] pdfBytes, Long clientId, Long templateId) {
byte[] modifiedPdfBytes = pdfBytes; byte[] modifiedPdfBytes = pdfBytes;


@@ -245,6 +234,8 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito
// logger.info("id? " + id); // logger.info("id? " + id);
CommonField commonField = commonFieldService.find(commonFieldId).orElse(null); CommonField commonField = commonFieldService.find(commonFieldId).orElse(null);


Template template = templateService.find(templateId).orElseThrow();

if(commonField == null){ if(commonField == null){
logger.info("No common field data found for clientId: " + clientId); logger.info("No common field data found for clientId: " + clientId);
// do nothing // do nothing
@@ -252,7 +243,7 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito
// It's highly recommended to check if the field exists before setting its value // It's highly recommended to check if the field exists before setting its value
// This prevents NullPointerExceptions if a field is missing in a template // This prevents NullPointerExceptions if a field is missing in a template
//temp using the remarks as form code //temp using the remarks as form code
String formCode = (String)template.get("remarks");
String formCode = (String)template.getRemarks();


if("IDA".equals(formCode)){ // This is IDA if("IDA".equals(formCode)){ // This is IDA
logger.info("Processing IDA form (ID: 1)"); logger.info("Processing IDA form (ID: 1)");
@@ -273,7 +264,7 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito
setValueIfPresent(form, "fill_5", commonField.getGender(), "commonField.getGender()"); setValueIfPresent(form, "fill_5", commonField.getGender(), "commonField.getGender()");
setValueIfPresent(form, "fill_6", commonField.getDateOfBirth(), "commonField.getDateOfBirth()"); // Note: fill_6 used here again setValueIfPresent(form, "fill_6", commonField.getDateOfBirth(), "commonField.getDateOfBirth()"); // Note: fill_6 used here again
setValueIfPresent(form, "fill_9", commonField.getOccupation(), "commonField.getOccupation()"); setValueIfPresent(form, "fill_9", commonField.getOccupation(), "commonField.getOccupation()");
}
}


// These fields are set unconditionally if commonField is not null // These fields are set unconditionally if commonField is not null
// Ensure these fields exist across all your templates or handle them conditionally // Ensure these fields exist across all your templates or handle them conditionally
@@ -338,9 +329,11 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito


StringBuilder sql = new StringBuilder("SELECT" StringBuilder sql = new StringBuilder("SELECT"
+ " ff.*, " + " ff.*, "
+ " f.filename, "
+ " fb.bytes AS blobValue" + " fb.bytes AS blobValue"
+ " FROM filled_form ff " + " FROM filled_form ff "
+ " LEFT JOIN file_blob fb ON ff.fileId = fb.fileId " + " LEFT JOIN file_blob fb ON ff.fileId = fb.fileId "
+ " LEFT JOIN file f ON fb.fileId = f.id "
+ " WHERE ff.deleted = FALSE" + " WHERE ff.deleted = FALSE"
+ " AND ff.id = :id " + " AND ff.id = :id "
); );
@@ -416,7 +409,7 @@ public class PdfService extends AbstractBaseEntityService<Pdf, Long, PdfReposito
case "fill_6" -> commonField.setName(fieldValue); case "fill_6" -> commonField.setName(fieldValue);
case "fill_7" -> commonField.setNameChi(fieldValue); case "fill_7" -> commonField.setNameChi(fieldValue);
case "fill_11" -> commonField.setIdCard(fieldValue); case "fill_11" -> commonField.setIdCard(fieldValue);
case "gender_m" -> { case "gender_m" -> {
commonField.setGenderMale(fieldValue); commonField.setGenderMale(fieldValue);
if("on".equals(fieldValue)) { if("on".equals(fieldValue)) {


+ 24
- 16
src/main/java/com/ffii/lioner/modules/lioner/pdf/web/PdfController.java Vedi File

@@ -79,28 +79,36 @@ public class PdfController {
} }


// Endpoint to serve the initial template PDF // Endpoint to serve the initial template PDF
@GetMapping(value = "/template", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getPdfTemplate(@RequestParam Long templateId, @RequestParam Long clientId) throws IOException {
byte[] pdfBytes = pdfService.getTemplateForm(templateId, clientId);
// @GetMapping(value = "/template2", produces = MediaType.APPLICATION_PDF_VALUE)
// public ResponseEntity<byte[]> getPdfTemplate(@RequestParam Long templateId, @RequestParam Long clientId) throws IOException {
// byte[] pdfBytes = pdfService.getTemplateForm(templateId, clientId);

// HttpHeaders headers = new HttpHeaders();
// headers.setContentDispositionFormData("inline", "template_form.pdf");
// headers.setContentType(MediaType.APPLICATION_PDF);
// return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
// }


HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("inline", "template_form.pdf");
headers.setContentType(MediaType.APPLICATION_PDF);
return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
// Endpoint to serve the initial template PDF
@GetMapping(value = "/template")
// public ResponseEntity<byte[]> getPdfTemplate(@RequestParam Long templateId, @RequestParam Long clientId) throws IOException {
public Map<String, Object> getPdfTemplate(@RequestParam Long templateId, @RequestParam Long clientId) throws IOException {
Map<String, Object> pdfTemplate = pdfService.getTemplate(templateId, clientId);
return pdfTemplate;
} }


// Get template by template id (Unused now) // Get template by template id (Unused now)
@GetMapping(value = "/template/{id}", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getPdfTemplateById(@PathVariable Long id) throws IOException {
// @GetMapping(value = "/template/{id}", produces = MediaType.APPLICATION_PDF_VALUE)
// public ResponseEntity<byte[]> getPdfTemplateById(@PathVariable Long id) throws IOException {
byte[] pdfBytes = pdfService.getTemplateForm(id, (long) 0);
// byte[] pdfBytes = pdfService.getTemplateForm(id, (long) 0);


HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("inline", "template_form.pdf");
headers.setContentType(MediaType.APPLICATION_PDF);
return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
}
// HttpHeaders headers = new HttpHeaders();
// headers.setContentDispositionFormData("inline", "template_form.pdf");
// headers.setContentType(MediaType.APPLICATION_PDF);
// return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
// }


// Endpoint to receive the filled PDF from the frontend // Endpoint to receive the filled PDF from the frontend
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false) @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false)


+ 1
- 1
src/main/java/com/ffii/lioner/modules/lioner/template/service/TemplateService.java Vedi File

@@ -164,7 +164,7 @@ public class TemplateService extends AbstractBaseEntityService<Template, Long, T
if (args.containsKey("createDateTo")) if (args.containsKey("createDateTo"))
sql.append(" AND t.created <= :createDateTo "); sql.append(" AND t.created <= :createDateTo ");
} }
sql.append(" ORDER BY t.id desc ");
sql.append(" ORDER BY t.id asc ");


return jdbcDao.queryForList(sql.toString(), args); return jdbcDao.queryForList(sql.toString(), args);
} }


Caricamento…
Annulla
Salva