@ -35,7 +35,7 @@ public class ChildSurveyService {
this . jdbcMobileMenuTemplate = new JdbcTemplate ( mobileMenuDataSource ) ;
}
public List < Map < String , Object > > getSurveyQuestions ( ) {
public List < Map < String , Object > > getSurveyQuestions ( Integer cid ) {
/ / Step 1 : Get all child_qids
String childSql = "SELECT DISTINCT child_qid FROM child_question_mapping " +
@ -45,9 +45,9 @@ public class ChildSurveyService {
/ / Step 2 : Get only parent questions
String questionSql = "SELECT * FROM child_survey_questions_master " +
"WHERE isactive=1 AND isdelete=0 ORDER BY orderby" ;
"WHERE cat_id = ? AND isactive=1 AND isdelete=0 ORDER BY orderby" ;
List < Map < String , Object > > questions = jdbcChildSurveyTemplate . queryForList ( questionSql ) ;
List < Map < String , Object > > questions = jdbcChildSurveyTemplate . queryForList ( questionSql , cid ) ;
List < Map < String , Object > > finalList = new ArrayList < > ( ) ;
@ -67,6 +67,13 @@ public class ChildSurveyService {
return finalList ;
}
public List < Map < String , Object > > getQuestionsByCatId ( Integer cid ) {
String sql = "select * from child_survey_questions_master where cat_id=? and isactive=1 and isdelete=0 order by orderby " ;
return jdbcChildSurveyTemplate . queryForList ( sql , cid ) ;
}
public List < Map < String , Object > > getParticipateSurveyQuestions ( ) {
String questionSql = "SELECT * FROM child_survey_participate_master " +
@ -96,6 +103,11 @@ public class ChildSurveyService {
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" ) ) ;
if ( q . get ( "cat_id" ) ! = null ) {
questionMap . put ( "cat_id" , q . get ( "cat_id" ) ) ;
} else {
questionMap . put ( "cat_id" , "" ) ;
}
questionMap . put ( "cdate" , q . get ( "cdate" ) ) ;
questionMap . put ( "question_type" , q . get ( "question_type" ) ) ;
questionMap . put ( "isactive" , q . get ( "isactive" ) ) ;
@ -133,6 +145,11 @@ public class ChildSurveyService {
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" ) ) ;
if ( q . get ( "cat_id" ) ! = null ) {
questionMap . put ( "cat_id" , q . get ( "cat_id" ) ) ;
} else {
questionMap . put ( "cat_id" , "" ) ;
}
questionMap . put ( "cdate" , q . get ( "cdate" ) ) ;
questionMap . put ( "question_type" , q . get ( "question_type" ) ) ;
questionMap . put ( "isactive" , q . get ( "isactive" ) ) ;
@ -308,9 +325,19 @@ public class ChildSurveyService {
public String saveSurveyFromParams ( Map < String , String > params ) {
String cby = params . getOrDefault ( "cby" , "1" ) ;
String timeStr = java . time . LocalDateTime . now ( )
. format ( java . time . format . DateTimeFormatter . ofPattern ( "ddMMyyHHmmss" ) ) ;
String surveyId = "GCC_CS_" + timeStr ;
String cat_id = params . get ( "cat_id" ) ;
String surveyId = params . get ( "survey_id" ) ;
if ( surveyId = = null | | surveyId . isEmpty ( ) ) {
surveyId = params . get ( "surveyId" ) ;
}
boolean isNewSurvey = ( surveyId = = null | | surveyId . trim ( ) . isEmpty ( ) | | surveyId . equalsIgnoreCase ( "null" ) ) ;
if ( isNewSurvey ) {
String timeStr = java . time . LocalDateTime . now ( )
. format ( java . time . format . DateTimeFormatter . ofPattern ( "ddMMyyHHmmss" ) ) ;
surveyId = "GCC_CS_" + timeStr ;
}
/ / Save surveyor location details first
String zone = params . get ( "zone" ) ;
@ -323,7 +350,7 @@ public class ChildSurveyService {
lng = params . get ( "long" ) ;
String address = params . get ( "address" ) ;
if ( zone ! = null | | lat ! = null | | lng ! = null ) {
if ( isNewSurvey & & ( zone ! = null | | lat ! = null | | lng ! = null ) ) {
try {
/ / 1 . Insert without survey_id using KeyHolder
String logindetailsSql = "INSERT INTO surveyor_location_details " +
@ -408,7 +435,7 @@ public class ChildSurveyService {
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 ) ;
saveFamilyMembers ( familyList , surveyId , cby , cat_id ) ;
}
}
if ( familyMembersJson ! = null & & ! familyMembersJson . trim ( ) . isEmpty ( )
@ -416,7 +443,7 @@ public class ChildSurveyService {
try {
List < Map < String , Object > > familyList = parseFamilyMembersJson ( familyMembersJson ) ;
if ( familyList ! = null & & ! familyList . isEmpty ( ) ) {
saveFamilyMembers ( familyList , surveyId , cby ) ;
saveFamilyMembers ( familyList , surveyId , cby , cat_id ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
@ -431,7 +458,7 @@ public class ChildSurveyService {
try {
List < Map < String , Object > > familyList = parseFamilyMembersJson ( answer ) ;
if ( familyList ! = null & & ! familyList . isEmpty ( ) ) {
saveFamilyMembers ( familyList , surveyId , cby ) ;
saveFamilyMembers ( familyList , surveyId , cby , cat_id ) ;
parsedJsonKeys . add ( fieldName ) ;
}
} catch ( Exception e ) {
@ -441,8 +468,8 @@ public class ChildSurveyService {
}
String insertSql = "INSERT INTO child_survey_response " +
"(survey_id, qid, answer, others_answer, cby, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
"(survey_id, qid, answer, others_answer, cat_id, c by, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
for ( Map . Entry < String , String > entry : params . entrySet ( ) ) {
@ -477,7 +504,7 @@ public class ChildSurveyService {
String finalAnswer = value + " " + typeName ;
/ / 👉 q3 = duration
jdbcChildSurveyTemplate . update ( insertSql , surveyId , 3 , finalAnswer , "" , cby , null ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , 3 , finalAnswer , "" , cat_id , c by , null ) ;
continue ;
}
@ -509,7 +536,7 @@ public class ChildSurveyService {
String finalAnswer = value + " " + typeName ;
/ / 👉 q30 = dropout_year
jdbcChildSurveyTemplate . update ( insertSql , surveyId , 30 , finalAnswer , "" , cby , null ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , 30 , finalAnswer , "" , cat_id , c by , null ) ;
continue ;
}
@ -531,10 +558,11 @@ public class ChildSurveyService {
/ / System . out . println ( "✔ QID: " + qid ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , qid , answer , othersValue , cby , null ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , qid , answer , othersValue , cat_id , cby ,
null ) ;
} catch ( Exception e ) {
System . out . println ( "❌ Invalid field: " + fieldName ) ;
System . out . println ( "❌ Invalid field: " + fieldName + " -> " + e . getMessage ( ) ) ;
}
}
}
@ -545,6 +573,7 @@ public class ChildSurveyService {
public String saveParticipateSurveyFromParams ( Map < String , String > params ) {
String cby = params . getOrDefault ( "cby" , "1" ) ;
String cat_id = params . get ( "cat_id" ) ;
String timeStr = java . time . LocalDateTime . now ( )
. format ( java . time . format . DateTimeFormatter . ofPattern ( "ddMMyyHHmmss" ) ) ;
String surveyId = "GCC_CSP_" + timeStr ;
@ -604,8 +633,8 @@ public class ChildSurveyService {
}
String insertSql = "INSERT INTO child_survey_participate_response " +
"(survey_id, qid, answer, others_answer, cby, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
"(survey_id, qid, answer, others_answer, cat_id, c by, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
for ( Map . Entry < String , String > entry : params . entrySet ( ) ) {
@ -620,10 +649,10 @@ public class ChildSurveyService {
String othersKey = fieldName + "_other" ;
String othersValue = params . getOrDefault ( othersKey , "" ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , qid , answer , othersValue , cby , null ) ;
jdbcChildSurveyTemplate . update ( insertSql , surveyId , qid , answer , othersValue , cat_id , c by , null ) ;
} catch ( Exception e ) {
System . out . println ( "❌ Invalid field: " + fieldName ) ;
System . out . println ( "❌ Invalid field: " + fieldName + " -> " + e . getMessage ( ) ) ;
}
}
}
@ -631,6 +660,193 @@ public class ChildSurveyService {
return "Saved Successfully" ;
}
public Map < String , Object > getCategoryStatus ( String surveyId ) {
Map < String , Object > result = new HashMap < > ( ) ;
try {
/ / Check if the Profile category responses or surveyor location record exists
/ / for this surveyId
String checkProfileSql = "SELECT COUNT(*) FROM child_survey_response WHERE survey_id = ? AND cat_id = '1' AND isactive = 1 AND isdelete = 0" ;
Integer profileCount = jdbcChildSurveyTemplate . queryForObject ( checkProfileSql , Integer . class , surveyId ) ;
String checkLocSql = "SELECT COUNT(*) FROM surveyor_location_details WHERE survey_id = ? AND is_active = 1 AND is_delete = 0" ;
Integer locCount = jdbcChildSurveyTemplate . queryForObject ( checkLocSql , Integer . class , surveyId ) ;
if ( ( profileCount = = null | | profileCount = = 0 ) & & ( locCount = = null | | locCount = = 0 ) ) {
result . put ( "status" , false ) ;
result . put ( "message" ,
"General Details not created for this survey ID. Please save the General Details category first." ) ;
return result ;
}
String catSql = "SELECT id, english_name, icon, orderby FROM question_category_master WHERE isactive=1 AND isdelete=0 ORDER BY orderby" ;
List < Map < String , Object > > categories = jdbcChildSurveyTemplate . queryForList ( catSql ) ;
List < Map < String , Object > > dataList = new ArrayList < > ( ) ;
for ( Map < String , Object > cat : categories ) {
Object cidObj = cat . get ( "id" ) ;
String cidStr = cidObj ! = null ? cidObj . toString ( ) : "" ;
String responseSql = "SELECT COUNT(*) FROM (" +
" SELECT id FROM child_survey_response WHERE survey_id = ? AND cat_id = ? AND isactive = 1 AND isdelete = 0 AND answer IS NOT NULL AND answer != '' "
+
" UNION ALL " +
" SELECT id FROM child_survey_participate_response WHERE survey_id = ? AND cat_id = ? AND isactive = 1 AND isdelete = 0 AND answer IS NOT NULL AND answer != '' "
+
") t" ;
Integer count = jdbcChildSurveyTemplate . queryForObject ( responseSql , Integer . class , surveyId , cidStr ,
surveyId , cidStr ) ;
Map < String , Object > statusMap = new HashMap < > ( ) ;
statusMap . put ( "cid" , cidObj ) ;
statusMap . put ( "english_name" , cat . get ( "english_name" ) ) ;
statusMap . put ( "orderby" , cat . get ( "orderby" ) ) ;
statusMap . put ( "icon" , cat . get ( "icon" ) ) ;
statusMap . put ( "status" , ( count ! = null & & count > 0 ) ? "Completed" : "Pending" ) ;
/ / dataList . add ( statusMap ) ;
if ( statusMap . get ( "status" ) . equals ( "Completed" ) ) {
dataList . remove ( statusMap ) ;
} else if ( statusMap . get ( "status" ) . equals ( "Pending" ) ) {
dataList . add ( statusMap ) ;
}
}
result . put ( "status" , true ) ;
result . put ( "data" , dataList ) ;
return result ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
result . put ( "status" , false ) ;
result . put ( "message" , "Error fetching category status: " + e . getMessage ( ) ) ;
return result ;
}
}
public List < Map < String , Object > > getProfileCreatedList ( String cby ) {
List < Map < String , Object > > resultList = new ArrayList < > ( ) ;
try {
String sql ;
List < Object > args = new ArrayList < > ( ) ;
if ( cby ! = null & & ! cby . trim ( ) . isEmpty ( ) & & ! cby . equalsIgnoreCase ( "null" ) ) {
sql = "SELECT DISTINCT survey_id FROM child_survey_response WHERE cat_id = '1' AND cby = ? AND isactive = 1 AND isdelete = 0" ;
args . add ( cby ) ;
} else {
sql = "SELECT DISTINCT survey_id FROM child_survey_response WHERE cat_id = '1' AND isactive = 1 AND isdelete = 0" ;
}
List < String > surveyIds = jdbcChildSurveyTemplate . queryForList ( sql , args . toArray ( ) , String . class ) ;
for ( String surveyId : surveyIds ) {
Map < String , Object > profileMap = new LinkedHashMap < > ( ) ;
profileMap . put ( "survey_id" , surveyId ) ;
/ / Fetch Location Details
String locSql = "SELECT zone, ward, latitude, longitude, address, loginId as cby, cdate FROM surveyor_location_details WHERE survey_id = ? AND is_active = 1 AND is_delete = 0 LIMIT 1" ;
List < Map < String , Object > > locData = jdbcChildSurveyTemplate . queryForList ( locSql , surveyId ) ;
if ( ! locData . isEmpty ( ) ) {
Map < String , Object > loc = locData . get ( 0 ) ;
profileMap . put ( "cby" , loc . get ( "cby" ) ) ;
profileMap . put ( "cdate" , loc . get ( "cdate" ) ) ;
profileMap . put ( "zone" , loc . get ( "zone" ) ) ;
profileMap . put ( "ward" , loc . get ( "ward" ) ) ;
profileMap . put ( "latitude" , loc . get ( "latitude" ) ) ;
profileMap . put ( "longitude" , loc . get ( "longitude" ) ) ;
profileMap . put ( "address" , loc . get ( "address" ) ) ;
} else {
profileMap . put ( "zone" , "" ) ;
profileMap . put ( "ward" , "" ) ;
profileMap . put ( "latitude" , "" ) ;
profileMap . put ( "longitude" , "" ) ;
profileMap . put ( "address" , "" ) ;
String fallbackSql = "SELECT cby, cdate FROM child_survey_response WHERE survey_id = ? AND cat_id = '1' AND isactive = 1 AND isdelete = 0 LIMIT 1" ;
List < Map < String , Object > > fallbackData = jdbcChildSurveyTemplate . queryForList ( fallbackSql ,
surveyId ) ;
if ( ! fallbackData . isEmpty ( ) ) {
profileMap . put ( "cby" , fallbackData . get ( 0 ) . get ( "cby" ) ) ;
profileMap . put ( "cdate" , fallbackData . get ( 0 ) . get ( "cdate" ) ) ;
} else {
profileMap . put ( "cby" , "" ) ;
profileMap . put ( "cdate" , "" ) ;
}
}
/ / Fetch Answers
String ansSql = "SELECT q.qid, q.field_name, q.question_type, q.q_english, q.master_table_name, r.answer, r.others_answer "
+
"FROM child_survey_response r " +
"JOIN child_survey_questions_master q ON r.qid = q.qid " +
"WHERE r.survey_id = ? AND r.cat_id = '1' AND r.isactive = 1 AND r.isdelete = 0 " +
"AND ((q.isactive = 1 AND q.isdelete = 0) OR (q.cat_id = '1' AND q.field_name IN ('q91', 'q2','q1','q5' ,'q24')))" ;
List < Map < String , Object > > ansData = jdbcChildSurveyTemplate . queryForList ( ansSql , surveyId ) ;
List < Map < String , Object > > answersList = new ArrayList < > ( ) ;
List < String > allowedFields = java . util . Arrays . asList ( "q91" , "q2" , "q1" , "q5" , "q24" ) ;
for ( Map < String , Object > ans : ansData ) {
String fieldName = ( String ) ans . get ( "field_name" ) ;
if ( fieldName ! = null & & allowedFields . contains ( fieldName ) ) {
Integer qid = ( Integer ) ans . get ( "qid" ) ;
String qEnglish = ( String ) ans . get ( "q_english" ) ;
String masterTable = ( String ) ans . get ( "master_table_name" ) ;
String questionType = ( String ) ans . get ( "question_type" ) ;
String answerVal = ( String ) ans . get ( "answer" ) ;
String resolvedValue = answerVal ! = null ? answerVal : "" ;
if ( answerVal ! = null & & ! answerVal . trim ( ) . isEmpty ( ) ) {
/ / Try to resolve the option ID to its english name
try {
int optionId = Integer . parseInt ( answerVal . trim ( ) ) ;
if ( masterTable ! = null & & ! masterTable . trim ( ) . isEmpty ( ) ) {
String query = "SELECT english_name FROM " + masterTable + " WHERE id = ? LIMIT 1" ;
List < String > names = jdbcChildSurveyTemplate . queryForList ( query , String . class ,
optionId ) ;
if ( ! names . isEmpty ( ) ) {
resolvedValue = names . get ( 0 ) ;
}
} else {
String query = "SELECT english_name FROM child_survey_answer_master WHERE qid = ? AND aid = ? LIMIT 1" ;
List < String > names = jdbcChildSurveyTemplate . queryForList ( query , String . class ,
qid , optionId ) ;
if ( ! names . isEmpty ( ) ) {
resolvedValue = names . get ( 0 ) ;
}
}
} catch ( NumberFormatException e ) {
/ / Keep original value if not a valid number ID
}
}
Map < String , Object > ansMap = new LinkedHashMap < > ( ) ;
ansMap . put ( "question" , qEnglish ! = null ? qEnglish : "" ) ;
ansMap . put ( "field_name" , fieldName ) ;
ansMap . put ( "field_value" , resolvedValue ! = null ? resolvedValue : "" ) ;
answersList . add ( ansMap ) ;
String othersVal = ( String ) ans . get ( "others_answer" ) ;
if ( othersVal ! = null & & ! othersVal . isEmpty ( ) ) {
Map < String , Object > othersMap = new LinkedHashMap < > ( ) ;
othersMap . put ( "question" , "Please specify" ) ;
othersMap . put ( "field_name" , fieldName + "_other" ) ;
othersMap . put ( "field_value" , othersVal ) ;
answersList . add ( othersMap ) ;
}
}
}
profileMap . put ( "answers" , answersList ) ;
resultList . add ( profileMap ) ;
}
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
return resultList ;
}
public Map < String , Object > getLoginDetails ( String mobileNo , String password ) {
try {
@ -665,14 +881,20 @@ public class ChildSurveyService {
}
}
private void saveFamilyMembers ( List < Map < String , Object > > familyList , String surveyId , String cby ) {
public List < Map < String , Object > > getQuestionsCategory ( ) {
String sql = "SELECT * FROM question_category_master where isactive=1 and isdelete=0 order by orderby" ;
return jdbcChildSurveyTemplate . queryForList ( sql ) ;
}
private void saveFamilyMembers ( List < Map < String , Object > > familyList , String surveyId , String cby , String cat_id ) {
if ( familyList = = null | | familyList . isEmpty ( ) ) {
return ;
}
String insertFamilySql = "INSERT INTO child_survey_response_family_members " +
"(qid, answer, others_answer, cby, survey_id, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
"(qid, answer, others_answer, cat_id, c by, survey_id, parent_answer_id, cdate, isactive, isdelete) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), 1, 0)" ;
for ( Map < String , Object > member : familyList ) {
for ( Map . Entry < String , Object > field : member . entrySet ( ) ) {
@ -753,17 +975,17 @@ public class ChildSurveyService {
}
/ / If still null , try resolving statically
if ( parentAnswerId = = null ) {
if ( qid = = 10 ) {
parentAnswerId = 1 ;
} else if ( qid = = 21 ) {
parentAnswerId = 8 ;
} else if ( qid = = 26 ) {
parentAnswerId = 9 ;
} else if ( qid = = 22 ) {
parentAnswerId = 5 ;
}
}
/ / if ( parentAnswerId = = null ) {
/ / if ( qid = = 10 ) {
/ / parentAnswerId = 1 ;
/ / } else if ( qid = = 21 ) {
/ / parentAnswerId = 8 ;
/ / } else if ( qid = = 26 ) {
/ / parentAnswerId = 9 ;
/ / } else if ( qid = = 22 ) {
/ / parentAnswerId = 5 ;
/ / }
/ / }
/ / Fallback : If still null , try resolving from mapping table ( without
/ / active / delete constraint )
@ -786,7 +1008,7 @@ public class ChildSurveyService {
/ / Insert to child_survey_response_family_members
try {
jdbcChildSurveyTemplate . update ( insertFamilySql , qid , answer , othersValue , cby , surveyId ,
jdbcChildSurveyTemplate . update ( insertFamilySql , qid , answer , othersValue , cat_id , c by , surveyId ,
parentAnswerId ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;