11package util ;
22
33import org .apache .commons .collections .CollectionUtils ;
4+ import org .apache .commons .collections .MapUtils ;
5+ import org .apache .commons .lang3 .StringUtils ;
6+ import org .sunbird .cassandra .CassandraOperation ;
47import org .sunbird .common .Constants ;
58import org .sunbird .common .exception .ProjectCommonException ;
9+ import org .sunbird .common .models .response .Response ;
610import org .sunbird .common .models .util .JsonKey ;
711import org .sunbird .common .request .BaseRequestValidator ;
812import org .sunbird .common .request .Request ;
13+ import org .sunbird .common .request .RequestContext ;
914import org .sunbird .common .responsecode .ResponseCode ;
15+ import org .sunbird .helper .ServiceFactory ;
1016import org .sunbird .learner .util .ContentCacheHandlerV2 ;
17+ import org .sunbird .learner .util .ExtendedUtil ;
1118
12- import java .util .List ;
13- import java .util .Map ;
19+ import java .util .*;
1420
1521import static org .sunbird .common .request .orgvalidator .BaseOrgRequestValidator .ERROR_CODE ;
1622
1723public class ExtendedLearnerStateRequestValidator extends BaseRequestValidator {
1824
25+ private CassandraOperation cassandraOperation = ServiceFactory .getInstance ();
26+ private static final ExtendedUtil .DbInfo enrolmentDBInfo = ExtendedUtil .dbInfoMap .get (JsonKey .LEARNER_COURSE_DB );
1927 /**
2028 * Method to validate the get content state request.
2129 *
@@ -37,59 +45,56 @@ public void validateGetContentState(Request request) {
3745 }
3846
3947 public void validateAndSetLanguage (Request request ) {
48+ String courseId = (String ) request .getRequest ().get (JsonKey .COURSE_ID );
4049 List <String > contentIds = (List <String >) request .getRequest ().get (JsonKey .CONTENT_IDS );
41- String contentId = (contentIds != null && CollectionUtils .isNotEmpty (contentIds ))
42- ? contentIds .get (0 )
43- : null ;
44-
45- Map <String , Object > courseContent ;
46- if (contentId != null ) {
47- courseContent = fetchCourseContent (contentId );
48- } else {
49- String courseId = (String ) request .getRequest ().get (JsonKey .COURSE_ID );
50- if (org .apache .commons .lang3 .StringUtils .isBlank (courseId )) {
51- courseId = (String ) request .getRequest ().get (JsonKey .COLLECTION_ID );
52- }
53- courseContent = fetchCourseContent (courseId );
54- }
50+ String incomingLang = (String ) request .getRequest ().get (JsonKey .LANGUAGE );
5551
56- if (courseContent == null ) {
57- throw new ProjectCommonException (
58- ResponseCode .invalidCourseId .getErrorCode (),
59- "Course content not found for the given id." ,
60- ERROR_CODE
61- );
62- }
52+ Map <String , Object > enrolmentData = fetchEnrolmentData (request );
53+ String recentLang = (String ) enrolmentData .get (JsonKey .RECENT_LANGUAGE );
6354
64- List <String > languages = ( List < String >) courseContent . get ( JsonKey . LANGUAGE );
65- String language = (String ) request . getRequest (). get (JsonKey .LANGUAGE );
55+ Map <String , Object > courseContent = fetchCourseContent ( courseId );
56+ Map < String , Object > languageMapV1 = (Map < String , Object >) courseContent . get (JsonKey .LANGUAGE_MAP );
6657
67- if (org .apache .commons .lang3 .StringUtils .isBlank (language )) {
68- if (CollectionUtils .isNotEmpty (languages )) {
69- request .getRequest ().put (JsonKey .LANGUAGE , languages .get (0 ).toLowerCase ());
70- } else {
71- throw new ProjectCommonException (
72- ResponseCode .languageRequired .getErrorCode (),
73- ResponseCode .languageRequired .getErrorMessage (),
74- ERROR_CODE
75- );
58+ if (StringUtils .isNotBlank (incomingLang )) {
59+ if (incomingLang .toLowerCase ().equalsIgnoreCase (recentLang .toLowerCase ()) && CollectionUtils .isEmpty (contentIds )) {
60+ //String multiCourseId = languageMapV1.get(incomingLang.toLowerCase());
61+ Map <String , Object > langEntry = (Map <String , Object >) languageMapV1 .get (incomingLang .toLowerCase ());
62+ String multiCourseId = (String ) langEntry .get (JsonKey .ID );
63+ List <String > leafNodes = fetchLeafNodes (multiCourseId );
64+ request .getRequest ().put (JsonKey .CONTENT_IDS , leafNodes );
65+ } else if (CollectionUtils .isEmpty (contentIds )) {
66+ Map <String , Object > langEntry = (Map <String , Object >) languageMapV1 .get (incomingLang .toLowerCase ());
67+ String multiCourseId = (String ) langEntry .get (JsonKey .ID );
68+ List <String > leafNodes = fetchLeafNodes (multiCourseId );
69+ request .getRequest ().put (JsonKey .CONTENT_IDS , leafNodes );
7670 }
77- } else {
78- boolean match = CollectionUtils .isNotEmpty (languages ) &&
79- languages .stream ()
80- .map (String ::toLowerCase )
81- .anyMatch (lang -> lang .equals (language .toLowerCase ()));
82- if (!match ) {
71+ request .getRequest ().put (JsonKey .LANGUAGE , incomingLang .toLowerCase ());
72+ return ;
73+ }
74+
75+ if (StringUtils .isBlank (incomingLang )) {
76+ if (StringUtils .isNotBlank (recentLang )) {
77+ Map <String , Object > langEntry = (Map <String , Object >) languageMapV1 .get (recentLang .toLowerCase ());
78+ String multiCourseId = (String ) langEntry .get (JsonKey .ID );
79+ List <String > leafNodes = fetchLeafNodes (multiCourseId );
80+ request .getRequest ().put (JsonKey .LANGUAGE , recentLang .toLowerCase ());
81+ request .getRequest ().put (JsonKey .CONTENT_IDS , leafNodes );
82+ return ;
83+ } else {
8384 throw new ProjectCommonException (
8485 ResponseCode .languageRequired .getErrorCode (),
85- ResponseCode . languageRequired . getErrorMessage () ,
86+ "Language is not provided and no recent_language found." ,
8687 ERROR_CODE
8788 );
8889 }
89- request .getRequest ().put (JsonKey .LANGUAGE , language .toLowerCase ());
9090 }
9191 }
9292
93+ private List <String > fetchLeafNodes (String courseId ) {
94+ Map <String , Object > courseData = fetchCourseContent (courseId );
95+ return (List <String >) courseData .getOrDefault (JsonKey .LEAF_NODES , Collections .emptyList ());
96+ }
97+
9398 public Map <String , Object > fetchCourseContent (String contentId ) {
9499 try {
95100 return ContentCacheHandlerV2 .getInstance ().getContent (contentId );
@@ -98,4 +103,48 @@ public Map<String, Object> fetchCourseContent(String contentId) {
98103 return null ;
99104 }
100105 }
106+
107+ private Map <String , Object > fetchEnrolmentData (Request request ) {
108+ String userId = (String ) request .getRequest ().get (JsonKey .USER_ID );
109+ String courseId = (String ) request .getRequest ().get (JsonKey .COURSE_ID );
110+ String batchId = (String ) request .getRequest ().get (JsonKey .BATCH_ID );
111+
112+ Map <String , Object > filters = new HashMap <>();
113+ filters .put (JsonKey .USER_ID_KEY , userId );
114+ filters .put (JsonKey .COURSE_ID_KEY , courseId );
115+ filters .put (JsonKey .BATCH_ID_KEY , batchId );
116+
117+ Response response = cassandraOperation .getRecords (
118+ request .getRequestContext (),
119+ enrolmentDBInfo .getKeySpace (),
120+ enrolmentDBInfo .getTableName (),
121+ filters ,
122+ null
123+ );
124+
125+ List <Map <String , Object >> resultList = (List <Map <String , Object >>)
126+ response .getResult ().getOrDefault (JsonKey .RESPONSE , new ArrayList <>());
127+
128+ if (resultList .isEmpty ()) {
129+ throw new ProjectCommonException (
130+ ResponseCode .invalidRequestData .getErrorCode (),
131+ String .format (
132+ "Enrolment not found for user: %s, course: %s, batch: %s" ,
133+ userId , courseId , batchId
134+ ),
135+ ERROR_CODE
136+ );
137+ }
138+
139+ Map <String , Object > enrolmentData = resultList .get (0 );
140+
141+ String language = (String ) request .getRequest ().get (JsonKey .LANGUAGE );
142+ if (StringUtils .isBlank (language )) {
143+ language = (String ) enrolmentData .get (JsonKey .RECENT_LANGUAGE );
144+ request .getRequest ().put (JsonKey .LANGUAGE , language );
145+ }
146+
147+ return enrolmentData ;
148+ }
149+
101150}
0 commit comments