Updated request body for form data
This commit is contained in:
@@ -323,9 +323,13 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
Utils.setIfUpdated(applicationFormFieldEntity::getFieldId, applicationFormFieldEntity::setFieldId, applicationFormFieldRequestBean.getFieldId());
|
||||
if(applicationFormFieldRequestBean.getFieldValue() ==null || Boolean.FALSE.equals(applicationFormFieldRequestBean.getFieldValue().isEmpty())) {
|
||||
applicationFormFieldEntity.setFieldValue(applicationFormFieldRequestBean.getFieldValue());
|
||||
|
||||
if(applicationFormFieldRequestBean.getFieldValue() !=null ) {
|
||||
applicationFormFieldEntity.setFieldValue(Utils.convertObjectToJsonString(applicationFormFieldRequestBean.getFieldValue()));
|
||||
}
|
||||
if(applicationFormFieldRequestBean.getFieldValue() ==null ) {
|
||||
applicationFormFieldEntity.setFieldValue(null);
|
||||
}
|
||||
return applicationFormFieldRepository.save(applicationFormFieldEntity);
|
||||
}
|
||||
|
||||
@@ -333,10 +337,15 @@ public class ApplicationDao {
|
||||
List<Long> documentIds=null;
|
||||
List<ContentResponseBean> contentResponseBeans=Utils.convertJsonStringToList(formEntity.getContent(),ContentResponseBean.class);
|
||||
for (ContentResponseBean contentResponseBean:contentResponseBeans){
|
||||
if(Boolean.TRUE.equals(contentResponseBean.getName().equals("fileupload"))){
|
||||
if(contentResponseBean.getId().equals(applicationFormFieldRequestBean.getFieldId())) {
|
||||
String documentId = applicationFormFieldRequestBean.getFieldValue();
|
||||
documentIds = validateDocumentIds(documentId);
|
||||
if(Boolean.TRUE.equals(contentResponseBean.getName().equals("fileupload"))) {
|
||||
if (contentResponseBean.getId().equals(applicationFormFieldRequestBean.getFieldId())) {
|
||||
Object fieldValueObject = applicationFormFieldRequestBean.getFieldValue();
|
||||
if (fieldValueObject instanceof String) {
|
||||
// Safely cast the object to a string
|
||||
String documentId = (String) fieldValueObject;
|
||||
// Now you can use documentId as needed
|
||||
documentIds = validateDocumentIds(documentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,7 +386,9 @@ public class ApplicationDao {
|
||||
ApplicationFormFieldResponseBean applicationFormFieldResponseBean = new ApplicationFormFieldResponseBean();
|
||||
applicationFormFieldResponseBean.setApplicationFormId(applicationFormId);
|
||||
applicationFormFieldResponseBean.setFieldId(applicationFormFieldEntity.getFieldId());
|
||||
applicationFormFieldResponseBean.setFieldValue(applicationFormFieldEntity.getFieldValue());
|
||||
if(applicationFormFieldEntity.getFieldValue() != null) {
|
||||
applicationFormFieldResponseBean.setFieldValue(Utils.getFieldValueAsObject(applicationFormFieldEntity.getFieldValue()));
|
||||
}
|
||||
applicationFormFieldResponseBean.setId(applicationFormFieldEntity.getId());
|
||||
applicationFormFieldResponseBean.setCreatedDate(applicationFormFieldEntity.getCreatedDate());
|
||||
applicationFormFieldResponseBean.setUpdatedDate(applicationFormFieldEntity.getUpdatedDate());
|
||||
@@ -447,7 +458,7 @@ public class ApplicationDao {
|
||||
}
|
||||
|
||||
private ApplicationGetResponseBean createApplicationGetResponseBean(ApplicationEntity applicationEntity, List<FormEntity> formEntities, List<FormApplicationResponse> formApplicationResponses) {
|
||||
ApplicationGetResponseBean applicationGetResponseBean =createApplicationGetResponseBean(applicationEntity);
|
||||
ApplicationGetResponseBean applicationGetResponseBean = createApplicationGetResponseBean(applicationEntity);
|
||||
applicationGetResponseBean.setForm(formApplicationResponses);
|
||||
return applicationGetResponseBean;
|
||||
}
|
||||
|
||||
@@ -205,12 +205,14 @@ 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) {
|
||||
if(applicationFormFieldRequestBean.getFieldValue()==null || applicationFormFieldRequestBean.getFieldValue().isEmpty())
|
||||
if(applicationFormFieldRequestBean.getFieldValue()==null )
|
||||
continue;
|
||||
formFieldMap.put(applicationFormFieldRequestBean.getFieldId(),applicationFormFieldRequestBean.getFieldValue());
|
||||
}
|
||||
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();
|
||||
@@ -238,6 +240,27 @@ 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, fieldValue, formFieldMap, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else setFormFieldMap(fieldId, fieldValue, formFieldMap, fieldValue);
|
||||
}
|
||||
|
||||
private void setFormFieldMap(String fieldId, Object fieldValue, Map<String, Object> formFieldMap, Object value) {
|
||||
if (value instanceof String) {
|
||||
if(fieldValue !=null && ((String) fieldValue).isEmpty())
|
||||
formFieldMap.put(fieldId, value);
|
||||
}
|
||||
}
|
||||
|
||||
private Boolean getApplicationFormExist(ApplicationFormEntity applicationFormEntity) {
|
||||
if(applicationFormEntity !=null) {
|
||||
return true;
|
||||
@@ -249,7 +272,7 @@ public class FormDao {
|
||||
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();
|
||||
|
||||
@@ -9,6 +9,6 @@ public class ApplicationFormFieldRequestBean {
|
||||
|
||||
private String fieldId;
|
||||
|
||||
private String fieldValue;
|
||||
private Object fieldValue;
|
||||
|
||||
}
|
||||
|
||||
@@ -227,4 +227,47 @@ public class Utils {
|
||||
Pattern pattern = Pattern.compile(EMAIL_REGEX);
|
||||
return pattern.matcher(email).matches();
|
||||
}
|
||||
public static String convertObjectToJsonString(Object object) {
|
||||
try {
|
||||
// Check if the object is a string
|
||||
if (object instanceof String) {
|
||||
String str = (String) object;
|
||||
// Return null if the string is null or empty
|
||||
if (str != null && !str.trim().isEmpty()) {
|
||||
return str; // Return the non-empty string
|
||||
} else {
|
||||
return null; // Return null for null or empty string
|
||||
}
|
||||
} else if (object != null) {
|
||||
// Convert non-string objects (arrays, objects) to JSON strings
|
||||
return mapper.writeValueAsString(object);
|
||||
}
|
||||
return null; // Return null if the object is null
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error while converting object to string: {}", e.getMessage(), e);
|
||||
return null; // Return null in case of exception
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Object getFieldValueAsObject(String fieldValue) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
// Check if the string is a valid JSON object, array, or simple string
|
||||
if (fieldValue.startsWith("{")) {
|
||||
// Convert to a Map (representing an object)
|
||||
return mapper.readValue(fieldValue, Map.class);
|
||||
} else if (fieldValue.startsWith("[")) {
|
||||
// Convert to a List (representing an array)
|
||||
return mapper.readValue(fieldValue, List.class);
|
||||
} else {
|
||||
// Return the raw string (it's a simple value)
|
||||
return fieldValue;
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error while converting string to object: {}", e.getMessage(), e);
|
||||
return fieldValue; // If there's an error, return the raw string
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -879,5 +879,10 @@
|
||||
<sqlFile dbms="postgresql"
|
||||
path="classpath:db/dump/updated_form_field_data_03-10-2024_1.sql" />
|
||||
</changeSet>
|
||||
|
||||
<changeSet id="07-10-2024_2" author="Nisha Kashyap">
|
||||
<modifyDataType
|
||||
tableName="application_form_field"
|
||||
columnName="field_value"
|
||||
newDataType="TEXT"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
|
||||
Reference in New Issue
Block a user