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, int bodyviewId) { // 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(); var db = client.GetDatabase("AIADB_bodyviewId"+bodyviewId.ToString()); 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(); //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 = @"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\bodyViewsData\body-views\" + bodyViewIndex + "\\da_dat_body_system_term_" + bodyViewIndex + ".json"; string termFilePath = @"D:\100-PROJECTS\008-AIAHighlight.FileSave\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 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 created."); dynamic systemTerms = systemTermMappingObject["BodySystem"]["BodySystemTerm"]; dynamic imageDataArray = data["data"]; //int[] imageDataIntArray = (int[])imageDataArray; //int numToRemove = 0; //dynamic imageDataWithoutZero = imageDataIntArray.Where(val => val != numToRemove).ToArray(); Dictionary termList = new Dictionary(); Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..before itrating imageDataArray. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); 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) if(Icolor=="000000") 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); this.AddToTermList(termNumber, systemNumber, i, termList); this.AddToTermList(termNumber, systemNumber, i + 1, termList); this.AddToTermList(termNumber, systemNumber, i + 2, termList); break; } } } Console.WriteLine("AT " + DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz") + "..after craeting termList.. for imageName: " + data["imageName"].ToString() + ",bodyViewIndex= " + data["bodyViewIndex"] + ",layerNumber: " + data["layerNumber"] + ",bodyRegionId= " + data["bodyRegion"]); 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"]); var logCol = db.GetCollection("DAImageLogs"); BodyRegionImage bodyRegionImageTolog = new BodyRegionImage(); bodyRegionImageTolog.bodyViewIndex = Convert.ToInt32(data["bodyViewIndex"]); bodyRegionImageTolog.layerNumber = Convert.ToInt32(data["layerNumber"]); bodyRegionImageTolog.bodyRegionId = Convert.ToInt32(data["bodyRegion"]); bodyRegionImageTolog.zoom = Convert.ToInt32(data["zoom"]); logCol.InsertOne(bodyRegionImageTolog); bodyRegionImageTolog = null; 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 Main(string[] args) //{ // Console.WriteLine("Enter BodyViewIndex"); // string bodyViewId = Console.ReadLine(); // string searchPattern = "BodyView" + bodyViewId+"_layer*"; // string[] files = Directory.GetFiles(@"\\ADAMSRVR\data", searchPattern); // Program program; // foreach (string filePath in files) // { // program = new Program(); // program.getDataFromFileAndSaveInDB(filePath); // program = null; // } //} static void Main(string[] args) { //string[] files = Directory.GetFiles("D:\\100-PROJECTS\\008-AIAHighlight.FileSave\\AIAHighlight\\AIAHighlight\\data", "*.json"); //string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data", "*.json"); Console.WriteLine("Enter BodyViewId"); int BodyViewIndex = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter LayerNumber"); string layerNumber = Console.ReadLine(); int totalLayers = 0; switch (BodyViewIndex) { case 1: totalLayers = 330; break; case 2: totalLayers = 288; break; case 3: totalLayers = 127; break; case 4: totalLayers = 186; break; case 5: totalLayers = 229; break; case 6: totalLayers = 289; break; case 7: totalLayers = 130; break; case 8: totalLayers = 186; break; case 9: totalLayers = 64; break; case 10: totalLayers = 72; break; case 11: totalLayers = 64; break; case 12: totalLayers = 72; break; } for (int i = Convert.ToInt32(layerNumber); i <= totalLayers; i++) { string searchPattern = "BodyView" + BodyViewIndex + "_layer" + i + "*"; // string searchPattern = "BodyView" + bodyViewId + "_layer*"; string[] files = Directory.GetFiles(@"D:\100-PROJECTS\008-AIAHighlight.FileSave\data", searchPattern); // string[] files = Directory.GetFiles(@"C:\ADAM-AWS-FTP\ADAM-AWS\AIAHTML5\data\", searchPattern); Program program = new Program(); program.processFiles(files, BodyViewIndex); program = null; } } internal void processFiles(string[] files,int bodyviewId) { //with multithreading // prepareArrayOfFiles(files, startIndex, endIndex); //without multithreading foreach(string filename in files) { getDataFromFileAndSaveInDB(filename, bodyviewId); } } //internal void prepareArrayOfFiles(string[] files,int startIndex, int endIndex) //{ // string[] filesToProcess = new string[20]; // int processedFiles = 1; // int j = 0; // for (int i = startIndex; i <= endIndex; i++) // { // filesToProcess[j] = files[i]; // j++; // } // Parallel.ForEach(filesToProcess, (currentFile) => // { // if (currentFile != null) // { // using (StreamReader reader = new StreamReader(currentFile)) // { // getDataFromFileAndSaveInDB(currentFile); // processedFiles++; // } // if (processedFiles == 20) // { // startIndex = endIndex + 1; // endIndex = startIndex + 19; // filesToProcess = null; // //in case after adding 20, the endIndex reaches more than length of files array(total files) // if (endIndex > files.Length - 1) // { // // int remainingFiles = endIndex - files.Length; // endIndex = files.Length - 1; // } // if (endIndex < files.Length) // { // processFiles(files, startIndex, endIndex); // } // //} // } // } // } // ); //} internal void getDataFromFileAndSaveInDB(string filePath,int bodyViewId) { if (File.Exists(filePath)) { string fileData; using (StreamReader reader = new StreamReader(filePath)) { fileData = reader.ReadToEnd(); JObject data = JObject.Parse(fileData); this.saveImageData(data, bodyViewId); } } else { Console.WriteLine("FILE.NOT.FOUND AT" + ": " + filePath); } } } }