public ActionResult Create(AircraftType aircrafttype)
 {
     if (ModelState.IsValid) {
         aircrafttypeRepository.Insert(aircrafttype);
         aircrafttypeRepository.Save();
         return RedirectToAction("Index");
     } else {
         ViewBag.PossibleDivision = divisionRepository.All;
         return View();
     }
 }
Exemplo n.º 2
0
        public ActionResult ImportSchedule()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Server.MapPath("~") + @"Schedule\");
            FileInfo[] fileInfos = directoryInfo.GetFiles("*.txt");
            string line;

            Dictionary<string, Airport> airportsDictionary = new Dictionary<string, Airport>();
            Dictionary<string, Connection> scheduleDictionary = new Dictionary<string, Connection>();

            IEnumerable<ScheduleAircraftReplacement> scheduleAircraftReplacements = scheduleAircraftReplacementRepository.All;
            IEnumerable<Airline> airlines = airlineRepository.All;

            foreach (FileInfo info in fileInfos) {
                StreamReader file = new StreamReader(info.FullName);

                string dep = null;
                string dest = null;

                while ((line = file.ReadLine()) != null) {

                    MatchCollection airportMatches = Regex.Matches(line, AirportAndTimeZonePattern, RegexOptions.Singleline);
                    Match connectionMatch = Regex.Match(line, FlightPattern, RegexOptions.Singleline);

                    if (airportMatches.Count > 0) {
                        try {
                            dep = IataToIcaoTable[airportMatches[0].Value.Substring(0, 3)];
                        } catch (KeyNotFoundException e) {
                            Debug.WriteLine("No ICAO code found for IATA code: " + airportMatches[0].Value.Substring(0, 3));
                            dep = null;
                        }

                        try {
                            dest = IataToIcaoTable[airportMatches[1].Value.Substring(0, 3)];
                        } catch (KeyNotFoundException e) {
                            Debug.WriteLine("No ICAO code found for IATA code: " + airportMatches[1].Value.Substring(0, 3));
                            dest = null;
                        }

                        if (dep != null && dest != null) {

                            foreach (Match airportMatch in airportMatches) {

                                string airportICAO = IataToIcaoTable[airportMatch.Value.Substring(0, 3)];

                                if (!airportsDictionary.ContainsKey(airportICAO)) {
                                    airportsDictionary.Add(airportICAO, GetAirport(airportMatch));
                                }
                            }
                        }

                    } else if (connectionMatch.Success && dep != null && dest != null) {

                        int flightNumber = int.Parse(Regex.Match(connectionMatch.Groups[4].Value, @"(\d){1,4}").Groups[0].Value);
                        string prefix = connectionMatch.Groups[4].Value.Substring(0, 2);
                        string sourceAircraftType = connectionMatch.Groups[6].Value;

                        bool excluded = !(flightNumber > 0 && flightNumber < 5000 && prefix == "LH" && sourceAircraftType != "BUS");

                        string key = connectionMatch.Groups[4].Value + "," + dep + "," + dest;

                        // adds only non-codeshare flights of Lufthansa, they are between 0 and approx. 5000 (see wikipedia for more information)
                        if (!scheduleDictionary.ContainsKey(key) && line.Contains("non stop") && !excluded) {
                            scheduleDictionary.Add(key, GetConnection(connectionMatch, dep, dest, scheduleAircraftReplacements, airlines));
                        }
                    }

                }

            }

            // 1. delete all Connections
            // 2. update Airports
            // 3. add all missing AircraftTypes
            // 4. add all new Connections

            DatabaseContext.Database.ExecuteSqlCommand("DELETE FROM Connections WHERE FlightNumber < 8000");

            IEnumerable<Airport> airports = airportRepository.All;

            foreach (var airportItem in airportsDictionary) {
                Airport currentAirport = airports.SingleOrDefault(t => t.IATA == airportItem.Value.IATA);

                if (currentAirport == null) {
                    throw new Exception("Airport not found!");
                    //airportRepository.Insert(airportItem.Value);
                }
            }

            airportRepository.Save();

            IEnumerable<AircraftType> aircraftTypes = aircraftTypeRepository.All;

            foreach (var aircraftType in scheduleDictionary.Select(t => t.Value.AircraftTypeICAO).Distinct()) {
                if (!aircraftTypes.Any(t => t.ICAO == aircraftType)) {
                    AircraftType currentAircraftType = new AircraftType();
                    currentAircraftType.ICAO = aircraftType;
                    aircraftTypeRepository.Insert(currentAircraftType);
                }
            }

            aircraftTypeRepository.Save();

            foreach (var airline in scheduleDictionary.Select(t => t.Value.AirlineICAO).Distinct()) {
                if (!airlines.Any(t => t.ICAO == airline)) {
                    Airline currentAirline = new Airline();
                    currentAirline.ICAO = airline;
                    airlineRepository.Insert(currentAirline);
                }
            }

            airlineRepository.Save();

            foreach (var connection in scheduleDictionary) {
                connectionRepository.InsertOrUpdate(connection.Value);
            }

            connectionRepository.Save();

            return View();
        }
