Skip to content

Commit 2b1918f

Browse files
KB-10798 fix(content-state-update): handle language resolution for mu… (#234)
* KB-10798 fix(content-state-update): handle language resolution for multilingual courses * KB-10798 fix(content-state-update): handle language resolution for multilingual courses
1 parent f03a311 commit 2b1918f

File tree

1 file changed

+79
-31
lines changed

1 file changed

+79
-31
lines changed

service/app/util/ExtendedRequestValidator.java

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@
44
import org.apache.commons.collections.CollectionUtils;
55
import org.apache.commons.collections4.MapUtils;
66
import org.apache.commons.lang3.StringUtils;
7+
import org.sunbird.cassandra.CassandraOperation;
78
import org.sunbird.common.Constants;
89
import org.sunbird.common.exception.ProjectCommonException;
10+
import org.sunbird.common.models.response.Response;
911
import org.sunbird.common.models.util.JsonKey;
1012
import org.sunbird.common.models.util.LoggerUtil;
1113
import org.sunbird.common.models.util.ProjectUtil;
1214
import org.sunbird.common.request.Request;
1315
import org.sunbird.common.responsecode.ResponseCode;
16+
import org.sunbird.helper.ServiceFactory;
1417
import org.sunbird.learner.util.ContentCacheHandlerV2;
18+
import org.sunbird.learner.util.ExtendedUtil;
1519

1620
import java.util.*;
1721

22+
import static org.sunbird.common.request.orgvalidator.BaseOrgRequestValidator.ERROR_CODE;
23+
1824
public class ExtendedRequestValidator {
1925

2026
private static final int ERROR_CODE = ResponseCode.CLIENT_ERROR.getResponseCode();
2127
public static LoggerUtil logger = new LoggerUtil(RequestValidator.class);
2228
private static ObjectMapper mapper = new ObjectMapper();
29+
private static CassandraOperation cassandraOperation = ServiceFactory.getInstance();
30+
private static final ExtendedUtil.DbInfo enrolmentDBInfo = ExtendedUtil.dbInfoMap.get(JsonKey.LEARNER_COURSE_DB);
2331

2432

2533
/**
@@ -71,66 +79,69 @@ public static void validateUpdateContent(Request contentRequestDto) throws Excep
7179
ResponseCode.contentStatusRequired.getErrorMessage(),
7280
ERROR_CODE);
7381
}
74-
7582
} else {
7683
throw new ProjectCommonException(
7784
ResponseCode.contentIdRequired.getErrorCode(),
7885
ResponseCode.contentIdRequiredError.getErrorMessage(),
7986
ERROR_CODE);
8087
}
81-
String courseId = map.containsKey(JsonKey.COURSE_ID) ? JsonKey.COURSE_ID : JsonKey.COLLECTION_ID;
82-
map.put(JsonKey.COURSE_ID, map.get(courseId));
88+
89+
map.put(JsonKey.COURSE_ID, map.containsKey(JsonKey.COURSE_ID)
90+
? map.get(JsonKey.COURSE_ID)
91+
: map.get(JsonKey.COLLECTION_ID));
92+
8393
if (StringUtils.isBlank((String) map.get(JsonKey.COURSE_ID))) {
8494
throw new ProjectCommonException(
8595
ResponseCode.courseIdRequired.getErrorCode(),
8696
ResponseCode.courseIdRequiredError.getErrorMessage(),
8797
ERROR_CODE);
88-
} else if (isProgramConsumptionAccepted((String) map.get(JsonKey.COURSE_ID), contentId)){
98+
}
99+
Map<String, Object> courseDetails = getCourseContent((String) map.get(JsonKey.COURSE_ID));
100+
if (isProgramConsumptionAccepted((String) courseDetails.get(JsonKey.COURSECATEGORY), contentId, (Boolean) courseDetails.get("cumulativeTracking"))) {
89101
throw new ProjectCommonException(
90102
ResponseCode.invalidProgramId.getErrorCode(),
91103
ResponseCode.invalidProgramId.getErrorMessage(),
92104
ERROR_CODE);
93105
}
94-
95-
Map<String, Object> courseDetails = getCourseContent(StringUtils.isNotBlank((String) map.get(JsonKey.COURSE_ID))
96-
? (String) map.get(JsonKey.COURSE_ID)
97-
: (String) map.get(JsonKey.COLLECTION_ID));
98-
String category = (String) courseDetails.get(JsonKey.COURSECATEGORY);
99-
if (StringUtils.equalsIgnoreCase(Constants.MULTI_LINGUAL_COURSE, category))
100-
{
101-
throw new ProjectCommonException(
102-
ResponseCode.languageRequired.getErrorCode(),
103-
JsonKey.MULTILINGUAL_COURSE_PROGRESS_UPDATE_ERROR,
104-
ERROR_CODE);
106+
if (StringUtils.equalsIgnoreCase(Constants.MULTI_LINGUAL_COURSE, (String) courseDetails.get(JsonKey.COURSECATEGORY))) {
107+
throw new ProjectCommonException(
108+
ResponseCode.languageRequired.getErrorCode(),
109+
JsonKey.MULTILINGUAL_COURSE_PROGRESS_UPDATE_ERROR,
110+
ERROR_CODE);
105111
}
106-
Map<String, Object> courseContent = getCourseContent(contentId);
107-
List<String> allowedLanguages = (List<String>) courseContent.get(JsonKey.LANGUAGE);
112+
113+
Map<String, Object> languageMapV1 = (Map<String, Object>) courseDetails.get(JsonKey.LANGUAGE_MAP);
114+
108115
String incomingLanguage = (String) map.get(JsonKey.LANGUAGE);
116+
Map<String, Object> enrolmentData = fetchEnrolmentData(
117+
(String) contentRequestDto.getRequest().get(JsonKey.USER_ID),
118+
(String) map.get(JsonKey.COURSE_ID),
119+
(String) map.get(JsonKey.BATCH_ID),
120+
contentRequestDto
121+
);
122+
String recentLanguage = (String) enrolmentData.get(JsonKey.RECENT_LANGUAGE);
123+
109124
if (StringUtils.isBlank(incomingLanguage)) {
110-
if (CollectionUtils.isNotEmpty(allowedLanguages)) {
111-
map.put(JsonKey.LANGUAGE, allowedLanguages.get(0).toLowerCase());
112-
} else {
125+
if (StringUtils.isBlank(recentLanguage)) {
113126
throw new ProjectCommonException(
114127
ResponseCode.languageRequired.getErrorCode(),
115128
ResponseCode.languageRequired.getErrorMessage(),
116129
ERROR_CODE
117130
);
131+
} else {
132+
incomingLanguage = recentLanguage.toLowerCase();
118133
}
119134
} else {
120-
boolean match = CollectionUtils.isNotEmpty(allowedLanguages) &&
121-
allowedLanguages.stream()
122-
.map(String::toLowerCase)
123-
.anyMatch(lang -> lang.equals(incomingLanguage.toLowerCase()));
124-
if (!match) {
135+
incomingLanguage = incomingLanguage.toLowerCase();
136+
if (!languageMapV1.containsKey(incomingLanguage)) {
125137
throw new ProjectCommonException(
126138
ResponseCode.languageRequired.getErrorCode(),
127139
ResponseCode.languageRequired.getErrorMessage(),
128140
ERROR_CODE
129141
);
130142
}
131-
map.put(JsonKey.LANGUAGE, incomingLanguage.toLowerCase());
132143
}
133-
144+
map.put(JsonKey.LANGUAGE, incomingLanguage);
134145
}
135146
}
136147
List<Map<String, Object>> assessmentData =
@@ -217,12 +228,9 @@ public static void validateUpdateContent(Request contentRequestDto) throws Excep
217228
}
218229
}
219230

220-
public static Boolean isProgramConsumptionAccepted(String courseId, String contentId) {
231+
public static Boolean isProgramConsumptionAccepted(String courseCategory, String contentId, Boolean cumulativeTracking) {
221232
Boolean isProgram = false;
222233
try {
223-
Map<String, Object> courseContent = getCourseContent(courseId);
224-
String courseCategory = (String) courseContent.get("courseCategory");
225-
Boolean cumulativeTracking = (Boolean) courseContent.get("cumulativeTracking");
226234
if (StringUtils.isBlank(courseCategory)) {
227235
throw new ProjectCommonException(
228236
ResponseCode.invalidCourseCategory.getErrorCode(),
@@ -268,4 +276,44 @@ private static boolean isProgramCategory(String category) {
268276
Set<String> programCategories = new HashSet<>(Arrays.asList(categoriesList.split(",\\s*")));
269277
return programCategories.contains(category);
270278
}
279+
280+
private static Map<String, Object> fetchEnrolmentData(String userId, String courseId, String batchId, Request request) {
281+
282+
Map<String, Object> filters = new HashMap<>();
283+
filters.put(JsonKey.USER_ID_KEY, userId);
284+
filters.put(JsonKey.COURSE_ID_KEY, courseId);
285+
filters.put(JsonKey.BATCH_ID_KEY, batchId);
286+
287+
Response response = cassandraOperation.getRecords(
288+
request.getRequestContext(),
289+
enrolmentDBInfo.getKeySpace(),
290+
enrolmentDBInfo.getTableName(),
291+
filters,
292+
null
293+
);
294+
295+
List<Map<String, Object>> resultList = (List<Map<String, Object>>)
296+
response.getResult().getOrDefault(JsonKey.RESPONSE, new ArrayList<>());
297+
298+
if (resultList.isEmpty()) {
299+
throw new ProjectCommonException(
300+
ResponseCode.invalidRequestData.getErrorCode(),
301+
String.format(
302+
"Enrolment not found for user: %s, course: %s, batch: %s",
303+
userId, courseId, batchId
304+
),
305+
ERROR_CODE
306+
);
307+
}
308+
309+
Map<String, Object> enrolmentData = resultList.get(0);
310+
311+
String language = (String) request.getRequest().get(JsonKey.LANGUAGE);
312+
if (StringUtils.isBlank(language)) {
313+
language = (String) enrolmentData.get(JsonKey.RECENT_LANGUAGE);
314+
request.getRequest().put(JsonKey.LANGUAGE, language);
315+
}
316+
317+
return enrolmentData;
318+
}
271319
}

0 commit comments

Comments
 (0)