Exemplo n.º 1
0
        /// <summary>
        /// Call to start the process of upload to integration account
        /// </summary>
        /// <param name="schemasToBeUploaded"></param>
        /// <param name="schemaDetailsList"></param>
        /// <param name="outputDir"></param>
        /// <param name="overrideExistingSchemasFlag"></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 <SchemaDetails> schemasToBeUploaded, ref List <SchemaDetails> schemaDetailsList, string outputDir, bool overrideExistingSchemasFlag, string subscriptionId, string resourceGroupName, string iaName, AuthenticationResult authResult)
        {
            if (schemasToBeUploaded.Count > 0)
            {
                IntegrationAccountContextForSchemas iacontext = new IntegrationAccountContextForSchemas();
                try
                {
                    IntegrationAccountDetails iaDetails = new IntegrationAccountDetails
                    {
                        SubscriptionId         = subscriptionId,
                        ResourceGroupName      = resourceGroupName,
                        IntegrationAccountName = iaName
                    };
                    iacontext.SchemaUploadFromFolder(outputDir, schemasToBeUploaded, overrideExistingSchemasFlag, iaDetails, authResult, ref schemaDetailsList);
                }

                catch (Exception e)
                {
                    string message = $"ERROR! Something went wrong while doing a schema upload from local folder ${outputDir}. \nErrorMessage:{e.Message}";
                    TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}");
                    //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}");

                    throw e;
                }
            }
        }
        /// <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 UploadSchema(AuthenticationResult authResult, string url, string fileLocation, string schemaName)
        {
            HttpResponseMessage response = null;

            try
            {
                InitializeAuthorizationCredentials();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            try
            {
                response = UploadToIA(authResult, url, fileLocation, schemaName);
                return(response);
            }
            catch (Exception ex)
            {
                try
                {
                    authResult = RefreshAccessToken(authResult.UserInfo, AuthenticationAccessToken.IntegrationAccount);
                    response   = UploadToIA(authResult, url, fileLocation, schemaName);
                    return(response);
                }
                catch (Exception e)
                {
                    string message = $"ERROR! Exception while putting schema {schemaName} to Integration Account. \nErrorMessage: {e.Message}";
                    TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}");
                    //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}");

                    throw new Exception(message);
                }
            }
        }
 /// <summary>
 /// Start of the Depth First Directed Acyclic Forest Traversal Algorithm to identify and extract the dependencies from within their references in the schema XSD
 /// </summary>
 /// <param name="schemaAtKey"></param>
 /// <param name="newGraphDict"></param>
 /// <param name="dllName"></param>
 /// <param name="outputDir"></param>
 public void FileReadDFS(SchemaDetails schemaAtKey, ref Dictionary <SchemaDetails, List <SchemaDetails> > newGraphDict, string dllName, string outputDir)
 {
     try
     {
         if (visited[schemaAtKey] == false)
         {
             visited[schemaAtKey] = true;
             var dependencyList = UpdateSchemaContent(dllName, schemaAtKey, outputDir);
             newGraphDict[schemaAtKey] = dependencyList;
             if (dependencyList.Count == 0)
             {
                 return;
             }
             foreach (var dep in dependencyList)
             {
                 FileReadDFS(dep, ref newGraphDict, dllName, outputDir);
             }
         }
     }
     catch (Exception e)
     {
         string message = $"ERROR! Problem during extraction of schema dependency from DLLs. Schema:{schemaAtKey.fullNameOfSchemaToUpload} \nErrorMessage:{e.Message}";
         TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}");
         //Console.WriteLine($"{message} \nStackTrace:{e.StackTrace}");
         schemaAtKey.errorDetailsForExtraction = schemaAtKey.errorDetailsForExtraction + "\n" + message;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Clear the directory for a new selection
        /// </summary>
        /// <param name="outputdir"></param>
        private void SetSchemAndDllDirectories(string outputdir)
        {
            try
            {
                if (ConfigurationManager.AppSettings["schemaOutputDir"].ToString() != "")
                {
                    schemaOutputDir = ConfigurationManager.AppSettings["schemaOutputDir"].ToString();
                }
                else
                {
                    schemaOutputDir = outputdir;
                }
            }
            catch (Exception)
            {
                schemaOutputDir = outputdir;
            }
            TraceProvider.WriteLine($"Schema Output Directory is {schemaOutputDir} ");
            //Console.WriteLine($"Schema Output Directory is {schemaOutputDir} ");

            if (Directory.Exists(schemaOutputDir))
            {
                DeleteDirectory(schemaOutputDir);
            }


            try
            {
                if (ConfigurationManager.AppSettings["dllLocation"].ToString() != "")
                {
                    dllLocation = ConfigurationManager.AppSettings["dllLocation"].ToString();
                    TraceProvider.WriteLine("DLLs are at location " + dllLocation);
                    //Console.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>
        /// Read thru the schema XSD file content to scoop out any other dependency reference. Recurse thru all XSDs to extract all the dependenct schemas.
        /// </summary>
        /// <param name="graphDict"></param>
        /// <param name="outputDir"></param>
        /// <returns></returns>
        public Dictionary <SchemaDetails, List <SchemaDetails> > FileReadDFSCaller(Dictionary <SchemaDetails, List <SchemaDetails> > graphDict, string outputDir)
        {
            TraceProvider.WriteLine("Starting dependency extraction from selected Schema, if any...");
            //Console.WriteLine("Starting dependency extraction from selected Schema, if any...");

            var newGraphDict = new Dictionary <SchemaDetails, List <SchemaDetails> >();

            visited = new Dictionary <SchemaDetails, bool>();
            foreach (var x in graphDict.Keys)
            {
                visited.Add(x, false);
            }

            foreach (var elem in graphDict)
            {
                var dllName = elem.Key.assemblyFullyQualifiedName.Replace(".dll", string.Empty).Replace('.', '_').Substring(0, Path.GetFileName(elem.Key.assemblyFullyQualifiedName).IndexOf(','));
                dllName = dllName + "_V" + elem.Key.assemblyFullyQualifiedName.Substring(elem.Key.assemblyFullyQualifiedName.IndexOf("Version"), 15).Replace(".", "_").Replace("Version=", "");

                newGraphDict[elem.Key] = new List <SchemaDetails>();
                FileReadDFS(elem.Key, ref newGraphDict, dllName, outputDir);
            }
            return(newGraphDict);
        }
        /// <summary>
        /// Starting point for upload of schema
        /// </summary>
        /// <param name="fileLocation">Location where schema XSDs are picked from after their extraction</param>
        /// <param name="schemaList">List of schemas to be uploaded</param>
        /// <param name="overrideExistingSchemasFlag">Flag abuot whether to override the existing schemas in the IA</param>
        /// <param name="iaDetails">IA auth details</param>
        /// <param name="schemaDetailsList">List of schemas to be uploaded</param>
        public void SchemaUploadFromFolder(string fileLocation, List <SchemaDetails> schemaList, bool overrideExistingSchemasFlag, IntegrationAccountDetails iaDetails, AuthenticationResult authResult, ref List <SchemaDetails> schemaDetailsList)
        {
            TraceProvider.WriteLine("Extraction of schemas and their dependencies completed. Starting upload to Integration Account...");
            //Console.WriteLine("Extraction of schemas and their dependencies completed. 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 schemaName in schemaList)
            {
                var url      = ConfigurationManager.AppSettings["SchemaRestUrl"];
                var response = new HttpResponseMessage();
                try
                {
                    url = string.Format(url, iaDetails.SubscriptionId, iaDetails.ResourceGroupName, iaDetails.IntegrationAccountName, schemaName.fullNameOfSchemaToUpload);


                    bool exists = CheckIfArtifactExists(url, authResult);

                    if (overrideExistingSchemasFlag)
                    {
                        if (schemaName.isSchemaExtractedFromDb == false)
                        {
                            TraceProvider.WriteLine("Uploading unselected dependency. Schema:" + schemaName.fullNameOfSchemaToUpload);
                            // Console.WriteLine("Uploading unselected dependency. Schema:" + schemaName.fullNameOfSchemaToUpload);
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Uploading schema {schemaName.fullNameOfSchemaToUpload}");
                            //Console.WriteLine($"Uploading schema {schemaName.fullNameOfSchemaToUpload}");
                        }
                        response = UploadSchema(authResult, url, fileLocation, schemaName.fullNameOfSchemaToUpload);
                    }
                    else if (exists)
                    {
                        response.StatusCode = System.Net.HttpStatusCode.Conflict;
                        if (schemaName.isSchemaExtractedFromDb == false)
                        {
                            TraceProvider.WriteLine($"Unselected dependent schema {schemaName.fullNameOfSchemaToUpload} already exists");
                            //Console.WriteLine($"Unselected dependent schema {schemaName.fullNameOfSchemaToUpload} already exists");
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Schema {schemaName.fullNameOfSchemaToUpload} already exists");
                            //Console.WriteLine($"Schema {schemaName.fullNameOfSchemaToUpload} already exists");
                        }
                    }
                    else
                    {
                        if (schemaName.isSchemaExtractedFromDb == false)
                        {
                            TraceProvider.WriteLine($"Uploading unselected dependency. Schema:{schemaName.fullNameOfSchemaToUpload}");
                            //Console.WriteLine($"Uploading unselected dependency. Schema:{schemaName.fullNameOfSchemaToUpload}");
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Uploading schema {schemaName}");
                            //Console.WriteLine($"Uploading schema {schemaName}");
                        }
                        response = UploadSchema(authResult, url, fileLocation, schemaName.fullNameOfSchemaToUpload);
                    }

                    // response status code for a particular schema after upload rest operation has been performed on it
                    if (response.StatusCode == System.Net.HttpStatusCode.Conflict)
                    {
                        if (schemaName.isSchemaExtractedFromDb)
                        {
                            string message = $"Conflict for schema {schemaName.fullNameOfSchemaToUpload}. Response:{response.StatusCode}";
                            schemaName.schemaUploadToIAStatus   = SchemaUploadToIAStatus.Partial;
                            schemaName.errorDetailsForMigration = schemaName.errorDetailsForMigration + ". " + message + ". Check logs for details.";
                            TraceProvider.WriteLine(message);
                            //Console.WriteLine(message);
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Conflict for unselected dependency schema {schemaName.fullNameOfSchemaToUpload}. Response:{response.StatusCode}");
                            //Console.WriteLine($"Conflict for unselected dependency schema {schemaName.fullNameOfSchemaToUpload}. Response:{response.StatusCode}");
                        }
                    }
                    else if (!response.IsSuccessStatusCode)
                    {
                        if (schemaName.isSchemaExtractedFromDb)
                        {
                            string message = $"Failed for schema {schemaName.fullNameOfSchemaToUpload}. ResponseStatusCode:{response}";
                            schemaName.schemaUploadToIAStatus   = SchemaUploadToIAStatus.Failure;
                            schemaName.errorDetailsForMigration = schemaName.errorDetailsForMigration + ". " + message + ". Check logs for details.";
                            TraceProvider.WriteLine(message);
                            //Console.WriteLine(message);
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Failed for hidden dependency schema {schemaName.fullNameOfSchemaToUpload}. Response:{response}");
                            //Console.WriteLine($"Failed for hidden dependency schema {schemaName.fullNameOfSchemaToUpload}. Response:{response}");
                        }
                    }
                    else
                    {
                        if (schemaName.isSchemaExtractedFromDb)
                        {
                            schemaName.schemaUploadToIAStatus = SchemaUploadToIAStatus.Success;
                            TraceProvider.WriteLine($"Successfully uploaded schema {schemaName.fullNameOfSchemaToUpload} to Integration Account. Response:{response.StatusCode}");
                            // Console.WriteLine($"Successfully uploaded schema {schemaName.fullNameOfSchemaToUpload} to Integration Account. Response:{response.StatusCode}");
                        }
                        else
                        {
                            TraceProvider.WriteLine($"Successfully uploaded unselected dependent schema {schemaName.fullNameOfSchemaToUpload} to Integration Account. Response: {response.StatusCode}");
                            //Console.WriteLine($"Successfully uploaded unselected dependent schema {schemaName.fullNameOfSchemaToUpload} to Integration Account. Response: {response.StatusCode}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    string message = $"Failed to upload schema {schemaName.fullNameOfSchemaToUpload}. IA Rest API returned a response {response}. \nErrorMessage:{ex.Message}";
                    TraceProvider.WriteLine($"{message} \nStackTrace:{ex.StackTrace}");
                    //Console.WriteLine($"{message} \nStackTrace:{ex.StackTrace}");

                    schemaName.schemaUploadToIAStatus   = SchemaUploadToIAStatus.Failure;
                    schemaName.errorDetailsForMigration = schemaName.errorDetailsForMigration + ". " + message + ". Check logs for details.";
                }
            }
        }
        /// <summary>
        /// Once you encounter a dependent schema name in the content of the current schema, replace the name of the schema as per the convention we have documented.
        /// </summary>
        /// <param name="dllName"></param>
        /// <param name="thisSchemaObj"></param>
        /// <param name="outputDir"></param>
        /// <returns>Returns the dependent schema list</returns>
        private List <SchemaDetails> UpdateSchemaContent(string dllName, SchemaDetails thisSchemaObj, string outputDir)
        {
            var dependentSchemaList = new List <SchemaDetails>();

            var schemaXmlDoc = new XmlDocument();


            // PUT IT IN TRY
            var schemaXmlContext = File.ReadAllText(outputDir + "\\AllSchemas\\" + thisSchemaObj.fullNameOfSchemaToUpload + ".xsd");

            // Read dependencies
            schemaXmlDoc.LoadXml(schemaXmlContext);
            var schemaNodeChildren = schemaXmlDoc.DocumentElement;
            var x = schemaNodeChildren.ChildNodes;

            // loop thru the child nodes of the XSD schema to look for "import" or imports" nodes to extract names of dependent schemas from them
            foreach (XmlNode child in schemaNodeChildren)
            {
                try
                {
                    // looks for dependent schema in "imports" XML tag
                    if (child.Name.Contains("annotation") && child.ChildNodes.Count > 0)
                    {
                        foreach (XmlNode node in child)
                        {
                            if (node.Name.ToLower().Contains("appinfo") && node.ChildNodes.Count > 0)
                            {
                                foreach (XmlNode subnode in node)
                                {
                                    if (subnode.Name.ToLower().Contains("imports") && subnode.ChildNodes.Count > 0)
                                    {
                                        foreach (XmlNode importNode in subnode)
                                        {
                                            if (importNode.Name.ToLower().Contains("namespace") && importNode.ChildNodes.Count == 0)
                                            {
                                                var location = importNode.Attributes["location"].Value;
                                                var dependentSchemaOriginalName = location.Substring(location.LastIndexOf(".")).Remove(0, 1);
                                                var dependentSchemaObj          = this.originalSchemDetailsList.First(r => r.schemaFullName == location);

                                                var dependentSchemaNewName = dependentSchemaObj.fullNameOfSchemaToUpload;
                                                if (dependentSchemaNewName.Length > 79)
                                                {
                                                    dependentSchemaNewName = dependentSchemaNewName.Replace("_", "");
                                                }
                                                importNode.Attributes["location"].Value = location.Replace(importNode.Attributes["location"].Value, ".\\" + dependentSchemaNewName + ".xsd");
                                                schemaXmlContext = schemaXmlContext.Replace("location=\"" + location + "\"", "location=\"" + importNode.Attributes["location"].Value + "\"");
                                                dependentSchemaList.Add(originalSchemDetailsList.First(r => r.fullNameOfSchemaToUpload == dependentSchemaNewName));
                                                thisSchemaObj.dependentSchemas.Add(dependentSchemaNewName);
                                                if (dependentSchemaObj.isSchemaExtractedFromDb == false)
                                                {
                                                    visited[dependentSchemaObj] = false;
                                                    TraceProvider.WriteLine($"Extracted an unselected dependency schema: {dependentSchemaObj.fullNameOfSchemaToUpload}");
                                                    Console.WriteLine($"Extracted an unselected dependency schema: {dependentSchemaObj.fullNameOfSchemaToUpload}");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // look for dependent schema in "import" XML tag
                    if (child.Name.ToLower().Contains("import") && child.ChildNodes.Count == 0)
                    {
                        var schemaLocation = child.Attributes["schemaLocation"].Value;

                        var dependentSchemaOriginalName = schemaLocation.Substring(schemaLocation.LastIndexOf(".")).Remove(0, 1);

                        var dependentSchemaObj = this.originalSchemDetailsList.First(r => r.schemaFullName == schemaLocation);

                        var m = schemaNodeChildren.GetElementsByTagName("import");
                        var dependentSchemaNewName = dependentSchemaObj.fullNameOfSchemaToUpload;



                        //var dependentSchemaNewName = dllName + "_" + dependentSchemaOriginalName;
                        if (dependentSchemaNewName.Length > 79)
                        {
                            dependentSchemaNewName = dependentSchemaNewName.Replace("_", "");
                        }

                        child.Attributes["schemaLocation"].Value = schemaLocation.Replace(child.Attributes["schemaLocation"].Value, ".\\" + dependentSchemaNewName + ".xsd");
                        schemaXmlContext = schemaXmlContext.Replace("schemaLocation=\"" + schemaLocation + "\"", "schemaLocation=\"" + child.Attributes["schemaLocation"].Value + "\"");
                        dependentSchemaList.Add(originalSchemDetailsList.First(r => r.fullNameOfSchemaToUpload == dependentSchemaNewName));
                        thisSchemaObj.dependentSchemas.Add(dependentSchemaNewName);
                        if (dependentSchemaObj.isSchemaExtractedFromDb == false)
                        {
                            visited[dependentSchemaObj] = false;
                            TraceProvider.WriteLine($"Extracted an unselected dependency schema: {dependentSchemaObj.fullNameOfSchemaToUpload}");
                            // Console.WriteLine($"Extracted an unselected dependency schema: {dependentSchemaObj.fullNameOfSchemaToUpload}");
                        }
                    }
                }
                catch (Exception e)
                {
                    string message = $"ERROR! Problem extracting dependencies from schema {thisSchemaObj.fullNameOfSchemaToUpload}. \nErrorMessage:{e.Message}";
                    TraceProvider.WriteLine(message + " \nStackTrace:{e.StackTrace}");
                    //Console.WriteLine(message + " \nStackTrace:{e.StackTrace}");

                    thisSchemaObj.errorDetailsForExtraction = thisSchemaObj.errorDetailsForExtraction + "\n" + message;
                }
            }

            var schemaFilePath = string.Format(outputDir + "\\{0}.xsd", thisSchemaObj.fullNameOfSchemaToUpload);

            File.WriteAllText(schemaFilePath, schemaXmlContext, Encoding.UTF8);

            return(dependentSchemaList);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Function to get the list of schemas
        /// </summary>
        /// <param name="sqlConnectionString"></param>
        /// <param name="outputDir"></param>
        /// <returns></returns>
        public List <SchemaDetails> GetListOfSchemas(string sqlConnectionString, string outputDir)
        {
            SetSchemAndDllDirectories(outputDir);

            var schemaDetailsObj         = new SchemaDetails();
            var originalSchemaDetailsObj = new SchemaDetails();

            using (SqlConnection cn = new SqlConnection(sqlConnectionString))
            {
                var query = ConfigurationManager.AppSettings["SchemaQuery"];
                //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())
                        {
                            schemaDetailsObj         = new SchemaDetails();
                            originalSchemaDetailsObj = new SchemaDetails();

                            schemaDetailsObj.assemblyFullyQualifiedName         = rdr["nvcFullName"].ToString();
                            originalSchemaDetailsObj.assemblyFullyQualifiedName = rdr["nvcFullName"].ToString();

                            schemaDetailsObj.schemaUploadToIAStatus    = SchemaUploadToIAStatus.NotYetStarted;
                            schemaDetailsObj.errorDetailsForMigration  = "";
                            schemaDetailsObj.errorDetailsForExtraction = "";
                            schemaDetailsObj.dependentSchemas          = new List <string>();
                            schemaDetailsObj.schemaFilesLocation       = outputDir;

                            schemaDetailsObj.schemaFullName         = rdr["FullName"].ToString();
                            originalSchemaDetailsObj.schemaFullName = rdr["FullName"].ToString();

                            schemaDetailsObj.schemaName         = rdr["Name"].ToString();
                            originalSchemaDetailsObj.schemaName = rdr["Name"].ToString();

                            schemaDetailsObj.schemaNamespace = rdr["Namespace"].ToString();
                            schemaDetailsObj.version         = rdr["nvcVersion"].ToString();

                            schemaDetailsList.Add(schemaDetailsObj);
                            originalSchemDetailsList.Add(originalSchemaDetailsObj);
                        }
                    }
                }
            }
            Directory.CreateDirectory(schemaOutputDir);
            Directory.CreateDirectory(schemaOutputDir + "\\AllSchemas");

            PutAllSchemasToLocalFolder(ref schemaDetailsList, outputDir);
            return(schemaDetailsList);
        }
Exemplo n.º 9
0
        /// <summary>
        /// To extract selected schemas from all the schemas which we had retrieved by reflecting upon the DLLs
        /// </summary>
        /// <param name="selectiveSchemaDetailsList"></param>
        /// <param name="outputDir"></param>
        /// <param name="originalSchemaDetailsList"></param>
        /// <returns>Returns the order of the schema to be uploaded to IA</returns>
        public List <SchemaDetails> ExtractSchemasFromDlls(ref List <SchemaDetails> selectiveSchemaDetailsList, string outputDir, List <SchemaDetails> originalSchemaDetailsList)
        {
            this.originalSchemDetailsList = originalSchemaDetailsList;
            // Location where the all the retrieved schemas are stored upon reflection of GAC'ed DLLs
            var d         = new DirectoryInfo(outputDir + "\\AllSchemas");
            var filesInfo = d.GetFiles("*.xsd");

            if (filesInfo.Length == 0)
            {
                string message = $"ERROR! No schemas got extracted from the assembly into the local.";
                TraceProvider.WriteLine(message);
                //Console.WriteLine(message);

                throw new Exception(message);
            }
            var graphDict           = new Dictionary <SchemaDetails, List <SchemaDetails> >();
            var dependentSchemaList = new List <SchemaDetails>();

            // loop thru all the schemas selected via the UI for extraction and enable their extraction flag on successful extraction
            foreach (var selectedSchema in selectiveSchemaDetailsList)
            {
                try
                {
                    if (selectedSchema.fullNameOfSchemaToUpload != null)
                    {
                        selectedSchema.isSchemaExtractedFromDb = true;
                        TraceProvider.WriteLine($"Selected schema extracted successfully: {selectedSchema.fullNameOfSchemaToUpload}");
                        //Console.WriteLine($"Selected schema extracted successfully: {selectedSchema.fullNameOfSchemaToUpload}");
                        graphDict.Add(selectedSchema, dependentSchemaList);
                    }
                    else
                    {
                        selectedSchema.isSchemaExtractedFromDb   = false;
                        selectedSchema.errorDetailsForExtraction = selectedSchema.errorDetailsForExtraction + " " + "ERROR! DLL to which this schema 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 schemas. \nErrorMessage:{ex.Message}";
                    TraceProvider.WriteLine($"{message} \nStackTrace:{ex.StackTrace}");
                    //Console.WriteLine($"{message} \nStackTrace:{ex.StackTrace}");

                    selectedSchema.errorDetailsForExtraction = selectedSchema.errorDetailsForExtraction + "\n" + message;
                    //throw new Exception(message);       // THROW NEEDED HERE OR NOT. ****ASK STUTI****
                }
            }

            // out of the selected schemas, there may be a case where the schemas (which are not selected in UI) are also a dependency and their reference would be their in the selected schemas.
            var identifyDep  = new IdentifyHiddenDependencies(originalSchemaDetailsList);
            var newGraphDict = new Dictionary <SchemaDetails, List <SchemaDetails> >();

            // Retrieve all the list of dependent schemas by reading thru the selected schemas' XSDs and identifying the references and then recursively reading thru them
            if (graphDict.Count > 0)
            {
                newGraphDict = identifyDep.FileReadDFSCaller(graphDict, outputDir);
            }

            // 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 schemasToBeUploaded = new List <SchemaDetails>();

            foreach (var key in newGraphDict.Keys)
            {
                schemasToBeUploaded.Add(key);
            }
            try
            {
                var g = new DependencyGraph();

                if (newGraphDict.Count > 0)
                {
                    g.createAdjacencyList(newGraphDict);

                    schemasToBeUploaded = g.DFSCaller();
                }
            }
            catch (Exception ex)
            {
                string message = $"ERROR! Problem while identifying dependencies and generating the order of upload for selective (and dependent) schemas. \nErrorMessage:{ex.Message}";
                TraceProvider.WriteLine($"{message} \nStackTrace:{ ex.StackTrace}");
                // Console.WriteLine($"{message} \nStackTrace:{ ex.StackTrace}");

                //throw new Exception(message);
            }
            return(schemasToBeUploaded);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Putting all schemas to a local folder
        /// </summary>
        /// <param name="schemaDetailsList"></param>
        /// <param name="outputDir"></param>
        public void PutAllSchemasToLocalFolder(ref List <SchemaDetails> schemaDetailsList, string outputDir)
        {
            var distinctAssembliesList = new HashSet <string>();

            foreach (var schemaObj in schemaDetailsList)
            {
                distinctAssembliesList.Add(schemaObj.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 schemaObj         = new SchemaDetails();
                    var originalSchemaObj = new SchemaDetails();
                    foreach (Type ty in asm.GetTypes())
                    {
                        try
                        {
                            schemaObj         = schemaDetailsList.First <SchemaDetails>(r => r.schemaName == ty.Name && r.assemblyFullyQualifiedName == ty.Assembly.FullName);
                            originalSchemaObj = originalSchemDetailsList.First <SchemaDetails>(r => r.schemaName == 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);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            string message = $"ERROR! Problem reading schema content from SchemaBase and Writing the file to Local Folder. \nSchema:{schemaObj.schemaFullName} \nAssembly:{schemaObj.assemblyFullyQualifiedName} \nErrorMessage: {e.Message}";
                            TraceProvider.WriteLine($"{message} \nStackTrace:{e.StackTrace}");
                            //Console.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);
            }
        }