From ff013827fbcbbee75c80ef2b4fe0a8983d5f0434 Mon Sep 17 00:00:00 2001 From: nisha Date: Mon, 28 Oct 2024 20:07:11 +0530 Subject: [PATCH] Updated code for pdf table issue --- .../gepafin/tendermanagement/dao/PdfDao.java | 29 ++++++++++++------- .../gepafin/tendermanagement/util/Utils.java | 27 +++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java b/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java index 702c61d1..3e93f84a 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java @@ -277,6 +277,9 @@ public class PdfDao { if(Utils.isValidDateString(fieldValue1)){ fieldValue1=Utils.formatDateString(String.valueOf(value)); } + if(Boolean.TRUE.equals(Utils.isItalianFormattedAmount(fieldValue)) ){ + fieldValue= String.valueOf(Utils.convertItalianAmountToDouble(fieldValue)); + } PdfPCell valueCell = new PdfPCell(new Phrase(fieldValue1, valueFont)); valueCell.setPadding(5f); // Increase padding for better spacing valueCell.setPaddingLeft(leftMargin); // Increase left margin for value @@ -352,7 +355,6 @@ public class PdfDao { List orderedKeys = new ArrayList<>(trueKeys); orderedKeys.addAll(falseKeys); // Iterate through extracted data to populate the table - for (Map row : extractedData) { // Add headers once if (!headersAdded) { for (String key : orderedKeys) { @@ -369,16 +371,23 @@ public class PdfDao { } // Add data rows matching stateFieldMap keys - for (String key : orderedKeys) { - Object value = row.getOrDefault(key, ""); // Fetch value or use empty string if key not present - PdfPCell dataCell = new PdfPCell(new Phrase(value != null ? value.toString() : "", textFont)); - dataCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell - dataCell.setMinimumHeight(rowHeight); - dataCell.setFixedHeight(rowHeight); - dataCell.setPadding(7f); + for (Map row : extractedData) { + // Add data rows matching stateFieldMap keys + for (String key : orderedKeys) { + if (stateFieldMap.containsKey(key)) { // Only add data cell if key is in stateFieldMap + Object value = row.getOrDefault(key, ""); // Fetch value or use empty string if key not present + String fieldValue= (String) value; + if(Boolean.TRUE.equals(Utils.isItalianFormattedAmount(fieldValue)) ){ + fieldValue= String.valueOf(Utils.convertItalianAmountToDouble(fieldValue)); + } + PdfPCell dataCell = new PdfPCell(new Phrase(fieldValue != null ?fieldValue: "", textFont)); + dataCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell + dataCell.setMinimumHeight(rowHeight); + dataCell.setPadding(7f); - table.addCell(dataCell); // Add the cell to the table - } + table.addCell(dataCell); // Add the cell to the table + } + } // Check if adding another row would exceed max height if (table.getTotalHeight() + rowHeight > maxTableHeight) { diff --git a/src/main/java/net/gepafin/tendermanagement/util/Utils.java b/src/main/java/net/gepafin/tendermanagement/util/Utils.java index 7dea2276..563a1805 100644 --- a/src/main/java/net/gepafin/tendermanagement/util/Utils.java +++ b/src/main/java/net/gepafin/tendermanagement/util/Utils.java @@ -5,6 +5,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; +import java.text.DecimalFormat; +import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; @@ -414,4 +416,29 @@ public class Utils { return null; } } + public static String convertItalianAmountToDouble(String amount) throws NumberFormatException { + // Step 1: Remove the thousands separator and replace the decimal separator + String normalizedAmount = amount.replace(".", "").replace(",", "."); + + // Step 2: Parse the string to a double + double value = Double.parseDouble(normalizedAmount); + if (value <= 0) { + return amount; + } + // Step 3: Format the double to 2 decimal places + DecimalFormat decimalFormat = new DecimalFormat("#.00"); + return decimalFormat.format(value); // Return formatted string + } + public static boolean isItalianFormattedAmount(String input) { + // Regular expression to match Italian-style amounts (e.g., 41.003,00 or 123,45) + if (!input.contains(",")) { + return false; // Return false if there is no comma + } + String sanitizedInput = input.replace(",", ""); + + // Step 2: Check if the remaining string is a whole number + String wholeNumberPattern = "^\\d+$"; // Regex to match whole numbers + + return sanitizedInput.matches(wholeNumberPattern); + } }