Browse Source

added notapplicable_qid in document master and added one child question for rescue family response

master
AJITHKUMAR12 2 weeks ago
parent
commit
57c1b579f6
  1. 188
      src/main/java/in/gov/chennaicorporation/mobileservice/gccNightShelterInspection/Service/NightShelterInspectionService.java
  2. 234
      src/main/java/in/gov/chennaicorporation/mobileservice/gccShelter/Service/ShelterResucueService.java

188
src/main/java/in/gov/chennaicorporation/mobileservice/gccNightShelterInspection/Service/NightShelterInspectionService.java

@ -9,6 +9,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@ -118,7 +119,7 @@ public class NightShelterInspectionService {
return finalList;
}
private Map<String, Object> buildQuestionWithOptions(Map<String, Object> q) {
private Map<String, Object> buildQuestionWithOptions1(Map<String, Object> q) {
Integer qid = (Integer) q.get("qid");
String masterTable = (String) safe(q.get("master_table_name"));
@ -185,6 +186,191 @@ public class NightShelterInspectionService {
return questionMap;
}
private Map<String, Object> buildQuestionWithOptions(Map<String, Object> q) {
Integer qid = (Integer) q.get("qid");
String masterTable = (String) safe(q.get("master_table_name"));
// Maintain order
Map<String, Object> questionMap = new LinkedHashMap<>();
questionMap.put("qid", q.get("qid"));
questionMap.put("q_english", q.get("q_english"));
questionMap.put("q_tamil", safe(q.get("q_tamil")));
questionMap.put("field_name", q.get("field_name"));
questionMap.put("cdate", q.get("cdate"));
questionMap.put("question_type", q.get("question_type"));
questionMap.put("isactive", q.get("isactive"));
questionMap.put("isdelete", q.get("isdelete"));
questionMap.put("orderby", q.get("orderby"));
questionMap.put("is_mandatory", q.get("is_mandatory"));
questionMap.put("is_dropdown", q.get("is_dropdown"));
questionMap.put("flag", q.get("flag"));
questionMap.put("master_table_name", masterTable);
List<Map<String, Object>> options = new ArrayList<>();
if (masterTable != null && !masterTable.isEmpty()) {
if (masterTable.equalsIgnoreCase("state_master")
|| masterTable.equalsIgnoreCase("district_master")) {
options = new ArrayList<>();
} else {
// Existing master options
options = getOptionsFromMaster(masterTable, qid);
// =====================================================
// NOT APPLICABLE LOGIC - ONLY FOR document_master
// =====================================================
if (masterTable.equalsIgnoreCase("document_master")) {
// Remove Not applicable from the normal master options
// because we will add it conditionally below.
options.removeIf(option ->
"Not applicable".equalsIgnoreCase(
String.valueOf(option.get("english_name"))
)
);
String notApplicableSql =
"SELECT id AS option_id, " +
" english_name, " +
" notapplicable_qid " +
"FROM gcc_night_shelter_inspection.document_master " +
"WHERE LOWER(english_name) = 'not applicable' " +
"AND isactive = 1 " +
"AND isdelete = 0";
List<Map<String, Object>> notApplicableOptions =
jdbcNightShelterInspectionTemplate.queryForList(
notApplicableSql
);
for (Map<String, Object> opt : notApplicableOptions) {
Object qidObj = opt.get("notapplicable_qid");
if (qidObj == null) {
continue;
}
String notApplicableQids = qidObj.toString().trim();
boolean shouldShow = false;
// 0 = show for ALL questions
if ("0".equals(notApplicableQids)) {
shouldShow = true;
} else {
// Example:
// 29,30,31,32,34,40
shouldShow = Arrays.stream(
notApplicableQids.split(",")
)
.map(String::trim)
.anyMatch(id ->
id.equals(String.valueOf(qid))
);
}
if (shouldShow) {
Map<String, Object> optionMap =
new LinkedHashMap<>();
optionMap.put(
"option_id",
opt.get("option_id")
);
optionMap.put(
"english_name",
opt.get("english_name")
);
optionMap.put("tamil_name", "");
optionMap.put("orderby", 999);
optionMap.put("is_mandatory", 0);
optionMap.put("is_others", false);
optionMap.put("text", false);
optionMap.put(
"child_questions",
new ArrayList<>()
);
options.add(optionMap);
}
}
}
}
} else {
String optSql =
"SELECT id AS option_id, answer AS english_name, " +
"orderby, is_mandatory " +
"FROM night_shelter_inspection_answer_master " +
"WHERE qid=? " +
"AND isactive=1 " +
"AND isdelete=0 " +
"ORDER BY orderby";
List<Map<String, Object>> dbOptions =
jdbcNightShelterInspectionTemplate.queryForList(
optSql,
qid
);
for (Map<String, Object> opt : dbOptions) {
Map<String, Object> optionMap =
new LinkedHashMap<>();
String englishName =
(String) opt.get("english_name");
optionMap.put("option_id", opt.get("option_id"));
optionMap.put("english_name", englishName);
optionMap.put("tamil_name", "");
optionMap.put("orderby", opt.get("orderby"));
optionMap.put(
"is_mandatory",
opt.get("is_mandatory")
);
boolean isOthers =
englishName != null
&& englishName.equalsIgnoreCase("Others");
optionMap.put("is_others", isOthers);
optionMap.put("text", isOthers);
Integer aid =
(Integer) opt.get("option_id");
List<Map<String, Object>> childQuestions =
getChildQuestionsRecursive(aid);
optionMap.put(
"child_questions",
childQuestions
);
options.add(optionMap);
}
}
questionMap.put("options", options);
return questionMap;
}
private List<Map<String, Object>> getChildQuestionsRecursive(Integer parentAid) {

234
src/main/java/in/gov/chennaicorporation/mobileservice/gccShelter/Service/ShelterResucueService.java

@ -60,7 +60,6 @@ public class ShelterResucueService {
public void setDataSource(@Qualifier("mysqlShelterDataSource") DataSource ShelterRescueataSource) {
this.jdbcShelterRescueTemplate = new JdbcTemplate(ShelterRescueataSource);
}
public List<Map<String, Object>> getSurveyQuestions(Integer cid) {
@ -251,7 +250,7 @@ public class ShelterResucueService {
String sql = "SELECT * FROM question_category_resuce_master where isactive=1 and isdelete=0 order by orderby";
return jdbcShelterRescueTemplate.queryForList(sql);
}
public List<Map<String, Object>> getResuceDesignationCategory() {
String sql = "SELECT id, english_name FROM resuce_designation_master where isactive=1 and isdelete=0 order by orderby";
return jdbcShelterRescueTemplate.queryForList(sql);
@ -272,7 +271,7 @@ public class ShelterResucueService {
return jdbcShelterRescueTemplate.queryForList(sql);
}
}
public List<Map<String, Object>> getShelterList() {
String sql = "SELECT " +
@ -355,14 +354,14 @@ public class ShelterResucueService {
String latitude = params.get("latitude");
String longitude = params.get("longitude");
String shelterId = params.get("shelter_id");
String resuceDesignation = params.get("resuce_designation");
String hospitalName = params.get("hospital_name");
System.out.println(" resuceDesignation: " + resuceDesignation);
System.out.println(" hospitalName: " + hospitalName);
//String address = params.get("address"); )
// String address = params.get("address"); )
// System.out.println(" surveyId: " + surveyId);
// System.out.println(" cid: " + cid);
// System.out.println(" cby: " + cby);
@ -380,7 +379,7 @@ public class ShelterResucueService {
"Survey ID is required for this category. Please save the Profile category first.");
return result;
}
// Check if the Profile category responses or surveyor location record exists
// for this surveyId
String checkProfileSql = "SELECT COUNT(*) FROM shelter_resuce_response WHERE survey_id = ? AND cid = '1' AND isactive = 1 AND isdelete = 0";
@ -428,14 +427,15 @@ public class ShelterResucueService {
// first (like in child_survey)
if (cid != null && cid.equals("1") && (zone != null || resolvedLat != null || resolvedLng != null)) {
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, " +
"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, 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();
@ -456,7 +456,7 @@ public class ShelterResucueService {
ps.setString(4, finalLat);
ps.setString(5, finalLng);
ps.setString(6, shelterId);
ps.setString(7, resuceDesignation);
ps.setString(8, hospitalName);
return ps;
@ -479,87 +479,88 @@ public class ShelterResucueService {
System.out.println("Error saving surveyor location details: " + ex.getMessage());
}
}
//to save family members details
// to save family members details
// 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
String deactivateSql = "UPDATE shelter_resuce_response SET isactive = 0, isdelete = 1 WHERE survey_id = ? AND cid = ? AND isactive = 1";
@ -571,14 +572,10 @@ public class ShelterResucueService {
String insertparticipateSql = "INSERT INTO shelter_resuce_participate_response (qid, answer, others_answer, survey_id, cid, cby, parent_answer_id, cdate, isactive, isdelete) "
+
"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) "
+
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)";
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')))";
@ -772,8 +769,8 @@ public class ShelterResucueService {
if (isNotWilling) {
jdbcShelterRescueTemplate.update(insertparticipateSql, qid, answer, othersAnswer, surveyId,
cid, cby,parentAnswerId);
cid, cby, parentAnswerId);
// System.out.println("--- DB SAVE (Participate) --- QID: " + qid + " |
// FieldName: " + fieldName + " | Answer: " + answer + " | OthersAnswer: " +
// othersAnswer);
@ -781,7 +778,7 @@ public class ShelterResucueService {
if (!hasNotWillingResponse) {
jdbcShelterRescueTemplate.update(insertSql, qid, answer, othersAnswer, surveyId, cid, cby,
parentAnswerId);
// System.out.println("--- DB SAVE (Response) --- QID: " + qid + " | FieldName:
// " + fieldName + " | Answer: " + answer + " | OthersAnswer: " + othersAnswer);
}
@ -1097,8 +1094,7 @@ public class ShelterResucueService {
}
return resultList;
}
private List<Map<String, Object>> parseFamilyMembersJson(String jsonStr) {
if (jsonStr == null || jsonStr.trim().isEmpty()) {
return new ArrayList<>();
@ -1155,8 +1151,7 @@ public class ShelterResucueService {
}
return list;
}
private void saveFamilyMembers(List<Map<String, Object>> familyList, String surveyId, String cby) {
if (familyList == null || familyList.isEmpty()) {
return;
@ -1202,13 +1197,12 @@ public class ShelterResucueService {
qid = 8;
} else if ("gender".equals(cleanKey)) {
qid = 9;
}
else if ("remark".equals(cleanKey)) {
} else if ("remark".equals(cleanKey)) {
qid = 10;
} else if ("resucelocaton".equals(cleanKey)) {
qid = 11;
}
}
// Fallback: If still null, try resolving by field_name or matching q_english
// (case-insensitive) from DB without active/delete constraint
@ -1251,20 +1245,20 @@ public class ShelterResucueService {
parentAnswerId = 2;
} else if (qid == 9) {
parentAnswerId = 3;
}
else if (qid == 10) {
} else if (qid == 10) {
parentAnswerId = 4;
}
} else if (qid == 11) {
parentAnswerId = 5;
}
}
// Fallback: If still null, try resolving from mapping table (without
// active/delete constraint)
if (parentAnswerId == null) {
try {
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()) {
parentAnswerId = list.get(0);
}
@ -1289,7 +1283,5 @@ public class ShelterResucueService {
}
}
}
}
Loading…
Cancel
Save