public List <Regulation> ParseReg() { List <Regulation> regs = new List <Regulation>(); List <string> meta; List <List <string> > metameta; List <List <string> > keys = InterpretFormat(format); Console.WriteLine("PARSING " + subscription); foreach (String file in Directory.EnumerateFiles(dir, "*-01.xml")) // each feed file { try { meta = InterpretMeta(file); PrintList(meta); metameta = InterpretFeed(file); PrintNestedList(metameta); Console.WriteLine("PARSING " + subscription + "/" + file); Console.WriteLine("Entries count: " + metameta.Count); // for (int i = 2; i < metameta.Count; i++) // but what happens if metameta is 1 or 2? for (int i = 2; i < (metameta.Count + 2); i++) { Guid g = Guid.NewGuid(); //Console.WriteLine("Should be reading > " + dir + meta[1] + "-" + i.ToString("D2") + ".xml"); Regulation r = new Regulation(dir + meta[1] + "-" + i.ToString("D2") + ".xml", subscription, keys.Count, metameta[i - 2][0]); foreach (string mitem in meta) { r.addMeta(mitem); } foreach (string mmitem in metameta[i - 2]) { r.addMeta(mmitem); } for (int j = 0; j < keys.Count; j++) { r.setColumn(j, r.ParseByKey(keys[j])); } regs.Add(r); } } catch (FileNotFoundException f) { Console.WriteLine(f); } } return(regs); }
public bool ParseReg() { bool feedback = true; string status_msg = ""; try { Globals.LOG.AppendLocation(); List <Regulation> regs = new List <Regulation>(); List <string> file_content; //List<List<string>> content_content; List <List <string> > keys = InterpretRegDefinition(); SqlCommand SqlComm; // identify feed //string file = Directory.EnumerateFiles(dir, "*-1.xml").Last(); string file = dir + "/" + (from f in new DirectoryInfo(dir).GetFiles("*-1.xml") orderby f.LastWriteTime descending select f).First().ToString(); string origin_num = Path.GetFileNameWithoutExtension(GetOriginPath(file, 1)); Console.WriteLine("\n\t text = " + origin_num); // record file start Guid file_g = Guid.NewGuid(); string origin_name = Path.GetFileNameWithoutExtension(file); SqlComm = new SqlCommand("INSERT INTO " + Globals.FILE_TABLE + "(Tbl_id, Subscription_id, File_name, Start_Time) VALUES(@guid, @sub, @file, @start)", Globals.CONNECTION); SqlComm.Parameters.AddWithValue("@guid", file_g); SqlComm.Parameters.AddWithValue("@sub", user_content[0]); SqlComm.Parameters.AddWithValue("@file", origin_num); SqlComm.Parameters.AddWithValue("@start", DateTime.Now.ToString("yyyy-MM-dd h:mm:ss.ff tt")); SqlComm.ExecuteNonQuery(); try { file_content = InterpretTable(file); //https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.load?view=netframework-4.8 XDocument xdoc = XDocument.Load(file); IEnumerable <XElement> entries = xdoc.Descendants(file_definition[file_definition.Count - 1]); // tag "entry" //IEnumerable<XElement> entries = StreamAxis(file, file_definition[file_definition.Count - 1]); if (file_content.Count <= 0 || entries.Count() <= 0) { Globals.LOG.AppendMessage("Consider defining a new structure for " + user_content[0] + "/" + Path.GetFileName(file)); status_msg = "Consider defining new structure"; } else { int entry_index = 0; int entry_num = 2; foreach (XElement entry in entries) { try { List <string> entry_meta = new List <string>(); for (int i = 0; i < content_definition.Count; i++) { entry_meta.Add(entries.Descendants(content_definition[i]).ElementAt(entry_index).Value); } Regulation r; if (entry_meta[2].Contains(Globals.DELETE_VAL)) // marked for deletion { Console.Write("\t\t" + entry_meta[0] + " --> " + entry_meta[2]); r = new Regulation(null, keys.Count); // fill fields foreach (string item in file_content) { r.AddFileItem(item); } foreach (string item in entry_meta) { r.AddContentItem(item); } foreach (string item in user_content) { r.AddUserItem(item); } for (int j = 0; j < keys.Count; j++) { r.SetRegulationItem(j, ""); } try { Console.Write(" --> " + Program.CreateInsertCommand(Globals.CONTENT_TABLE, Globals.RELATIONSHIP_MAP, r, Globals.CONNECTION).ExecuteNonQuery()); Console.Write(" --> commit delete item \n"); } catch (Exception e) { Globals.LOG.AppendException(e, false); Console.Write(" --> delete item caused " + e.GetType() + "\n"); } } else { // generate path string entry_path = dir + origin_num + "-" + entry_num + ".xml"; string content_id = entry_meta[0]; r = new Regulation(entry_path, keys.Count); // fill fields foreach (string item in file_content) { r.AddFileItem(item); } foreach (string item in entry_meta) { r.AddContentItem(item); } foreach (string item in user_content) { r.AddUserItem(item); } for (int j = 0; j < keys.Count; j++) { r.SetRegulationItem(j, r.ParseByKey(keys[j])); } Console.Write("\t\t xml = " + Path.GetFileName(entry_path) + " --> " + entry_meta[2] + " --> " + r.GetFileItem(1)); // check with delete filter if (r.GetRegulationItem(12).Contains(content_id)) // delete feed items & misaligned contents { try { Console.Write(" --> " + Program.CreateInsertCommand(Globals.CONTENT_TABLE, Globals.RELATIONSHIP_MAP, r, Globals.CONNECTION).ExecuteNonQuery()); Console.Write(" --> commit matched item \n"); } catch (Exception e) { Globals.LOG.AppendException(e, false); Console.Write(" --> match item caused " + e.GetType() + "\n"); } } else if (r.GetRegulationItem(Globals.DELETE_FIL).Trim() != "") // not a feed item { Globals.LOG.AppendMessage("Misalignment with feed item " + content_id + " and " + ((r.GetRegulationItem(12).Trim() == "") ? "undefined" : r.GetRegulationItem(12)) + " in " + user_content[0] + "/" + Path.GetFileNameWithoutExtension(entry_path)); status_msg = "Feed-content misalignment starting with " + Path.GetFileName(entry_path); feedback = false; Console.Write(" --> non-feed item caused misalignment error \n"); } // mark as processed if (Globals.KEEP_XML) { TransferFile(entry_path, Globals.PROCESSED_DIR_XML + "/" + user_content[0] + "/" + Path.GetFileName(entry_path)); } File.Delete(entry_path); if (!feedback) { //if (Globals.TRANSFER) { TransferFile(GetOriginPath(file, 1), Globals.PROCESSED_DIR_TXT + "/" + user_content[0] + "/" + origin_num + ".txt"); } if (Globals.KEEP_XML) { TransferFile(file, Globals.PROCESSED_DIR_XML + "/" + user_content[0] + "/" + Path.GetFileName(file)); } File.Delete(file); Globals.LOG.AppendMessage("TERMINATING SUBSCRIPTION " + user_content[0] + " PREMATURELY"); SqlComm = new SqlCommand("UPDATE " + Globals.FILE_TABLE + " SET Message=@msg WHERE Tbl_id=@guid", Globals.CONNECTION); SqlComm.Parameters.AddWithValue("@guid", file_g); SqlComm.Parameters.AddWithValue("@msg", status_msg); SqlComm.ExecuteNonQuery(); return(false); } entry_num++; } } catch (Exception e) { Globals.LOG.AppendException(e, false); Console.WriteLine(e); } entry_index++; } // transfer processed if (Globals.TRANSFER) { TransferFile(GetOriginPath(file, 1), Globals.PROCESSED_DIR_TXT + "/" + user_content[0] + "/" + origin_num + ".txt"); } if (Globals.KEEP_XML) { TransferFile(file, Globals.PROCESSED_DIR_XML + "/" + user_content[0] + "/" + Path.GetFileName(file)); } File.Delete(file); } } catch (Exception f) { //if (Globals.TRANSFER) { TransferFile(GetOriginPath(file, 1), Globals.PROCESSED_DIR_TXT + "/" + user_content[0] + "/" + origin_num + ".txt"); } Globals.LOG.AppendException(f, true); Console.WriteLine(f); status_msg = "Nonfatal:" + f.GetType().ToString(); } // record file end SqlComm = new SqlCommand("UPDATE " + Globals.FILE_TABLE + " SET End_Time=@end, Message=@msg WHERE Tbl_id=@guid", Globals.CONNECTION); SqlComm.Parameters.AddWithValue("@guid", file_g); SqlComm.Parameters.AddWithValue("@end", DateTime.Now.ToString("yyyy-MM-dd h:mm:ss.ff tt")); SqlComm.Parameters.AddWithValue("@msg", status_msg); SqlComm.ExecuteNonQuery(); } catch (Exception e) { Globals.LOG.AppendException(e, false); } return(true); }