Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
feat: note about training system (#237)
Browse files Browse the repository at this point in the history
* initial hint for training system

* 45 degrees transform for english page

* don't show training text for production system

* don't show training text for production system

* testcase for appearing text for testsystem

* deleted file save

* hint for training system also for dcc

Co-authored-by: fOppenheimer <[email protected]>
  • Loading branch information
bergmann-dierk and fOppenheimer authored Apr 21, 2022
1 parent 6e191ab commit c5eca12
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/app/coronawarn/quicktest/config/PdfConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public class PdfConfig {
private String certTestTypeRat = "Rapid immunoassay";
private String certTestTypePcr = "Nucleic acid amplification";
private String certIssuerState = "DE";
private String certForTrainingEn = "Only for training purposes";
private String certForTrainingDe = "Nur zu Schulungszwecken";

private String certQrDescription = "Scannen Sie den <br> nebenstehenden <br> QR-Code mit der <br> "
+ "CovPass-App oder der <br> Corona-Warn-App, um <br> Ihren digitalen Nachweis <br> zu erstellen. Laden Sie <br> "
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/app/coronawarn/quicktest/utils/DccPdfGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static app.coronawarn.quicktest.utils.PdfUtils.splitStringToParagraph;

import app.coronawarn.quicktest.config.PdfConfig;
import app.coronawarn.quicktest.config.QuickTestConfig;
import app.coronawarn.quicktest.dgc.DccDecodeResult;
import app.coronawarn.quicktest.dgc.DccDecoder;
import app.coronawarn.quicktest.domain.QuickTest;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.util.Matrix;
Expand All @@ -67,6 +69,7 @@
public class DccPdfGenerator {

private final PdfConfig pdfConfig;
private final QuickTestConfig quickTestConfig;

private final DccDecoder dccDecoder;

Expand Down Expand Up @@ -138,6 +141,9 @@ private void generateCertPage(PDDocument document, QuickTest quicktest, String d
generateQrCode(document, cos, rect, dcc, false);
generateMemberStateInfoPage(document, cos, rect, false);
generateCertificateInfoPage(cos, rect, quicktest, dccDecodeResult, false);
if (!isEnvironmentNameEmpty()) {
generateTrainingText(cos, rect, false);
}
cos.close();
}

Expand All @@ -161,6 +167,9 @@ private void generateCertPageFoldable(PDDocument document, QuickTest quicktest,

generateMemberStateInfoPage(document, cos, rect, true);
generateCertificateInfoPage(cos, rect, quicktest, dccDecodeResult, true);
if (!isEnvironmentNameEmpty()) {
generateTrainingText(cos, rect, true);
}
cos.close();
}

Expand Down Expand Up @@ -596,4 +605,42 @@ private void close(PDDocument document, ByteArrayOutputStream output) throws IOE
document.save(output);
document.close();
}

private void generateTrainingText(PDPageContentStream cos, PDRectangle rect, boolean foldable) throws IOException {
PDFont font = PDType1Font.HELVETICA_BOLD;
int trainingFontSize = 28;

cos.beginText();
cos.setFont(font, trainingFontSize);
cos.setNonStrokingColor(Color.ORANGE);

float trainingFontWidth = 0.0f;
trainingFontWidth = font.getStringWidth(pdfConfig.getCertForTrainingDe()) / 1000 * trainingFontSize;
float x = 0;
float y = 0;
float tx = (trainingFontWidth / 2) / (float)Math.sqrt(2);
if (foldable) {
x = rect.getWidth() / 2 - tx;
y = rect.getHeight() - tx;
} else {
x = rect.getWidth() / 2 - tx;
y = rect.getHeight() / 2 - tx;
}

cos.transform(Matrix.getRotateInstance(
Math.toRadians(45), x, y));
cos.showText(pdfConfig.getCertForTrainingDe());
cos.endText();
}

private boolean isEnvironmentNameEmpty() {
if (quickTestConfig != null
&& quickTestConfig.getFrontendContextConfig() != null
&& quickTestConfig.getFrontendContextConfig().getEnvironmentName() != null
&& !quickTestConfig.getFrontendContextConfig().getEnvironmentName().isEmpty()) {
return false;
}

return true;
}
}
45 changes: 44 additions & 1 deletion src/main/java/app/coronawarn/quicktest/utils/PdfGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import static app.coronawarn.quicktest.utils.PdfUtils.splitStringToParagraph;

import app.coronawarn.quicktest.config.PdfConfig;
import app.coronawarn.quicktest.config.QuickTestConfig;
import app.coronawarn.quicktest.domain.QuickTest;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -49,6 +51,7 @@
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.util.Matrix;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

Expand All @@ -58,6 +61,7 @@
public class PdfGenerator {

private final PdfConfig pdfConfig;
private final QuickTestConfig quickTestConfig;

private final int pendingPcr = 10;
private final int negativePcr = 11;
Expand Down Expand Up @@ -138,6 +142,9 @@ private void write(PDDocument document, PDPageContentStream cos, PDRectangle rec
generateSubject(cos, rect, quicktest, english);
generateText(cos, rect, quicktest, user, english);
generateEnd(cos, rect, english);
if (!isEnvironmentNameEmpty()) {
generateTrainingText(cos, rect, english);
}
cos.close();
}

Expand Down Expand Up @@ -449,6 +456,33 @@ private void generateText(PDPageContentStream cos, PDRectangle rect, QuickTest q

}

private void generateTrainingText(PDPageContentStream cos, PDRectangle rect,
boolean english) throws IOException {
PDFont font = PDType1Font.HELVETICA_BOLD;
int trainingFontSize = 28;
float trainingFontWidth = 0.0f;

cos.beginText();
cos.setFont(font, trainingFontSize);
cos.setNonStrokingColor(Color.ORANGE);
if (english) {
trainingFontWidth = font.getStringWidth(pdfConfig.getCertForTrainingEn()) / 1000 * trainingFontSize;
float tx = (trainingFontWidth / 2) / (float)Math.sqrt(2);
cos.transform(Matrix.getRotateInstance(
Math.toRadians(45), rect.getWidth() / 2 - tx, rect.getHeight() / 2 - tx));
cos.showText(pdfConfig.getCertForTrainingEn());
} else {
trainingFontWidth = font.getStringWidth(pdfConfig.getCertForTrainingDe()) / 1000 * trainingFontSize;
//Horizontal zentrierte Schrift
//cos.newLineAtOffset(rect.getWidth() / 2 - trainingFontWidth /2, (rect.getHeight() / 2));
float tx = (trainingFontWidth / 2) / (float)Math.sqrt(2);
cos.transform(Matrix.getRotateInstance(
Math.toRadians(45), rect.getWidth() / 2 - tx, rect.getHeight() / 2 - tx));
cos.showText(pdfConfig.getCertForTrainingDe());
}
cos.endText();
}

private void generateEnd(PDPageContentStream cos, PDRectangle rect, boolean english) throws IOException {
cos.beginText();
cos.setFont(fontType, fontSize);
Expand All @@ -461,12 +495,21 @@ private void generateEnd(PDPageContentStream cos, PDRectangle rect, boolean engl
}
cos.newLine();
cos.endText();

}

private void close(PDDocument document, ByteArrayOutputStream output) throws IOException {
document.save(output);
document.close();
}

private boolean isEnvironmentNameEmpty() {
if (quickTestConfig != null
&& quickTestConfig.getFrontendContextConfig() != null
&& quickTestConfig.getFrontendContextConfig().getEnvironmentName() != null
&& !quickTestConfig.getFrontendContextConfig().getEnvironmentName().isEmpty()) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.Mockito.when;

import app.coronawarn.quicktest.config.PdfConfig;
import app.coronawarn.quicktest.config.QuickTestConfig;
import app.coronawarn.quicktest.dgc.DccDecoder;
import app.coronawarn.quicktest.domain.QuickTest;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -59,6 +60,10 @@ class DccPdfGeneratorTest {

private PdfConfig pdc = new PdfConfig();

@Mock
private QuickTestConfig quickTestConfig;
QuickTestConfig.FrontendContextConfig frontendContextConfig = new QuickTestConfig.FrontendContextConfig();

@BeforeEach
void setUp() {
when(dccDecoderMock.decodeDcc(any())).thenCallRealMethod();
Expand Down Expand Up @@ -102,6 +107,14 @@ void setUp() {

@Test
void appendDccPageTest() throws IOException {
frontendContextConfig.setEnvironmentName("");
when(quickTestConfig.getFrontendContextConfig()).thenReturn(frontendContextConfig);

String environment = frontendContextConfig.getEnvironmentName();
if(environment != null && !environment.isEmpty()) {
when(pdfConfig.getCertForTrainingDe()).thenReturn(pdc.getCertForTrainingDe());
}

when(pdfConfig.getCertTestTypeRat()).thenReturn(pdc.getCertTestTypeRat());

QuickTest quicktest = getQuickTest();
Expand Down Expand Up @@ -134,6 +147,14 @@ void appendDccPageTest() throws IOException {

@Test
void appendPcrDccPageTest() throws IOException {
frontendContextConfig.setEnvironmentName("testsystem");
when(quickTestConfig.getFrontendContextConfig()).thenReturn(frontendContextConfig);

String environment = frontendContextConfig.getEnvironmentName();
if(environment != null && !environment.isEmpty()) {
when(pdfConfig.getCertForTrainingDe()).thenReturn(pdc.getCertForTrainingDe());
}

when(pdfConfig.getCertTestTypePcr()).thenReturn(pdc.getCertTestTypePcr());

QuickTest quicktest = getQuickTest("LP6464-4", 1);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/app/coronawarn/quicktest/utils/PdfGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.Mockito.when;

import app.coronawarn.quicktest.config.PdfConfig;
import app.coronawarn.quicktest.config.QuickTestConfig;
import app.coronawarn.quicktest.domain.QuickTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -48,10 +49,13 @@ public class PdfGeneratorTest {
private PdfGenerator pdfGenerator;
@Mock
private PdfConfig pdfConfig;
@Mock
private QuickTestConfig quickTestConfig;

@Test
void generatePdfTest() throws IOException {
PdfConfig pdc = new PdfConfig();
QuickTestConfig.FrontendContextConfig frontendContextConfig = new QuickTestConfig.FrontendContextConfig();
when(pdfConfig.getLogoPath()).thenReturn("/logo");
when(pdfConfig.getAuthorPdfPropertiesText()).thenReturn("Unittest");
when(pdfConfig.getQuickTestHeadlineText()).thenReturn("Unittest");
Expand Down Expand Up @@ -89,6 +93,15 @@ void generatePdfTest() throws IOException {
when(pdfConfig.getAdditionalInfoDescriptionText()).thenReturn("Zusätzliche Informationen: ");
when(pdfConfig.getAdditionalInfoDescriptionTextEn()).thenReturn(pdc.getAdditionalInfoDescriptionTextEn());

frontendContextConfig.setEnvironmentName("testsystem");
when(quickTestConfig.getFrontendContextConfig()).thenReturn(frontendContextConfig);

String environment = frontendContextConfig.getEnvironmentName();
if(environment != null && !environment.isEmpty()) {
when(pdfConfig.getCertForTrainingDe()).thenReturn(pdc.getCertForTrainingDe());
when(pdfConfig.getCertForTrainingEn()).thenReturn(pdc.getCertForTrainingEn());
}

List<String> pocInformation = new ArrayList();
pocInformation.add("PoC Unittest");
pocInformation.add("Unittest Way 15");
Expand Down

0 comments on commit c5eca12

Please sign in to comment.