/// <summary> /// Upload schema to IA /// </summary> /// <param name="authorizationKey"></param> /// <param name="url">Rest URL</param> /// <param name="fileLocation">Location from where the schema XSD is to be picked</param> /// <param name="schemaName">name of the schema</param> /// <returns></returns> private HttpResponseMessage UploadMap(AuthenticationResult authResult, string url, string fileLocation, string mapName) { HttpResponseMessage response = null; try { InitializeAuthorizationCredentials(); } catch (Exception ex) { throw ex; } try { response = UploadToIA(authResult, url, fileLocation, mapName); return(response); } catch (Exception ex) { try { authResult = RefreshAccessToken(authResult.UserInfo, AuthenticationAccessToken.IntegrationAccount); response = UploadToIA(authResult, url, fileLocation, mapName); return(response); } catch (Exception e) { string message = $"ERROR! Exception while putting map {mapName} to Integration Account. \nErrorMessage: {e.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw new Exception(message); } } }
/// <summary> /// Call to start the process of upload to integration account /// </summary> /// <param name="mapsToBeUploaded"></param> /// <param name="mapsDetailsList"></param> /// <param name="outputDir"></param> /// <param name="overrideExistingMapsFlag"></param> /// <param name="aadInstance"></param> /// <param name="resource"></param> /// <param name="clientId"></param> /// <param name="clientSecret"></param> /// <param name="subscriptionId"></param> /// <param name="resourceGroupName"></param> /// <param name="iaName"></param> public void UploadToIntegrationAccount(List <MapDetails> mapsToBeUploaded, ref List <MapDetails> mapDetailsList, string outputDir, bool overrideExistingMapsFlag, string subscriptionId, string resourceGroupName, string iaName, AuthenticationResult authResult) { if (mapsToBeUploaded.Count > 0) { IntegrationAccountContextForMaps iacontext = new IntegrationAccountContextForMaps(); try { IntegrationAccountDetails iaDetails = new IntegrationAccountDetails { SubscriptionId = subscriptionId, ResourceGroupName = resourceGroupName, IntegrationAccountName = iaName }; iacontext.SchemaUploadFromFolder(outputDir, mapsToBeUploaded, overrideExistingMapsFlag, iaDetails, authResult, ref mapDetailsList); } catch (Exception e) { string message = $"ERROR! Something went wrong while doing a map upload from local folder ${outputDir}. \nErrorMessage:{e.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw e; } } }
/// <summary> /// To extract selected maps from all the maps which we had retrieved by reflecting upon the DLLs /// </summary> /// <param name="selectiveMapDetailsList"></param> /// <param name="outputDir"></param> /// <param name="originalMapDetailsList"></param> /// <returns>Returns the order of the maps to be uploaded to IA</returns> public List <MapDetails> ExtractMapsFromDlls(ref List <MapDetails> selectiveMapDetailsList, string outputDir, List <MapDetails> originalMapDetailsList) { this.originalMapDetailsList = originalMapDetailsList; // Location where the all the retrieved maps are stored upon reflection of GAC'ed DLLs var d = new DirectoryInfo(outputDir + "\\AllMaps"); var filesInfo = d.GetFiles("*.xslt"); if (filesInfo.Length == 0) { string message = $"ERROR! No maps got extracted from the assembly into the local."; TraceProvider.WriteLine(message); //Console.WriteLine(message); throw new Exception(message); } // loop thru all the maps selected via the UI for extraction and enable their extraction flag on successful extraction foreach (var selectedMap in selectiveMapDetailsList) { try { if (selectedMap.fullNameOfMapToUpload != null) { selectedMap.isMapExtractedFromDb = true; TraceProvider.WriteLine($"Selected map extracted successfully: {selectedMap.fullNameOfMapToUpload}"); } else { selectedMap.isMapExtractedFromDb = false; selectedMap.errorDetailsForExtraction = selectedMap.errorDetailsForExtraction + " " + "ERROR! DLL to which this map belongs is not GAC'ed on your machine. Please GAC this required DLL (as warned in the Log File)."; } } catch (Exception ex) { string message = $"ERROR! Problem during extracttion of selected maps. \nErrorMessage:{ex.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{ex.StackTrace}"); //Console.WriteLine($"{message} \nStackTrace:{ex.StackTrace}"); selectedMap.errorDetailsForExtraction = selectedMap.errorDetailsForExtraction + "\n" + message; //throw new Exception(message); } } // the dictionary of the schemas and all their dependencies are now with us, pass on this dictionary to below code to get the order of upload to IA var mapsToBeUploaded = new List <MapDetails>(); foreach (var item in selectiveMapDetailsList) { string source = outputDir + "AllMaps\\" + item.fullNameOfMapToUpload + ".xslt"; string dest = outputDir + item.fullNameOfMapToUpload + ".xslt"; File.Copy(source, dest); mapsToBeUploaded.Add(item); } return(mapsToBeUploaded); }
/// <summary> /// Clear the directory for a new selection /// </summary> /// <param name="outputdir"></param> private void SetMapAndDllDirectories(string outputdir) { try { if (ConfigurationManager.AppSettings["mapOutputDir"].ToString() != "") { mapOutputDir = ConfigurationManager.AppSettings["mapOutputDir"].ToString(); } else { mapOutputDir = outputdir; } } catch (Exception) { mapOutputDir = outputdir; } TraceProvider.WriteLine($"Map Output Directory is {mapOutputDir} "); if (Directory.Exists(mapOutputDir)) { DeleteDirectory(mapOutputDir); } try { if (ConfigurationManager.AppSettings["dllLocation"].ToString() != "") { dllLocation = ConfigurationManager.AppSettings["dllLocation"].ToString(); TraceProvider.WriteLine("DLLs are at location " + dllLocation); } else { TraceProvider.WriteLine("The configured DLL Directory Location is Empty. Assuming DLLs are to be read from the GAC"); //Console.WriteLine("The configured DLL Directory Location is Empty. Assuming DLLs are to be read from the GAC"); } } catch (Exception) { TraceProvider.WriteLine("No DLL Directory Location Configured. Assuming DLLs are to be read from the GAC"); //Console.WriteLine("No DLL Directory Location Configured. Assuming DLLs are to be read from the GAC"); } }
/// <summary> /// Starting point for upload of map /// </summary> /// <param name="fileLocation">Location where map XSLXs are picked from after their extraction</param> /// <param name="schemaList">List of maps to be uploaded</param> /// <param name="overrideExistingMapsFlag">Flag about whether to override the existing maps in the IA</param> /// <param name="iaDetails">IA auth details</param> /// <param name="mapDetailsList">List of schemas to be uploaded</param> public void SchemaUploadFromFolder(string fileLocation, List <MapDetails> mapList, bool overrideExistingMapsFlag, IntegrationAccountDetails iaDetails, AuthenticationResult authResult, ref List <MapDetails> mapDetailsList) { TraceProvider.WriteLine("Extraction of maps. Starting upload to Integration Account..."); try { // AAD Authentication - Getting of Token if (string.IsNullOrEmpty(authResult.AccessToken)) { string message = $"ERROR! Problem during AAD authentication. \nErrorMessage: Invalid access token"; TraceProvider.WriteLine($"{message}"); //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}"); throw new Exception(message); } } catch (Exception ex) { throw ex; } // Do one by one upload on each schema XSD based on flags - whether to override, whether schema already exists, whether schema got successfully extracted foreach (var mapName in mapList) { var url = ConfigurationManager.AppSettings["MapRestUrl"]; var response = new HttpResponseMessage(); try { url = string.Format(url, iaDetails.SubscriptionId, iaDetails.ResourceGroupName, iaDetails.IntegrationAccountName, mapName.fullNameOfMapToUpload); bool exists = CheckIfArtifactExists(url, authResult); if (overrideExistingMapsFlag) { if (mapName.isMapExtractedFromDb == false) { TraceProvider.WriteLine("Uploading unselected dependency. Schema:" + mapName.fullNameOfMapToUpload); // Console.WriteLine("Uploading unselected dependency. Schema:" + schemaName.fullNameOfSchemaToUpload); } else { TraceProvider.WriteLine($"Uploading map {mapName.fullNameOfMapToUpload}"); //Console.WriteLine($"Uploading schema {schemaName.fullNameOfSchemaToUpload}"); } response = UploadMap(authResult, url, fileLocation, mapName.fullNameOfMapToUpload); } else if (exists) { response.StatusCode = System.Net.HttpStatusCode.Conflict; if (mapName.isMapExtractedFromDb == false) { TraceProvider.WriteLine($"Unselected dependent schema {mapName.fullNameOfMapToUpload} already exists"); } else { TraceProvider.WriteLine($"Schema {mapName.fullNameOfMapToUpload} already exists"); } } else { if (mapName.isMapExtractedFromDb == false) { TraceProvider.WriteLine($"Uploading unselected dependency. Map:{mapName.fullNameOfMapToUpload}"); } else { TraceProvider.WriteLine($"Uploading map {mapName}"); } response = UploadMap(authResult, url, fileLocation, mapName.fullNameOfMapToUpload); } // response status code for a particular map after upload rest operation has been performed on it if (response.StatusCode == System.Net.HttpStatusCode.Conflict) { if (mapName.isMapExtractedFromDb) { string message = $"Conflict for map {mapName.fullNameOfMapToUpload}. Response:{response.StatusCode}"; mapName.mapUploadToIAStatus = MapUploadToIAStatus.Partial; mapName.errorDetailsForMigration = mapName.errorDetailsForMigration + ". " + message + ". Check logs for details."; TraceProvider.WriteLine(message); //Console.WriteLine(message); } else { TraceProvider.WriteLine($"Conflict for unselected dependency map {mapName.fullNameOfMapToUpload}. Response:{response.StatusCode}"); } } else if (!response.IsSuccessStatusCode) { if (mapName.isMapExtractedFromDb) { string message = $"Failed for map {mapName.fullNameOfMapToUpload}. ResponseStatusCode:{response}"; mapName.mapUploadToIAStatus = MapUploadToIAStatus.Failure; mapName.errorDetailsForMigration = mapName.errorDetailsForMigration + ". " + message + ". Check logs for details."; TraceProvider.WriteLine(message); //Console.WriteLine(message); } else { TraceProvider.WriteLine($"Failed for hidden dependency map {mapName.fullNameOfMapToUpload}. Response:{response}"); } } else { if (mapName.isMapExtractedFromDb) { mapName.mapUploadToIAStatus = MapUploadToIAStatus.Success; TraceProvider.WriteLine($"Successfully uploaded map {mapName.fullNameOfMapToUpload} to Integration Account. Response:{response.StatusCode}"); } else { TraceProvider.WriteLine($"Successfully uploaded unselected dependent map {mapName.fullNameOfMapToUpload} to Integration Account. Response: {response.StatusCode}"); } } } catch (Exception ex) { string message = $"Failed to upload map {mapName.fullNameOfMapToUpload}. IA Rest API returned a response {response}. \nErrorMessage:{ex.Message}"; TraceProvider.WriteLine($"{message} \nStackTrace:{ex.StackTrace}"); //Console.WriteLine($"{message} \nStackTrace:{ex.StackTrace}"); mapName.mapUploadToIAStatus = MapUploadToIAStatus.Failure; mapName.errorDetailsForMigration = mapName.errorDetailsForMigration + ". " + message + ". Check logs for details."; } } }
/// <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); } }