Untitled
unknown
plain_text
10 months ago
20 kB
5
Indexable
package com.mpc.cloud.hris.dynamic.multitenant.controller.fieldset.providers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.mpc.cloud.hris.common.Constants;
import com.mpc.cloud.hris.common.ObjectMapperUtil;
import com.mpc.cloud.hris.dynamic.multitenant.controller.fieldset.FieldSetProvider;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.entity.NationalIdentifierDetails;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.entity.NominationDTO;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.entity.UploadDocument;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.FieldSetConfigurationService;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.misc.DBProcedureInvocationService;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.model.ControlItem;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.model.Field;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.model.FieldGroupHeader;
import com.mpc.cloud.hris.dynamic.multitenant.tenant.service.model.FieldSet;
@Service(value = "nomination")
public class NominationFSProvider {
@Autowired
private FieldSetConfigurationService fieldSetConfigurationService;
@Autowired
private DBProcedureInvocationService dbProcedureInvocationService;
// Configure ObjectMapper with JavaTimeModule for handling LocalDate
private ObjectMapper objectMapper;
public NominationFSProvider() {
this.objectMapper = new ObjectMapper();
this.objectMapper.registerModule(new JavaTimeModule());
}
// @Override
// protected List<FieldSet> getFSData(String viewName, String fieldSetName, Long id, String requestedBy)
public List<Map<String, Object>> getNominationData (String viewName, String fieldSetName, String requestedBy)
throws Exception {
List<String> fieldSetNames = fieldSetName != null ? List.of(fieldSetName.split(",")) : null;
// List<NominationDTO> nominationlist = null;
List<NominationDTO> nominationlist = new ArrayList<>();
Map<String, String> data = new HashMap<>();
data.put("loginEmail", requestedBy);
// Fetch data using DB procedure
List<Map<String, Object>> result = dbProcedureInvocationService
.invokeFunctionParam(Constants.FUNCTION_GET_NOMINATION_BY_EMAIL, data);
if (!result.isEmpty()) {
Map<String, Object> row = result.get(0);
if (row.get(Constants.FUNCTION_GET_NOMINATION_BY_EMAIL.split("\\.")[1]) != null
&& !row.get(Constants.FUNCTION_GET_NOMINATION_BY_EMAIL.split("\\.")[1]).toString()
.equalsIgnoreCase("[]")) {
String values = row.get(Constants.FUNCTION_GET_NOMINATION_BY_EMAIL.split("\\.")[1]).toString();
JsonNode rootNode = objectMapper.readTree(values);
// boolean isValid = isValidSupportingDocument(rootNode, "supportingDocument");
//
// if (!isValid) {
// ArrayNode root = (ArrayNode) objectMapper.readTree(values);
// for (JsonNode node : root) {
// ((ObjectNode) node).remove("supportingDocument");
// }
// }
try {
// Deserialize JSON to NationalIdentifierDetails with LocalDate support
// nominationlist = objectMapper.readValue(values,
// new TypeReference<List<NominationDTO>>() {});
List<Map<String, Object>> rawList = objectMapper.readValue(values, new TypeReference<>() {});
for (Map<String, Object> rawData : rawList) {
NominationDTO nominationDTO = new NominationDTO();
nominationDTO.setId(Long.valueOf(rawData.get("id").toString()));
// nominationDTO.setProfileId(Long.valueOf(rawData.get("profileId").toString()));
nominationDTO.setNominationType((String) rawData.get("nominationType"));
nominationDTO.setReasonForChange((String) rawData.get("reasonForChange"));
// Extract nominee data dynamically
Map<String, String> nomineeNames = rawData.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("nomineeName"))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
Map<String, String> relationships = rawData.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("relationship"))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
Map<String, String> nomineePercentages = rawData.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("nomineePercentage"))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
Map<String, String> nomineeHeaders = rawData.entrySet().stream()
.filter(entry -> entry.getKey().startsWith("nomineeName"))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
nominationDTO.setNomineeName(nomineeNames);
nominationDTO.setRelationship(relationships);
nominationDTO.setNomineePercentage(nomineePercentages);
nominationDTO.setNomineeHeader(nomineeHeaders);
nominationDTO.setIsEditRequired((Boolean) rawData.get("isEditRequired"));
// // Create and set the 'isEditRequired' map, but only set it to 'true' if 'isAddedRequired' is true
// Map<String, Object> isEditRequired = new HashMap<>();
// isEditRequired.put("fieldSetName", rawData.get("id"));
//
// // Check if isAddedRequired is true; if yes, set isEditRequired value to true, otherwise keep it false
// if (rawData.containsKey("isEditRequired") && Boolean.parseBoolean(rawData.get("isEditRequired").toString())) {
// isEditRequired.put("value", true); // Set true when isAddedRequired is true
// } else if (!isEditRequired.containsKey("value")) {
// isEditRequired.put("value", false); // Set false if it's not set in previous iteration
// }
//
// // Set isEditRequired in the NominationDTO object
// nominationDTO.setIsEditRequired(isEditRequired);
nominationlist.add(nominationDTO);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
// return fieldSetConfigurationService.getFieldSetForWS(viewName, fieldSetNames, null);
return List.of();
}
}
List<FieldSet> fieldSet = new ArrayList<>();
Map<String, Object> fieldSetNomination = new HashMap<>();
List<Map<String, Object>> response = new ArrayList<>();
Map<String, Object> isEditRequired = new HashMap<>();
isEditRequired.put("fieldSetName", 0);
isEditRequired.put("value", false);
if (nominationlist != null) {
for (NominationDTO nomination : nominationlist) {
List<FieldSet> fieldSets = fieldSetConfigurationService.getFieldSetForWS(viewName, fieldSetNames,
nomination);
for (FieldSet fieldset : fieldSets) {
fieldset.setHeader(nomination.getNominationType());
updateFS(fieldset);
}
fieldSet.addAll(fieldSets);
System.out.println("Vishv " + nomination.getIsEditRequired());
if (nomination.getIsEditRequired()) {
isEditRequired.put("fieldSetName", nomination.getId());
isEditRequired.put("value", nomination.getIsEditRequired());
}
}
fieldSetNomination.put("fieldSet", fieldSet);
fieldSetNomination.put("isEditRequired", isEditRequired);
}
response.add(fieldSetNomination);
return response;
}
public static FieldSet updateFS(FieldSet fieldset) {
List<Field> fields = fieldset.getFields();
Map<Integer, List<Field>> nomineeFieldMap = new TreeMap<>(); // Maintain order by nominee index
try {
for (Field field : fields) {
String fieldValue = (String) field.getValue();
if (field.getName().startsWith("nomineePercentage") ||
field.getName().startsWith("nomineeName") ||
field.getName().startsWith("relationship") ||
field.getName().equals("nomineeHeader")) {
if (fieldValue != null && !fieldValue.isEmpty()) {
Map<String, String> fieldValueMap = parseStringToMap(fieldValue);
for (Map.Entry<String, String> entry : fieldValueMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// Extract the nominee index (e.g., 1, 2, 3, etc.) from the field name
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(key);
int index = 0; // Default to 0 if no number is found
if (matcher.find()) {
index = Integer.parseInt(matcher.group());
}
List<Field> nomineeFields = nomineeFieldMap.getOrDefault(index, new ArrayList<>());
if (field.getName().equals("nomineeHeader")) {
// Handle Nominee Header
Field newField = new Field();
String newFieldName = "nomineeHeader" + index;
newField.setName(newFieldName);
newField.setBaseType("field-group-header");
newField.setLabel("Nominee Header");
newField.setIndex(index);
newField.setRetainExistingOnEdit(false);
FieldGroupHeader fieldGroupHeader = new FieldGroupHeader();
fieldGroupHeader.setLabel("Nominee " + index);
fieldGroupHeader.setHeaderStyle("profile-nomination");
newField.setFieldGroupHeader(fieldGroupHeader);
nomineeFields.add(newField);
} else {
// Handle nomineePercentage, nomineeName, and relationship
Field newField = new Field();
newField.setName(key);
newField.setValue(value);
newField.setLabel(field.getLabel());
newField.setBaseType("drop-down");
if (newField.getName().startsWith("nomineeName")) {
newField.setOptionDataSet(field.getOptionDataSet());
}
newField.setIndex(index);
newField.setFieldLayout(field.getFieldLayout());
newField.setReadOnly("true");
nomineeFields.add(newField);
}
nomineeFieldMap.put(index, nomineeFields);
}
} else {
System.out.println("Field value is null for field: " + field.getName());
}
}
}
// **Maintain the desired order for each nominee group**
List<Field> orderedNewFields = new ArrayList<>();
for (Map.Entry<Integer, List<Field>> entry : nomineeFieldMap.entrySet()) {
List<Field> nomineeFields = entry.getValue();
// Sort the nominee fields in the desired order
nomineeFields.sort((f1, f2) -> {
// List of priorities
List<String> order = Arrays.asList(
"nomineeHeader",
"nomineeName",
"relationship",
"nomineePercentage"
);
// Remove any numbers from the names for direct comparison
String fieldName1 = f1.getName().replaceAll("\\d+", "");
String fieldName2 = f2.getName().replaceAll("\\d+", "");
int pos1 = order.indexOf(fieldName1);
int pos2 = order.indexOf(fieldName2);
return Integer.compare(pos1, pos2);
});
orderedNewFields.addAll(nomineeFields);
}
fields.addAll(orderedNewFields);
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
return fieldset;
}
// public static FieldSet updateFS(FieldSet fieldset) {
// List<Field> fields = fieldset.getFields();
// List<Field> newFields = new ArrayList<>(); // Temporary list for new fields
//
// try {
// for (Field field : fields) {
// String fieldValue = (String) field.getValue();
//
// // Combined if condition to handle all field names in one place
// if (field.getName().startsWith("nomineePercentage") ||
// field.getName().startsWith("nomineeName") ||
// field.getName().startsWith("relationship") ||
// field.getName().equals("nomineeHeader")) {
//
// if (fieldValue != null && !fieldValue.isEmpty()) {
// Map<String, String> fieldValueMap = parseStringToMap(fieldValue);
// int counter = 1; // Counter for nomineeHeader1, nomineeHeader2, etc.
//
// for (Map.Entry<String, String> entry : fieldValueMap.entrySet()) {
// String key = entry.getKey();
// String value = entry.getValue();
// List<Field> nomineeFields = nomineeFieldMap.getOrDefault(index, new ArrayList<>());
//
// if (field.getName().equals("nomineeHeader")) {
// String newFieldName = "nomineeHeader" + counter;
//
// Field newField = new Field();
// newField.setName(newFieldName);
// newField.setBaseType("field-group-header");
// newField.setLabel("Nominee Header");
// newField.setIndex(counter); // Use the counter to distinguish nomineeHeader1, nomineeHeader2, etc.
// newField.setRetainExistingOnEdit(false);
//
// // Create and set the fieldGroupHeader object properties
// FieldGroupHeader fieldGroupHeader = new FieldGroupHeader();
// fieldGroupHeader.setLabel("Nominee " + counter);
// fieldGroupHeader.setHeaderStyle("profile-nomination");
//
// newField.setFieldGroupHeader(fieldGroupHeader);
//
// newFields.add(newField); // Add to the temporary list
// counter++;
// }
// else {
// // Handle nomineePercentage, nomineeName, and relationship
// Field newField = new Field();
//
// Pattern pattern = Pattern.compile("\\d+");
// Matcher matcher = pattern.matcher(key);
// String index = "0";
//
// if (matcher.find()) {
// index = matcher.group();
// }
// newField.setName(key);
// newField.setValue(value);
// newField.setLabel(field.getLabel());
// newField.setBaseType(field.getBaseType());
// if (newField.getName().startsWith("nomineeName")) {
// newField.setOptions(field.getOptions());
// }
// newField.setIndex(Integer.parseInt(index));
// newField.setFieldLayout(field.getFieldLayout());
// newField.setReadOnly("true");
// newFields.add(newField);
// }
// }
// } else {
// System.out.println("Field value is null for field: " + field.getName());
// }
// }
// }
// fields.addAll(newFields);
// } catch (Exception ex) {
// System.out.println("Error: " + ex.getMessage());
// }
//
// return fieldset;
// }
//
//
private static Map<String, String> parseStringToMap(String fieldValue) {
Map<String, String> map = new HashMap<>();
// Remove the curly braces and split the string by commas
fieldValue = fieldValue.substring(1, fieldValue.length() - 1); // Remove "{ }"
String[] entries = fieldValue.split(",\\s*");
// Iterate through each entry and split by "="
for (String entry : entries) {
String[] keyValue = entry.split("=");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String value = keyValue[1].trim();
map.put(key, value);
}
}
return map;
}
}
Editor is loading...
Leave a Comment