Merge branch 'develop' of https://github.com/Kitzanos/GEPAFIN-BE into feature/GEPAFINBE-62

This commit is contained in:
harish
2024-10-26 17:50:11 +05:30

View File

@@ -10,7 +10,6 @@ import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.*;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.gepafin.tendermanagement.entities.*; import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.model.request.FieldLabelValuePairRequest; import net.gepafin.tendermanagement.model.request.FieldLabelValuePairRequest;
import net.gepafin.tendermanagement.model.response.*; import net.gepafin.tendermanagement.model.response.*;
@@ -25,6 +24,7 @@ import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -60,7 +60,8 @@ public class PdfDao {
// writer.setPageEvent(pageEvent); // writer.setPageEvent(pageEvent);
document.open(); document.open();
// pageEvent.setTotalPages(writer.getPageNumber()); // pageEvent.setTotalPages(writer.getPageNumber());
addLogo(document, "https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/logo.jpg"); // Add your image path here // addLogo(document, "logo.jpg"); // Add your image path here the migration code after cherry-pick
addLogo(document, "https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/logo.jpg");
BaseColor customColor = new BaseColor(0, 128, 0); // Adjust RGB values as needed BaseColor customColor = new BaseColor(0, 128, 0); // Adjust RGB values as needed
@@ -82,8 +83,6 @@ public class PdfDao {
ApplicationGetResponseBean applicationGetResponseBean=applicationDao.getApplicationByFormId(request, applicationId, null); ApplicationGetResponseBean applicationGetResponseBean=applicationDao.getApplicationByFormId(request, applicationId, null);
for(FormApplicationResponse formApplicationResponse: applicationGetResponseBean.getForm()) { for(FormApplicationResponse formApplicationResponse: applicationGetResponseBean.getForm()) {
document.add(new Paragraph(formApplicationResponse.getLabel(),sectionFont));
document.add(new Paragraph(" ")); // Add line break
List<FieldLabelValuePairRequest> fieldLabelValuePairRequests = getFormFieldsToLabels(formApplicationResponse,writer,document); List<FieldLabelValuePairRequest> fieldLabelValuePairRequests = getFormFieldsToLabels(formApplicationResponse,writer,document);
addColoredLines(writer,document,greenColor); addColoredLines(writer,document,greenColor);
document.add(new Paragraph(" ")); // Add line break document.add(new Paragraph(" ")); // Add line break
@@ -231,6 +230,48 @@ public class PdfDao {
} }
} }
else { else {
String fieldValue=Utils.convertToString(value);
Image img = null; // This may throw MalformedURLException
if (fieldValue.trim().equalsIgnoreCase("true")) {
// Use images for tick and cross
try {
// img = Image.getInstance("true.jpg"); update code after cherry-pick
img = Image.getInstance("https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/true.png");
} catch (IOException e) {
log.error("Error while uploading image for pdf for true");
}
img.scaleAbsolute(15, 15); // Resize the image if needed
PdfPCell cell = new PdfPCell(img);
cell.setPadding(0); // Remove padding
cell.setBorder(Rectangle.NO_BORDER); // Remove border
cell.setMinimumHeight(15f); // Set height to fit checkbox image
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT); // Align the checkbox image to the left
valueTable.addCell(cell); // Add cell with checkbox to the table
document.add(valueTable);
} else if (fieldValue.trim().equalsIgnoreCase("false")) {
// Use images for tick and cross
try {
img = Image.getInstance("https://mementoresources.s3.eu-west-1.amazonaws.com/gepafin/false.png");
} catch (IOException e) {
log.error("Error while uploading image for pdf for false");
}
img.scaleAbsolute(15, 15); // Resize the image if needed
PdfPCell cell = new PdfPCell(img);
cell.setPadding(0); // Remove padding
cell.setBorder(Rectangle.NO_BORDER); // Remove border
cell.setMinimumHeight(15f); // Set height to fit checkbox image
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT); // Align the checkbox image to the left
valueTable.addCell(cell); // Add cell with checkbox to the table
document.add(valueTable);
}
else {
PdfPCell valueCell = new PdfPCell(new Phrase(String.valueOf(value), valueFont)); PdfPCell valueCell = new PdfPCell(new Phrase(String.valueOf(value), valueFont));
valueCell.setPadding(5f); // Increase padding for better spacing valueCell.setPadding(5f); // Increase padding for better spacing
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
@@ -243,12 +284,14 @@ public class PdfDao {
document.add(valueTable); document.add(valueTable);
} }
}
document.add(new Paragraph("\n")); // Add line break after each value document.add(new Paragraph("\n")); // Add line break after each value
} }
private Document createPdfTable(List<Map<String, Object>> extractedData, Document document, ContentResponseBean contentResponseBean) throws DocumentException { private Document createPdfTable(List<Map<String, Object>> extractedData, Document document, ContentResponseBean contentResponseBean) throws DocumentException {
// Create a PdfPTable with dynamic column count based on stateFieldMap size // Create a PdfPTable with dynamic column count based on stateFieldMap size
Map<String, String> stateFieldMap = new HashMap<>(); Map<String, String> stateFieldMap = new HashMap<>();
Map<String, Boolean> stateFieldBoolean = new HashMap<>();
// Populate stateFieldMap from contentResponseBean settings // Populate stateFieldMap from contentResponseBean settings
contentResponseBean.getSettings().stream() contentResponseBean.getSettings().stream()
@@ -268,7 +311,22 @@ public class PdfDao {
stateFieldMap.put(fieldName, fieldDataValue); stateFieldMap.put(fieldName, fieldDataValue);
} }
}); });
contentResponseBean.getSettings().stream()
.filter(setting -> "table_columns".equals(setting.getName())) // Check for "table_columns"
.map(SettingResponseBean::getValue)
.filter(Objects::nonNull) // Ensure value is not null
.filter(settingValue -> settingValue instanceof Map) // Ensure value is a Map
.map(settingValue -> (Map<String, Object>) settingValue) // Cast to Map
.map(valueMap -> (List<Map<String, Object>>) valueMap.get("stateFieldData")) // Extract stateFieldData list
.filter(Objects::nonNull) // Ensure stateFieldData is not null
.flatMap(List::stream) // Flatten the list of field data maps
.forEach(fieldData -> {
String fieldName = (String) fieldData.get("name"); // Get the name field
Boolean predefined = (Boolean) fieldData.get("predefined"); // Get the predefined field
if (fieldName != null && fieldName != null) {
stateFieldBoolean.put(fieldName, predefined);
}
});
PdfPTable table = new PdfPTable(stateFieldMap.size()); // Number of columns equals the number of map entries PdfPTable table = new PdfPTable(stateFieldMap.size()); // Number of columns equals the number of map entries
table.setWidthPercentage(100); // Set table width to 100% table.setWidthPercentage(100); // Set table width to 100%
table.setTableEvent(new RoundedBorderEvent()); table.setTableEvent(new RoundedBorderEvent());
@@ -278,13 +336,23 @@ public class PdfDao {
float maxTableHeight = 700f; // Maximum height of the table before a page break float maxTableHeight = 700f; // Maximum height of the table before a page break
boolean headersAdded = false; // Flag to check if headers have been added boolean headersAdded = false; // Flag to check if headers have been added
List<String> trueKeys = new ArrayList<>();
List<String> falseKeys = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : stateFieldBoolean.entrySet()) {
if (Boolean.TRUE.equals(entry.getValue())) {
trueKeys.add(entry.getKey()); // Store true keys
} else {
falseKeys.add(entry.getKey()); // Store false keys
}
}
List<String> orderedKeys = new ArrayList<>(trueKeys);
orderedKeys.addAll(falseKeys);
// Iterate through extracted data to populate the table // Iterate through extracted data to populate the table
for (Map<String, Object> row : extractedData) { for (Map<String, Object> row : extractedData) {
// Add headers once // Add headers once
if (!headersAdded) { if (!headersAdded) {
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) { for (String key : orderedKeys) {
String headerValue = stateField.getValue(); // Header text String headerValue = stateFieldMap.get(key); // Header text
PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE); headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
@@ -297,10 +365,8 @@ public class PdfDao {
// Add data rows matching stateFieldMap keys // Add data rows matching stateFieldMap keys
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) { for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) {
String stateKey = stateField.getKey(); // Get the key from stateFieldMap for (String key : orderedKeys) { // Iterate over the ordered keys
if (row.containsKey(stateKey)) { // If row contains the stateKey Object value = row.getOrDefault(key, ""); // Fetch value or use empty string if key not present
Object value = row.get(stateKey); // Get the value from the row map
PdfPCell dynamicCell = new PdfPCell(new Phrase(value != null ? value.toString() : "", textFont)); PdfPCell dynamicCell = new PdfPCell(new Phrase(value != null ? value.toString() : "", textFont));
dynamicCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell dynamicCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell
dynamicCell.setMinimumHeight(rowHeight); dynamicCell.setMinimumHeight(rowHeight);