updated code

This commit is contained in:
harish
2024-10-19 13:10:13 +05:30
174 changed files with 7882 additions and 1012 deletions

View File

@@ -6,18 +6,17 @@ import net.gepafin.tendermanagement.entities.*;
import net.gepafin.tendermanagement.model.request.*;
import net.gepafin.tendermanagement.model.response.ContentResponseBean;
import net.gepafin.tendermanagement.model.response.FormResponseBean;
import net.gepafin.tendermanagement.model.response.VatNumberResponseBean;
import net.gepafin.tendermanagement.repositories.*;
import net.gepafin.tendermanagement.service.CallService;
import net.gepafin.tendermanagement.util.DateTimeUtil;
import net.gepafin.tendermanagement.util.FieldValidator;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.util.Validator;
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.text.MessageFormat;
import java.time.LocalDateTime;
@@ -32,9 +31,6 @@ public class FormDao {
@Autowired
private FormRepository formRepository;
@Autowired
private CallService callService;
@Autowired
private ApplicationFormRepository applicationFormRepository;
@@ -52,15 +48,17 @@ public class FormDao {
@Autowired
private CallRepository callRepository;
@Autowired
private Validator validator;
public FormEntity saveFormEntity(FormEntity formEntity){
formEntity=formRepository.save(formEntity);
return formEntity;
}
public FormEntity convertFormRequestToFormEntity(Long callId,FormRequest formRequest){
public FormEntity convertFormRequestToFormEntity(CallEntity callEntity, FormRequest formRequest){
FormEntity formEntity=new FormEntity();
CallEntity callEntity=callService.getCallEntityById(callId);
formEntity.setCall(callEntity);
formEntity.setLabel(formRequest.getLabel());
formEntity.setContent(setContentResponseBean(formRequest.getContent()));
@@ -73,13 +71,13 @@ public class FormDao {
formResponseBean.setContent(Utils.convertJsonStringToList(formEntity.getContent(), ContentResponseBean.class));
formResponseBean.setLabel(formEntity.getLabel());
formResponseBean.setCallId(formEntity.getCall().getId());
formResponseBean.setCallStatus(formEntity.getCall().getStatus());
return formResponseBean;
}
public FormResponseBean createForm(Long callId,FormRequest formRequest){
public FormResponseBean createForm(CallEntity callEntity,FormRequest formRequest){
validateForm(formRequest);
CallEntity callEntity=callService.validateCall(callId);
List<FlowDataEntity> flowDataEntities=flowDataRepository.findByCallId(callId);
List<FlowEdgesEntity> flowEdgesEntities=flowEdgesRepository.findByCallId(callId);
List<FlowDataEntity> flowDataEntities=flowDataRepository.findByCallId(callEntity.getId());
List<FlowEdgesEntity> flowEdgesEntities=flowEdgesRepository.findByCallId(callEntity.getId());
if(Boolean.FALSE.equals(flowDataEntities.isEmpty() || flowDataEntities==null ) || Boolean.FALSE.equals(flowEdgesEntities.isEmpty() || flowEdgesEntities==null) ){
flowDataRepository.deleteAll(flowDataEntities);
flowEdgesRepository.deleteAll(flowEdgesEntities);
@@ -87,7 +85,7 @@ public class FormDao {
callEntity.setFinalForm(null);
callRepository.save(callEntity);
}
FormEntity formEntity=convertFormRequestToFormEntity(callId,formRequest);
FormEntity formEntity=convertFormRequestToFormEntity(callEntity, formRequest);
return convertFormEntityToFormResponseBean(formEntity);
}
public void validateForm(FormRequest formRequest){
@@ -95,11 +93,12 @@ public class FormDao {
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.REQUIRED_PARAMETER_NOT_FOUND_FOR_FORM));
}
}
public FormResponseBean updateForm(Long formId, FormRequest formRequest,Boolean forceDeleteFlow){
public FormResponseBean updateForm(UserEntity user, Long formId, FormRequest formRequest,Boolean forceDeleteFlow){
ContentRequestBean contentRequestBean2=null;
String choosenField=null;
FormEntity formEntity = validateForm(formId);
callDao.validateUpdate(formEntity.getCall());
validator.validateUserWithCall(user, formEntity.getCall().getId());
callDao.validateUpdate(formEntity.getCall());
List<ContentRequestBean> contentRequestBean = Utils.convertJsonStringToList(formEntity.getContent(), ContentRequestBean.class);
for (ContentRequestBean contentRequestBean1 : contentRequestBean) {
FlowDataEntity flowDataEntity = flowDataRepository.findByFormIdAndChoosenField(formEntity.getId(), contentRequestBean1.getId());
@@ -140,6 +139,13 @@ public class FormDao {
);
}
}
else {
Utils.setIfUpdated(formEntity::getLabel, formEntity::setLabel, formRequest.getLabel());
Utils.setIfUpdated(formEntity::getContent, formEntity::setContent, setContentResponseBean(formRequest.getContent()));
formEntity.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
formEntity = saveFormEntity(formEntity);
return convertFormEntityToFormResponseBean(formEntity);
}
}
}
}
@@ -162,12 +168,14 @@ public class FormDao {
return formEntity;
}
public FormResponseBean getFormEntityById(Long formId) {
public FormResponseBean getFormEntityById(UserEntity user, Long formId) {
FormEntity formEntity = validateForm(formId);
validator.validateUserWithCall(user, formEntity.getCall().getId());
return convertFormEntityToFormResponseBean(formEntity);
}
public void deleteFormById(Long formId){
public void deleteFormById(UserEntity user, Long formId){
FormEntity formEntity = validateForm(formId);
validator.validateUserWithCall(user, formEntity.getCall().getId());
List<FlowDataEntity> flowDataEntities=flowDataRepository.findByCallId(formEntity.getCall().getId());
List<FlowEdgesEntity> flowEdgesEntities=flowEdgesRepository.findByCallId(formEntity.getCall().getId());
flowDataRepository.deleteAll(flowDataEntities);
@@ -178,13 +186,12 @@ public class FormDao {
callRepository.save(callEntity);
formRepository.delete(formEntity);
}
public List<FormResponseBean> getFormsByCallId(Long callId){
CallEntity callEntity=callService.validateCall(callId);
public List<FormResponseBean> getFormsByCallId(CallEntity callEntity){
if(callEntity== null){
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND));
}
List<FormEntity> formEntities=formRepository.findByCallId(callId);
List<FormEntity> formEntities=formRepository.findByCallId(callEntity.getId());
List<FormResponseBean> formResponseBeanList = formEntities.stream()
.map(req -> convertFormEntityToFormResponseBean(req))
.collect(Collectors.toList());
@@ -197,29 +204,33 @@ public class FormDao {
public void validateFormField(List<ApplicationFormFieldRequestBean> applicationFormFieldRequestList, ApplicationEntity applicationEntity, FormEntity formEntity) {
Map<String, Object> formFieldMap = new LinkedHashMap<String, Object>();
for(ApplicationFormFieldRequestBean applicationFormFieldRequestBean:applicationFormFieldRequestList) {
formFieldMap.put(applicationFormFieldRequestBean.getFieldId(),applicationFormFieldRequestBean.getFieldValue());
}
if(applicationFormFieldRequestBean.getFieldValue()==null )
continue;
if (applicationFormFieldRequestBean.getFieldValue() != null ) {
Object fieldValue = applicationFormFieldRequestBean.getFieldValue();
checkObjectData(applicationFormFieldRequestBean.getFieldId(), fieldValue, formFieldMap);
}}
FormResponseBean formResponseBean = convertFormEntityToFormResponseBean(formEntity);
FormResponseBean formResponseBean = convertFormEntityToFormResponseBean(formEntity);
ApplicationFormEntity applicationFormEntity=applicationFormRepository.findByApplicationIdAndFormId(applicationEntity.getId(),formEntity.getId());
Boolean isApplicationFormExist= getApplicationFormExist(applicationFormEntity);
FieldValidator validator = FieldValidator.create();
formResponseBean.getContent().forEach(contentResponseBean -> {
String fieldId = contentResponseBean.getId();
String value = (String) formFieldMap.get(fieldId);
String fieldLabel=contentResponseBean.getLabel();
if(value == null && isApplicationFormExist) {
return;
}
FieldValidatorBean fieldValidatorBean = Utils.convertSourceObjectToDestinationObject(contentResponseBean.getValidators(), FieldValidatorBean.class);
validator
.isRequired(value,fieldValidatorBean.getIsRequired(),fieldId)
.minLength(value, fieldValidatorBean.getMinLength(), fieldId) // Only applies if minLength is not null
.maxLength(value, fieldValidatorBean.getMaxLength(), fieldId) // Only applies if maxLength is not null
.matchesPattern(value, fieldValidatorBean.getPattern(), fieldId) // Only applies if pattern is present
.validateCustom(value, fieldValidatorBean.getCustom(), fieldId); // Add the custom validation here
.minLength(value, fieldValidatorBean.getMinLength(), fieldLabel) // Only applies if minLength is not null
.maxLength(value, fieldValidatorBean.getMaxLength(), fieldLabel) // Only applies if maxLength is not null
.matchesPattern(value, fieldValidatorBean.getPattern(), fieldLabel) // Only applies if pattern is present
.validateCustom(value, fieldValidatorBean.getCustom(), fieldLabel); // Add the custom validation here
if (fieldValidatorBean.getCustom() != null && fieldValidatorBean.getCustom().equals(GepafinConstant.IS_PIVA)) {
String error = validateVatNumber(value, fieldValidatorBean.getCustom(), fieldId);
String error = validateVatNumber(value, fieldValidatorBean.getCustom(), fieldLabel);
if(error != null) {
validator.addError(error);
}
@@ -228,6 +239,28 @@ public class FormDao {
validator.validate();
}
private void checkObjectData(String fieldId, Object fieldValue, Map<String, Object> formFieldMap) {
if (fieldValue instanceof List<?>) {
List<?> list = (List<?>) fieldValue;
// Only map if the list is not empty and contains Strings
if (!list.isEmpty() && list.get(0) instanceof String) {
for (Object value : list) {
setFormFieldMap(fieldId, formFieldMap, value);
}
}
}
else setFormFieldMap(fieldId, formFieldMap, fieldValue);
}
private void setFormFieldMap(String fieldId, Map<String, Object> formFieldMap, Object value) {
if (value instanceof String) {
if(value !=null && Boolean.FALSE.equals(StringUtils.isEmpty((String)value))) {
formFieldMap.put(fieldId, value);
}
}
}
private Boolean getApplicationFormExist(ApplicationFormEntity applicationFormEntity) {
if(applicationFormEntity !=null) {
return true;
@@ -235,19 +268,43 @@ public class FormDao {
return false;
}
public Boolean validateCompletedSteps(List<ApplicationFormFieldEntity> applicationFormFieldEntityList, ApplicationEntity applicationEntity, FormEntity formEntity) {
Map<String, Object> formFieldMap = new LinkedHashMap<String, Object>();
for(ApplicationFormFieldEntity applicationFormFieldEntity:applicationFormFieldEntityList) {
formFieldMap.put(applicationFormFieldEntity.getFieldId(),applicationFormFieldEntity.getFieldValue());
}
FormResponseBean formResponseBean = convertFormEntityToFormResponseBean(formEntity);
FieldValidator validator = FieldValidator.create();
formResponseBean.getContent().forEach(contentResponseBean -> {
String fieldId = contentResponseBean.getId();
String value = (String) formFieldMap.get(fieldId);
FieldValidatorBean fieldValidatorBean = Utils.convertSourceObjectToDestinationObject(contentResponseBean.getValidators(), FieldValidatorBean.class);
validator
.isRequired(value,fieldValidatorBean.getIsRequired(),fieldId);
});
if (validator.hasErrors()) {
return false; // Validation failed, return false
}
return true;
}
public String validateVatNumber(String value,String customRule,String fieldId){
String error=null;
if (value.matches("^\\d{1,11}$")) {
Map<String, Object> customData=null;
if (value!=null && value.matches("^\\d{1,11}$")) {
// Map<String, Object> customData=null;
try {
Map<String, Object> vatCheckResponse = vatCheckDao.checkVatNumberApi(value);
if (Boolean.FALSE.equals(CollectionUtils.isEmpty(vatCheckResponse))) {
customData = vatCheckResponse;
}
// Map<String, Object> vatCheckResponse = vatCheckDao.checkVatNumberApi(value);
vatCheckDao.checkVatNumberApi(value);
// if (Boolean.FALSE.equals(CollectionUtils.isEmpty(vatCheckResponse))) {
// customData = vatCheckResponse;
// }
} catch (Exception e) {
error=(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_VALID_PIVA), fieldId));
}
}
return error;
}
}