Exemplo n.º 3
0
        public ActionResult ImportCargoSchedule()
        {
            string[] csvFiles = Directory.GetFiles(HttpContext.Server.MapPath("~") + @"\Schedule\", "*.csv", SearchOption.TopDirectoryOnly);

            StreamReader file = new StreamReader(csvFiles.First());

            string line;

            Dictionary<string, Airport> airportsDictionary = new Dictionary<string, Airport>();
            Dictionary<string, Connection> scheduleDictionary = new Dictionary<string, Connection>();

            int lineNumber = 1;

            IEnumerable<ScheduleAircraftReplacement> scheduleAircraftReplacements = scheduleAircraftReplacementRepository.All;

            while ((line = file.ReadLine()) != null) {
                string[] c = line.Split(';');

                if (lineNumber > 2) {

                    if (c.Length >= 22 && c[7] != "0" && (c[21].Equals("77F") || c[21].Equals("M1F"))) {

                        Connection cargoConn = GetCargoConnection(c, scheduleAircraftReplacements);

                        if (cargoConn.FlightNumber > 8000) {

                            if (!airportsDictionary.ContainsKey(cargoConn.DepartureAirportICAO)) {
                                airportsDictionary.Add(cargoConn.DepartureAirportICAO, new Airport() { ICAO = cargoConn.DepartureAirportICAO });
                            }

                            if (!airportsDictionary.ContainsKey(cargoConn.DestinationAirportICAO)) {
                                airportsDictionary.Add(cargoConn.DestinationAirportICAO, new Airport() { ICAO = cargoConn.DestinationAirportICAO });
                            }

                            scheduleDictionary.Add(cargoConn.FlightNumber + "," + cargoConn.DepartureAirportICAO + "," + cargoConn.DestinationAirportICAO + "," + cargoConn.StartsOn.Value.Date + "," + cargoConn.EndsOn.Value.Date, cargoConn);
                        }
                    }
                }

                lineNumber++;
            }

            DatabaseContext.Database.ExecuteSqlCommand("DELETE FROM Connections WHERE FlightNumber > 8000");

            IEnumerable<Airport> airports = airportRepository.All;

            foreach (var airportItem in airportsDictionary) {
                Airport currentAirport = airports.SingleOrDefault(t => t.ICAO == airportItem.Value.ICAO);

                if (currentAirport == null) {
                    throw new Exception("Airport not found!");
                    //airportRepository.Insert(airportItem.Value);
                }
            }

            airportRepository.Save();

            IEnumerable<AircraftType> aircraftTypes = aircraftTypeRepository.All;

            foreach (var aircraftType in scheduleDictionary.Select(t => t.Value.AircraftTypeICAO).Distinct()) {
                if (!aircraftTypes.Any(t => t.ICAO == aircraftType)) {
                    AircraftType currentAircraftType = new AircraftType();
                    currentAircraftType.ICAO = aircraftType;
                    aircraftTypeRepository.Insert(currentAircraftType);
                }
            }

            aircraftTypeRepository.Save();

            IEnumerable<Airline> airlines = airlineRepository.All;

            foreach (var airline in scheduleDictionary.Select(t => t.Value.AirlineICAO).Distinct()) {
                if (!airlines.Any(t => t.ICAO == airline)) {
                    Airline currentAirline = new Airline();
                    currentAirline.ICAO = airline;
                    airlineRepository.Insert(currentAirline);
                }
            }

            airlineRepository.Save();

            foreach (var connection in scheduleDictionary) {
                Connection conn = connection.Value;
                connectionRepository.InsertOrUpdate(conn);
            }

            connectionRepository.Save();

            return View();
        }
 public void Update(AircraftType aircrafttype)
 {
     context.AircraftType.Attach(aircrafttype);
         context.Entry(aircrafttype).State = EntityState.Modified;
 }
 public void Insert(AircraftType aircrafttype)
 {
     context.AircraftType.Add(aircrafttype);
 }