|
@@ -1,10 +1,26 @@
|
|
|
package pl.sudra.service;
|
|
package pl.sudra.service;
|
|
|
|
|
|
|
|
|
|
+import com.itextpdf.text.*;
|
|
|
|
|
+import com.itextpdf.text.pdf.PdfPCell;
|
|
|
|
|
+import com.itextpdf.text.pdf.PdfPTable;
|
|
|
|
|
+import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
|
+import jakarta.mail.MessagingException;
|
|
|
|
|
+import jakarta.mail.internet.MimeMessage;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.mail.MailException;
|
|
import org.springframework.mail.MailException;
|
|
|
import org.springframework.mail.SimpleMailMessage;
|
|
import org.springframework.mail.SimpleMailMessage;
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
|
+import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import pl.sudra.domain.Boat;
|
|
|
|
|
+import pl.sudra.domain.Reservation;
|
|
|
|
|
+import pl.sudra.domain.User;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
public class EmailServiceImpl implements EmailService {
|
|
public class EmailServiceImpl implements EmailService {
|
|
@@ -12,6 +28,18 @@ public class EmailServiceImpl implements EmailService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private JavaMailSender javaMailSender;
|
|
private JavaMailSender javaMailSender;
|
|
|
|
|
|
|
|
|
|
+ private PdfService pdfService;
|
|
|
|
|
+ private UserService userService;
|
|
|
|
|
+ private ReservationService reservationService;
|
|
|
|
|
+ private BoatService boatService;
|
|
|
|
|
+
|
|
|
|
|
+ public EmailServiceImpl(PdfService pdfService, UserService userService, ReservationService reservationService, BoatService boatService) {
|
|
|
|
|
+ this.pdfService = pdfService;
|
|
|
|
|
+ this.userService = userService;
|
|
|
|
|
+ this.reservationService = reservationService;
|
|
|
|
|
+ this.boatService = boatService;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public void sendMail(String receiver, String content, String subject) throws MailException {
|
|
public void sendMail(String receiver, String content, String subject) throws MailException {
|
|
|
SimpleMailMessage mail = new SimpleMailMessage();
|
|
SimpleMailMessage mail = new SimpleMailMessage();
|
|
|
mail.setFrom("EmailAuthor-SpringApplication"); // you may specify the name of the "from" field
|
|
mail.setFrom("EmailAuthor-SpringApplication"); // you may specify the name of the "from" field
|
|
@@ -21,5 +49,103 @@ public class EmailServiceImpl implements EmailService {
|
|
|
javaMailSender.send(mail);
|
|
javaMailSender.send(mail);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public ByteArrayOutputStream generatePDF(User user) throws DocumentException {
|
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
+
|
|
|
|
|
+ Document document = new Document();
|
|
|
|
|
+ PdfWriter writer = PdfWriter.getInstance(document, outputStream);
|
|
|
|
|
+
|
|
|
|
|
+ document.open();
|
|
|
|
|
+ Font font = FontFactory.getFont(FontFactory.HELVETICA, 20);
|
|
|
|
|
+
|
|
|
|
|
+ // Create a paragraph with centered and bigger text
|
|
|
|
|
+ Paragraph paragraph = new Paragraph("BILLING\n", font);
|
|
|
|
|
+ paragraph.setAlignment(Element.ALIGN_CENTER);
|
|
|
|
|
+
|
|
|
|
|
+ document.add(paragraph);
|
|
|
|
|
+ document.add(new Paragraph("\tUser Info:\n\n"));
|
|
|
|
|
+
|
|
|
|
|
+ PdfPTable userInfoTable = new PdfPTable(4);
|
|
|
|
|
+// userInfoTable.setWidthPercentage(100);
|
|
|
|
|
+ userInfoTable.setWidths(new float[]{30.0F, 90.0F, 170.0F, 60.0F});
|
|
|
|
|
+ System.out.println("WIDTH: " + userInfoTable.getTotalWidth());
|
|
|
|
|
+
|
|
|
|
|
+ // Add table headers
|
|
|
|
|
+ for (String header : List.of("ID", "USERNAME", "EMAIL", "ROLES")) {
|
|
|
|
|
+ PdfPCell cell = new PdfPCell(new Paragraph(header));
|
|
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
|
+
|
|
|
|
|
+ userInfoTable.addCell(cell);
|
|
|
|
|
+ }
|
|
|
|
|
+ // Add user info
|
|
|
|
|
+ for (String text : List.of(user.getId().toString(), user.getUsername(), user.getEmail(), user.getRole())) {
|
|
|
|
|
+ PdfPCell cell = new PdfPCell(new Paragraph(text));
|
|
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
|
+ userInfoTable.addCell(cell);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ document.add(userInfoTable);
|
|
|
|
|
+
|
|
|
|
|
+ document.add(new Paragraph("\n\tUser's reservations info:\n\n"));
|
|
|
|
|
+
|
|
|
|
|
+ List<Reservation> reservations = reservationService.findReservationsByUserId(user.getId());
|
|
|
|
|
+ List<Boat> boats = boatService.getBoats();
|
|
|
|
|
+
|
|
|
|
|
+ PdfPTable table = new PdfPTable(7);
|
|
|
|
|
+
|
|
|
|
|
+ // Set table width as a percentage of the page width
|
|
|
|
|
+ table.setWidthPercentage(100);
|
|
|
|
|
+
|
|
|
|
|
+ // Add table headers
|
|
|
|
|
+ for (String header : List.of("ID", "BOAT", "DATE", "FROM", "TO", "COST", "STATUS")) {
|
|
|
|
|
+ PdfPCell cell = new PdfPCell(new Paragraph(header));
|
|
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
|
+
|
|
|
|
|
+ table.addCell(cell);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (Reservation res : reservations) {
|
|
|
|
|
+ // Add user info
|
|
|
|
|
+ for (String text : List.of(
|
|
|
|
|
+ res.getId().toString(),
|
|
|
|
|
+ boats.stream().filter(boat -> Objects.equals(boat.getId(), res.getBoatId())).findFirst().get().getName(),
|
|
|
|
|
+ res.getDate().toString(),
|
|
|
|
|
+ "" + res.getStartHour(),
|
|
|
|
|
+ "" + res.getEndHour(),
|
|
|
|
|
+ res.getBill().getTotalCost().toString(),
|
|
|
|
|
+ res.getBill().getStatus())) {
|
|
|
|
|
+ PdfPCell cell = new PdfPCell(new Paragraph(text));
|
|
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
|
+ table.addCell(cell);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Add table to the document
|
|
|
|
|
+ document.add(table);
|
|
|
|
|
+ document.close();
|
|
|
|
|
+
|
|
|
|
|
+ return outputStream;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void sendMailWithPDF(String username, String content, String subject) throws MailException, MessagingException, DocumentException {
|
|
|
|
|
+// SimpleMailMessage mail = new SimpleMailMessage();
|
|
|
|
|
+ MimeMessage message = javaMailSender.createMimeMessage();
|
|
|
|
|
+ MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
|
|
|
|
+
|
|
|
|
|
+ User u = userService.findByUsername(username).get();
|
|
|
|
|
+
|
|
|
|
|
+ helper.setFrom("EmailAuthor-SpringApplication"); // you may specify the name of the "from" field
|
|
|
|
|
+ helper.setTo(u.getEmail());
|
|
|
|
|
+ helper.setSubject(subject);
|
|
|
|
|
+ helper.setText(content);
|
|
|
|
|
+
|
|
|
|
|
+ helper.addAttachment(
|
|
|
|
|
+ username + "_billing.pdf",
|
|
|
|
|
+ new ByteArrayResource(generatePDF(u).toByteArray()));
|
|
|
|
|
+
|
|
|
|
|
+ javaMailSender.send(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|