Merge pull request #201 from Kitzanos/feature/GEPAFINBE-162
GEPAFINBE-162 (Calculation Process)
This commit is contained in:
@@ -31,6 +31,7 @@ import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundExceptio
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
import org.h2.util.IOUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -49,11 +50,16 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.MessageFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@@ -94,29 +100,29 @@ public class ApplicationDao {
|
||||
|
||||
@Autowired
|
||||
private FlowDataRepository flowDataRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserCompanyDelegationRepository userCompanyDelegationRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
@Autowired
|
||||
private S3PathConfig s3PathConfig;
|
||||
|
||||
|
||||
@Autowired
|
||||
private SystemEmailTemplatesService systemEmailTemplatesService;
|
||||
@Autowired
|
||||
private AssignedApplicationsRepository assignedApplicationsRepository;
|
||||
|
||||
|
||||
@Value("${default_System_Receiver_Email}")
|
||||
private String defaultSystemReceiverEmail;
|
||||
|
||||
|
||||
@Value("${rinaldo_email}")
|
||||
private String rinaldoEmail;
|
||||
|
||||
|
||||
@Value("${carlo_email}")
|
||||
private String carloEmail;
|
||||
|
||||
@@ -125,37 +131,37 @@ public class ApplicationDao {
|
||||
|
||||
@Autowired
|
||||
private AmazonS3Service amazonS3Service;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ApplicationSignedDocumentRepository applicationSignedDocumentRepository;
|
||||
|
||||
|
||||
// @Value("${aws.s3.url.folder.signed.document}")
|
||||
// private String signedDocumentS3Folder;
|
||||
|
||||
|
||||
@Value("${default.hub.uuid}")
|
||||
private String defaultHubUuid;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private S3PathConfig s3ConfigBean;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ProtocolDao protocolDao;
|
||||
|
||||
|
||||
@Autowired
|
||||
private HubService hubService;
|
||||
|
||||
@Autowired
|
||||
private EmailNotificationDao emailNotificationDao;
|
||||
|
||||
|
||||
@Autowired
|
||||
private FormDao formDao;
|
||||
|
||||
@Autowired
|
||||
private EmailLogDao emailLogDao;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserWithCompanyRepository userWithCompanyRepository;
|
||||
|
||||
@@ -183,6 +189,7 @@ public class ApplicationDao {
|
||||
@Autowired
|
||||
private ApplicationEvaluationRepository applicationEvaluationRepository;
|
||||
|
||||
|
||||
public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) {
|
||||
FormEntity formEntity = formService.validateForm(formId);
|
||||
// callService.validatePublishedCall(formEntity.getCall().getId());
|
||||
@@ -258,7 +265,7 @@ public class ApplicationDao {
|
||||
|
||||
// List<ContentResponseBean> contentResponseBeans = Utils.convertJsonStringToList(
|
||||
// applicationFormEntity.getForm().getContent(), ContentResponseBean.class);
|
||||
|
||||
|
||||
List<ContentResponseBean> contentResponseBeans = formDao.convertFormEntityToFormResponseBean(applicationFormEntity.getForm()).getContent();
|
||||
|
||||
for (ApplicationFormFieldEntity applicationFormFieldEntity : applicationFormFieldEntities) {
|
||||
@@ -309,7 +316,7 @@ public class ApplicationDao {
|
||||
);
|
||||
}
|
||||
ApplicationEntity oldApplicationDataEntity = Utils.getClonedEntityForData(applicationEntity);
|
||||
|
||||
|
||||
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
||||
applicationEntity.setIsDeleted(true);
|
||||
applicationEntity = applicationRepository.save(applicationEntity);
|
||||
@@ -356,9 +363,9 @@ public class ApplicationDao {
|
||||
//
|
||||
// return applicationResponses;
|
||||
// }
|
||||
|
||||
|
||||
public List<ApplicationResponse> getAllApplications(UserEntity userEntity, Long callId, Long companyId,List<ApplicationStatusTypeEnum> statusList) {
|
||||
|
||||
|
||||
log.info("Fetching applications for RoleType: {}", userEntity.getRoleEntity().getRoleType());
|
||||
|
||||
Specification<ApplicationEntity> spec = search(userEntity, callId, companyId,statusList);
|
||||
@@ -482,15 +489,18 @@ public class ApplicationDao {
|
||||
|
||||
public List<ApplicationFormFieldEntity> createOrUpdateMultipleFormFields(List<ApplicationFormFieldRequestBean> formFieldResponseBeans,
|
||||
ApplicationFormEntity applicationFormEntity, FormEntity formEntity) {
|
||||
FieldValidator fieldValidator = FieldValidator.create();
|
||||
|
||||
List<ApplicationFormFieldEntity> existingFields = applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId());
|
||||
|
||||
return formFieldResponseBeans.stream().map(requestBean -> createOrUpdateApplicationFormField(requestBean, applicationFormEntity, existingFields, formEntity))
|
||||
List<ApplicationFormFieldEntity> applicationFormFieldEntities=formFieldResponseBeans.stream().map(requestBean -> createOrUpdateApplicationFormField(requestBean, applicationFormEntity, existingFields, formEntity,fieldValidator))
|
||||
.collect(Collectors.toList());
|
||||
fieldValidator.validate();
|
||||
return applicationFormFieldEntities;
|
||||
}
|
||||
|
||||
public ApplicationFormFieldEntity createOrUpdateApplicationFormField(ApplicationFormFieldRequestBean applicationFormFieldRequestBean,
|
||||
ApplicationFormEntity applicationFormEntity, List<ApplicationFormFieldEntity> applicationFormFieldEntities, FormEntity formEntity) {
|
||||
ApplicationFormEntity applicationFormEntity, List<ApplicationFormFieldEntity> applicationFormFieldEntities, FormEntity formEntity,FieldValidator fieldValidator) {
|
||||
|
||||
ApplicationFormFieldEntity applicationFormFieldEntity = new ApplicationFormFieldEntity();
|
||||
|
||||
@@ -506,7 +516,7 @@ public class ApplicationDao {
|
||||
.filter(setting -> "isRequestedAmount".equals(setting.getName()) && Boolean.TRUE.equals(setting.getValue()))
|
||||
.findFirst()
|
||||
.ifPresent(setting -> {
|
||||
|
||||
|
||||
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
||||
if(fieldValue!=null) {
|
||||
try {
|
||||
@@ -538,6 +548,7 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
}
|
||||
calculationProcessForFormula(applicationFormEntity,contentResponseBeans,applicationFormFieldRequestBean,fieldValidator);
|
||||
Utils.setIfUpdated(applicationFormFieldEntity::getFieldId, applicationFormFieldEntity::setFieldId, applicationFormFieldRequestBean.getFieldId());
|
||||
|
||||
if (applicationFormFieldRequestBean.getFieldValue() != null) {
|
||||
@@ -560,7 +571,6 @@ public class ApplicationDao {
|
||||
VersionHistoryRequest.builder().request(request).actionType(actionType).oldData(oldApplicationFormFieldData).newData(applicationFormField).build());
|
||||
|
||||
log.info("Version history logged for action: {}, Field ID: {}", actionType, applicationFormFieldEntity.getFieldId());
|
||||
|
||||
return applicationFormField;
|
||||
}
|
||||
|
||||
@@ -818,7 +828,7 @@ public class ApplicationDao {
|
||||
if(formApplicationResponse.getContent() != null && formApplicationResponse.getFormFields() != null) {
|
||||
formApplicationResponses.add(formApplicationResponse);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public FormApplicationResponse processForm(FormEntity formEntity, ApplicationEntity applicationEntity) {
|
||||
@@ -954,7 +964,7 @@ public class ApplicationDao {
|
||||
return (int) Math.round(progress);
|
||||
}
|
||||
public void validateFormFields(ApplicationRequestBean request, FormEntity formEntity) {
|
||||
|
||||
|
||||
// List<ContentResponseBean> contentResponseBeans=Utils.convertJsonStringToList(formEntity.getContent(),ContentResponseBean.class);
|
||||
List<ContentResponseBean> contentResponseBeans=formDao.convertFormEntityToFormResponseBean(formEntity).getContent();
|
||||
|
||||
@@ -1034,7 +1044,7 @@ public class ApplicationDao {
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
|
||||
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum.APPLICATION_SUBMISSION_TO_USER_AND_COMPANY,
|
||||
hub, null);
|
||||
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", call.getName());
|
||||
@@ -1211,17 +1221,17 @@ public class ApplicationDao {
|
||||
}
|
||||
|
||||
public ApplicationSignedDocumentResponse getSignedDocument(HttpServletRequest request, Long applicationId) {
|
||||
|
||||
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
// validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
||||
|
||||
|
||||
if (validator.checkIsPreInstructor()) {
|
||||
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationService.validateApplicationEvaluationByApplicationId(applicationId);
|
||||
validator.validatePreInstructor(request, applicationEvaluationEntity.getUserId());
|
||||
} else {
|
||||
validator.validateUserId(request, applicationEntity.getUserId());
|
||||
}
|
||||
|
||||
|
||||
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository
|
||||
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
if(applicationSignedDocument == null) {
|
||||
@@ -1230,11 +1240,11 @@ public class ApplicationDao {
|
||||
}
|
||||
return convertApplicationSignedDocumentToApplicationSignedDocumentResponse(applicationSignedDocument);
|
||||
}
|
||||
|
||||
|
||||
public void deleteSignedDocument(HttpServletRequest request, Long applicationId) {
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
validator.validateUserWithCompany(request, applicationEntity.getCompanyId());
|
||||
|
||||
|
||||
ApplicationSignedDocumentEntity applicationSignedDocument = applicationSignedDocumentRepository
|
||||
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
//cloned entity for old data
|
||||
@@ -1539,5 +1549,135 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
|
||||
public void calculationProcessForFormula(ApplicationFormEntity applicationFormEntity, List<ContentResponseBean> contentResponseBeans, ApplicationFormFieldRequestBean applicationFormFieldRequestBean,FieldValidator fieldValidator) {
|
||||
List<String> formulaValue = new ArrayList<>();
|
||||
String formulaValueOpt=null;
|
||||
String label=null;
|
||||
for (ContentResponseBean contentResponseBean:contentResponseBeans){
|
||||
if(contentResponseBean.getId().equals(applicationFormFieldRequestBean.getFieldId())){
|
||||
for (SettingResponseBean settingResponseBean:contentResponseBean.getSettings()){
|
||||
if (settingResponseBean.getName().equals("label")){
|
||||
label= String.valueOf(settingResponseBean.getValue());
|
||||
}
|
||||
if(settingResponseBean.getName().equals("formula")){
|
||||
String value= (String) settingResponseBean.getValue();
|
||||
formulaValueOpt=value;
|
||||
formulaValue=Utils.extractValues(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, String> mappedFormulaValue = new HashMap<>();
|
||||
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
|
||||
if (formulaValueOpt != null && fieldValue==null) {
|
||||
fieldValue=0;
|
||||
}
|
||||
|
||||
for (ContentResponseBean contentResponseBean : contentResponseBeans) {
|
||||
String contentId = contentResponseBean.getId();
|
||||
|
||||
// Extract variable values once per contentResponseBean to avoid repeated stream operations
|
||||
Set<String> variableValues = contentResponseBean.getSettings().stream()
|
||||
.filter(setting -> "variable".equals(setting.getName()))
|
||||
.flatMap(setting -> {
|
||||
Object value = setting.getValue(); // Get the raw value
|
||||
if (value instanceof String) {
|
||||
return Stream.of((String) value); // Handle single String case
|
||||
} else if (value instanceof List) {
|
||||
return ((List<?>) value).stream()
|
||||
.filter(item -> item instanceof String) // Ensure it's a String
|
||||
.map(item -> (String) item); // Convert to String
|
||||
} else {
|
||||
return Stream.empty(); // Ignore unexpected types
|
||||
}
|
||||
})
|
||||
.collect(Collectors.toSet()); // Collect into a Set for uniqueness
|
||||
|
||||
for (String formula : formulaValue) {
|
||||
if (variableValues.contains(formula)) { // O(1) lookup instead of O(n)
|
||||
mappedFormulaValue.put(formula, contentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, String> updatedMappedFormulaValue = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, String> entry : mappedFormulaValue.entrySet()) {
|
||||
String variable = entry.getKey();
|
||||
String contentId = entry.getValue();
|
||||
|
||||
// Repository call using contentId
|
||||
Optional<ApplicationFormFieldEntity> optionalEntity = applicationFormFieldRepository.findByApplicationFormIdAndFieldId(applicationFormEntity.getId(),contentId);
|
||||
// If entity is found, extract fieldValue and fieldId
|
||||
optionalEntity.ifPresent(entity -> {
|
||||
String entityFieldValue = entity.getFieldValue(); // Assuming getter method exists
|
||||
String fieldId = entity.getFieldId(); // Assuming getter method exists
|
||||
String tableType = contentResponseBeans.stream()
|
||||
.filter(content -> content.getId().equals(fieldId)) // Match Content ID with fieldId
|
||||
.flatMap(content -> content.getSettings().stream()) // Extract settings
|
||||
.filter(setting -> "criteria_table_columns".equals(setting.getName())) // Match name
|
||||
.map(setting -> setting.getName()) // Return the name of the setting
|
||||
.findFirst() // Get the first match
|
||||
.orElse(null); // Default to null if no match
|
||||
|
||||
if(tableType!=null){
|
||||
JSONObject jsonObject = new JSONObject(entityFieldValue);
|
||||
|
||||
// Extract the value of total
|
||||
entityFieldValue = jsonObject.getString("total");
|
||||
|
||||
}
|
||||
|
||||
updatedMappedFormulaValue.put(fieldId, entityFieldValue);
|
||||
});
|
||||
}
|
||||
if(formulaValueOpt==null || formulaValueOpt.isEmpty()){
|
||||
return;
|
||||
}
|
||||
double finalValue = evaluateFormula(formulaValueOpt, mappedFormulaValue, updatedMappedFormulaValue);
|
||||
|
||||
fieldValidator.formulaValidation(fieldValue, finalValue, label);
|
||||
}
|
||||
|
||||
|
||||
public static double evaluateFormula(String formula, Map<String, String> mappedFormulaValue, Map<String, String> updatedMappedFormulaValue) {
|
||||
// Step 1: Extract all placeholders (variables) like {rest}, {another_var}, etc.
|
||||
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
|
||||
Matcher matcher = pattern.matcher(formula);
|
||||
List<String> variables = new ArrayList<>();
|
||||
|
||||
while (matcher.find()) {
|
||||
variables.add(matcher.group(1)); // Extract variable names inside the curly braces
|
||||
}
|
||||
|
||||
// Step 2: Replace placeholders with corresponding fieldValue
|
||||
Map<String, Double> variableValues = new HashMap<>();
|
||||
|
||||
for (String variable : variables) {
|
||||
String fieldId = mappedFormulaValue.get(variable);
|
||||
if (fieldId != null && updatedMappedFormulaValue.containsKey(fieldId)) {
|
||||
String fieldValueStr = updatedMappedFormulaValue.get(fieldId);
|
||||
try {
|
||||
double fieldValue = Double.parseDouble(fieldValueStr); // Assuming fieldValue is numeric
|
||||
variableValues.put(variable, fieldValue);
|
||||
} catch (NumberFormatException e) {
|
||||
// Handle invalid number format gracefully (e.g., log an error or default to 0)
|
||||
variableValues.put(variable, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Replace variables in the formula with their corresponding values
|
||||
String expression = formula;
|
||||
for (String variable : variables) {
|
||||
Double value = variableValues.get(variable);
|
||||
if (value != null) {
|
||||
// Replace {variable} with its corresponding value in the formula
|
||||
expression = expression.replace("{" + variable + "}", String.valueOf(value));
|
||||
}
|
||||
}
|
||||
|
||||
// Step 4: Evaluate the mathematical expression
|
||||
return Utils.evaluateExpression(expression);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user