Skip to content

Commit

Permalink
fix: fixed styling of email body in generated PDF (#597)
Browse files Browse the repository at this point in the history
Fixed styling of email body in generated PDF by adding missing line
breaks.

Solves PZ-1502
  • Loading branch information
edgarvonk authored Feb 20, 2024
1 parent 3ba78a7 commit 0f0b387
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 57 deletions.
112 changes: 64 additions & 48 deletions src/main/java/net/atos/zac/mail/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.stream.Collectors.joining;
import static net.atos.client.zgw.shared.util.InformatieobjectenUtil.convertByteArrayToBase64String;
import static net.atos.zac.configuratie.ConfiguratieService.OMSCHRIJVING_VOORWAARDEN_GEBRUIKSRECHTEN;
import static net.atos.zac.mail.MailjetClientHelper.createMailjetClient;
import static net.atos.zac.util.JsonbUtil.JSONB;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -47,9 +48,7 @@
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.mailjet.client.ClientOptions;
import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.errors.MailjetException;
Expand Down Expand Up @@ -77,7 +76,6 @@

@ApplicationScoped
public class MailService {

private static final String MAILJET_API_KEY =
ConfigProvider.getConfig().getValue("mailjet.api.key", String.class);

Expand Down Expand Up @@ -125,36 +123,42 @@ public class MailService {
@Inject
private Instance<LoggedInUser> loggedInUserInstance;

private final ClientOptions clientOptions = ClientOptions.builder().apiKey(MAILJET_API_KEY)
.apiSecretKey(MAILJET_API_SECRET_KEY).build();

private final MailjetClient mailjetClient = new MailjetClient(clientOptions);
private final MailjetClient mailjetClient = createMailjetClient(MAILJET_API_KEY, MAILJET_API_SECRET_KEY);

public MailAdres getGemeenteMailAdres() {
return new MailAdres(configuratieService.readGemeenteMail(), configuratieService.readGemeenteNaam());
}

public String sendMail(final MailGegevens mailGegevens, final Bronnen bronnen) {

final String subject = StringUtils.abbreviate(
resolveVariabelen(mailGegevens.getSubject(), bronnen),
SUBJECT_MAXWIDTH);
SUBJECT_MAXWIDTH
);
final String body = resolveVariabelen(mailGegevens.getBody(), bronnen);
final List<Attachment> attachments = getAttachments(mailGegevens.getAttachments());

final EMail eMail = new EMail(
mailGegevens.getFrom(), List.of(mailGegevens.getTo()), mailGegevens.getReplyTo(),
subject, body, attachments);
mailGegevens.getFrom(),
List.of(mailGegevens.getTo()),
mailGegevens.getReplyTo(),
subject,
body,
attachments
);
final MailjetRequest request = new MailjetRequest(Emailv31.resource)
.setBody(JSONB.toJson(new EMails(List.of(eMail))));
try {
final int status = mailjetClient.post(request).getStatus();
if (status < 300) {
if (mailGegevens.isCreateDocumentFromMail()) {
createZaakDocumentFromMail(
mailGegevens.getFrom().getEmail(), mailGegevens.getTo().getEmail(),
subject, body, attachments,
bronnen.zaak);
mailGegevens.getFrom().getEmail(),
mailGegevens.getTo().getEmail(),
subject,
body,
attachments,
bronnen.zaak
);
}
} else {
LOG.log(Level.WARNING,
Expand All @@ -167,17 +171,14 @@ public String sendMail(final MailGegevens mailGegevens, final Bronnen bronnen) {
return body;
}

private void createZaakDocumentFromMail(final String verzender, final String ontvanger, final String subject,
final String body, final List<Attachment> attachments, final Zaak zaak) {
final EnkelvoudigInformatieObjectData informatieObject =
createDocumentInformatieObject(verzender, ontvanger, subject, body, attachments, zaak);
zgwApiService.createZaakInformatieobjectForZaak(zaak, informatieObject, subject,
subject, OMSCHRIJVING_VOORWAARDEN_GEBRUIKSRECHTEN);
}

private EnkelvoudigInformatieObjectData createDocumentInformatieObject(final String verzender,
final String ontvanger, final String subject, final String body, final List<Attachment> attachments,
final Zaak zaak) {
private void createZaakDocumentFromMail(
final String verzender,
final String ontvanger,
final String subject,
final String body,
final List<Attachment> attachments,
final Zaak zaak
) {
final InformatieObjectType eMailObjectType = getEmailInformatieObjectType(zaak);
final byte[] pdfDocument = createPdfDocument(verzender, ontvanger, subject, body, attachments);

Expand All @@ -197,47 +198,60 @@ private EnkelvoudigInformatieObjectData createDocumentInformatieObject(final Str
enkelvoudigInformatieobjectWithInhoud.setVertrouwelijkheidaanduiding(
EnkelvoudigInformatieObjectData.VertrouwelijkheidaanduidingEnum.OPENBAAR);
enkelvoudigInformatieobjectWithInhoud.setVerzenddatum(LocalDate.now());
return enkelvoudigInformatieobjectWithInhoud;

zgwApiService.createZaakInformatieobjectForZaak(
zaak,
enkelvoudigInformatieobjectWithInhoud,
subject,
subject,
OMSCHRIJVING_VOORWAARDEN_GEBRUIKSRECHTEN
);
}

private byte[] createPdfDocument(final String verzender, final String ontvanger, final String subject,
final String body, final List<Attachment> attachments) {
private byte[] createPdfDocument(
final String verzender,
final String ontvanger,
final String subject,
final String body,
final List<Attachment> attachments
) {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (
final PdfWriter pdfWriter = new PdfWriter(byteArrayOutputStream);
final PdfDocument pdfDoc = new PdfDocument(pdfWriter);
final Document document = new Document(pdfDoc);
) {
final Paragraph paragraph = new Paragraph();
final Paragraph headerParagraph = new Paragraph();
final PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER);

paragraph.setFont(font).setFontSize(16).setFontColor(ColorConstants.BLACK);
paragraph.add(String.format("%s: %s %n %n", MAIL_VERZENDER, verzender));
paragraph.add(String.format("%s: %s %n %n", MAIL_ONTVANGER, ontvanger));
headerParagraph.setFont(font).setFontSize(16).setFontColor(ColorConstants.BLACK);
headerParagraph.add(String.format("%s: %s %n %n", MAIL_VERZENDER, verzender));
headerParagraph.add(String.format("%s: %s %n %n", MAIL_ONTVANGER, ontvanger));
if (!attachments.isEmpty()) {
String content = attachments.stream().map(attachment -> String.valueOf(attachment.getFilename()))
.collect(joining(", "));
paragraph.add(String.format("%s: %s %n %n", MAIL_BIJLAGE, content));
headerParagraph.add(String.format("%s: %s %n %n", MAIL_BIJLAGE, content));
}

paragraph.add(String.format("%s: %s %n %n", MAIL_ONDERWERP, subject));
paragraph.add(String.format("%s %n", MAIL_BERICHT));
headerParagraph.add(String.format("%s: %s %n %n", MAIL_ONDERWERP, subject));
headerParagraph.add(String.format("%s: %n", MAIL_BERICHT));
document.add(headerParagraph);

Paragraph emailBodyParagraph = new Paragraph();
final HtmlCleaner cleaner = new HtmlCleaner();
final TagNode rootTagNode = cleaner.clean(body);
final CleanerProperties cleanerProperties = cleaner.getProperties();
cleanerProperties.setOmitXmlDeclaration(true);

final XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
final String html = xmlSerializer.getAsString(rootTagNode);

final List<IElement> elements = HtmlConverter.convertToElements(html);
for (IElement element : elements) {
paragraph.add((IBlockElement)element);
}

document.add(paragraph);

HtmlConverter.convertToElements(html).forEach(element -> {
emailBodyParagraph.add((IBlockElement) element);
// the individual (HTML paragraph) block elements are not separated
// with new lines, so we add them explicitly here
emailBodyParagraph.add("\n");
});
document.add(emailBodyParagraph);
} catch (final PdfException | IOException e) {
LOG.log(Level.SEVERE, "Failed to create pdf document", e);
}
Expand Down Expand Up @@ -283,11 +297,13 @@ private String resolveVariabelen(final String tekst, final Bronnen bronnen) {
return mailTemplateHelper.resolveVariabelen(
mailTemplateHelper.resolveVariabelen(
mailTemplateHelper.resolveVariabelen(
mailTemplateHelper.resolveVariabelen(
tekst),
getZaakBron(bronnen))
, bronnen.document)
, bronnen.taskInfo);
mailTemplateHelper.resolveVariabelen(tekst),
getZaakBron(bronnen)
),
bronnen.document
),
bronnen.taskInfo
);
}

private Zaak getZaakBron(final Bronnen bronnen) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/net/atos/zac/mail/MailjetClientHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.atos.zac.mail;

import com.mailjet.client.ClientOptions;
import com.mailjet.client.MailjetClient;

public class MailjetClientHelper {
public static MailjetClient createMailjetClient(String mailjetApiKey, String mailjetSecretKey) {
return new MailjetClient(
ClientOptions.builder()
.apiKey(mailjetApiKey)
.apiSecretKey(mailjetSecretKey)
.build()
);
}
}
7 changes: 5 additions & 2 deletions src/main/java/net/atos/zac/mail/model/Bronnen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class Bronnen {

public final TaskInfo taskInfo;

private Bronnen(final Zaak zaak, final EnkelvoudigInformatieObject document,
final TaskInfo taskInfo) {
private Bronnen(
final Zaak zaak,
final EnkelvoudigInformatieObject document,
final TaskInfo taskInfo
) {
this.zaak = zaak;
this.document = document;
this.taskInfo = taskInfo;
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/net/atos/zac/mailtemplates/model/MailGegevens.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ public class MailGegevens {
private final boolean createDocumentFromMail;

public MailGegevens(
final MailAdres from, final MailAdres to, final MailAdres replyTo,
final String subject, final String body, final String attachments,
final boolean createDocumentFromMail) {
final MailAdres from,
final MailAdres to,
final MailAdres replyTo,
final String subject,
final String body,
final String attachments,
final boolean createDocumentFromMail
) {
this.from = from;
this.to = to;
this.replyTo = replyTo;
Expand All @@ -37,8 +42,11 @@ public MailGegevens(
}

public MailGegevens(
final MailAdres from, final MailAdres to,
final String subject, final String body) {
final MailAdres from,
final MailAdres to,
final String subject,
final String body
) {
this(from, to, null, subject, body, null, false);
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/net/atos/client/zgw/ztc/model/ZtcFixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fun createRolType(

fun createZaakType(
uuid: UUID = UUID.randomUUID(),
omschrijving: String = "dummyZaakTypeOmschrijving"
omschrijving: String = "dummyZaakTypeOmschrijving",
informatieObjectTypen: Set<URI>? = setOf(URI("dummyInformatieObjectType1"), URI("dummyInformatieObjectType2")),
) = ZaakType(
URI("http://example.com/zaaktypes/$uuid"),
false,
Expand All @@ -28,7 +29,7 @@ fun createZaakType(
setOf(URI("dummyStatusType1"), URI("dummyStatusType2")),
setOf(URI("dummyResultaatType1"), URI("dummyResultaatType2")),
setOf(URI("dummyEigenschap1"), URI("dummyEigenschap2")),
setOf(URI("dummyInformatieObjectType1"), URI("dummyInformatieObjectType2")),
informatieObjectTypen,
setOf(URI("dummyRolType1"), URI("dummyRolType2")),
null
).apply {
Expand Down
Loading

0 comments on commit 0f0b387

Please sign in to comment.