Merge pull request #66 from Kitzanos/pdf-checkbox-changes-prod

Cherry-pick (PDF changes)
This commit is contained in:
rbonazzo-KZ
2024-10-26 15:49:13 +02:00
committed by GitHub
2 changed files with 96 additions and 30 deletions

View File

@@ -10,7 +10,6 @@ import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.model.request.FieldLabelValuePairRequest;
import net.gepafin.tendermanagement.model.response.*;
@@ -25,6 +24,7 @@ import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
@@ -60,7 +60,8 @@ public class PdfDao {
// writer.setPageEvent(pageEvent);
document.open();
// 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
@@ -82,8 +83,6 @@ public class PdfDao {
ApplicationGetResponseBean applicationGetResponseBean=applicationDao.getApplicationByFormId(request, applicationId, null);
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);
addColoredLines(writer,document,greenColor);
document.add(new Paragraph(" ")); // Add line break
@@ -231,6 +230,48 @@ public class PdfDao {
}
}
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));
valueCell.setPadding(5f); // Increase padding for better spacing
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
@@ -243,12 +284,14 @@ public class PdfDao {
document.add(valueTable);
}
}
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 {
// Create a PdfPTable with dynamic column count based on stateFieldMap size
Map<String, String> stateFieldMap = new HashMap<>();
Map<String, Boolean> stateFieldBoolean = new HashMap<>();
// Populate stateFieldMap from contentResponseBean settings
contentResponseBean.getSettings().stream()
@@ -268,7 +311,22 @@ public class PdfDao {
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
table.setWidthPercentage(100); // Set table width to 100%
table.setTableEvent(new RoundedBorderEvent());
@@ -278,13 +336,23 @@ public class PdfDao {
float maxTableHeight = 700f; // Maximum height of the table before a page break
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
for (Map<String, Object> row : extractedData) {
// Add headers once
if (!headersAdded) {
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) {
String headerValue = stateField.getValue(); // Header text
for (String key : orderedKeys) {
String headerValue = stateFieldMap.get(key); // Header text
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);
@@ -297,10 +365,8 @@ public class PdfDao {
// Add data rows matching stateFieldMap keys
for (Map.Entry<String, String> stateField : stateFieldMap.entrySet()) {
String stateKey = stateField.getKey(); // Get the key from stateFieldMap
if (row.containsKey(stateKey)) { // If row contains the stateKey
Object value = row.get(stateKey); // Get the value from the row map
for (String key : orderedKeys) { // Iterate over the ordered keys
Object value = row.getOrDefault(key, ""); // Fetch value or use empty string if key not present
PdfPCell dynamicCell = new PdfPCell(new Phrase(value != null ? value.toString() : "", textFont));
dynamicCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell
dynamicCell.setMinimumHeight(rowHeight);

View File

@@ -98,7 +98,7 @@ public interface UserApi {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
@Operation(summary = "Api to login user Changed the URL to identify the deployment issue",
@Operation(summary = "Api to login user",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "400", description = "Bad Request"),