/// <summary> /// Function to get the list of maps /// </summary> /// <param name="sqlConnectionString"></param> /// <param name="outputDir"></param> /// <returns></returns> public List <MapDetails> GetListOfMaps(string sqlConnectionString, string outputDir, string parameter) { SetMapAndDllDirectories(outputDir); var mapDetailsObj = new MapDetails(); var originalMapDetailsObj = new MapDetails(); using (SqlConnection cn = new SqlConnection(sqlConnectionString)) { var query = ConfigurationManager.AppSettings["MapQueryApplication"]; query = string.Format(query, parameter); //or nvcFullName like 'MSIT.EAS.ICOE.VL.PosOrdrsp.Shared.Schemas.AP%' //or nvcFullName like 'MSIT.EAS.ICOE.VL.ZInvoic.Shared.Schemas.AP%' //or nvcFullName like 'MSIT.EAS.ICOE.VL.PropertySchemas%') //and nvcFullName like 'MSIT.EAS.ICOE.VL.Ordrsp.Shared.Schemas.AP%'"; using (var cmd = new SqlCommand(query, cn)) { if (cn.State == System.Data.ConnectionState.Closed) { try { cn.Open(); } catch (Exception e) { string message = $"ERROR! Unable to establish connection to the database. \nErrorMessage:{e.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw new Exception(message); } } using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { mapDetailsObj = new MapDetails(); originalMapDetailsObj = new MapDetails(); mapDetailsObj.assemblyFullyQualifiedName = rdr["nvcFullName"].ToString(); originalMapDetailsObj.assemblyFullyQualifiedName = rdr["nvcFullName"].ToString(); mapDetailsObj.mapUploadToIAStatus = MapUploadToIAStatus.NotYetStarted; mapDetailsObj.errorDetailsForMigration = ""; mapDetailsObj.errorDetailsForExtraction = ""; mapDetailsObj.mapFilesLocation = outputDir; mapDetailsObj.mapFullName = rdr["FullName"].ToString(); originalMapDetailsObj.mapFullName = rdr["FullName"].ToString(); mapDetailsObj.mapName = rdr["Name"].ToString(); originalMapDetailsObj.mapName = rdr["Name"].ToString(); mapDetailsObj.mapNamespace = rdr["Namespace"].ToString(); mapDetailsObj.version = rdr["nvcVersion"].ToString(); mapDetailsList.Add(mapDetailsObj); originalMapDetailsList.Add(originalMapDetailsObj); } } } } Directory.CreateDirectory(mapOutputDir); Directory.CreateDirectory(mapOutputDir + "\\AllMaps"); PutAllMapsToLocalFolder(ref mapDetailsList, outputDir); return(mapDetailsList); }
/// <summary> /// Putting all maps to a local folder /// </summary> /// <param name="mapDetailsList"></param> /// <param name="outputDir"></param> public void PutAllMapsToLocalFolder(ref List <MapDetails> mapDetailsList, string outputDir) { var distinctAssembliesList = new HashSet <string>(); foreach (var mapObj in mapDetailsList) { distinctAssembliesList.Add(mapObj.assemblyFullyQualifiedName); } try { foreach (var fqnForAssembly in distinctAssembliesList) { var dllName = Path.GetFileName(fqnForAssembly).Replace(".dll", string.Empty).Replace('.', '_').Substring(0, Path.GetFileName(fqnForAssembly).IndexOf(',')); dllName = dllName + "_V" + fqnForAssembly.Substring(fqnForAssembly.IndexOf("Version"), 15).Replace(".", "_").Replace("Version=", ""); //Load the assembly into memory Assembly asm; try { asm = Assembly.Load(fqnForAssembly); } catch (FileNotFoundException e) { TraceProvider.WriteLine($"WARNING! Assembly with name {fqnForAssembly} to which identified Schemas belong is not found in GAC'ed DLLs. Message:{e.Message}"); //Console.WriteLine($"WARNING! Assembly with name {fqnForAssembly} to which identified Schemas belong is not found in GAC'ed DLLs. Message:{e.Message}"); continue; } var mapObj = new MapDetails(); var originalMapObj = new MapDetails(); var types = asm.GetTypes(); var classTypes = types.Where(type => type.IsClass == true && (type.BaseType.Name == "TransformBase") || (type.BaseType.BaseType != null && type.BaseType.BaseType.Name == "TransformBase")); foreach (Type ty in types) { try { mapObj = mapDetailsList.First <MapDetails>(r => r.mapName == ty.Name && r.assemblyFullyQualifiedName == ty.Assembly.FullName); originalMapObj = originalMapDetailsList.First <MapDetails>(r => r.mapName == ty.Name && r.assemblyFullyQualifiedName == ty.Assembly.FullName); } catch (Exception e) { continue; } try { //// for every new schema in the DLL //if (ty.BaseType != null && ty.BaseType.FullName == "Microsoft.XLANGs.BaseTypes.SchemaBase") //{ // var sb = System.Activator.CreateInstance(ty) as SchemaBase; // var schemaFileName = ty.Name; // if (ty.DeclaringType == null) // { // var schemaXmlContext = sb.XmlContent; // schemaXmlContext = schemaXmlContext.Replace("utf-16", "utf-8"); // Match matchVersion = Regex.Match(schemaXmlContext, "standards_version=\"(.*?)\""); // Match matchNamespace = Regex.Match(schemaXmlContext, "targetNamespace=\"(.*?)\""); // if (matchVersion.Success && matchNamespace.Success) // { // if (!schemaNamespaceVersionDict.ContainsKey(matchNamespace.Groups[1].Value)) // { // schemaNamespaceVersionDict.Add(matchNamespace.Groups[1].Value, matchVersion.Groups[1].Value); // } // } // schemaFileName = dllName + "_" + schemaFileName; // // truncate the length of the schema if its more than what the IA can handle // if (schemaFileName.Length > 79) // schemaFileName = schemaFileName.Replace("_", ""); // schemaObj.fullNameOfSchemaToUpload = schemaFileName; // originalSchemaObj.fullNameOfSchemaToUpload = schemaFileName; // // Write to file // var schemaFilePath = string.Format(outputDir + "\\AllSchemas\\{0}.xsd", schemaFileName); // File.WriteAllText(schemaFilePath, schemaXmlContext, Encoding.UTF8); // } //} object mapObject = Activator.CreateInstance(ty); object map = ty.InvokeMember("XmlContent", BindingFlags.GetProperty, null, mapObject, null); string mapstr = map.ToString(); Match matchVersion = Regex.Match(mapstr, "standards_version=\"(.*?)\""); Match matchNamespace = Regex.Match(mapstr, "targetNamespace=\"(.*?)\""); if (matchVersion.Success && matchNamespace.Success) { if (!mapNamespaceVersionDict.ContainsKey(matchNamespace.Groups[1].Value)) { mapNamespaceVersionDict.Add(matchNamespace.Groups[1].Value, matchVersion.Groups[1].Value); } } var mapFileName = ty.Name; mapFileName = dllName + "_" + mapFileName; // truncate the length of the schema if its more than what the IA can handle if (mapFileName.Length > 79) { mapFileName = mapFileName.Replace("_", ""); } mapObj.fullNameOfMapToUpload = mapFileName; originalMapObj.fullNameOfMapToUpload = mapFileName; // Write to file var mapFilePath = string.Format(outputDir + "\\AllMaps\\{0}.xslt", mapFileName); File.WriteAllText(mapFilePath, mapstr, Encoding.UTF8); } catch (Exception e) { string message = $"ERROR! Problem reading schema content from MapBase and Writing the file to Local Folder. \nMap:{mapObj.mapFullName} \nAssembly:{mapObj.assemblyFullyQualifiedName} \nErrorMessage: {e.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw new Exception(message); } } } } catch (Exception e) { string message = $"ERROR! Something went wrong. \nErrorMessage:{e.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw new Exception(message); } }