/*** * Parse a fil with BidFile properties passed */ public static BidFile ParseFile(BidFile bf, String filename) { String content = File.ReadAllText(filename); String header2 = ""; String header1Match = "PM, (.*), (.*), TCE, APITCE01, , (.*);"; String header2Match = @"((.*),\s*,* (.*), (.*), , (.*), (.*), (.*);)|((.*), (.*),\s+, (.*), (.*), (.*);)"; String bodyMatch = @"(\d+(-)*\d*).*({.*})+,+(.*);"; String mPqrr = @"({.*})"; var applicationType = ""; var marketType = ""; var deliveryDate = ""; var participantId = ""; var userId = ""; var mode = ""; var bidType = ""; var resourceId = ""; var reserveClass = ""; var opresRampRate = ""; var cancelFlag = ""; var bids = new List<BidFileDetail>(); var pwpslist = new List<PriceQuantityPair>(); var rrlist = new List<RRDetail>(); foreach (var line in content.Split('\n')) { Match mHeader1 = Regex.Match(line, header1Match); Match mHeader2 = Regex.Match(line, header2Match); Match mBody = Regex.Match(line, bodyMatch); Match pqrrMatch = Regex.Match(line, mPqrr); var hourRange = ""; if (mHeader1.Success) { applicationType = "PM"; marketType = mHeader1.Groups[1].ToString(); deliveryDate = mHeader1.Groups[2].ToString(); participantId = "TCE"; userId = "APITCE01"; mode = mHeader1.Groups[3].ToString(); bidType = ""; resourceId = ""; reserveClass = ""; opresRampRate = ""; cancelFlag = ""; } else if (mHeader2.Success) { bidType = mHeader2.Groups[2].Value; if (!bidType.Equals("")) { resourceId = mHeader2.Groups[3].ToString(); opresRampRate = mHeader2.Groups[5].ToString(); cancelFlag = mHeader2.Groups[6].ToString(); } else { bidType = mHeader2.Groups[9].ToString(); resourceId = mHeader2.Groups[10].ToString(); reserveClass = mHeader2.Groups[11].ToString(); cancelFlag = mHeader2.Groups[12].ToString(); } } else if (mBody.Success) { // ReSharper disable once SuggestUseVarKeywordEvident BidFileDetail t = new BidFileDetail(); // Header 1 Info t.BidFileId = bf.BidFileId; t.ApplicationType = applicationType; t.MarketType = marketType; t.DeliveryDate = deliveryDate; t.ParticipantId = participantId; t.UserId = userId; t.Mode = mode; // Header 2 info t.BidFileDetailId = Guid.NewGuid(); t.BidType = bidType; t.ResourceId = resourceId; t.OpresRampRate = opresRampRate; t.CancelFlag = cancelFlag; t.ReserveClass = reserveClass; // Bid Body Info hourRange = mBody.Groups[1].Value; var pqps = pqrrMatch.Groups[1].Value; t.ReserveLoadingPoint = mBody.Groups[4].Value; // If the value in ReserveLoadingPoint at this point is just // a comma, we know that there was no reserve loading point. if (t.ReserveLoadingPoint.Contains(",")) { t.ReserveLoadingPoint = null; } // Misc System properties t.CreatedBy = "TCPL\\jon_gaudette"; t.CreatedDateTime = DateTime.Now; /* * If the hourRange contains a hyphen we need to seperate out each hour into their own * records, to ensure we have 24 hours of data for each category. Everythign will be * identical with the exception of the GUID and Hour. */ if (hourRange.Contains("-")) { var from = Int32.Parse(hourRange.Split('-')[0]); var to = Int32.Parse(hourRange.Split('-')[1]); for (var i = from; i <= to; i++) { var bfd = new BidFileDetail { BidFileDetailId = Guid.NewGuid(), BidFileId = bf.BidFileId, Hour = i, ApplicationType = t.ApplicationType, MarketType = t.MarketType, DeliveryDate = t.DeliveryDate, ParticipantId = t.ParticipantId, UserId = t.UserId, Mode = t.Mode, BidType = t.BidType, ResourceId = t.ResourceId, OpresRampRate = t.OpresRampRate, CancelFlag = t.CancelFlag, ReserveClass = t.ReserveClass, ReserveLoadingPoint = t.ReserveLoadingPoint, CreatedBy = "TCPL\\jon_gaudette", CreatedDateTime = DateTime.Now, }; DetectAndParsePairs(pqps, bfd, pwpslist, rrlist); bids.Add(bfd); } } else { t.Hour = Int32.Parse(hourRange); DetectAndParsePairs(pqps, t, pwpslist, rrlist); bids.Add(t); } } } // Write our entity objects to the database. var db = new TBSPEntities1(); db.BidFiles.Add(bf); db.BidFileDetails.AddRange(bids); db.PriceQuantityPairs.AddRange(pwpslist); db.RRDetails.AddRange(rrlist); db.SaveChanges(); return bf; }
/* * Method only valid for prefilling database */ public static void ProcessFolder(String folder) { string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); filepath = filepath + @"\toronto_bid_tool\their_network_drive\Offers\2014\" + folder; var d = new DirectoryInfo(filepath); foreach (var dir in d.GetDirectories()) { foreach (var file in dir.GetFiles("*.txt")) { Match m = Regex.Match(file.Name, @"(\d\d\d\d\d\d\d\d) - (\d\d\d\d\d\d\d\d) - (\d\d\d\d) - (\w+) - (DACP|HE\d+).txt"); if (m.Success) { var bf = new BidFile { BidFileId = Guid.NewGuid(), CreatedDateTime = file.CreationTime, Filename = file.Name, DeliveryDate = m.Groups[1].Value, CreatedBy = m.Groups[4].Value, Market = m.Groups[5].Value }; Console.WriteLine("Parsing " + file.Name); Console.WriteLine("Parsed: " + ParseFile(bf, file.FullName)); } else { Console.WriteLine("Skipping " + file.Name); } } } }
/*** * Parse a file with BidFile properties extracted from filename */ public static BidFile ParseFile(String filename) { Match m = Regex.Match(filename, @"(\d\d\d\d\d\d\d\d) - (\d\d\d\d\d\d\d\d) - (\d\d\d\d) - (\w+) - (DACP|HE\d+).txt"); if (filename != null && m.Success) { var f = new FileInfo(filename); var bf = new BidFile { BidFileId = Guid.NewGuid(), CreatedDateTime = f.CreationTime, Filename = f.Name, DeliveryDate = m.Groups[1].Value, CreatedBy = m.Groups[4].Value, Market = m.Groups[5].Value }; Console.WriteLine("Parsing " + filename); return ParseFile(bf, filename); } return null; }