Fixed field ids coming in the response as value to give document response
This commit is contained in:
@@ -24,6 +24,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
|
||||
import static org.apache.commons.lang3.StringUtils.isNumeric;
|
||||
|
||||
@Component
|
||||
public class ApplicationEvaluationDao {
|
||||
@@ -163,10 +164,8 @@ public class ApplicationEvaluationDao {
|
||||
}
|
||||
|
||||
private List<CriteriaMappedField> getMappedFieldsForCriteria(Long evaluationCriteriaId, Long applicationId) {
|
||||
|
||||
List<CriteriaFormFieldEntity> criteriaFormFields = criteriaFormFieldRepository.findByEvaluationCriteriaIdAndIsDeletedFalse(evaluationCriteriaId);
|
||||
List<CriteriaMappedField> mappedFields = new ArrayList<>();
|
||||
|
||||
Set<String> uniqueFieldIds = new HashSet<>();
|
||||
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
@@ -178,28 +177,23 @@ public class ApplicationEvaluationDao {
|
||||
CriteriaMappedField mappedField = new CriteriaMappedField();
|
||||
mappedField.setId(formFieldId);
|
||||
|
||||
FormEntity formEntity = formRepository.findById(formField.getFormId()).orElse(null);
|
||||
FormEntity formEntity = getFormEntity(formField.getFormId());
|
||||
if (formEntity != null) {
|
||||
List<ContentResponseBean> contentBeans = Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class);
|
||||
contentBeans.stream().filter(contentBean -> contentBean.getId().equals(formFieldId)).findFirst().ifPresent(contentBean -> {
|
||||
String label = contentBean.getLabel();
|
||||
if (contentBean.getSettings() != null) {
|
||||
for (SettingResponseBean setting : contentBean.getSettings()) {
|
||||
if ("label".equals(setting.getName())) {
|
||||
label = setting.getValue() != null ? setting.getValue().toString() : label;
|
||||
break;
|
||||
contentBeans.stream()
|
||||
.filter(contentBean -> contentBean.getId().equals(formFieldId))
|
||||
.findFirst()
|
||||
.ifPresent(contentBean -> {
|
||||
mappedField.setFieldLabel(getLabelForField(contentBean));
|
||||
|
||||
if ("fileupload".equals(contentBean.getName())) {
|
||||
mapFileFieldDetails(mappedField, formFieldId, applicationForm.getId(), applicationId);
|
||||
} else {
|
||||
mapNonFileFieldDetails(mappedField, formFieldId, applicationForm.getId(), applicationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
mappedField.setFieldLabel(label);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Optional<ApplicationFormFieldEntity> formFieldEntityOptional = applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||
formFieldId, applicationForm.getId(), applicationId);
|
||||
|
||||
formFieldEntityOptional.ifPresent(field -> mappedField.setFieldValue(field.getFieldValue()));
|
||||
|
||||
mappedFields.add(mappedField);
|
||||
uniqueFieldIds.add(formFieldId);
|
||||
}
|
||||
@@ -208,6 +202,58 @@ public class ApplicationEvaluationDao {
|
||||
return mappedFields;
|
||||
}
|
||||
|
||||
private FormEntity getFormEntity(Long formId) {
|
||||
return formRepository.findById(formId).orElse(null);
|
||||
}
|
||||
|
||||
private String getLabelForField(ContentResponseBean contentBean) {
|
||||
String label = contentBean.getLabel();
|
||||
if (contentBean.getSettings() != null) {
|
||||
for (SettingResponseBean setting : contentBean.getSettings()) {
|
||||
if ("label".equals(setting.getName())) {
|
||||
label = setting.getValue() != null ? setting.getValue().toString() : label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
private void mapFileFieldDetails(CriteriaMappedField mappedField, String formFieldId, Long applicationFormId, Long applicationId) {
|
||||
Optional<ApplicationFormFieldEntity> formFieldEntityOptional = applicationFormFieldRepository
|
||||
.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(formFieldId, applicationFormId, applicationId);
|
||||
|
||||
if (formFieldEntityOptional.isPresent()) {
|
||||
String[] documentIds = formFieldEntityOptional.get().getFieldValue().split(",");
|
||||
List<DocumentResponseBean> documentResponseBeans = new ArrayList<>();
|
||||
|
||||
for (String docId : documentIds) {
|
||||
Long documentId = Long.valueOf(docId.trim());
|
||||
documentRepository.findByIdAndNotDeleted(documentId).ifPresent(documentEntity -> {
|
||||
DocumentResponseBean responseBean = new DocumentResponseBean();
|
||||
responseBean.setId(documentEntity.getId());
|
||||
responseBean.setName(documentEntity.getFileName());
|
||||
responseBean.setType(DocumentTypeEnum.valueOf(documentEntity.getType()));
|
||||
responseBean.setSource(DocumentSourceTypeEnum.valueOf(documentEntity.getSource()));
|
||||
responseBean.setSourceId(documentEntity.getSourceId());
|
||||
responseBean.setFilePath(documentEntity.getFilePath());
|
||||
responseBean.setCreatedDate(documentEntity.getCreatedDate());
|
||||
responseBean.setUpdatedDate(documentEntity.getUpdatedDate());
|
||||
documentResponseBeans.add(responseBean);
|
||||
});
|
||||
}
|
||||
mappedField.setFieldValue(documentResponseBeans);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void mapNonFileFieldDetails(CriteriaMappedField mappedField, String formFieldId, Long applicationFormId, Long applicationId) {
|
||||
applicationFormFieldRepository
|
||||
.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(formFieldId, applicationFormId, applicationId)
|
||||
.ifPresent(field -> mappedField.setFieldValue(field.getFieldValue()));
|
||||
}
|
||||
|
||||
|
||||
private void setChecklistResponses(ApplicationEvaluationEntity entity, ApplicationEvaluationResponse response, List<CallTargetAudienceChecklistEntity> checklistEntities) {
|
||||
|
||||
List<ChecklistResponse> checklistResponsesFromEntity = entity.getChecklist() != null ?
|
||||
@@ -562,57 +608,23 @@ public class ApplicationEvaluationDao {
|
||||
return response;
|
||||
}
|
||||
|
||||
private void setCriteriaResponses(ApplicationEvaluationEntity entity, Long applicationId, ApplicationEvaluationResponse response,
|
||||
List<EvaluationCriteriaEntity> evaluationCriterias) {
|
||||
private void setCriteriaResponses(ApplicationEvaluationEntity entity, Long applicationId,
|
||||
ApplicationEvaluationResponse response,
|
||||
List<EvaluationCriteriaEntity> evaluationCriterias) {
|
||||
|
||||
List<CriteriaResponse> criteriaResponses = entity.getCriteria() != null ? Utils.convertJsonToList(entity.getCriteria(), new TypeReference<List<CriteriaResponse>>() {
|
||||
}) : getCriteriaResponse(applicationId);
|
||||
List<CriteriaResponse> criteriaResponses = getInitialCriteriaResponses(entity, applicationId);
|
||||
|
||||
criteriaResponses.forEach(criteriaResponse -> {
|
||||
EvaluationCriteriaEntity matchingEvaluationCriteria = evaluationCriterias.stream()
|
||||
.filter(evaluationCriteria -> evaluationCriteria.getId().equals(criteriaResponse.getId())).findFirst().orElse(null);
|
||||
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
Map<String, CriteriaMappedField> mappedFieldMap = new HashMap<>();
|
||||
EvaluationCriteriaEntity matchingEvaluationCriteria =
|
||||
findMatchingEvaluationCriteria(criteriaResponse, evaluationCriterias);
|
||||
|
||||
if (matchingEvaluationCriteria != null) {
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
Map<String, CriteriaMappedField> mappedFieldMap = populateMappedFields(
|
||||
matchingEvaluationCriteria, applicationForms, applicationId);
|
||||
|
||||
criteriaResponse.setLabel(matchingEvaluationCriteria.getLookupData().getValue());
|
||||
criteriaResponse.setMaxScore(matchingEvaluationCriteria.getScore());
|
||||
|
||||
List<CriteriaFormFieldEntity> criteriaFormFields = criteriaFormFieldRepository.findByEvaluationCriteriaIdAndIsDeletedFalse(matchingEvaluationCriteria.getId());
|
||||
|
||||
for (ApplicationFormEntity applicationForm : applicationForms) {
|
||||
for (CriteriaFormFieldEntity criteriaFormField : criteriaFormFields) {
|
||||
String formFieldId = criteriaFormField.getFormFieldId();
|
||||
if (!mappedFieldMap.containsKey(formFieldId)) {
|
||||
CriteriaMappedField mappedField = new CriteriaMappedField();
|
||||
mappedField.setId(formFieldId);
|
||||
FormEntity formEntity = formRepository.findById(criteriaFormField.getFormId()).orElse(null);
|
||||
|
||||
if (formEntity != null) {
|
||||
List<ContentResponseBean> contentResponseBeans = Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class);
|
||||
contentResponseBeans.stream().filter(bean -> bean.getId().equals(formFieldId)).findFirst().ifPresent(contentResponseBean -> {
|
||||
String label = contentResponseBean.getLabel();
|
||||
if (contentResponseBean.getSettings() != null) {
|
||||
for (SettingResponseBean setting : contentResponseBean.getSettings()) {
|
||||
if ("label".equals(setting.getName())) {
|
||||
label = setting.getValue() != null ? setting.getValue().toString() : label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
mappedField.setFieldLabel(label);
|
||||
});
|
||||
}
|
||||
Optional<ApplicationFormFieldEntity> formFieldEntityOptional =
|
||||
applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||
formFieldId, applicationForm.getId(), applicationId);
|
||||
|
||||
formFieldEntityOptional.ifPresent(formField -> mappedField.setFieldValue(formField.getFieldValue()));
|
||||
mappedFieldMap.put(formFieldId, mappedField);
|
||||
}
|
||||
}
|
||||
}
|
||||
criteriaResponse.setCriteriaMappedFields(new ArrayList<>(mappedFieldMap.values()));
|
||||
}
|
||||
});
|
||||
@@ -620,6 +632,108 @@ public class ApplicationEvaluationDao {
|
||||
response.setCriteria(criteriaResponses);
|
||||
}
|
||||
|
||||
private List<CriteriaResponse> getInitialCriteriaResponses(ApplicationEvaluationEntity entity, Long applicationId) {
|
||||
return entity.getCriteria() != null
|
||||
? Utils.convertJsonToList(entity.getCriteria(), new TypeReference<List<CriteriaResponse>>() {})
|
||||
: getCriteriaResponse(applicationId);
|
||||
}
|
||||
|
||||
private EvaluationCriteriaEntity findMatchingEvaluationCriteria(CriteriaResponse criteriaResponse,
|
||||
List<EvaluationCriteriaEntity> evaluationCriterias) {
|
||||
return evaluationCriterias.stream()
|
||||
.filter(evaluationCriteria -> evaluationCriteria.getId().equals(criteriaResponse.getId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private Map<String, CriteriaMappedField> populateMappedFields(EvaluationCriteriaEntity matchingEvaluationCriteria,
|
||||
List<ApplicationFormEntity> applicationForms,
|
||||
Long applicationId) {
|
||||
Map<String, CriteriaMappedField> mappedFieldMap = new HashMap<>();
|
||||
List<CriteriaFormFieldEntity> criteriaFormFields = criteriaFormFieldRepository
|
||||
.findByEvaluationCriteriaIdAndIsDeletedFalse(matchingEvaluationCriteria.getId());
|
||||
|
||||
for (ApplicationFormEntity applicationForm : applicationForms) {
|
||||
for (CriteriaFormFieldEntity criteriaFormField : criteriaFormFields) {
|
||||
String formFieldId = criteriaFormField.getFormFieldId();
|
||||
if (!mappedFieldMap.containsKey(formFieldId)) {
|
||||
CriteriaMappedField mappedField = new CriteriaMappedField();
|
||||
populateMappedField(mappedField, formFieldId, criteriaFormField, applicationForm, applicationId);
|
||||
mappedFieldMap.put(formFieldId, mappedField);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappedFieldMap;
|
||||
}
|
||||
|
||||
private void populateMappedField(CriteriaMappedField mappedField, String formFieldId,
|
||||
CriteriaFormFieldEntity criteriaFormField,
|
||||
ApplicationFormEntity applicationForm, Long applicationId) {
|
||||
mappedField.setId(formFieldId);
|
||||
formRepository.findById(criteriaFormField.getFormId()).ifPresent(formEntity -> {
|
||||
List<ContentResponseBean> contentResponseBeans = Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class);
|
||||
contentResponseBeans.stream().filter(bean -> bean.getId().equals(formFieldId)).findFirst().ifPresent(contentResponseBean -> {
|
||||
String label = getLabel(contentResponseBean);
|
||||
mappedField.setFieldLabel(label);
|
||||
if ("fileupload".equals(contentResponseBean.getName())) {
|
||||
populateFileDetailsAsFieldValue(mappedField, formFieldId, applicationForm, applicationId);
|
||||
} else {
|
||||
applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||
formFieldId, applicationForm.getId(), applicationId)
|
||||
.ifPresent(formField -> mappedField.setFieldValue(formField.getFieldValue()));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void populateFileDetailsAsFieldValue(CriteriaMappedField mappedField, String formFieldId,
|
||||
ApplicationFormEntity applicationForm, Long applicationId) {
|
||||
applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||
formFieldId, applicationForm.getId(), applicationId)
|
||||
.ifPresent(formField -> {
|
||||
if (formField.getFieldValue() != null) {
|
||||
List<DocumentResponseBean> documentResponseBeans = new ArrayList<>();
|
||||
for (String docId : formField.getFieldValue().split(",")) {
|
||||
Long documentId = Long.valueOf(docId.trim());
|
||||
documentRepository.findByIdAndNotDeleted(documentId).ifPresent(documentEntity -> {
|
||||
DocumentResponseBean responseBean = createDocumentResponseBean(documentEntity);
|
||||
documentResponseBeans.add(responseBean);
|
||||
});
|
||||
}
|
||||
// Set the list of DocumentResponseBean directly
|
||||
mappedField.setFieldValue(documentResponseBeans);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String getLabel(ContentResponseBean contentResponseBean) {
|
||||
String label = contentResponseBean.getLabel();
|
||||
if (contentResponseBean.getSettings() != null) {
|
||||
for (SettingResponseBean setting : contentResponseBean.getSettings()) {
|
||||
if ("label".equals(setting.getName())) {
|
||||
label = setting.getValue() != null ? setting.getValue().toString() : label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
private DocumentResponseBean createDocumentResponseBean(DocumentEntity documentEntity) {
|
||||
DocumentResponseBean responseBean = new DocumentResponseBean();
|
||||
responseBean.setId(documentEntity.getId());
|
||||
responseBean.setName(documentEntity.getFileName());
|
||||
responseBean.setType(DocumentTypeEnum.valueOf(documentEntity.getType()));
|
||||
responseBean.setSource(DocumentSourceTypeEnum.valueOf(documentEntity.getSource()));
|
||||
responseBean.setSourceId(documentEntity.getSourceId());
|
||||
responseBean.setFilePath(documentEntity.getFilePath());
|
||||
responseBean.setCreatedDate(documentEntity.getCreatedDate());
|
||||
responseBean.setUpdatedDate(documentEntity.getUpdatedDate());
|
||||
return responseBean;
|
||||
}
|
||||
|
||||
private void setChecklistResponses(ApplicationEvaluationEntity entity, Long applicationId, ApplicationEvaluationResponse response,
|
||||
List<CallTargetAudienceChecklistEntity> checklistEntities) {
|
||||
|
||||
@@ -764,13 +878,47 @@ public class ApplicationEvaluationDao {
|
||||
mappedField.setFieldLabel(label);
|
||||
});
|
||||
}
|
||||
List<DocumentResponseBean> documentResponseBeans = new ArrayList<>();
|
||||
applicationFormRepository.findByApplicationId(applicationId).stream()
|
||||
.flatMap(applicationForm -> applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(
|
||||
criteriaFormField.getFormFieldId(), applicationForm.getId(), applicationId).stream())
|
||||
.findFirst().ifPresent(formField -> {
|
||||
String fieldValue = formField.getFieldValue();
|
||||
if (fieldValue != null) {
|
||||
String[] fieldValues = fieldValue.split(",");
|
||||
for (String value : fieldValues) {
|
||||
String trimmedValue = value.trim();
|
||||
if (isNumeric(trimmedValue)) {
|
||||
Long documentId = Long.valueOf(trimmedValue);
|
||||
documentRepository.findByIdAndNotDeleted(documentId).ifPresent(documentEntity -> {
|
||||
DocumentResponseBean responseBean = new DocumentResponseBean();
|
||||
responseBean.setId(documentEntity.getId());
|
||||
responseBean.setName(documentEntity.getFileName());
|
||||
responseBean.setType(DocumentTypeEnum.valueOf(documentEntity.getType()));
|
||||
responseBean.setSource(DocumentSourceTypeEnum.valueOf(documentEntity.getSource()));
|
||||
responseBean.setSourceId(documentEntity.getSourceId());
|
||||
responseBean.setFilePath(documentEntity.getFilePath());
|
||||
responseBean.setCreatedDate(documentEntity.getCreatedDate());
|
||||
responseBean.setUpdatedDate(documentEntity.getUpdatedDate());
|
||||
documentResponseBeans.add(responseBean);
|
||||
});
|
||||
} else {
|
||||
mappedField.setFieldValue(trimmedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
applicationFormRepository.findByApplicationId(applicationId).stream().flatMap(
|
||||
applicationForm -> applicationFormFieldRepository.findByFieldIdAndApplicationFormIdAndApplicationFormApplicationId(criteriaFormField.getFormFieldId(),
|
||||
applicationForm.getId(), applicationId).stream()).findFirst().ifPresent(formField -> mappedField.setFieldValue(formField.getFieldValue()));
|
||||
if (!documentResponseBeans.isEmpty()) {
|
||||
mappedField.setFieldValue(documentResponseBeans);
|
||||
} else {
|
||||
mappedField.setFieldValue(fieldValue != null ? fieldValue.trim() : null);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
mappedFields.add(mappedField);
|
||||
processedFormFieldIds.add(criteriaFormField.getFormFieldId());
|
||||
|
||||
}
|
||||
|
||||
response.setCriteriaMappedFields(mappedFields);
|
||||
@@ -852,8 +1000,6 @@ public class ApplicationEvaluationDao {
|
||||
}
|
||||
|
||||
fieldResponse.setFileDetail(documentResponseBeans);
|
||||
|
||||
// Now add fieldResponse to the list
|
||||
fieldResponses.add(fieldResponse);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user