Пример #1
0
        // insert all object members as a new row in table, in a transaction
        // the transaction and or connection state is not changed in any way other than what SqlClient does to it.
        // it is the callers responsibility to commit or rollback the transaction
        // links:
        //  docLink: http://sql2x.org/documentationLink/542f9458-c3b9-4edf-8575-0870086f3a7f
        public void Insert(CrudeFlightContract contract, SqlConnection connection, SqlTransaction transaction)
        {
            var data = new CrudeFlightData();

            ContractToData(contract, data);
            data.Insert(connection, transaction);
        }
Пример #2
0
        // insert all object members as a new row in table
        // links:
        //  docLink: http://sql2x.org/documentationLink/75aad010-e6aa-4f19-a6e5-597456aa20d8
        public void Insert(CrudeFlightContract contract)
        {
            var data = new CrudeFlightData();

            ContractToData(contract, data);
            data.Insert();
        }
Пример #3
0
        // transfer model to data and insert, on transaction
        // links:
        //  docLink: http://sql2x.org/documentationLink/fbeb7c34-b2d7-403b-a9fd-503ab705ef81
        public void Insert(CrudeFlightModel model, SqlConnection connection, SqlTransaction transaction)
        {
            var data = new CrudeFlightData();

            ModelToData(model, data);
            data.Insert(connection, transaction);
        }
Пример #4
0
        // transfer model to data and insert
        // links:
        //  docLink: http://sql2x.org/documentationLink/17cd8423-3c78-459f-a45b-773fcfbc3b7d
        public void Insert(CrudeFlightModel model)
        {
            var data = new CrudeFlightData();

            ModelToData(model, data);
            data.Insert();
        }
Пример #5
0
        public void UpdateFlight(
            FlightContract flightContract,
            Guid userId
            )
        {
            Logging.ActionLog("SolutionNorSolutionPort.BusinessLogicLayer.FlightService.UpdateFlight",
                              userId
                              );

            // start transaction
            using (var connection = new SqlConnection(Conn.ConnectionString)) {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                try {
                    // insert new flight
                    var crudeNewFlightData = new CrudeFlightData();
                    CrudeFlightService.ContractToData(
                        flightContract.Flight,
                        crudeNewFlightData
                        );

                    crudeNewFlightData.FlightId = Guid.NewGuid();

                    // binding flight id carries on forward
                    crudeNewFlightData.BindingFlightId = flightContract.Flight.BindingFlightId;

                    crudeNewFlightData.UserId   = userId;
                    crudeNewFlightData.DateTime = DateTime.UtcNow;
                    crudeNewFlightData.Insert(connection, transaction);

                    // todo, can be more than one
                    if (!String.IsNullOrEmpty(flightContract.FlightIdentifier.FlightIdentifierCode))
                    {
                        var crudeNewFlightIdentifierData = new CrudeFlightIdentifierData();

                        CrudeFlightIdentifierService.ContractToData(
                            flightContract.FlightIdentifier,
                            crudeNewFlightIdentifierData
                            );

                        crudeNewFlightIdentifierData.FlightIdentifierId      = Guid.NewGuid();
                        crudeNewFlightIdentifierData.FlightId                = crudeNewFlightData.FlightId;
                        crudeNewFlightIdentifierData.FlightIdentifierTypeRcd = FlightIdentifierTypeRef.FlightNumberThree;
                        crudeNewFlightIdentifierData.DateTime                = DateTime.UtcNow;
                        crudeNewFlightIdentifierData.UserId = userId;
                        crudeNewFlightIdentifierData.Insert(connection, transaction);
                    }

                    // update old flight schedule 'became' identifier
                    var crudeOldFlightData = new CrudeFlightData();
                    crudeOldFlightData.FetchByFlightId(flightContract.Flight.FlightId);
                    crudeOldFlightData.BecameFlightId = crudeNewFlightData.FlightId;
                    crudeOldFlightData.Update(connection, transaction);

                    // copy segments
                    List <CrudeFlightSegmentData> crudeFlightSegmentsData =
                        CrudeFlightSegmentData.FetchByFlightId(
                            flightContract.Flight.FlightId
                            );

                    foreach (CrudeFlightSegmentData crudeFlightSegmentData in crudeFlightSegmentsData)
                    {
                        crudeFlightSegmentData.FlightSegmentId = Guid.NewGuid();
                        crudeFlightSegmentData.FlightId        = crudeNewFlightData.FlightId;
                        crudeFlightSegmentData.DateTime        = DateTime.UtcNow;
                        crudeFlightSegmentData.UserId          = userId;
                        crudeFlightSegmentData.Insert(connection, transaction);
                    }

                    // commit transaction
                    transaction.Commit();
                } catch (Exception ex) {
                    transaction.Rollback();

                    Logging.ErrorLog("Flight",
                                     "FlightService",
                                     "UpdateFlight",
                                     ex.Message,
                                     ex.StackTrace,
                                     userId
                                     );
                    throw ex;
                }
            }
        }
