Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRDP-273] fix: Date format to correctly parse zoned date with milliseconds #100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReasonErrorCode;
import it.gov.pagopa.receipt.pdf.generator.exception.PdfJsonMappingException;
import it.gov.pagopa.receipt.pdf.generator.exception.TemplateDataMappingException;
import it.gov.pagopa.receipt.pdf.generator.model.template.*;
import it.gov.pagopa.receipt.pdf.generator.model.template.Cart;
import it.gov.pagopa.receipt.pdf.generator.model.template.Debtor;
import it.gov.pagopa.receipt.pdf.generator.model.template.Item;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSP;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSPFee;
import it.gov.pagopa.receipt.pdf.generator.model.template.Payee;
import it.gov.pagopa.receipt.pdf.generator.model.template.PaymentMethod;
import it.gov.pagopa.receipt.pdf.generator.model.template.ReceiptPDFTemplate;
import it.gov.pagopa.receipt.pdf.generator.model.template.RefNumber;
import it.gov.pagopa.receipt.pdf.generator.model.template.Transaction;
import it.gov.pagopa.receipt.pdf.generator.model.template.User;
import it.gov.pagopa.receipt.pdf.generator.model.template.UserData;
import it.gov.pagopa.receipt.pdf.generator.service.BuildTemplateService;
import it.gov.pagopa.receipt.pdf.generator.utils.ObjectMapperUtils;
import it.gov.pagopa.receipt.pdf.generator.utils.TemplateDataField;
Expand All @@ -15,12 +26,16 @@
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -36,7 +51,6 @@ public class BuildTemplateServiceImpl implements BuildTemplateService {
private static final String PAGO_PA_CHANNEL_IO = "IO";
private static final String PAGO_PA_CHANNEL_IO_PAY = "IO-PAY";
private static final String RECEIPT_DATE_FORMAT = "dd MMMM yyyy, HH:mm:ss";
private static final String ZONED_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

/**
* Hide from public usage.
Expand Down Expand Up @@ -396,22 +410,26 @@ private String currencyFormat(String value) {
}

private String dateFormatZoned(String date) throws TemplateDataMappingException {
SimpleDateFormat stringDateFormat = new SimpleDateFormat(ZONED_DATE_FORMAT, Locale.ITALY);
stringDateFormat.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
Date parsed;
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern(RECEIPT_DATE_FORMAT))
.toFormatter(Locale.ITALY)
.withZone(TimeZone.getTimeZone("Europe/Rome").toZoneId());
try {
parsed = stringDateFormat.parse(date);
} catch (ParseException e) {
return OffsetDateTime.parse(date).format(formatter);
} catch (DateTimeException e) {
String errMsg = String.format("Error mapping bizEvent data to template, parse failed for property %s", TemplateDataField.TRANSACTION_TIMESTAMP);
throw new TemplateDataMappingException(errMsg, ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(RECEIPT_DATE_FORMAT, Locale.ITALY);
return dateFormat.format(parsed);
}

private String dateFormat(String date) {
private String dateFormat(String date) throws TemplateDataMappingException {
DateTimeFormatter simpleDateFormat = DateTimeFormatter.ofPattern(RECEIPT_DATE_FORMAT).withLocale(Locale.ITALY);
return LocalDateTime.parse(date).format(simpleDateFormat);
try {
return LocalDateTime.parse(date).format(simpleDateFormat);
} catch (DateTimeException e) {
String errMsg = String.format("Error mapping bizEvent data to template, parse failed for property %s", TemplateDataField.TRANSACTION_TIMESTAMP);
throw new TemplateDataMappingException(errMsg, ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e);
}
}

private String formatErrorMessage(String missingProperty) {
Expand Down
Loading
Loading