using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; //using log4net; using MongoDB.Driver; using System.IO; using Newtonsoft.Json.Linq; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; namespace AIAHighlight.DBSave { [BsonIgnoreExtraElements] public class BodyRegionImage { public ObjectId _id { get; set; } public string imageName; public int bodyViewIndex; public int layerNumber; public int bodyRegionId; public int zoom; public List terms; public BodyRegionImage() { this.terms = new List(); } } public class Term { public int TermNumber; public List SystemNumbers; public List PixelLocations; public int ActualTermNumber; public Term(int termNumber, int systemNumber, int pixelLocation) { this.SystemNumbers = new List(); this.PixelLocations = new List(); this.TermNumber = termNumber; this.SystemNumbers.Add(systemNumber); this.PixelLocations.Add(pixelLocation); } } public class Program { //public JObject data { get; set; } public Dictionary termList; internal void saveImageData(JObject data) { // DateTime datetime = new DateTime(); Console.WriteLine("AT "+ DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz")+"..In saveImageData. for imageName: "+ data["imageName"].ToString()+ ",bodyViewIndex= "+ data["bodyViewIndex"]+ ",layerNumber: "+ data["layerNumber"]+ ",bodyRegionId= "+ data["bodyRegion"]); try { BodyRegionImage bodyRegionImage = new BodyRegionImage(); // var client = new MongoClient("mongodb://192.168.90.43:27017"); var client = new MongoClient("mongodb://192.168.90.43:27017"); var db = client.GetDatabase("aia"); var col = db.GetCollection("DAImages"); // var list = col.Find(Builders.Filter.Eq("imageName", image["imageName"].ToString())).ToList(); JObject imageData = data; var projection = Builders.Projection.Include("_id"); FilterDefinition[] filterCondition = { Builders.Filter.Eq("imageName", data["imageName"].ToString()), Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])), Builders.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])), Builders.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])), Builders.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))}; var list = col.Find(Builders.Filter.And(filterCondition)).Project(projection).ToList(); //FilterDefinition[] filterCondition = { Builders.Filter.Eq("imageName", data["imageName"].ToString()), // Builders.Filter.Eq("bodyViewIndex", Convert.ToInt32(data["bodyViewIndex"])), // Builders.Filter.Eq("layerNumber", Convert.ToInt32(data["layerNumber"])), // Builders.Filter.Eq("bodyRegionId", Convert.ToInt32(data["bodyRegion"])), // Builders.Filter.Eq("zoom", Convert.ToInt32(data["zoom"]))}; //var list = col.Find(Builders.Filter.And(filterCondition)).ToList(); if (list.Count > 0) return; Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..record not found."); string bodyViewIndex = data["bodyViewIndex"].ToString(); // string termFilePath = "D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; // string termFilePath = " D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; string path = @"\\ADAMSRVR\dist\assets\data\json\da\body-views\" + bodyViewIndex+"\\"; string fileName = "da_dat_body_system_term_" + bodyViewIndex + ".json"; string termFilePath = " D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight\\dist\\assets\\data\\json\\da\\body-views\\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; string systemTermMapping; // using (StreamReader reader = new StreamReader(termFilePath)) using (StreamReader reader = new StreamReader(path+fileName)) { systemTermMapping = reader.ReadToEnd(); } JObject systemTermMappingObject = JObject.Parse(systemTermMapping); Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + ".systemTermMappingObject craeted."); dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; dynamic imageDataArray = data["data"]; Dictionary termList = new Dictionary(); Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray"); for (int i = 0; i < imageDataArray.Count; i += 4) { var RED = Convert.ToInt32(imageDataArray[i]) - 9; var GREEN = Convert.ToInt32(imageDataArray[i + 1]) - 9; var BLUE = Convert.ToInt32(imageDataArray[i + 2]) - 9; if (RED == -9 && GREEN == -9 && BLUE == -9) continue; var Red = ""; var Green = ""; var Blue = ""; string zero = "0"; if ((RED).ToString().Length != 2) { Red = zero + RED.ToString(); } else { Red = RED.ToString(); } if ((GREEN).ToString().Length != 2) { Green = zero + GREEN.ToString(); } else { Green = GREEN.ToString(); } if ((BLUE).ToString().Length != 2) { Blue = zero + BLUE.ToString(); } else { Blue = BLUE.ToString(); } string Icolor = (Red + Green + Blue); // alert('Icolor: ' + Icolor) foreach (dynamic systemTerm in systemTerms) { int iColor = Convert.ToInt32(Icolor); int termNumber = Convert.ToInt32(systemTerm._TermNumber.ToString()); int systemNumber = Convert.ToInt32(systemTerm._SystemNumber.ToString()); if (systemTerm._TermNumber.ToString() == Icolor) { this.AddToTermList(termNumber, systemNumber, i, termList); //new Program().AddToTermList(termNumber, systemNumber, i,termList); //new Program().AddToTermList(termNumber, systemNumber, i + 1, termList); //new Program().AddToTermList(termNumber, systemNumber, i + 2, termList); this.AddToTermList(termNumber, systemNumber, i, termList); this.AddToTermList(termNumber, systemNumber, i + 1, termList); this.AddToTermList(termNumber, systemNumber, i + 2, termList); } } } Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList."); bodyRegionImage.imageName = data["imageName"].ToString(); bodyRegionImage.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); bodyRegionImage.layerNumber = Convert.ToInt32(data["layerNumber"]); bodyRegionImage.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); bodyRegionImage.zoom = Convert.ToInt32(data["zoom"]); foreach (int key in termList.Keys) { bodyRegionImage.terms.Add(termList[key]); } Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "...inserting for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); col.InsertOne(bodyRegionImage); Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..inserted for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); data = null; termList = null; systemTermMappingObject = null; systemTerms = null; } catch(Exception ex) { Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..Exception occured for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]+"... EXCEPTION= "+ex.Message+".. STACKTRACE= "+ex.StackTrace); } } public void AddToTermList(int termNumber, int systemNumber, int pixelLocation,Dictionary termList) { if (!termList.ContainsKey(termNumber)) { termList.Add(termNumber, new Term(termNumber, systemNumber, pixelLocation)); } else { bool isSystemNumberExisting = false; foreach (int sn in termList[termNumber].SystemNumbers) { if (systemNumber == sn) { isSystemNumberExisting = true; break; } } if (!isSystemNumberExisting) termList[termNumber].SystemNumbers.Add(systemNumber); bool isPixelLocationExisting = false; foreach (int pl in termList[termNumber].PixelLocations) { if (pixelLocation == pl) { isPixelLocationExisting = true; break; } } if (!isPixelLocationExisting) termList[termNumber].PixelLocations.Add(pixelLocation); } } //static void saveImageData() //{ //} static void Main(string[] args) { Console.WriteLine("Enter BodyViewIndex"); string bodyViewId = Console.ReadLine(); string searchPattern = "BodyView" + bodyViewId+"_layer*"; // string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data"); // string[] files = Directory.GetFiles("D:\\ADAM\\100-Projects\\001-AIAHighlight.FileSave\\AIAHighlight.Serevr\\AIAHighlight\\data", "*.json"); // string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data",searchPattern); string[] files = Directory.GetFiles(@"\\ADAMSRVR\data", searchPattern); Program program; foreach (string filePath in files) { program = new Program(); program.getDataFromFileAndSaveInDB(filePath); program = null; } } internal void getDataFromFileAndSaveInDB(string filePath) { if (File.Exists(filePath)) { string fileData; using (StreamReader reader = new StreamReader(filePath)) { fileData = reader.ReadToEnd(); JObject data = JObject.Parse(fileData); this.saveImageData(data); } } } } }