/// <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);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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);
            }
        }