From 7adee09f44bf7e47b71370dcd9b683c2f1bf7ac5 Mon Sep 17 00:00:00 2001 From: Kumar Gauraw Date: Mon, 7 Aug 2023 17:10:19 +0530 Subject: [PATCH] Issue #IQ-545 fix: added loggers and custom exception --- .../exceptions/QumlMigrationException.scala | 3 ++ .../migrator/helpers/QuestionMigrator.scala | 16 +++++++--- .../helpers/QuestionSetMigrator.scala | 31 +++++++++++-------- 3 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/exceptions/QumlMigrationException.scala diff --git a/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/exceptions/QumlMigrationException.scala b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/exceptions/QumlMigrationException.scala new file mode 100644 index 00000000..adb82b46 --- /dev/null +++ b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/exceptions/QumlMigrationException.scala @@ -0,0 +1,3 @@ +package org.sunbird.job.quml.migrator.exceptions + +class QumlMigrationException(message: String) extends java.lang.Exception(message) diff --git a/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionMigrator.scala b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionMigrator.scala index 1e784ec1..dc2c5d4d 100644 --- a/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionMigrator.scala +++ b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionMigrator.scala @@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils import org.slf4j.LoggerFactory import org.sunbird.job.domain.`object`.ObjectDefinition import org.sunbird.job.quml.migrator.domain.{ExtDataConfig, ObjectData, ObjectExtData} +import org.sunbird.job.quml.migrator.exceptions.QumlMigrationException import org.sunbird.job.quml.migrator.task.QumlMigratorConfig import org.sunbird.job.util._ @@ -89,15 +90,18 @@ trait QuestionMigrator extends MigrationObjectReader with MigrationObjectUpdater val migratedExtData = migrateExtData(data.identifier, extMeta) migratedExtData.remove("primaryCategory") val migrGrpahData: util.Map[String, AnyRef] = migrateGrpahData(data.identifier, jMap) - if(migrGrpahData.containsKey("bloomsLevel")) migrGrpahData.remove("bloomsLevel") + migrGrpahData.remove("bloomsLevel") + migrGrpahData.remove("version") val updatedMeta: Map[String, AnyRef] = migrGrpahData.asScala.toMap ++ Map[String, AnyRef]("qumlVersion" -> 1.1.asInstanceOf[AnyRef], "schemaVersion" -> "1.1", "migrationVersion" -> 3.0.asInstanceOf[AnyRef]) logger.info("QuestionMigrator ::: migrateQuestion ::: migrated metadata :::: "+migrGrpahData) logger.info("QuestionMigrator ::: migrateQuestion ::: migrated ext data :::: "+migratedExtData) logger.info("QuestionMigrator ::: migrateQuestion ::: Completed Data Transformation For : " + data.identifier) Some(new ObjectData(data.identifier, updatedMeta, Some(migratedExtData.asScala.toMap), data.hierarchy)) } catch { - case e: Exception => { + case e: java.lang.Exception => { logger.info("QuestionMigrator ::: migrateQuestion ::: Failed Data Transformation For : " + data.identifier) + logger.info("QuestionMigrator ::: migrateQuestion ::: exception message :: "+ e.getMessage) + logger.info("QuestionMigrator ::: migrateQuestion ::: exception message :: "+ e.getLocalizedMessage) e.printStackTrace() val updatedMeta: Map[String, AnyRef] = data.metadata ++ Map[String, AnyRef]("migrationVersion" -> 2.1.asInstanceOf[AnyRef], "migrationError"->e.getMessage) Some(new ObjectData(data.identifier, updatedMeta, data.extData, data.hierarchy)) @@ -114,8 +118,9 @@ trait QuestionMigrator extends MigrationObjectReader with MigrationObjectUpdater } else data } catch { case e: Exception => { + logger.info(s"QuestionMigrator ::: migrateGrpahData ::: Error occurred while converting graph data for ${identifier} | Error: " +e.getMessage) e.printStackTrace() - throw new Exception(s"Error Occurred While Converting Graph Data To Quml 1.1 Format for ${identifier}") + throw new QumlMigrationException(s"Error Occurred While Converting Graph Data To Quml 1.1 Format for ${identifier} | Error: "+e.getMessage) } } } @@ -134,9 +139,10 @@ trait QuestionMigrator extends MigrationObjectReader with MigrationObjectUpdater data } else data } catch { - case e: Exception => { + case e: java.lang.Exception => { e.printStackTrace() - throw new Exception(s"Error Occurred While Converting External Data To Quml 1.1 Format for ${identifier}") + logger.info(s"QuestionMigrator ::: migrateExtData ::: Error occurred while converting external data for ${identifier} | Error: " + e.getMessage ) + throw new QumlMigrationException(s"Error Occurred While Converting External Data To Quml 1.1 Format for ${identifier} | Error: "+e.getMessage) } } } diff --git a/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionSetMigrator.scala b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionSetMigrator.scala index 6066e0c6..6040b22a 100644 --- a/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionSetMigrator.scala +++ b/quml-migrator/src/main/scala/org/sunbird/job/quml/migrator/helpers/QuestionSetMigrator.scala @@ -4,8 +4,9 @@ import com.datastax.driver.core.Row import com.datastax.driver.core.querybuilder.{Clause, Insert, QueryBuilder} import org.apache.commons.lang3.StringUtils import org.slf4j.LoggerFactory -import org.sunbird.job.domain.`object`.{ObjectDefinition} +import org.sunbird.job.domain.`object`.ObjectDefinition import org.sunbird.job.quml.migrator.domain.{ExtDataConfig, ObjectData, ObjectExtData} +import org.sunbird.job.quml.migrator.exceptions.QumlMigrationException import org.sunbird.job.quml.migrator.task.QumlMigratorConfig import org.sunbird.job.util.{CassandraUtil, Neo4JUtil, ScalaJsonUtil} @@ -18,6 +19,8 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda private[this] val logger = LoggerFactory.getLogger(classOf[QuestionSetMigrator]) + val propsToRemove = List("outcomeDeclaration", "bloomsLevel", "maxScore","version") + def validateQuestionSet(identifier: String, obj: ObjectData)(implicit neo4JUtil: Neo4JUtil): List[String] = { val messages = ListBuffer[String]() if (obj.hierarchy.getOrElse(Map()).isEmpty) messages += s"""There is no hierarchy available for : $identifier""" @@ -98,10 +101,9 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda val migrGrpahData: util.Map[String, AnyRef] = migrateGrpahData(data.identifier, metaMap) val migrExtData: util.Map[String, AnyRef] = migrateExtData(data.identifier, extMeta) val outcomeDeclaration: util.Map[String, AnyRef] = migrGrpahData.getOrDefault("outcomeDeclaration", Map[String, AnyRef]()).asInstanceOf[util.Map[String, AnyRef]] - migrGrpahData.remove("outcomeDeclaration") + propsToRemove.foreach(prop => migrGrpahData.remove(prop)) migrExtData.put("outcomeDeclaration", outcomeDeclaration) val migrHierarchy: util.Map[String, AnyRef] = migrateHierarchy(data.identifier, hierarchyData) - logger.info("migrateQuestionSet :: migrated graph data ::: " + migrGrpahData) logger.info("migrateQuestionSet :: migrated ext data ::: " + migrExtData) logger.info("migrateQuestionSet :: migrated hierarchy ::: " + migrHierarchy) @@ -109,8 +111,10 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda logger.info("QuestionSetMigrator ::: migrateQuestionSet ::: Completed Data Transformation For : " + data.identifier) Some(new ObjectData(data.identifier, updatedMeta, Some(migrExtData.asScala.toMap), Some(migrHierarchy.asScala.toMap))) } catch { - case e: Exception => { + case e: java.lang.Exception => { logger.info("QuestionSetMigrator ::: migrateQuestionSet ::: Failed Data Transformation For : " + data.identifier) + logger.info("QuestionSetMigrator ::: migrateQuestionSet ::: exception message :: "+ e.getMessage) + logger.info("QuestionSetMigrator ::: migrateQuestionSet ::: exception message :: "+ e.getLocalizedMessage) val updatedMeta: Map[String, AnyRef] = data.metadata ++ Map[String, AnyRef]("migrationVersion" -> 2.1.asInstanceOf[AnyRef], "migrationError"->e.getMessage) Some(new ObjectData(data.identifier, updatedMeta, data.extData, data.hierarchy)) } @@ -127,9 +131,10 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda data } else data } catch { - case e: Exception => { + case e: java.lang.Exception => { e.printStackTrace() - throw new Exception(s"Error Occurred While Converting Graph Data To Quml 1.1 Format for ${identifier}") + logger.info(s"QuestionSetMigrator :: migrateGrpahData ::: Error Occurred While Graph Data Transformation For ${identifier} | Error: "+ e.getMessage) + throw new QumlMigrationException(s"Error Occurred While Converting Graph Data To Quml 1.1 Format for ${identifier} | Error: "+e.getMessage) } } } @@ -141,9 +146,10 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda data } else data } catch { - case e: Exception => { + case e: java.lang.Exception => { e.printStackTrace() - throw new Exception(s"Error Occurred While Converting External Data To Quml 1.1 Format for ${identifier}") + logger.info(s"QuestionSetMigrator :: migrateExtData ::: Error Occurred While External Data Transformation For ${identifier} | Error: "+ e.getMessage) + throw new QumlMigrationException(s"Error Occurred While Converting External Data To Quml 1.1 Format for ${identifier} | Error : "+e.getMessage) } } } @@ -168,9 +174,10 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda data } else data } catch { - case e: Exception => { + case e: java.lang.Exception => { + logger.info(s"QuestionSetMigrator :: migrateHierarchy ::: Error Occurred While Hierarchy Data Transformation For ${identifier} | Error: "+ e.getMessage) e.printStackTrace() - throw new Exception(s"Error Occurred While Converting Hierarchy Data To Quml 1.1 Format for ${identifier}") + throw new QumlMigrationException(s"Error Occurred While Converting Hierarchy Data To Quml 1.1 Format for ${identifier} | Error: "+e.getMessage) } } } @@ -198,10 +205,8 @@ trait QuestionSetMigrator extends MigrationObjectReader with MigrationObjectUpda val chStr = ScalaJsonUtil.serialize(chData) val chMap: util.Map[String, AnyRef] = mapper.readValue(chStr, classOf[util.Map[String, AnyRef]]) ch.putAll(chMap) - } else throw new Exception(s"Please migrate children having identifier ${childrenId}") + } else throw new QumlMigrationException(s"Please migrate children having identifier ${childrenId}") } - - }) } }