From 56ae86fbdbefba50b7e78b66f3dec38b5c8f6078 Mon Sep 17 00:00:00 2001 From: dkttarento <138442957+dkttarento@users.noreply.github.com> Date: Mon, 8 Jan 2024 14:45:52 +0530 Subject: [PATCH 1/2] Added Null pointer Exception handler --- .../sunbird/enrolments/CourseEnrolmentActor.scala | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala b/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala index 05d1d7ecc..00d4f25e8 100644 --- a/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala +++ b/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala @@ -5,7 +5,7 @@ import java.text.{MessageFormat, SimpleDateFormat} import java.time.format.DateTimeFormatter import java.time.{LocalDate, LocalDateTime, LocalTime, Month, ZoneId} import java.util -import java.util.{Comparator, Date, UUID} +import java.util.{Collections, Comparator, Date, UUID} import akka.actor.ActorRef import com.fasterxml.jackson.databind.ObjectMapper import org.sunbird.common.models.util.JsonKey @@ -669,15 +669,19 @@ class CourseEnrolmentActor @Inject()(@Named("course-batch-notification-actor") c userId, util.Arrays.asList(JsonKey.USER_KARMA_TOTAL_POINTS, JsonKey.ADD_INFO) ) - // dbResponse is a list of maps to extract points for each record + //dbResponse is a list of maps to extract points for each record val dbResponse: java.util.List[util.Map[String, AnyRef]] = userKarmaPoints.get(JsonKey.RESPONSE).asInstanceOf[java.util.List[util.Map[String, AnyRef]]] val totalUserKarmaPoints: Int = dbResponse.asScala.collectFirst { case record: util.Map[String, AnyRef] if record.containsKey(JsonKey.USER_KARMA_TOTAL_POINTS) => record.get(JsonKey.USER_KARMA_TOTAL_POINTS).asInstanceOf[Integer].toInt }.getOrElse(0) - val addInfoString: String = dbResponse.get(0).get(JsonKey.ADD_INFO).asInstanceOf[String] - val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule) + val addInfoString: String = if (dbResponse.isEmpty) { + null + } else { + dbResponse.get(0).get(JsonKey.ADD_INFO).asInstanceOf[String] + } if (addInfoString != null) { + val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule) addInfo = objectMapper.readValue(addInfoString, classOf[util.Map[String, AnyRef]]) } val enrolmentCourseDetails = new util.HashMap[String, AnyRef]() From 7baebda1546dbfcf2394c5bd05cdb8d3ea7b9aaf Mon Sep 17 00:00:00 2001 From: dkttarento <138442957+dkttarento@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:19:50 +0530 Subject: [PATCH 2/2] Fix addinfo representation in JSON --- .../org/sunbird/enrolments/CourseEnrolmentActor.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala b/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala index 00d4f25e8..b0b602a4c 100644 --- a/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala +++ b/course-mw/enrolment-actor/src/main/scala/org/sunbird/enrolments/CourseEnrolmentActor.scala @@ -643,7 +643,7 @@ class CourseEnrolmentActor @Inject()(@Named("course-batch-notification-actor") c var certificateIssued: Int = 0 var coursesInProgress: Int = 0 var hoursSpentOnCompletedCourses: Int = 0 - var addInfo: util.Map[String, AnyRef] = null + var addInfo: util.Map[String, AnyRef] = new util.HashMap[String, AnyRef]() finalEnrolment.foreach { courseDetails => val courseStatus = courseDetails.get(JsonKey.STATUS) if (courseStatus != 2) { @@ -676,11 +676,11 @@ class CourseEnrolmentActor @Inject()(@Named("course-batch-notification-actor") c record.get(JsonKey.USER_KARMA_TOTAL_POINTS).asInstanceOf[Integer].toInt }.getOrElse(0) val addInfoString: String = if (dbResponse.isEmpty) { - null + "" } else { - dbResponse.get(0).get(JsonKey.ADD_INFO).asInstanceOf[String] + Option(dbResponse.get(0)).flatMap(record => Option(record.get(JsonKey.ADD_INFO)).collect { case str: String => str }).getOrElse("") } - if (addInfoString != null) { + if (addInfoString != null && addInfoString.nonEmpty) { val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule) addInfo = objectMapper.readValue(addInfoString, classOf[util.Map[String, AnyRef]]) }