|
|
@ -61,7 +61,6 @@ public class ShelterResucueService { |
|
|
this.jdbcShelterRescueTemplate = new JdbcTemplate(ShelterRescueataSource); |
|
|
this.jdbcShelterRescueTemplate = new JdbcTemplate(ShelterRescueataSource); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> getSurveyQuestions(Integer cid) { |
|
|
public List<Map<String, Object>> getSurveyQuestions(Integer cid) { |
|
|
|
|
|
|
|
|
String childSql = "SELECT DISTINCT child_qid FROM shelter_resuce_question_mapping " + |
|
|
String childSql = "SELECT DISTINCT child_qid FROM shelter_resuce_question_mapping " + |
|
|
@ -362,7 +361,7 @@ public class ShelterResucueService { |
|
|
System.out.println(" resuceDesignation: " + resuceDesignation); |
|
|
System.out.println(" resuceDesignation: " + resuceDesignation); |
|
|
System.out.println(" hospitalName: " + hospitalName); |
|
|
System.out.println(" hospitalName: " + hospitalName); |
|
|
|
|
|
|
|
|
//String address = params.get("address"); ) |
|
|
|
|
|
|
|
|
// String address = params.get("address"); ) |
|
|
// System.out.println(" surveyId: " + surveyId); |
|
|
// System.out.println(" surveyId: " + surveyId); |
|
|
// System.out.println(" cid: " + cid); |
|
|
// System.out.println(" cid: " + cid); |
|
|
// System.out.println(" cby: " + cby); |
|
|
// System.out.println(" cby: " + cby); |
|
|
@ -428,14 +427,15 @@ public class ShelterResucueService { |
|
|
// first (like in child_survey) |
|
|
// first (like in child_survey) |
|
|
if (cid != null && cid.equals("1") && (zone != null || resolvedLat != null || resolvedLng != null)) { |
|
|
if (cid != null && cid.equals("1") && (zone != null || resolvedLat != null || resolvedLng != null)) { |
|
|
try { |
|
|
try { |
|
|
// String logindetailsSql = "INSERT INTO surveyor_location_resuce_details " + |
|
|
|
|
|
// "(loginId, zone, ward, latitude, longitude, shelter_id, is_active, is_delete, cdate) " + |
|
|
|
|
|
// "VALUES (?, ?, ?, ?, ?, ?, 1, 0, NOW())"; |
|
|
|
|
|
|
|
|
// String logindetailsSql = "INSERT INTO surveyor_location_resuce_details " + |
|
|
|
|
|
// "(loginId, zone, ward, latitude, longitude, shelter_id, is_active, is_delete, |
|
|
|
|
|
// cdate) " + |
|
|
|
|
|
// "VALUES (?, ?, ?, ?, ?, ?, 1, 0, NOW())"; |
|
|
|
|
|
|
|
|
String logindetailsSql = "INSERT INTO surveyor_location_resuce_details " + |
|
|
|
|
|
"(loginId, zone, ward, latitude, longitude, shelter_id, " + |
|
|
|
|
|
"resuce_designation, hospital_name, is_active, is_delete, cdate) " + |
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, 0, NOW())"; |
|
|
|
|
|
|
|
|
String logindetailsSql = "INSERT INTO surveyor_location_resuce_details " + |
|
|
|
|
|
"(loginId, zone, ward, latitude, longitude, shelter_id, " + |
|
|
|
|
|
"resuce_designation, hospital_name, is_active, is_delete, cdate) " + |
|
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, 0, NOW())"; |
|
|
|
|
|
|
|
|
org.springframework.jdbc.support.KeyHolder keyHolder = new org.springframework.jdbc.support.GeneratedKeyHolder(); |
|
|
org.springframework.jdbc.support.KeyHolder keyHolder = new org.springframework.jdbc.support.GeneratedKeyHolder(); |
|
|
|
|
|
|
|
|
@ -480,86 +480,87 @@ public class ShelterResucueService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//to save family members details |
|
|
|
|
|
|
|
|
// to save family members details |
|
|
// Detect and save family members details if passed as JSON string |
|
|
// Detect and save family members details if passed as JSON string |
|
|
String familyMembersJson = params.get("family_members"); |
|
|
|
|
|
if (familyMembersJson == null) { |
|
|
|
|
|
familyMembersJson = params.get("familyMembers"); |
|
|
|
|
|
} |
|
|
|
|
|
if (familyMembersJson == null) { |
|
|
|
|
|
familyMembersJson = params.get("treeData"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// We will keep a list of keys that were parsed as family members JSON to skip |
|
|
|
|
|
// them in the main q-param loop |
|
|
|
|
|
List<String> parsedJsonKeys = new ArrayList<>(); |
|
|
|
|
|
// if (familyMembersJson != null && !familyMembersJson.trim().isEmpty() |
|
|
|
|
|
// && familyMembersJson.trim().startsWith("[")) { |
|
|
|
|
|
// try { |
|
|
|
|
|
// List<Map<String, Object>> familyList = parseFamilyMembersJson(familyMembersJson); |
|
|
|
|
|
// if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
// saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
// } |
|
|
|
|
|
// } catch (Exception e) { |
|
|
|
|
|
// e.printStackTrace(); |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Map<Integer, Map<String, Object>>> structuredFamilyMap = new HashMap<>(); |
|
|
|
|
|
java.util.regex.Pattern structPattern = java.util.regex.Pattern |
|
|
|
|
|
.compile("^q(\\d+)\\[(\\d+)\\]\\[([a-zA-Z0-9_]+)\\]$"); |
|
|
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
|
|
String key = entry.getKey().trim(); |
|
|
|
|
|
String val = entry.getValue() == null ? "" : entry.getValue().trim(); |
|
|
|
|
|
java.util.regex.Matcher m = structPattern.matcher(key); |
|
|
|
|
|
if (m.matches()) { |
|
|
|
|
|
String qidStr = m.group(1); |
|
|
|
|
|
String qPrefix = "q" + qidStr; |
|
|
|
|
|
int index = Integer.parseInt(m.group(2)); |
|
|
|
|
|
String prop = m.group(3); |
|
|
|
|
|
|
|
|
|
|
|
structuredFamilyMap.computeIfAbsent(qPrefix, k -> new java.util.TreeMap<>()) |
|
|
|
|
|
.computeIfAbsent(index, k -> new LinkedHashMap<>()) |
|
|
|
|
|
.put(prop, val); |
|
|
|
|
|
|
|
|
|
|
|
// Track this key to skip in the normal parameter loop |
|
|
|
|
|
parsedJsonKeys.add(key); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// Save the grouped structured family members |
|
|
|
|
|
for (Map.Entry<String, Map<Integer, Map<String, Object>>> entry : structuredFamilyMap.entrySet()) { |
|
|
|
|
|
List<Map<String, Object>> familyList = new ArrayList<>(entry.getValue().values()); |
|
|
|
|
|
if (!familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (familyMembersJson != null && !familyMembersJson.trim().isEmpty() |
|
|
|
|
|
&& familyMembersJson.trim().startsWith("[")) { |
|
|
|
|
|
try { |
|
|
|
|
|
List<Map<String, Object>> familyList = parseFamilyMembersJson(familyMembersJson); |
|
|
|
|
|
if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// Also check if any qX parameter contains the JSON list of family members |
|
|
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
|
|
String fieldName = entry.getKey().trim(); |
|
|
|
|
|
String answer = entry.getValue() == null ? "" : entry.getValue().trim(); |
|
|
|
|
|
if (fieldName.startsWith("q") && answer.startsWith("[") && answer.endsWith("]")) { |
|
|
|
|
|
try { |
|
|
|
|
|
List<Map<String, Object>> familyList = parseFamilyMembersJson(answer); |
|
|
|
|
|
if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
parsedJsonKeys.add(fieldName); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
// Ignore, not a valid JSON list, process as normal q parameter |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String familyMembersJson = params.get("family_members"); |
|
|
|
|
|
if (familyMembersJson == null) { |
|
|
|
|
|
familyMembersJson = params.get("familyMembers"); |
|
|
|
|
|
} |
|
|
|
|
|
if (familyMembersJson == null) { |
|
|
|
|
|
familyMembersJson = params.get("treeData"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// We will keep a list of keys that were parsed as family members JSON to skip |
|
|
|
|
|
// them in the main q-param loop |
|
|
|
|
|
List<String> parsedJsonKeys = new ArrayList<>(); |
|
|
|
|
|
// if (familyMembersJson != null && !familyMembersJson.trim().isEmpty() |
|
|
|
|
|
// && familyMembersJson.trim().startsWith("[")) { |
|
|
|
|
|
// try { |
|
|
|
|
|
// List<Map<String, Object>> familyList = |
|
|
|
|
|
// parseFamilyMembersJson(familyMembersJson); |
|
|
|
|
|
// if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
// saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
// } |
|
|
|
|
|
// } catch (Exception e) { |
|
|
|
|
|
// e.printStackTrace(); |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Map<Integer, Map<String, Object>>> structuredFamilyMap = new HashMap<>(); |
|
|
|
|
|
java.util.regex.Pattern structPattern = java.util.regex.Pattern |
|
|
|
|
|
.compile("^q(\\d+)\\[(\\d+)\\]\\[([a-zA-Z0-9_]+)\\]$"); |
|
|
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
|
|
String key = entry.getKey().trim(); |
|
|
|
|
|
String val = entry.getValue() == null ? "" : entry.getValue().trim(); |
|
|
|
|
|
java.util.regex.Matcher m = structPattern.matcher(key); |
|
|
|
|
|
if (m.matches()) { |
|
|
|
|
|
String qidStr = m.group(1); |
|
|
|
|
|
String qPrefix = "q" + qidStr; |
|
|
|
|
|
int index = Integer.parseInt(m.group(2)); |
|
|
|
|
|
String prop = m.group(3); |
|
|
|
|
|
|
|
|
|
|
|
structuredFamilyMap.computeIfAbsent(qPrefix, k -> new java.util.TreeMap<>()) |
|
|
|
|
|
.computeIfAbsent(index, k -> new LinkedHashMap<>()) |
|
|
|
|
|
.put(prop, val); |
|
|
|
|
|
|
|
|
|
|
|
// Track this key to skip in the normal parameter loop |
|
|
|
|
|
parsedJsonKeys.add(key); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// Save the grouped structured family members |
|
|
|
|
|
for (Map.Entry<String, Map<Integer, Map<String, Object>>> entry : structuredFamilyMap.entrySet()) { |
|
|
|
|
|
List<Map<String, Object>> familyList = new ArrayList<>(entry.getValue().values()); |
|
|
|
|
|
if (!familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (familyMembersJson != null && !familyMembersJson.trim().isEmpty() |
|
|
|
|
|
&& familyMembersJson.trim().startsWith("[")) { |
|
|
|
|
|
try { |
|
|
|
|
|
List<Map<String, Object>> familyList = parseFamilyMembersJson(familyMembersJson); |
|
|
|
|
|
if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// Also check if any qX parameter contains the JSON list of family members |
|
|
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
|
|
|
|
|
String fieldName = entry.getKey().trim(); |
|
|
|
|
|
String answer = entry.getValue() == null ? "" : entry.getValue().trim(); |
|
|
|
|
|
if (fieldName.startsWith("q") && answer.startsWith("[") && answer.endsWith("]")) { |
|
|
|
|
|
try { |
|
|
|
|
|
List<Map<String, Object>> familyList = parseFamilyMembersJson(answer); |
|
|
|
|
|
if (familyList != null && !familyList.isEmpty()) { |
|
|
|
|
|
saveFamilyMembers(familyList, surveyId, cby); |
|
|
|
|
|
parsedJsonKeys.add(fieldName); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
// Ignore, not a valid JSON list, process as normal q parameter |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Deactivate existing active responses for this surveyId and cid |
|
|
// Deactivate existing active responses for this surveyId and cid |
|
|
String deactivateSql = "UPDATE shelter_resuce_response SET isactive = 0, isdelete = 1 WHERE survey_id = ? AND cid = ? AND isactive = 1"; |
|
|
String deactivateSql = "UPDATE shelter_resuce_response SET isactive = 0, isdelete = 1 WHERE survey_id = ? AND cid = ? AND isactive = 1"; |
|
|
@ -572,14 +573,10 @@ public class ShelterResucueService { |
|
|
+ |
|
|
+ |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)"; |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String insertSql = "INSERT INTO shelter_resuce_response (qid, answer, others_answer, survey_id, cid, cby, parent_answer_id, cdate, isactive, isdelete) " |
|
|
String insertSql = "INSERT INTO shelter_resuce_response (qid, answer, others_answer, survey_id, cid, cby, parent_answer_id, cdate, isactive, isdelete) " |
|
|
+ |
|
|
+ |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)"; |
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String qSql = "SELECT qid, question_type, field_name, q_english FROM shelter_resuce_questions_master " |
|
|
String qSql = "SELECT qid, question_type, field_name, q_english FROM shelter_resuce_questions_master " |
|
|
+ "WHERE cid = ? AND (isactive = 1 AND isdelete = 0 OR (cid = '1' AND field_name IN ('q2', 'q78', 'q11', 'q8', 'q9', 'q10', 'q77')))"; |
|
|
+ "WHERE cid = ? AND (isactive = 1 AND isdelete = 0 OR (cid = '1' AND field_name IN ('q2', 'q78', 'q11', 'q8', 'q9', 'q10', 'q77')))"; |
|
|
List<Map<String, Object>> questions = jdbcShelterRescueTemplate.queryForList(qSql, cid); |
|
|
List<Map<String, Object>> questions = jdbcShelterRescueTemplate.queryForList(qSql, cid); |
|
|
@ -772,7 +769,7 @@ public class ShelterResucueService { |
|
|
|
|
|
|
|
|
if (isNotWilling) { |
|
|
if (isNotWilling) { |
|
|
jdbcShelterRescueTemplate.update(insertparticipateSql, qid, answer, othersAnswer, surveyId, |
|
|
jdbcShelterRescueTemplate.update(insertparticipateSql, qid, answer, othersAnswer, surveyId, |
|
|
cid, cby,parentAnswerId); |
|
|
|
|
|
|
|
|
cid, cby, parentAnswerId); |
|
|
|
|
|
|
|
|
// System.out.println("--- DB SAVE (Participate) --- QID: " + qid + " | |
|
|
// System.out.println("--- DB SAVE (Participate) --- QID: " + qid + " | |
|
|
// FieldName: " + fieldName + " | Answer: " + answer + " | OthersAnswer: " + |
|
|
// FieldName: " + fieldName + " | Answer: " + answer + " | OthersAnswer: " + |
|
|
@ -1098,7 +1095,6 @@ public class ShelterResucueService { |
|
|
return resultList; |
|
|
return resultList; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<Map<String, Object>> parseFamilyMembersJson(String jsonStr) { |
|
|
private List<Map<String, Object>> parseFamilyMembersJson(String jsonStr) { |
|
|
if (jsonStr == null || jsonStr.trim().isEmpty()) { |
|
|
if (jsonStr == null || jsonStr.trim().isEmpty()) { |
|
|
return new ArrayList<>(); |
|
|
return new ArrayList<>(); |
|
|
@ -1156,7 +1152,6 @@ public class ShelterResucueService { |
|
|
return list; |
|
|
return list; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveFamilyMembers(List<Map<String, Object>> familyList, String surveyId, String cby) { |
|
|
private void saveFamilyMembers(List<Map<String, Object>> familyList, String surveyId, String cby) { |
|
|
if (familyList == null || familyList.isEmpty()) { |
|
|
if (familyList == null || familyList.isEmpty()) { |
|
|
return; |
|
|
return; |
|
|
@ -1202,14 +1197,13 @@ public class ShelterResucueService { |
|
|
qid = 8; |
|
|
qid = 8; |
|
|
} else if ("gender".equals(cleanKey)) { |
|
|
} else if ("gender".equals(cleanKey)) { |
|
|
qid = 9; |
|
|
qid = 9; |
|
|
} |
|
|
|
|
|
else if ("remark".equals(cleanKey)) { |
|
|
|
|
|
|
|
|
} else if ("remark".equals(cleanKey)) { |
|
|
qid = 10; |
|
|
qid = 10; |
|
|
|
|
|
} else if ("resucelocaton".equals(cleanKey)) { |
|
|
|
|
|
qid = 11; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback: If still null, try resolving by field_name or matching q_english |
|
|
// Fallback: If still null, try resolving by field_name or matching q_english |
|
|
// (case-insensitive) from DB without active/delete constraint |
|
|
// (case-insensitive) from DB without active/delete constraint |
|
|
if (qid == null) { |
|
|
if (qid == null) { |
|
|
@ -1251,20 +1245,20 @@ public class ShelterResucueService { |
|
|
parentAnswerId = 2; |
|
|
parentAnswerId = 2; |
|
|
} else if (qid == 9) { |
|
|
} else if (qid == 9) { |
|
|
parentAnswerId = 3; |
|
|
parentAnswerId = 3; |
|
|
} |
|
|
|
|
|
else if (qid == 10) { |
|
|
|
|
|
|
|
|
} else if (qid == 10) { |
|
|
parentAnswerId = 4; |
|
|
parentAnswerId = 4; |
|
|
|
|
|
} else if (qid == 11) { |
|
|
|
|
|
parentAnswerId = 5; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback: If still null, try resolving from mapping table (without |
|
|
// Fallback: If still null, try resolving from mapping table (without |
|
|
// active/delete constraint) |
|
|
// active/delete constraint) |
|
|
if (parentAnswerId == null) { |
|
|
if (parentAnswerId == null) { |
|
|
try { |
|
|
try { |
|
|
String parentAidSql = "SELECT parent_aid FROM rag_picker_question_mapping WHERE child_qid = ? LIMIT 1"; |
|
|
String parentAidSql = "SELECT parent_aid FROM rag_picker_question_mapping WHERE child_qid = ? LIMIT 1"; |
|
|
List<Integer> list = jdbcShelterRescueTemplate.queryForList(parentAidSql, Integer.class, qid); |
|
|
|
|
|
|
|
|
List<Integer> list = jdbcShelterRescueTemplate.queryForList(parentAidSql, Integer.class, |
|
|
|
|
|
qid); |
|
|
if (!list.isEmpty()) { |
|
|
if (!list.isEmpty()) { |
|
|
parentAnswerId = list.get(0); |
|
|
parentAnswerId = list.get(0); |
|
|
} |
|
|
} |
|
|
@ -1290,6 +1284,4 @@ public class ShelterResucueService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |