From d29ac8cc05effbee91ef6def7f3f7fca785009a5 Mon Sep 17 00:00:00 2001 From: nisha Date: Mon, 28 Oct 2024 22:15:25 +0530 Subject: [PATCH] Updated code for pdf updation --- .../gepafin/tendermanagement/dao/PdfDao.java | 7 ++- .../gepafin/tendermanagement/util/Utils.java | 52 ++++++++++++++----- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java b/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java index 2cf1d32b..d2e29e84 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/PdfDao.java @@ -278,7 +278,7 @@ public class PdfDao { fieldValue1=Utils.formatDateString(String.valueOf(value)); } if(Boolean.TRUE.equals(Utils.isItalianFormattedAmount(fieldValue)) ){ - fieldValue= String.valueOf(Utils.convertItalianAmountToDouble(fieldValue)); + fieldValue= String.valueOf(Utils.convertToItalianFormat(fieldValue)); } PdfPCell valueCell = new PdfPCell(new Phrase(fieldValue1, valueFont)); valueCell.setPadding(5f); // Increase padding for better spacing @@ -362,7 +362,6 @@ public class PdfDao { PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE); - headerCell.setFixedHeight(rowHeight); headerCell.setBackgroundColor(new BaseColor(178, 190, 181)); // Light gray background for header table.addCell(headerCell); // Add the header cell to the table @@ -378,7 +377,7 @@ public class PdfDao { 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)); + fieldValue= String.valueOf(Utils.convertToItalianFormat(fieldValue)); } PdfPCell dataCell = new PdfPCell(new Phrase(fieldValue != null ?fieldValue: "", textFont)); dataCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell @@ -498,7 +497,7 @@ public class PdfDao { } fieldValue = matchedLabels; } - + // Further processing of field value (e.g., finding labels in options) fieldValue = findLabelInOptions(content.getSettings(), fieldValue); } else { diff --git a/src/main/java/net/gepafin/tendermanagement/util/Utils.java b/src/main/java/net/gepafin/tendermanagement/util/Utils.java index 563a1805..3bd7041e 100644 --- a/src/main/java/net/gepafin/tendermanagement/util/Utils.java +++ b/src/main/java/net/gepafin/tendermanagement/util/Utils.java @@ -416,24 +416,50 @@ 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(",", "."); +// 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 +// } - // Step 2: Parse the string to a double - double value = Double.parseDouble(normalizedAmount); - if (value <= 0) { - return amount; + public static String convertToItalianFormat(String amount) { + try { + // Step 1: Sanitize and standardize the input + String sanitizedAmount = amount.trim(); + if (sanitizedAmount.matches("\\d")) { + return sanitizedAmount; + } + // Step 2: Handle Italian-style input (e.g., "37.192,00") + if (sanitizedAmount.contains(".") && sanitizedAmount.contains(",")) { + // Assume the period is a thousand separator and the comma is a decimal separator + sanitizedAmount = sanitizedAmount.replace(".", "").replace(",", "."); + } else if (sanitizedAmount.contains(",")) { + // If the input contains only a comma, treat it as a decimal separator + sanitizedAmount = sanitizedAmount.replace(",", "."); + } + // Step 3: Parse the cleaned-up string into a double + double parsedAmount = Double.parseDouble(sanitizedAmount); + // Step 4: Format the parsed amount to Italian format + NumberFormat italianFormat = NumberFormat.getInstance(Locale.ITALY); + italianFormat.setMinimumFractionDigits(2); + italianFormat.setMaximumFractionDigits(2); + + return italianFormat.format(parsedAmount); + } catch (NumberFormatException e) { + // In case of any issues with parsing, return a default or error message + return "Invalid amount format"; } - // 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