public string GenerateReport(string fileName, ReportType reportType, ObjectScriptHashReportData reportData) { MemoryStream ms = new MemoryStream(); StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) { System.Xml.Serialization.XmlSerializer xmlS = new System.Xml.Serialization.XmlSerializer(typeof(ObjectScriptHashReportData)); xmlS.Serialize(sw, reportData); sw.Flush(); sw.Close(); } if (fileName.Length > 0) { XmlTextReader xsltReader; XslCompiledTransform trans = new XslCompiledTransform(); StringReader sr = new StringReader(sb.ToString()); StringReader xsltText; XmlTextReader xmlReader = new XmlTextReader(sr); XPathDocument xPathDoc = new XPathDocument(xmlReader); using (XmlTextWriter fileWriter = new XmlTextWriter(fileName, Encoding.UTF8)) { string extension = Path.GetExtension(fileName).ToLower(); switch (reportType) { case ReportType.Summary: xsltText = new StringReader(Properties.Resources.DatabaseDiff_Summary); xsltReader = new XmlTextReader(xsltText); trans.Load(xsltReader); trans.Transform(xPathDoc, null, fileWriter); break; case ReportType.XML: byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); string utf8String = System.Text.Encoding.UTF8.GetString(utf8Bytes); utf8String = utf8String.Replace("utf-16", "utf-8"); fileWriter.WriteRaw(utf8String); break; } } } return(sb.ToString()); }
public ObjectScriptHashReportData GetObjectHashes(ref BackgroundWorker bgWorker, string fileName, ReportType reportType, bool runThreaded) { this.bgWorker = bgWorker; int threadTotal = 0; string db; bool baseLineSet = false; foreach (ServerData srv in multiDbData) { srv.OverrideSequence.Sort(); //sort so the sequence is in proper order. foreach (string sequenceKey in srv.OverrideSequence.Keys) { foreach (DatabaseOverride ovr in srv.OverrideSequence[sequenceKey]) { db = srv.ServerName + "." + ovr.OverrideDbTarget; if (!dbsSelected.Contains(db)) { threadTotal++; lock (HashCollector.SyncObj) { HashCollector.SyncObj.WorkingRunners++; } HashCollectionRunner runner = new HashCollectionRunner(srv.ServerName, ovr.OverrideDbTarget); runner.HashCollectionRunnerUpdate += new HashCollectionRunner.HashCollectionRunnerUpdateEventHandler(runner_HashCollectionRunnerUpdate); if (!baseLineSet) //set the baseline to the first database handled. { runner.IsBaseLine = true; baseLineSet = true; } runners.Add(runner); if (runThreaded) { System.Threading.ThreadPool.QueueUserWorkItem(ProcessThreadedHashCollection, runner); } else { runner.CollectHashes(); } } } } } if (runThreaded) { int counter = 0; while (HashCollector.SyncObj.WorkingRunners > 0) { System.Threading.Thread.Sleep(100); counter++; if (bgWorker != null && (counter % 2 == 0)) { bgWorker.ReportProgress(HashCollector.SyncObj.WorkingRunners, String.Format("Threads remaining: {0}", HashCollector.SyncObj.WorkingRunners.ToString())); } } } if (bgWorker != null) { bgWorker.ReportProgress(0, "Collating Results..."); } List <ObjectScriptHashData> hashes = new List <ObjectScriptHashData>(); ObjectScriptHashData baseLine = null; foreach (HashCollectionRunner runner in this.runners) { if (runner.IsBaseLine) { baseLine = runner.HashData; } else { hashes.Add(runner.HashData); } } ObjectScriptHashReportData reportData = ProcessHashDifferences(baseLine, hashes); GenerateReport(fileName, reportType, reportData); hashes.Add(baseLine); ObjectScriptHashReportData rawReportData = new ObjectScriptHashReportData(); rawReportData.ProcessTime = reportData.ProcessTime; rawReportData.DatabaseData = hashes; return(rawReportData); }