Пример #6
0
        /// <summary>Create all flights based on schedule record</summary>
        /// <summary>Schedule main dates will be used if segments does not exist</summary>
        /// <summary>Identifiers are taken from schedule</summary>
        /// <summary>Segments are taken from schedule if existing, default segment created if not</summary>
        /// <summary>Flight and segment 'created' events inserted</summary>
        /// <summary>Action log event inserted, exceptions logged</summary>
        public void MakeFlightsFromSchedule(
            Guid flightScheduleId,
            Guid userId
            )
        {
            Logging.ActionLog("SolutionNorSolutionPort.BusinessLogicLayer.ScheduleService.MakeFlightsFromSchedule",
                              userId
                              );

            // get schedule
            ScheduleContract scheduleContract = GetSchedule(flightScheduleId, userId);

            // make sure there are segments
            if (scheduleContract.CrudeFlightScheduleSegments.Length == 0)
            {
                Logging.ErrorLog("Schedule",
                                 "ScheduleService",
                                 "MakeFlightsFromSchedule",
                                 "Schedule.MakeFlightsFromSchedule: There are no segments on schedule",
                                 string.Empty,
                                 userId
                                 );

                throw new Exception("Schedule.MakeFlightsFromSchedule: There are no segments on schedule");
            }

            // make sure there is at least one identifier
            if (scheduleContract.FlightScheduleIdentifier == null ||
                scheduleContract.FlightScheduleIdentifier.FlightScheduleIdentifierId == Guid.Empty)
            {
                Logging.ErrorLog("Schedule",
                                 "ScheduleService",
                                 "MakeFlightsFromSchedule",
                                 "Schedule.MakeFlightsFromSchedule: There are no flight identifiers",
                                 string.Empty,
                                 userId
                                 );

                throw new Exception("Schedule.MakeFlightsFromSchedule: There are no flight identifiers");
            }

            // get first aircraft available, TODO
            List <CrudeAircraftContract> aircrafts = new CrudeAircraftService().FetchAll();

            if (aircrafts.Count == 0)
            {
                Logging.ErrorLog("Schedule",
                                 "ScheduleService",
                                 "MakeFlightsFromSchedule",
                                 "Schedule.MakeFlightsFromSchedule: There are no aircrafts available",
                                 string.Empty,
                                 userId
                                 );

                throw new Exception("Schedule.MakeFlightsFromSchedule: There are no aircrafts available");
            }

            // start transaction
            using (var connection = new SqlConnection(Conn.ConnectionString)) {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();

                try {
                    // iterate all dates between from / until
                    DateTime currentDate = scheduleContract.FlightSchedule.FromDateTime.Date;

                    while (currentDate >= scheduleContract.FlightSchedule.FromDateTime.Date &&
                           currentDate <= scheduleContract.FlightSchedule.UntilDateTime)
                    {
                        // make new flight
                        var flight = new CrudeFlightData();
                        flight.FlightId           = Guid.NewGuid();
                        flight.BindingFlightId    = flight.FlightId;
                        flight.AirlineId          = scheduleContract.FlightSchedule.AirlineId;
                        flight.AircraftId         = aircrafts[0].AircraftId;
                        flight.AircraftTypeRcd    = scheduleContract.FlightSchedule.AircraftTypeRcd;
                        flight.DepartureAirportId = scheduleContract.FlightSchedule.DepartureAirportId;
                        flight.ArrivalAirportId   = scheduleContract.FlightSchedule.ArrivalAirportId;

                        // compose flight date time departure / arrival time from first and last segment
                        // todo, what about day shift +-
                        flight.FromDateTime = currentDate.Add(
                            scheduleContract.FlightScheduleSegments[0].DepartureTime
                            );

                        flight.UntilDateTime = currentDate.Add(
                            scheduleContract.FlightScheduleSegments[scheduleContract.FlightScheduleSegments.Length - 1].ArrivalTime
                            );

                        flight.Comment  = scheduleContract.FlightSchedule.Comment;
                        flight.DateTime = DateTime.UtcNow;
                        flight.UserId   = userId;

                        // make sure flight does not exist
                        List <CrudeFlightData> flightDataList =
                            CrudeFlightData.FetchWithFilter(
                                flightId: Guid.Empty,
                                becameFlightId: Guid.Empty,
                                bindingFlightId: Guid.Empty,
                                airlineId: flight.AirlineId,
                                aircraftId: flight.AircraftId,
                                aircraftTypeRcd: string.Empty,
                                departureAirportId: flight.DepartureAirportId,
                                arrivalAirportId: flight.ArrivalAirportId,
                                fromDateTime: flight.FromDateTime,
                                untilDateTime: flight.UntilDateTime,
                                comment: string.Empty,
                                userId: Guid.Empty,
                                dateTime: DateTime.MinValue
                                );

                        if (flightDataList.Count != 0)
                        {
                            continue;
                        }

                        flight.Insert(connection, transaction);

                        // flight identifier
                        // todo, can be more than one
                        var identifier = new CrudeFlightIdentifierData();
                        identifier.FlightIdentifierTypeRcd = scheduleContract.FlightScheduleIdentifier.FlightIdentifierTypeRcd;
                        identifier.FlightIdentifierCode    = scheduleContract.FlightScheduleIdentifier.FlightIdentifierCode;
                        identifier.FlightId = flight.FlightId;
                        identifier.UserId   = userId;
                        identifier.DateTime = DateTime.UtcNow;
                        identifier.Insert(connection, transaction);

                        // events
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.Created, DateTime.UtcNow, userId);

                        // planned / estimated times
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.PlannedDeparture, flight.FromDateTime, userId);
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.PlannedArrival, flight.UntilDateTime, userId);
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.EstimatedDeparture, flight.FromDateTime, userId);
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.EstimatedArrival, flight.UntilDateTime, userId);

                        // open for booking
                        AddFlightEvent(connection, transaction, flight.FlightId, DateTimeTypeRef.BookingOpen, DateTime.UtcNow, userId);

                        // check rules

                        // create flight segments for that day
                        foreach (CrudeFlightScheduleSegmentContract scheduleSegment in scheduleContract.CrudeFlightScheduleSegments)
                        {
                            var flightSegment = new CrudeFlightSegmentData();
                            flightSegment.FlightSegmentId       = Guid.NewGuid();
                            flightSegment.FlightId              = flight.FlightId;
                            flightSegment.DepartureAirportId    = scheduleSegment.DepartureAirportId;
                            flightSegment.ArrivalAirportId      = scheduleSegment.ArrivalAirportId;
                            flightSegment.LogicalSegmentNumber  = scheduleSegment.LogicalSegmentNumber;
                            flightSegment.PhysicalSegmentNumber = scheduleSegment.PhysicalSegmentNumber;
                            flightSegment.FromDateTime          = currentDate.Add(scheduleSegment.DepartureTime);
                            flightSegment.UntilDateTime         = currentDate.Add(scheduleSegment.ArrivalTime);
                            flightSegment.DepartureGate         = scheduleSegment.DepartureGate;
                            flightSegment.ArrivalGate           = scheduleSegment.ArrivalGate;
                            flightSegment.UserId   = userId;
                            flightSegment.DateTime = DateTime.UtcNow;
                            flightSegment.Insert(connection, transaction);

                            // events
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.Created, DateTime.UtcNow, userId);

                            // planned / estimated times
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.PlannedDeparture, flightSegment.FromDateTime, userId);
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.PlannedArrival, flightSegment.UntilDateTime, userId);
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.EstimatedDeparture, flightSegment.FromDateTime, userId);
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.EstimatedArrival, flightSegment.UntilDateTime, userId);

                            // open for booking
                            AddFlightSegmentEvent(connection, transaction, flightSegment.FlightSegmentId, DateTimeTypeRef.BookingOpen, DateTime.UtcNow, userId);
                        }

                        currentDate = currentDate.AddDays(1);

                        break; // break here since we are testing with a fresh database where from date is todays date
                    }

                    // commit transaction
                    transaction.Commit();
                } catch (Exception ex) {
                    transaction.Rollback();
                    Logging.ErrorLog("Schedule",
                                     "ScheduleService",
                                     "MakeFlightsFromSchedule",
                                     ex.Message,
                                     ex.StackTrace,
                                     userId
                                     );
                    throw ex;
                }
            }
        }