feat: continued implementing adminApiService

This commit is contained in:
Théo Le Lez
2025-03-12 12:16:01 +01:00
parent ef964c4d35
commit 8d486dce89
8 changed files with 158 additions and 18 deletions

View File

@ -10,6 +10,11 @@ import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@ -118,8 +123,9 @@ public class SharedApiService {
return appointments;
}
// TODO
public void getPDFReport(long appointmentId, String mail) {
//
public void getPDFReport(long appointmentId, String mail)
throws FileNotFoundException, DocumentException {
long projectId =
this.appointmentService
.getAppointmentById(appointmentId)
@ -139,15 +145,82 @@ public class SharedApiService {
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
/* return this.appointmentService
.getAppointmentById(appointmentId)
.getAppointmentReport().getReportContent(); */
// generate pdf from this string, and format it to be decent looking
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
logger.info(
"User {} generated the PDF report related to appointment {}", mail, appointmentId);
String reportContent =
this.appointmentService
.getAppointmentById(appointmentId)
.getAppointmentReport()
.getReportContent();
// PDF generation
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("Report" + appointmentId + ".pdf"));
document.open();
Paragraph title =
new Paragraph(
new Phrase(
"Compte Rendu - Réunion du "
+ this.appointmentService
.getAppointmentById(appointmentId)
.getAppointmentDate()
.toString(),
FontFactory.getFont(
FontFactory.HELVETICA,
20,
Font.BOLDITALIC,
BaseColor.BLACK)));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
Font subsection =
FontFactory.getFont(FontFactory.HELVETICA, 14, Font.UNDERLINE, BaseColor.DARK_GRAY);
Font body = FontFactory.getFont(FontFactory.COURIER, 12, BaseColor.BLACK);
String[] split = reportContent.split(" ");
String tmp = "";
int counter = 1;
for (String s : split) {
if (s.equals("//")) {
Chunk chunk = new Chunk(tmp, body);
document.add(chunk);
document.add(new Paragraph("\n"));
tmp = "";
Paragraph paragraph = new Paragraph("Point n°" + counter + " : ", subsection);
document.add(paragraph);
document.add(new Paragraph("\n"));
counter++;
} else {
tmp = tmp.concat(s + " ");
}
}
Chunk chunk = new Chunk(tmp, body);
document.add(chunk);
document.add(new Paragraph("\n"));
document.close();
}
// TODO
public void createAppointmentRequest(Appointment appointment, String mail) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
long projectId =
appointment
.getAppointmentListSectionCell()
.getFirst()
.getProjectSectionCell()
.getIdProject();
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
logger.warn(
"User {} tried to create for the project {} but is not allowed to.",
mail,
projectId);
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
logger.info("User {} tried to create an appointment for project {}", mail, projectId);
this.appointmentService.addNewAppointment(appointment);
}
}