public void DeleteSchedule(ScheduleTrain train)
        {
            const string sql = @"
                UPDATE [natrail].[dbo].[ScheduleTrain]
                SET [Deleted] = 1
                WHERE [TrainUid] = @TrainUid
                    AND [StartDate] = @StartDate
                    AND [STPIndicatorId] = @STPIndicator";

            ExecuteNonQuery(sql, new
            {
                train.TrainUid,
                train.StartDate,
                train.STPIndicator
            });
        }
        /// <exception cref="TiplocNotFoundException"></exception>
        public static ScheduleTrain ParseJsonTrain(dynamic s, IEnumerable<TiplocCode> tiplocs)
        {
            var t = new ScheduleTrain();
            t.TransactionType = TransactionTypeField.ParseDataString(DynamicValueToString(s.transaction_type));
            t.TrainUid = StringField.ParseDataString(DynamicValueToString(s.CIF_train_uid));
            t.StartDate = DateField.ParseDataString(DynamicValueToString(s.schedule_start_date));
            t.STPIndicator = STPIndicatorField.ParseDataString(DynamicValueToString(s.CIF_stp_indicator));
            switch (t.TransactionType)
            {
                case TransactionType.Create:
                    t.EndDate = NullableDateField.ParseDataString(DynamicValueToString(s.schedule_end_date));
                    t.AtocCode = AtocCodeField.ParseDataString(DynamicValueToString(s.atoc_code));
                    t.Status = StatusField.ParseDataString(DynamicValueToString(s.train_status));
                    t.Schedule = ScheduleField.ParseDataString(DynamicValueToString(s.schedule_days_runs));

                    t.Headcode = StringField.ParseDataString(DynamicValueToString(s.schedule_segment.signalling_id));
                    t.PowerType = PowerTypeField.ParseDataString(DynamicValueToString(s.schedule_segment.CIF_power_type));
                    t.TrainCategory = TrainCategoryField.ParseDataString(DynamicValueToString(s.schedule_segment.CIF_train_category));
                    t.Speed = SpeedField.ParseDataString(DynamicValueToString(s.schedule_segment.CIF_speed));

                    t.Stops = ParseJsonStops(s.schedule_segment.schedule_location, tiplocs);

                    if (t.Stops.Any())
                    {
                        t.Origin = t.Stops
                            .Where(st => st.Origin)
                            .Select(st => st.Tiploc)
                            .FirstOrDefault();
                        t.Destination = t.Stops
                            .Where(st => st.Terminate)
                            .Select(st => st.Tiploc)
                            .FirstOrDefault();
                    }
                    break;
            }

            return t;
        }
        public void InsertSchedule(ScheduleTrain train, ScheduleSource source = ScheduleSource.CIF)
        {
            using (var ts = GetTransactionScope())
            {
                if (train.AtocCode == null || string.IsNullOrEmpty(train.AtocCode.Code))
                {
                    const string atocCodeLookup = @"
                        SELECT TOP 1
                            [AtocCode]
                        FROM [ScheduleTrain]
                        WHERE [TrainUid] = @trainUid
                            AND [AtocCode] IS NOT NULL";

                    string atocCode = ExecuteScalar<string>(atocCodeLookup, new { trainUid = train.TrainUid });
                    train.AtocCode = new AtocCode
                    {
                        Code = atocCode
                    };
                }

                const string sql = @"
                INSERT INTO [natrail].[dbo].[ScheduleTrain]
                       ([TrainUid]
                       ,[Headcode]
                       ,[StartDate]
                       ,[EndDate]
                       ,[AtocCode]
                       ,[RunsMonday]
                       ,[RunsTuesday]
                       ,[RunsWednesday]
                       ,[RunsThursday]
                       ,[RunsFriday]
                       ,[RunsSaturday]
                       ,[RunsSunday]
                       ,[RunsBankHoliday]
                       ,[STPIndicatorId]
                       ,[ScheduleStatusId]
                       ,[OriginStopTiplocId]
                       ,[DestinationStopTiplocId]
                       ,[Source]
                       ,[PowerTypeId]
                       ,[CategoryTypeId]
                       ,[Speed])
                    OUTPUT [inserted].[ScheduleId]
                    VALUES
                       (@TrainUid
                       ,@Headcode
                       ,@StartDate
                       ,@EndDate
                       ,@Code
                       ,@Monday
                       ,@Tuesday
                       ,@Wednesday
                       ,@Thursday
                       ,@Friday
                       ,@Saturday
                       ,@Sunday
                       ,@BankHoliday
                       ,@STPIndicator
                       ,@Status
                       ,@OriginTiplocId
                       ,@DestinationTiplocId
                       ,@Source
                       ,@PowerTypeId
                       ,@CategoryTypeId
                       ,@Speed)";

                Guid id = ExecuteInsert(sql, new
                {
                    train.TrainUid,
                    train.Headcode,
                    train.StartDate,
                    train.EndDate,
                    train.AtocCode.Code,
                    train.Schedule.Monday,
                    train.Schedule.Tuesday,
                    train.Schedule.Wednesday,
                    train.Schedule.Thursday,
                    train.Schedule.Friday,
                    train.Schedule.Saturday,
                    train.Schedule.Sunday,
                    train.Schedule.BankHoliday,
                    train.STPIndicator,
                    train.Status,
                    OriginTiplocId = train.Origin != null ? new short?(train.Origin.TiplocId) : default(short?),
                    DestinationTiplocId = train.Destination != null ? new short?(train.Destination.TiplocId) : default(short?),
                    Source = source,
                    PowerTypeId = train.PowerType != null ? (byte)train.PowerType : default(byte?),
                    CategoryTypeId = train.TrainCategory != null ? (byte)train.TrainCategory : default(byte?),
                    Speed = train.Speed != null ? (byte)train.Speed : default(byte?)
                });
                if (source == ScheduleSource.VSTP)
                {
                    Trace.TraceInformation("Saving VSTP Schedule for UID: {0} with ID {1}",
                        train.TrainUid, id);
                }

                InsertScheduleStops(id, train.Stops);
                ts.Complete();
            }
        }
 public void CacheVSTPSchedule(ScheduleTrain train)
 {
     try
     {
         _scheduleRepository.InsertSchedule(train, ScheduleSource.VSTP);
     }
     catch (Exception e)
     {
         Trace.TraceError("Could not save VSTP Train: {0}", e);
     }
 }