253 lines
10 KiB
Java
253 lines
10 KiB
Java
package enseirb.myinpulse.service;
|
|
|
|
import com.itextpdf.text.*;
|
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
import enseirb.myinpulse.model.*;
|
|
import enseirb.myinpulse.service.database.*;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardCopyOption;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class SharedApiService {
|
|
|
|
protected static final Logger logger = LogManager.getLogger();
|
|
|
|
private final ProjectService projectService;
|
|
private final EntrepreneurService entrepreneurService;
|
|
private final SectionCellService sectionCellService;
|
|
private final AppointmentService appointmentService;
|
|
|
|
private final UtilsService utilsService;
|
|
|
|
@Autowired
|
|
SharedApiService(
|
|
ProjectService projectService,
|
|
EntrepreneurService entrepreneurService,
|
|
SectionCellService sectionCellService,
|
|
AppointmentService appointmentService,
|
|
UtilsService utilsService) {
|
|
this.projectService = projectService;
|
|
this.entrepreneurService = entrepreneurService;
|
|
this.sectionCellService = sectionCellService;
|
|
this.appointmentService = appointmentService;
|
|
this.utilsService = utilsService;
|
|
}
|
|
|
|
// TODO filter this with date
|
|
public Iterable<SectionCell> getSectionCells(
|
|
long projectId, long sectionId, String date, String mail) {
|
|
|
|
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
|
logger.warn(
|
|
"User {} tried to check section cells of the project {} but is not allowed to.",
|
|
mail,
|
|
projectId);
|
|
throw new ResponseStatusException(
|
|
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
|
|
}
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
|
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
|
|
|
|
Project project = this.projectService.getProjectById(projectId);
|
|
return this.sectionCellService.getSectionCellsByProjectAndSectionIdBeforeDate(
|
|
project, sectionId, dateTime);
|
|
}
|
|
|
|
// TODO: test
|
|
public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
|
|
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
|
logger.warn(
|
|
"User {} tried to check the member of the project {} but is not allowed to.",
|
|
mail,
|
|
projectId);
|
|
throw new ResponseStatusException(
|
|
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
|
|
}
|
|
Project project = this.projectService.getProjectById(projectId);
|
|
return this.entrepreneurService.GetEntrepreneurByProject(project);
|
|
}
|
|
|
|
// TODO: test
|
|
public Administrator getAdminByProjectId(long projectId, String mail) {
|
|
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
|
logger.warn(
|
|
"User {} tried to check the admin of the project {} but is not allowed to.",
|
|
mail,
|
|
projectId);
|
|
throw new ResponseStatusException(
|
|
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
|
|
}
|
|
Project project = this.projectService.getProjectById(projectId);
|
|
return project.getProjectAdministrator();
|
|
}
|
|
|
|
public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
|
|
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
|
logger.warn(
|
|
"User {} tried to check the appointments related to 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 check the appointments related to the project {}",
|
|
mail,
|
|
projectId);
|
|
Iterable<SectionCell> sectionCells =
|
|
this.sectionCellService.getSectionCellsByProject(
|
|
projectService.getProjectById(projectId),
|
|
2L); // sectionId useless in this function ?
|
|
List<Appointment> appointments = new ArrayList<Appointment>();
|
|
sectionCells.forEach(
|
|
sectionCell -> {
|
|
appointments.addAll(
|
|
this.sectionCellService.getAppointmentsBySectionCellId(
|
|
sectionCell.getIdSectionCell()));
|
|
});
|
|
return appointments;
|
|
}
|
|
|
|
public void getPDFReport(long appointmentId, String mail)
|
|
throws DocumentException, URISyntaxException, IOException {
|
|
long projectId =
|
|
this.appointmentService
|
|
.getAppointmentById(appointmentId)
|
|
.getAppointmentListSectionCell()
|
|
.getFirst()
|
|
.getProjectSectionCell()
|
|
.getIdProject();
|
|
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
|
logger.warn(
|
|
"User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.",
|
|
mail,
|
|
this.appointmentService
|
|
.getAppointmentById(appointmentId)
|
|
.getAppointmentReport()
|
|
.getIdReport(),
|
|
appointmentId);
|
|
throw new ResponseStatusException(
|
|
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
|
|
}
|
|
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();
|
|
|
|
// Replace uri with website address
|
|
Files.copy(
|
|
new URI(
|
|
"http://localhost:8080/shared/projects/appointments/report/"
|
|
+ appointmentId)
|
|
.toURL()
|
|
.openStream(),
|
|
Paths.get("Report" + appointmentId + ".pdf"),
|
|
StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
// delete file, we don't want to stock all reports on the server
|
|
File file = new File("Report" + appointmentId + ".pdf");
|
|
if (!file.delete()) {
|
|
logger.warn("Failed to delete report {}", file.getAbsolutePath());
|
|
}
|
|
}
|
|
|
|
public void createAppointmentRequest(Appointment appointment, String mail) {
|
|
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);
|
|
Appointment newAppointment = this.appointmentService.addNewAppointment(appointment);
|
|
newAppointment
|
|
.getAppointmentListSectionCell()
|
|
.forEach(
|
|
sectionCell -> {
|
|
sectionCell.updateAppointmentSectionCell(newAppointment);
|
|
});
|
|
newAppointment.getAppointmentReport().setAppointmentReport(newAppointment);
|
|
}
|
|
}
|