public override IAnswer execute() { Database.LocationRequest lr = new Database.LocationRequest (); Database.AirlineRequest ar = new Database.AirlineRequest (); Database.AirplaneRequest apr = new Database.AirplaneRequest (); Database.FlightTemplateRequest ftr = new Database.FlightTemplateRequest (); string airlineCode = ""; string digits = ""; Database.Util.split(this.Flight.Template.Code, ref airlineCode, ref digits); if (this.Flight.StartAirport == null) { return new AnswerAdd("The start airport of the flights isn't set"); } List<Database.Location> locations = lr.fetchLocationFromAirports (new Database.Airport (code: this.Flight.StartAirport.Code), new Database.Airport (code: this.Flight.DestinationAirport.Code)); if (locations.Count () == 0) { Database.AirportRequest airportRequest = new Database.AirportRequest(); List<Database.Airport> airports = airportRequest.fetchAirportFromCode(this.Flight.StartAirport.Code); if(airports.Count() != 1){ return new AnswerAdd("Couldn't find a (unique) airport with code " + this.Flight.StartAirport.Code); } int airport1ID = airports[0].ID; airports = airportRequest.fetchAirportFromCode(this.Flight.DestinationAirport.Code); if(airports.Count() != 1){ return new AnswerAdd("Couldn't find a (unique) airport with code " + this.Flight.DestinationAirport.Code); } int airport2ID = airports[0].ID; Database.Location location = new Database.Location(airport1ID, airport2ID, this.Flight.distance); try{ location.insert(); } catch(Exception e){ return new AnswerAdd(e.Message); } locations = lr.fetchLocationFromAirports (new Database.Airport (code: this.Flight.StartAirport.Code), new Database.Airport (code: this.Flight.DestinationAirport.Code)); } int locationID = locations [0].ID; List<Database.Airline> airlines = ar.fetchAirlineFromCode (airlineCode); if (airlines.Count () != 1) { return new AnswerAdd ("Couldn't find a (unique) airline with code " + airlineCode); } int airlineID = airlines [0].ID; List<Database.Airplane> airplanes = apr.fetchAirplaneFromCode (this.Flight.Airplane.Code); if (airplanes.Count () != 1) { return new AnswerAdd ("Couldn't find a (unique) airplane with code " + this.Flight.Airplane.Code); } int airplaneID = airplanes [0].ID; List<Database.FlightTemplate> templates = ftr.fetchTemplateFromAirlineAndDigits(airlineID, digits); if (templates.Count () != 1) { return new AnswerAdd("Couldn't find a (unique) template from airline " + airlines[0].code + " with digits " + this.Flight.Template.digits); } int templateID = templates [0].ID; Database.Flight flight = new Database.Flight (locationID, airlineID, this.Flight.StartDate, this.Flight.EndDate, airplaneID, templateID, this.Flight.TravelTime); AnswerAdd aa = new AnswerAdd (); try { flight.insert(); } catch (Exception e) { aa = new AnswerAdd(e.Message); } return aa; }
private List<Flight> adapt(List<Database.Flight> dfs) { Database.FlightTemplateRequest ftr = new Database.FlightTemplateRequest(); Database.AirlineRequest alr = new Database.AirlineRequest(); Database.AirplaneRequest apr = new Database.AirplaneRequest(); Database.LocationRequest lr = new Database.LocationRequest(); Database.SeatRequest sr = new Database.SeatRequest(); Database.ClassRequest cr = new Database.ClassRequest(); Database.CountryRequest cor = new Database.CountryRequest(); Database.CityRequest cir = new Database.CityRequest(); Database.AirportRequest ar = new Database.AirportRequest(); List<Flight> fs = new List<Flight>(); foreach(Database.Flight f in dfs){ Database.FlightTemplate ft = ftr.fetchFromID(f.template)[0]; Database.Airline airline_ = alr.fetchFromID(ft.airline)[0]; Airline airline = new Airline(Name: airline_.name, Code: airline_.code); FlightTemplate template = new FlightTemplate(ft.digits, airline); Database.Location l = lr.fetchFromID(f.location)[0]; Database.Airport startAirport_ = ar.fetchFromID(l.start_airport)[0]; Database.Airport destinationAirport_ = ar.fetchFromID(l.destination_airport)[0]; Country startCountry = new Country(cor.fetchFromID(startAirport_.country)[0].name); Country destinationCountry = new Country(cor.fetchFromID(destinationAirport_.country)[0].name); City startCity = new City(cir.fetchFromID(startAirport_.city)[0].name, startCountry); City destinationCity = new City(cir.fetchFromID(destinationAirport_.city)[0].name, destinationCountry); Airport startAirport = new Airport(startAirport_.name, startAirport_.code, startCity); Airport destinationAirport = new Airport(destinationAirport_.name, destinationAirport_.code, destinationCity); Database.Airplane airplane_ = apr.fetchFromID(f.airplane)[0]; List<Seat> seats = new List<Seat>(); foreach(int s in airplane_.seat){ Database.Seat seat_ = sr.fetchFromID(s)[0]; Database.Class seatClass_ = cr.fetchFromID(seat_.class_)[0]; SeatClass seatClass = new SeatClass(seatClass_.name); seats.Add(new Seat(seatClass, seat_.number)); } Airplane airplane = new Airplane(airplane_.type, seats, airplane_.code); fs.Add(new Flight(template, f.start, f.end, startAirport, destinationAirport, airplane)); } return fs; }
private List<Database.Airport> executeOnCountry() { Database.AirportRequest ar = new Database.AirportRequest(); return ar.fetchAirportFromCountry(new Database.Country(this.Country.Name)); }
private List<Database.Flight> executeOnAirport() { Database.AirportRequest ar = new Database.AirportRequest (); List<Database.Airport> airports = ar.fetchAirportFromCode (this.Airport1.Code); if (airports.Count () != 1) { throw new Exception ("A (unique) airport with code " + this.Airport1.Code + " couldn't be found"); } Database.Airport airport1 = airports [0]; airports = ar.fetchAirportFromCode (this.Airport2.Code); if (airports.Count () != 1) { throw new Exception("A (unique) airport with code " + this.Airport2.Code + " couldn't be found"); } Database.Airport airport2 = airports [0]; return doDispatch(airport1, airport2); }