|
4 | 4 | import org.apache.commons.collections.CollectionUtils; |
5 | 5 | import org.apache.commons.collections4.MapUtils; |
6 | 6 | import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.sunbird.cassandra.CassandraOperation; |
7 | 8 | import org.sunbird.common.Constants; |
8 | 9 | import org.sunbird.common.exception.ProjectCommonException; |
| 10 | +import org.sunbird.common.models.response.Response; |
9 | 11 | import org.sunbird.common.models.util.JsonKey; |
10 | 12 | import org.sunbird.common.models.util.LoggerUtil; |
11 | 13 | import org.sunbird.common.models.util.ProjectUtil; |
12 | 14 | import org.sunbird.common.request.Request; |
13 | 15 | import org.sunbird.common.responsecode.ResponseCode; |
| 16 | +import org.sunbird.helper.ServiceFactory; |
14 | 17 | import org.sunbird.learner.util.ContentCacheHandlerV2; |
| 18 | +import org.sunbird.learner.util.ExtendedUtil; |
15 | 19 |
|
16 | 20 | import java.util.*; |
17 | 21 |
|
| 22 | +import static org.sunbird.common.request.orgvalidator.BaseOrgRequestValidator.ERROR_CODE; |
| 23 | + |
18 | 24 | public class ExtendedRequestValidator { |
19 | 25 |
|
20 | 26 | private static final int ERROR_CODE = ResponseCode.CLIENT_ERROR.getResponseCode(); |
21 | 27 | public static LoggerUtil logger = new LoggerUtil(RequestValidator.class); |
22 | 28 | 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); |
23 | 31 |
|
24 | 32 |
|
25 | 33 | /** |
@@ -71,66 +79,69 @@ public static void validateUpdateContent(Request contentRequestDto) throws Excep |
71 | 79 | ResponseCode.contentStatusRequired.getErrorMessage(), |
72 | 80 | ERROR_CODE); |
73 | 81 | } |
74 | | - |
75 | 82 | } else { |
76 | 83 | throw new ProjectCommonException( |
77 | 84 | ResponseCode.contentIdRequired.getErrorCode(), |
78 | 85 | ResponseCode.contentIdRequiredError.getErrorMessage(), |
79 | 86 | ERROR_CODE); |
80 | 87 | } |
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 | + |
83 | 93 | if (StringUtils.isBlank((String) map.get(JsonKey.COURSE_ID))) { |
84 | 94 | throw new ProjectCommonException( |
85 | 95 | ResponseCode.courseIdRequired.getErrorCode(), |
86 | 96 | ResponseCode.courseIdRequiredError.getErrorMessage(), |
87 | 97 | 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"))) { |
89 | 101 | throw new ProjectCommonException( |
90 | 102 | ResponseCode.invalidProgramId.getErrorCode(), |
91 | 103 | ResponseCode.invalidProgramId.getErrorMessage(), |
92 | 104 | ERROR_CODE); |
93 | 105 | } |
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); |
105 | 111 | } |
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 | + |
108 | 115 | 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 | + |
109 | 124 | 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)) { |
113 | 126 | throw new ProjectCommonException( |
114 | 127 | ResponseCode.languageRequired.getErrorCode(), |
115 | 128 | ResponseCode.languageRequired.getErrorMessage(), |
116 | 129 | ERROR_CODE |
117 | 130 | ); |
| 131 | + } else { |
| 132 | + incomingLanguage = recentLanguage.toLowerCase(); |
118 | 133 | } |
119 | 134 | } 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)) { |
125 | 137 | throw new ProjectCommonException( |
126 | 138 | ResponseCode.languageRequired.getErrorCode(), |
127 | 139 | ResponseCode.languageRequired.getErrorMessage(), |
128 | 140 | ERROR_CODE |
129 | 141 | ); |
130 | 142 | } |
131 | | - map.put(JsonKey.LANGUAGE, incomingLanguage.toLowerCase()); |
132 | 143 | } |
133 | | - |
| 144 | + map.put(JsonKey.LANGUAGE, incomingLanguage); |
134 | 145 | } |
135 | 146 | } |
136 | 147 | List<Map<String, Object>> assessmentData = |
@@ -217,12 +228,9 @@ public static void validateUpdateContent(Request contentRequestDto) throws Excep |
217 | 228 | } |
218 | 229 | } |
219 | 230 |
|
220 | | - public static Boolean isProgramConsumptionAccepted(String courseId, String contentId) { |
| 231 | + public static Boolean isProgramConsumptionAccepted(String courseCategory, String contentId, Boolean cumulativeTracking) { |
221 | 232 | Boolean isProgram = false; |
222 | 233 | try { |
223 | | - Map<String, Object> courseContent = getCourseContent(courseId); |
224 | | - String courseCategory = (String) courseContent.get("courseCategory"); |
225 | | - Boolean cumulativeTracking = (Boolean) courseContent.get("cumulativeTracking"); |
226 | 234 | if (StringUtils.isBlank(courseCategory)) { |
227 | 235 | throw new ProjectCommonException( |
228 | 236 | ResponseCode.invalidCourseCategory.getErrorCode(), |
@@ -268,4 +276,44 @@ private static boolean isProgramCategory(String category) { |
268 | 276 | Set<String> programCategories = new HashSet<>(Arrays.asList(categoriesList.split(",\\s*"))); |
269 | 277 | return programCategories.contains(category); |
270 | 278 | } |
| 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 | + } |
271 | 319 | } |
0 commit comments