private void btnImport_Click(object sender, EventArgs e) { DbUtilities dbu = new DbUtilities(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JSON files (*.json)|*.json"; DialogResult result = ofd.ShowDialog(); json_path = ofd.FileName; if (json_path.Trim() == "" || result != DialogResult.OK) { return; } //init lblImpFile.Text = "Αρχείο: -"; objList.Clear(); vtObjList.Clear(); stationsObjList.Clear(); dgvReceiptData.Rows.Clear(); json_filename = ""; //json_path = ""; if (fileBytes != null) { Array.Clear(fileBytes, 0, fileBytes.Length); } counters.Clear(); Output.WriteToFile("***** STARTING... *****"); Output.WriteToFile("-- Import Function --"); Output.WriteToFile("Filename: " + json_path); string read_data = dbu.getAllDataFromJsonFile(json_path); if (read_data.Trim() == "") { Output.WriteToFile("Empty file!"); return; } //get byte[] ready to import into table // VehicleTrace --> ImpData_And_VehicleTrace AllData = dbu.JsonToMultipleObject(read_data); List <ImpData> DataToMigrateNew = AllData.impData; List <VehicleTrace> VehicleTraceToMigrate = AllData.vehicleTrace; vtObjList = VehicleTraceToMigrate; List <Station> StationToMigrate = AllData.stationData; stationsObjList = StationToMigrate; // VehicleTrace <-- List <ImpData> DataToMigrate = dbu.JsonToObjectList(read_data); // VehicleTrace --> DataToMigrate = DataToMigrateNew; // VehicleTrace <-- counters.fileRows = DataToMigrate.Count; counters.fileAccRows = DataToMigrate.Count(i => i.accepted == true); counters.fileNAccRows = DataToMigrate.Count(i => i.accepted == false); Output.WriteToFile("Rows in file: " + counters.fileRows.ToString()); Output.WriteToFile("File - Accepted=true Rows: " + counters.fileAccRows.ToString() + ", Accepted=false Rows: " + counters.fileNAccRows.ToString()); objList = dbu.GetDataNotExistsInSQLSrvTable(DataToMigrate); counters.toSaveRows = objList.Count; Output.WriteToFile("Rows to save: " + objList.Count.ToString()); json_filename = json_path.Substring(json_path.LastIndexOf("\\") + 1); //get file contents as byte[] fileBytes = System.IO.File.ReadAllBytes(json_path); if (objList.Count > 0) { counters.toSaveAccRows = objList.Count(i => i.accepted == true); counters.toSaveNAccRows = objList.Count(i => i.accepted == false); Output.WriteToFile("To save - Accepted=true Rows: " + counters.toSaveAccRows.ToString() + ", Accepted=false Rows: " + counters.toSaveNAccRows.ToString()); counters.vehicleTraceRows = vtObjList.Count; counters.stationsUpdRows = stationsObjList.Count; List <object[]> ObjRows = GridViewUtils.ImpDataListToGridViewRowList(objList, true); GridViewUtils.ShowDataToDataGridView(dgvReceiptData, ObjRows); lblImpFile.Text = "Αρχείο: " + json_filename; } else { MessageBox.Show("Δε βρέθηκαν εγγραφές προς επεξεργασία! \r\n" + "Παρακαλώ μετακινήστε στο φάκελο αρχειοθέτησης το αρχείο " + json_path + "."); } }
private void AutomaticProcedure() { Output.WriteToFile("***** STARTING... *****"); //get all json files from folder string targetDirectory = Application.StartupPath + "\\Import"; string[] fileEntries = Directory.GetFiles(targetDirectory, "*.json"); Output.WriteToFile(fileEntries.Length + " file(s) found."); int cnt = 0; foreach (string fileName in fileEntries) { cnt++; Output.WriteToFile("Starting - File " + cnt.ToString() + "/" + fileEntries.Length); //init lblImpFile.Text = "Αρχείο: -"; objList.Clear(); vtObjList.Clear(); stationsObjList.Clear(); json_filename = ""; if (fileBytes != null) { Array.Clear(fileBytes, 0, fileBytes.Length); } counters.Clear(); DbUtilities dbu = new DbUtilities(); dbu.automaticMode = true; json_path = fileName; Output.WriteToFile("Filename: " + json_path); string read_data = dbu.getAllDataFromJsonFile(json_path); //get file contents as byte[] fileBytes = System.IO.File.ReadAllBytes(json_path); json_filename = json_path.Substring(json_path.LastIndexOf("\\") + 1); //import the whole file into sql DB table - imported group bool success = dbu.InsertImportedFileIntoTable(json_filename, fileBytes, counters, false); Output.WriteToFile("Saved file: " + json_filename); try { System.IO.File.Move(json_path, Application.StartupPath + "\\Archive\\" + json_filename); Output.WriteToFile("File moved to 'Archive' folder"); } catch (Exception ex) { Output.WriteToFile(ex.Message, true); } if (!success) { continue; } if (read_data.Trim() == "") { Output.WriteToFile("Empty file!"); continue; } //get max id int ImportedGroupId = dbu.getMaxImportedGroupId(json_filename); if (ImportedGroupId <= 0) //error: -1 (& 0 -> id starts from 1...) { continue; } Output.WriteToFile("ImportedGroupId: " + ImportedGroupId.ToString()); // VehicleTrace --> ImpData_And_VehicleTrace AllData = dbu.JsonToMultipleObject(read_data); List <ImpData> DataToMigrateNew = AllData.impData; List <VehicleTrace> VehicleTraceToMigrate = AllData.vehicleTrace; vtObjList = VehicleTraceToMigrate; List <Station> StationToMigrate = AllData.stationData; stationsObjList = StationToMigrate; // VehicleTrace <-- //List<ImpData> DataToMigrate = dbu.JsonToObjectList(read_data); // VehicleTrace --> List <ImpData> DataToMigrate = DataToMigrateNew; // VehicleTrace <-- counters.fileRows = DataToMigrate.Count; counters.fileAccRows = DataToMigrate.Count(i => i.accepted == true); counters.fileNAccRows = DataToMigrate.Count(i => i.accepted == false); Output.WriteToFile("Rows in file: " + counters.fileRows.ToString()); Output.WriteToFile("File - Accepted=true Rows: " + counters.fileAccRows.ToString() + ", Accepted=false Rows: " + counters.fileNAccRows.ToString()); objList = dbu.GetDataNotExistsInSQLSrvTable(DataToMigrate); counters.toSaveRows = objList.Count; Output.WriteToFile("Rows to save: " + objList.Count.ToString()); if (objList.Count > 0) { counters.toSaveAccRows = objList.Count(i => i.accepted == true); counters.toSaveNAccRows = objList.Count(i => i.accepted == false); } Output.WriteToFile("To save - Accepted=true Rows: " + counters.toSaveAccRows.ToString() + ", Accepted=false Rows: " + counters.toSaveNAccRows.ToString()); counters.vehicleTraceRows = vtObjList.Count; counters.stationsUpdRows = stationsObjList.Count; bool result = dbu.update_ImportedGroup_Counters(ImportedGroupId, counters); Output.WriteToFile("Counters updated."); if (objList.Count <= 0) { continue; } lblImpFile.Text = "Αρχείο: " + json_filename; bool insSuccess = dbu.ObjectList_To_SQLServerReceiptDataLines(objList, ImportedGroupId); // VehicleTrace --> //insert into dbo.VehicleTrace Output.WriteToFile("Inserting Vehicle Trace data into Database. " + vtObjList.Count.ToString() + " records."); dbu.Insert_List_Into_VehicleTrace(vtObjList); // VehicleTrace <-- Output.WriteToFile("Inserting Stations data into Database. " + stationsObjList.Count.ToString() + " records."); dbu.Insert_List_Into_Station(stationsObjList); if (insSuccess) { dbu.update_ImportedGroup_Table(ImportedGroupId); } Output.WriteToFile("Finished - File " + cnt.ToString() + "/" + fileEntries.Length); objList.Clear(); vtObjList.Clear(); stationsObjList.Clear(); Array.Clear(fileBytes, 0, fileBytes.Length); lblImpFile.Text = "Αρχείο: -"; json_filename = ""; counters.Clear(); } Output.WriteToFile("***** FINISHED... *****"); }