Commit 76a79024 authored by Deep Dey's avatar Deep Dey

feat: add api to download admission information with list of ids

parent acb6faa9
......@@ -131,6 +131,20 @@
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.1.22</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -3,11 +3,14 @@ package com.example.LaMartin.admission.controller;
import com.example.LaMartin.admission.dao.controller.AdmissionFormDAOController;
import com.example.LaMartin.admission.model.AdmissionFormData;
import com.example.LaMartin.common.model.GeneralResponse;
import com.example.LaMartin.common.service.PdfService;
import com.example.LaMartin.common.service.TemplateService;
import com.example.LaMartin.constants.AppConstants;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
......@@ -16,13 +19,21 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RestController
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class AdmissionFormController {
private final AdmissionFormDAOController admissionFormDAOController;
private final TemplateService templateService;
private final PdfService pdfService;
@PostMapping(
value = "/admission-form/submit",
......@@ -73,7 +84,7 @@ public class AdmissionFormController {
@RequestHeader("apikey") String apiKey
) throws Exception {
//TODO: validation of api key using redis
// TODO: validation of api key using redis
return new ResponseEntity<>(
new GeneralResponse(
true,
......@@ -83,4 +94,44 @@ public class AdmissionFormController {
),
HttpStatus.OK);
}
@GetMapping(
value = "/admission-form/download-form"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "SUCCESS"),
@ApiResponse(code = 400, message = "FAILED"),
@ApiResponse(code = 500, message = "Exception Occurred")
})
@ApiOperation(value = "")
public void downloadAdmissionForm(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam ("apikey") String apiKey,
@RequestParam ("formIds") String formIds
) throws Exception {
// TODO: validation of api key using redis
List<Long> formIdList = Stream.of(formIds.split(","))
.map(Long::valueOf)
.collect(Collectors.toList());
if (formIdList.isEmpty()) {
throw new Exception("Provide formIds to download");
}
List<AdmissionFormData> admissionFormDataList =
this.admissionFormDAOController.getAdmissionFormDataListByIds(formIdList);
if (admissionFormDataList.isEmpty()) {
throw new Exception("No Admission Data Found");
}
String output = templateService
.generateAdmissionFormListTemplate(admissionFormDataList);
ByteArrayInputStream pdfStream = pdfService.convertHtmlToPdf(output);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=admission-form-list.pdf");
IOUtils.copy(pdfStream, response.getOutputStream());
}
}
......@@ -50,4 +50,8 @@ public class AdmissionFormDAOController {
return admissionFormDataRepository.getAdmissionFormDataList();
}
public List<AdmissionFormData> getAdmissionFormDataListByIds(List<Long> idList) {
return admissionFormDataRepository.getAdmissionFormDataListByIds(idList);
}
}
......@@ -119,4 +119,13 @@ public class AdmissionFormDataRepository {
String sql = "SELECT * FROM admission_form_data";
return namedParameterJdbcTemplate.query(sql, new AdmissionFormMapper());
}
public List<AdmissionFormData> getAdmissionFormDataListByIds(List<Long> ids) {
String sql = "SELECT * FROM admission_form_data WHERE id IN (:ids)";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("ids", ids);
return namedParameterJdbcTemplate.query(sql, parameters, new AdmissionFormMapper());
}
}
package com.example.LaMartin.common.service;
import org.springframework.stereotype.Service;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
@Service
public class PdfService {
public ByteArrayInputStream convertHtmlToPdf(String htmlContent) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(outputStream, false);
renderer.finishPDF();
return new ByteArrayInputStream(outputStream.toByteArray());
}
}
package com.example.LaMartin.common.service;
import com.example.LaMartin.admission.model.AdmissionFormData;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.util.List;
@Service
@AllArgsConstructor(onConstructor_ = @__({@Autowired}) )
public class TemplateService {
private final TemplateEngine templateEngine;
public String generateAdmissionFormListTemplate(List<AdmissionFormData> admissionFormDataList) {
Context context = new Context();
context.setVariable("admissionFormList", admissionFormDataList);
return templateEngine.process("AdmissionFormListTemplate", context);
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Information Table</title>
<style>
* {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
body {
width: 100%;
}
.container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 100%;
}
.table-container {
width: 100%;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
font-size: 14px;
}
td {
background-color: #f9f9f9;
border-bottom: 1px solid #ddd;
font-size: 12px;
}
tr:hover td {
background-color: #f1f1f1;
}
tr:nth-child(even) td {
background-color: #f2f2f2;
}
th:first-child, td:first-child {
width: 30%;
}
th:nth-child(2), td:nth-child(2) {
width: 70%;
}
.title {
/*text-align: center;*/
font-size: 18px;
margin-bottom: 10px;
color: #333;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container" th:each="admissionForm : ${admissionFormList}">
<div class="table-container">
<div class="title">Candidate Information (<span th:text="${admissionForm.fullName}"></span>)</div>
<table>
<tr>
<th>Field</th>
<th>Information</th>
</tr>
<tr>
<td>Choose your school</td>
<td th:text="${admissionForm.school}"></td>
</tr>
<tr>
<td>Category</td>
<td th:text="${admissionForm.category}"></td>
</tr>
<tr>
<td>Full Name</td>
<td th:text="${admissionForm.fullName}"></td>
</tr>
<tr>
<td>Class</td>
<td th:text="${admissionForm.admissionClass}"></td>
</tr>
<tr>
<td>Date of Birth (YYYY-MM-DD)</td>
<td th:text="${admissionForm.dateOfBirth}"></td>
</tr>
<tr>
<td>Nationality</td>
<td th:text="${admissionForm.nationality}"></td>
</tr>
<tr>
<td>Religion</td>
<td th:text="${admissionForm.religion}"></td>
</tr>
<tr>
<td>Mother Tongue</td>
<td th:text="${admissionForm.motherTongue}"></td>
</tr>
<tr>
<td>Community</td>
<td th:text="${admissionForm.community}"></td>
</tr>
<tr>
<td>Marital Status</td>
<td th:text="${admissionForm.maritalStatus}"></td>
</tr>
<tr>
<td>Father's Name</td>
<td th:text="${admissionForm.fatherName}"></td>
</tr>
<tr>
<td>Father's Contact Number</td>
<td th:text="${admissionForm.fatherMobile}"></td>
</tr>
<tr>
<td>Father's Email</td>
<td th:text="${admissionForm.fatherEmail}"></td>
</tr>
<tr>
<td>Father's Nationality</td>
<td th:text="${admissionForm.fatherNationality}"></td>
</tr>
<tr>
<td>Father's Religion</td>
<td th:text="${admissionForm.fatherReligion}"></td>
</tr>
<tr>
<td>Father's Mother Tongue</td>
<td th:text="${admissionForm.fatherMotherTongue}"></td>
</tr>
<tr>
<td>Father's Community</td>
<td th:text="${admissionForm.fatherCommunity}"></td>
</tr>
<tr>
<td>Father's School Info</td>
<td th:text="${admissionForm.fatherSchoolInfo}"></td>
</tr>
<tr>
<td>Father's College Info</td>
<td th:text="${admissionForm.fatherCollegeInfo}"></td>
</tr>
<tr>
<td>Father's University Info</td>
<td th:text="${admissionForm.fatherUniversityInfo}"></td>
</tr>
<tr>
<td>Father's Professional Qualification</td>
<td th:text="${admissionForm.fatherProfessionalQualification}"></td>
</tr>
<tr>
<td>Father's Occupation</td>
<td th:text="${admissionForm.fatherOccupation}"></td>
</tr>
<tr>
<td>Father's Monthly Income</td>
<td th:text="${admissionForm.fatherMonthlyIncome}"></td>
</tr>
<tr>
<td>Father's Workplace Info</td>
<td th:text="${admissionForm.fatherWorkplaceInfo}"></td>
</tr>
<tr>
<td>Mother's Name</td>
<td th:text="${admissionForm.motherName}"></td>
</tr>
<tr>
<td>Mother's Contact Number</td>
<td th:text="${admissionForm.motherMobile}"></td>
</tr>
<tr>
<td>Mother's Email</td>
<td th:text="${admissionForm.motherEmail}"></td>
</tr>
<tr>
<td>Mother's Nationality</td>
<td th:text="${admissionForm.motherNationality}"></td>
</tr>
<tr>
<td>Mother's Religion</td>
<td th:text="${admissionForm.motherReligion}"></td>
</tr>
<tr>
<td>Mother's Mother Tongue</td>
<td th:text="${admissionForm.motherMotherTongue}"></td>
</tr>
<tr>
<td>Mother's Community</td>
<td th:text="${admissionForm.motherCommunity}"></td>
</tr>
<tr>
<td>Mother's School Info</td>
<td th:text="${admissionForm.motherSchoolInfo}"></td>
</tr>
<tr>
<td>Mother's College Info</td>
<td th:text="${admissionForm.motherCollegeInfo}"></td>
</tr>
<tr>
<td>Mother's University Info</td>
<td th:text="${admissionForm.motherUniversityInfo}"></td>
</tr>
<tr>
<td>Mother's Professional Qualification</td>
<td th:text="${admissionForm.motherProfessionalQualification}"></td>
</tr>
<tr>
<td>Mother's Occupation</td>
<td th:text="${admissionForm.motherOccupation}"></td>
</tr>
<tr>
<td>Mother's Monthly Income</td>
<td th:text="${admissionForm.motherMonthlyIncome}"></td>
</tr>
<tr>
<td>Mother's Workplace Info</td>
<td th:text="${admissionForm.motherWorkplaceInfo}"></td>
</tr>
</table>
</div>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Title</title>
</head>
<body>
<h1 th:text="${title}">Title</h1>
<p th:text="${content}">Content goes here...</p>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment