Inheritance: DbRepository
 private static void AddSchedule(TiplocRepository tiprep, List<TiplocCode> tiplocs, ScheduleRepository schedrep, dynamic rowData, int retryCount = 0)
 {
     try
     {
         AddSchedule(tiplocs, schedrep, rowData);
     }
     catch (TiplocNotFoundException tnfe)
     {
         TiplocCode t = new TiplocCode
         {
             Tiploc = tnfe.Code
         };
         t.TiplocId = tiprep.InsertTiploc(t.Tiploc);
         tiplocs.Add(t);
         AddSchedule(tiplocs, schedrep, rowData);
     }
     catch (DbException dbe)
     {
         Trace.TraceError(dbe.ToString());
         if (retryCount <= 3)
         {
             Trace.TraceInformation("Retrying");
             AddSchedule(tiprep, tiplocs, schedrep, rowData, ++retryCount);
         }
         else
         {
             Trace.TraceError("Retry count exceeded");
             //throw;
         }
     }
 }
 private static void AddAssociation(dynamic rowData, List<TiplocCode> tiplocs, TiplocRepository tipRep, AssociationRepository aRep)
 {
     try
     {
         Association assoc = AssociationJsonMapper.ParseJsonAssociation(rowData.JsonAssociationV1, tiplocs);
         switch (assoc.TransactionType)
         {
             case TransactionType.Create:
                 Trace.TraceInformation("Inserted Association From UID {0} -> {1}, Type {2}, Indicator {3}",
                     assoc.MainTrainUid, assoc.AssocTrainUid, assoc.AssociationType, assoc.STPIndicator);
                 break;
             case TransactionType.Delete:
                 Trace.TraceInformation("Delete Association UID {0} For {1}",
                     assoc.MainTrainUid, assoc.StartDate);
                 break;
         }
         aRep.AddAssociation(assoc);
     }
     catch (TiplocNotFoundException tnfe)
     {
         TiplocCode t = new TiplocCode
         {
             Tiploc = tnfe.Code
         };
         t.TiplocId = tipRep.InsertTiploc(t.Tiploc);
         tiplocs.Add(t);
         AddAssociation(rowData, tiplocs, tipRep, aRep);
     }
 }
        static void Main(string[] args)
        {
            // this data is fetchable via http://nrodwiki.rockshore.net/index.php/ReferenceData
            string jsonData = File.ReadAllText("CORPUSExtract.json");

            TiplocContainer container = JsonConvert.DeserializeObject<TiplocContainer>(jsonData);

            System.Console.WriteLine("Loaded {0} Tiplocs", container.TIPLOCDATA.Count());

            TiplocRepository tiplocRepo = new TiplocRepository();

            ulong counter = 1;
            foreach (var tiploc in container.TIPLOCDATA)
            {
                System.Console.WriteLine("Processing number {0}", counter);
                if (!string.IsNullOrWhiteSpace(tiploc.STANOX) &&
                    !string.IsNullOrWhiteSpace(tiploc.TIPLOC) &&
                    tiplocRepo.GetTiplocByStanox(tiploc.STANOX) == null)
                {
                    System.Console.WriteLine("Inserting {0}", tiploc.NLCDESC);
                    tiplocRepo.InsertTiploc(tiploc.ToTiplocCode());
                }
                counter++;
            }
        }
        public void TestVSTP()
        {
            const string msg = "{\"VSTPCIFMsgV1\":{\"schemaLocation\":\"http://xml.networkrail.co.uk/ns/2008/Train itm_vstp_cif_messaging_v1.xsd\",\"classification\":\"industry\",\"timestamp\":\"1376123424000\",\"owner\":\"Network Rail\",\"originMsgId\":\"2013-08-10T08:30:24-00:00vstp.networkrail.co.uk\",\"Sender\":{\"organisation\":\"Network Rail\",\"application\":\"TOPS\",\"component\":\"VSTP\"},\"schedule\":{\"schedule_id\":\"\",\"transaction_type\":\"Create\",\"schedule_start_date\":\"2013-08-10\",\"schedule_end_date\":\"2013-08-10\",\"schedule_days_runs\":\"0000010\",\"applicable_timetable\":\"Y\",\"CIF_bank_holiday_running\":\" \",\"CIF_train_uid\":\" 08698\",\"train_status\":\"2\",\"CIF_stp_indicator\":\"N\",\"schedule_segment\":[{\"signalling_id\":\"\",\"uic_code\":\"\",\"atoc_code\":\"\",\"CIF_train_category\":\"\",\"CIF_headcode\":\"\",\"CIF_course_indicator\":\"\",\"CIF_train_service_code\":\"55460180\",\"CIF_business_sector\":\"\",\"CIF_power_type\":\"D\",\"CIF_timing_load\":\"\",\"CIF_speed\":\"\",\"CIF_operating_characteristics\":\"\",\"CIF_train_class\":\"\",\"CIF_sleepers\":\"\",\"CIF_reservations\":\"0\",\"CIF_connection_indicator\":\"\",\"CIF_catering_code\":\"\",\"CIF_service_branding\":\"\",\"CIF_traction_class\":\"\",\"schedule_location\":[{\"scheduled_arrival_time\":\" \",\"scheduled_departure_time\":\"124400\",\"scheduled_pass_time\":\" \",\"public_arrival_time\":\" \",\"public_departure_time\":\"00\",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\" \",\"CIF_activity\":\"TB\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"HMSHBRF\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"125000\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"WACRJN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"130900\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"NNTNABJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"131100\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"NNTN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"131800\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"HINCKLY\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"132430\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"CROFTS\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"133400\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"WGSTNNJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"133800\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"LESTRSJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"134030\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"LESTER\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"135900\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"SYSTNSJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"135930\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"SYSTNEJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"140700\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MLTNFBY\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"141100\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MLTNSDG\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"141830\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MLTNWHI\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"142100\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"OAKHASH\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"142230\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"LAGHJN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"142400\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"OAKHAM\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"142830\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MANTONJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"143200\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MANTLFF\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"143400\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"KETTON\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"144000\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"STAMUFF\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"144330\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"HELPSTN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"144930\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"NENGLNN\"}}},{\"scheduled_arrival_time\":\"145230\",\"scheduled_departure_time\":\"161830\",\"scheduled_pass_time\":\"      \",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"PBROVGB\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"162100\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"PBROEFL\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"162330\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"PBRO\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"162500\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"PBROE\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"163200\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"WHTLSEA\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"164130\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MRCH\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"165730\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"ELYYNJN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"170030\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"ELYY\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"170130\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"ELYYDLN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"171030\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"SOHAM\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"171830\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"CHPNJN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"172200\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"KENNETT\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"173330\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"BSTEDMS\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"175000\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"HAGHLYJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"175200\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"STWMRKT\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"180300\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"IPSWESJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"180430\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"IPSWICH\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"180600\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"IPSWHJN\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"181800\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MANNTNJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"181900\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MANNTEJ\"}}},{\"scheduled_arrival_time\":\"      \",\"scheduled_departure_time\":\"      \",\"scheduled_pass_time\":\"182200\",\"public_arrival_time\":\"      \",\"public_departure_time\":\"      \",\"CIF_platform\":\"\",\"CIF_line\":\"\",\"CIF_path\":\"\",\"CIF_activity\":\"\",\"CIF_engineering_allowance\":\"\",\"CIF_pathing_allowance\":\"\",\"CIF_performance_allowance\":\"\",\"location\":{\"tiploc\":{\"tiploc_id\":\"MISTLEY\"}}},{\"scheduled_arrival_time\":\"183800\",\"scheduled_departure_time\":\" \",\"scheduled_pass_time\":\" \",\"public_arrival_time\":\"00\",\"public_departure_time\":\" \",\"CIF_platform\":\"\",\"CIF_line\":\" \",\"CIF_path\":\"\",\"CIF_activity\":\"TF\",\"CIF_engineering_allowance\":\" \",\"CIF_pathing_allowance\":\" \",\"CIF_performance_allowance\":\" \",\"location\":{\"tiploc\":{\"tiploc_id\":\"PRKSGBR\"}}}]}]}}}";
            ScheduleTrain train = null;

            var tiplocs = new TiplocRepository().GetTiplocs().ToList();

            var data = JsonConvert.DeserializeObject<dynamic>(msg);
            try
            {
                train = VSTPMapper.ParseJsonVSTP(data, tiplocs);
            }
            catch (TiplocNotFoundException tnfe)
            {
                Trace.TraceError("Could not add VSTP: {0}", tnfe);
            }
            catch (Exception e)
            {
                Trace.TraceError("Could not add VSTP: {0}", e);
            }
            Trace.TraceInformation(train.Headcode);
        }
        static void Main(string[] args)
        {
            TraceHelper.SetupTrace();
            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                Trace.TraceError("Unhandled Exception: {0}", e.ExceptionObject);
                TraceHelper.FlushLog();
                Environment.Exit(1);
            };

            var options = new Options();
            CommandLine.CommandLineParser.Default.ParseArguments(args, options);
            bool delete = true;
            var tiprep = new TiplocRepository();
            var tiplocs = tiprep.GetTiplocs().ToList();
            var schedrep = new ScheduleRepository();
            var aRep = new AssociationRepository();

            string tempDir = Path.GetTempPath();
            Trace.TraceInformation("Temporary Directory is {0}", tempDir);
            string gzFile = Path.Combine(tempDir, string.Format("{0:yyyyMMdd}.gz", DateTime.UtcNow));
            string jsonFile = Path.Combine(tempDir, string.Format("{0:yyyyMMdd}.json", DateTime.UtcNow));
            Trace.TraceInformation("GZIP File is {0}", gzFile);
            Trace.TraceInformation("JSON File is {0}", jsonFile);
            bool fail = false;
            try
            {
                if (options.ForceDownload || !File.Exists(gzFile))
                {
                    ScheduleService.DownloadSchedule(gzFile,
                        // defaults to all
                        options.Toc,
                        // defaults to daily
                        options.ScheduleType,
                        // defaults to previous day (e.g. on monday download sunday - http://nrodwiki.rockshore.net/index.php/Schedule)
                        options.Day.HasValue ? options.Day.Value : DateTime.Today.AddDays(-1).DayOfWeek);
                }
                else
                {
                    Trace.TraceInformation("File {0} already exists", gzFile);
                }
                if (options.ForceDownload || !File.Exists(jsonFile))
                {
                    using (FileStream originalFileStream = File.OpenRead(gzFile))
                    {
                        using (FileStream decompressedFileStream = File.Create(jsonFile))
                        {
                            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                            {
                                Console.WriteLine("Decompressing {0} to {1}", gzFile, jsonFile);
                                decompressionStream.CopyTo(decompressedFileStream);
                                Console.WriteLine("Decompressed: {0}", jsonFile);
                            }
                        }
                    }
                }
                else
                {
                    Trace.TraceInformation("File {0} already exists", jsonFile);
                }
                if (File.Exists(jsonFile))
                {
                    foreach (string row in File.ReadLines(jsonFile))
                    {
                        var rowData = JsonConvert.DeserializeObject<dynamic>(row);
                        try
                        {
                            if (rowData.JsonTimetableV1 != null
                                && options.ScheduleType == ScheduleType.DailyUpdate
                                && !options.Day.HasValue)
                            {
                                if (!options.IgnoreWrongDate)
                                {
                                    // defaults to previous day (e.g. on monday download sunday - http://nrodwiki.rockshore.net/index.php/Schedule)
                                    DateTime dateCheck = DateTime.Today;
                                    DateTime unixTs = new DateTime(1970, 1, 1);
                                    DateTime date = unixTs.AddSeconds((double)rowData.JsonTimetableV1.timestamp);
                                    if (date.Date != dateCheck)
                                    {
                                        fail = true;
                                        throw new Exception(string.Format("Time stamp in file is for {0:dd/MM/yyyy} but requested {1:dd/MM/yyyy}",
                                            date, dateCheck));
                                    }
                                }
                            }
                            else if (rowData.JsonScheduleV1 != null)
                            {
                                AddSchedule(tiprep, tiplocs, schedrep, rowData);
                            }
                            else if (rowData.JsonAssociationV1 != null)
                            {
                                AddAssociation(rowData, tiplocs, tiprep, aRep);
                            }
                        }
                        catch (Exception e)
                        {
                            Trace.TraceError(e.ToString());
                            fail = true;
                            delete = false;
                            throw;
                        }
                    }
                }
            }
            catch (Exception)
            {
                fail = true;
            }
            finally
            {
                if (options.Delete && delete)
                {
                    File.Delete(gzFile);
                    File.Delete(jsonFile);
                }
                TraceHelper.FlushLog();
            }
            if (fail)
            {
                Environment.Exit(1);
            }
        }
        static void Main(string[] args)
        {
            // this data is fetchable via http://nrodwiki.rockshore.net/index.php/ReferenceData
            List<TDElement> allSmartData = new List<TDElement>();

            foreach (string smartExtract in Directory.GetFiles(".", "SMARTExtract*.json"))
            {
                Trace.TraceInformation("Loading SMART data from {0}", smartExtract);
                string smartData = File.ReadAllText(smartExtract);
                allSmartData.AddRange(JsonConvert.DeserializeObject<TDContainer>(smartData).BERTHDATA);
            }

            TDContainer container = new TDContainer
            {
                BERTHDATA = allSmartData
            };
            ILookup<string, TDElement> tdElementsByArea = allSmartData
                .Where(td => !string.IsNullOrEmpty(td.STANOX))
                .ToLookup(td => td.TD);

            if (args.Length > 0 && args[0].Equals("extract", System.StringComparison.InvariantCultureIgnoreCase))
            {
                System.Console.WriteLine("Writing to SMARTExtract.txt");
                File.WriteAllText("SMARTExtract.txt",
                    string.Join(Environment.NewLine, container.BERTHDATA.Select(b => b.ToString())));
                return;
            }

            TiplocRepository _tiplocRepo = new TiplocRepository();

            var dbTiplocs = _tiplocRepo.Get();

            string tiplocData = File.ReadAllText("CORPUSExtract.json");
            ILookup<string, TiplocCode> tiplocByStanox = JsonConvert.DeserializeObject<TiplocContainer>(tiplocData).TIPLOCDATA
                .Select(t => t.ToTiplocCode())
                .ToLookup(t => t.Stanox);

            System.Console.WriteLine("Loaded {0} TD Elements", container.BERTHDATA.Count());

            System.Console.WriteLine("Enter Query");
            string data;
            while (!string.IsNullOrEmpty((data = System.Console.ReadLine())))
            {
                if (data.StartsWith("query=", StringComparison.InvariantCultureIgnoreCase))
                {
                    GenerateQuery(data, tdElementsByArea, tiplocByStanox);
                    continue;
                }
                var clauses = data.Split(',');
                if (!clauses.Any())
                    break;

                var results = container.BERTHDATA
                    .Where(t => t.TD.Equals(clauses[0], System.StringComparison.InvariantCultureIgnoreCase));

                if (clauses.Length > 1)
                {
                    results = results.Where(t => t.FROMBERTH.Equals(clauses[1], System.StringComparison.InvariantCultureIgnoreCase));
                }

                results = results
                    .OrderBy(t => t.STANME)
                    .ThenBy(t => string.IsNullOrEmpty(t.PLATFORM) ? -1 : int.Parse(t.PLATFORM))
                    .ThenBy(t => t.EventType);

                System.Console.WriteLine(string.Join("", Enumerable.Repeat("=", 72).ToArray()));

                System.Console.WriteLine("TD".PadRight(4) +
                    "F-B".PadRight(6) +
                    "T-B".PadRight(6) +
                    "F-L".PadRight(4) +
                    "T-L".PadRight(4) +
                    "STANME".PadRight(10) +
                    "P".PadRight(4) +
                    "EVENT-TYPE".PadRight(12) +
                    "STEP-TYPE".ToString().PadRight(10) +
                    "OFFSET".ToString().PadRight(6));
                System.Console.WriteLine(string.Join("", Enumerable.Repeat("=", 72).ToArray()));

                foreach (var result in results)
                {
                    System.Console.WriteLine(result);
                }
                System.Console.WriteLine(string.Join("", Enumerable.Repeat("=", 72).ToArray()));

            }
        }