/// <summary> /// Add a new airport variation. /// </summary> /// <param name="variationName">The name of the airport variation.</param> /// <param name="airport"> The airport it will reference to.</param> /// <returns>True variation added to db; False something went wrong.</returns> public bool AddNewVariation(string variationName, Airport airport) { // Use the YapbtDbEntities to store the data. using (var db = new YapbtDbEntities()) { try { // Create a new variation, add the data and safe it. AirportVariations variation = new AirportVariations(); variation.Airport = airport; variation.variationname = variationName; variation.cts = DateTime.Now; // Add the new variation to the db. db.AirportVariations.Add(variation); db.SaveChanges(); return true; } catch (Exception) { return false; } } }
/// <summary> /// Remove all points belonging to a single pushback path. /// </summary> /// <param name="pushBackPath">The pushback path we are looking for.</param> /// <returns>True allright; False something went wrong.</returns> public bool RemovePushbackPoint(AirportPushBackPath pushBackPath) { try { using (var db = new YapbtDbEntities()) { // Find a list of all points, which belongs to this path. var points = db.AirportPushPoints.Where(c => c.AirportPushBackPath == pushBackPath).ToList(); // If any point was found, remove it. if (points.Any()) { points.ForEach(delegate (AirportPushPoints point) { db.AirportPushPoints.Remove(point); db.SaveChanges(); }); } return true; } } catch (Exception) { return false; throw; } }
/// <summary> /// Load all parking positions from temp table. /// </summary> /// <returns>All parking positions.</returns> public IEnumerable<TempParking> GetParkingPositions() { using (var db = new YapbtDbEntities()) { return db.TempParking.ToList(); } }
/// <summary> /// Add a list of paths to the db. /// </summary> /// <param name="position">The position we are using for this paths.</param> /// <param name="pathList">A list of paths.</param> /// <returns>A list of all paths that made trouble.</returns> public List<AirportPushBackPath> AddPathByList(List<AirportPushBackPath> pathList) { // The return list with error path objects. Hope it will always be null. List<AirportPushBackPath> errorPath = new List<AirportPushBackPath>(); using (var db = new YapbtDbEntities()) { foreach (var path in pathList) { try { // Add the new path. AirportPushBackPath newPath = new AirportPushBackPath(); newPath = path; db.AirportPushBackPath.Add(newPath); // TODO bulk inserts? db.SaveChanges(); } catch (System.Exception) { // unable to store it into the db. Add it to the list for an error message. errorPath.Add(path); } } } return errorPath; }
/// <summary> /// Reset the temp tables. /// </summary> /// <returns></returns> public ReturnCodes.Codes ResetTempDatabase() { try { using (var db = new YapbtDbEntities()) { // Drop the table. string sql = "DROP TABLE [TempParking];"; db.Database.ExecuteSqlCommand(sql); sql = "DROP TABLE [TempPoint];"; db.Database.ExecuteSqlCommand(sql); sql = "DROP TABLE [TempTaxiway];"; db.Database.ExecuteSqlCommand(sql); // Recreate the table. sql = "CREATE TABLE [TempParking] (" + "[id] INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT," + "[Name] VARCHAR(10) NOT NULL," + "[Index] INTEGER NOT NULL," + "[Number] INTEGER NOT NULL," + "[Latitude] REAL NOT NULL," + "[Longitude] REAL NOT NULL" + ");"; db.Database.ExecuteSqlCommand(sql); sql = "CREATE TABLE [TempPoint] (" + "[id] INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT," + "[Index] INTEGER NOT NULL," + "[Latitude] REAL NOT NULL," + "[Longitude] REAL NOT NULL" + "); "; db.Database.ExecuteSqlCommand(sql); sql = "CREATE TABLE [TempTaxiway] (" + "[id] INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT," + "[FromPoint] INTEGER NOT NULL," + "[ToPoint] INTEGER NOT NULL" + "); "; db.Database.ExecuteSqlCommand(sql); } return ReturnCodes.Codes.ResetOk; } catch (System.Exception) { throw; return ReturnCodes.Codes.ResetError; } }
/// <summary> /// Return the needed configuration value. /// </summary> /// <param name="needle">The configuration part.</param> /// <returns>The value of the needle.</returns> public string ReadConfig(string needle) { using (var db = new YapbtDbEntities()) { try { return db.Configuration.Where(c => c.Setting == needle).Select(d => d.Value).FirstOrDefault(); } catch (Exception ex) { return string.Empty; } } }
/// <summary> /// Add a single point to the db. /// </summary> /// <param name="pointToAdd">The point object.</param> /// <returns>True everything ok; False something went wrong.</returns> public bool AddPoint(AirportPushPoints pointToAdd) { try { using (var db = new YapbtDbEntities()) { db.AirportPushPoints.Add(pointToAdd); return true; } } catch (Exception) { return false; } }
/// <summary> /// Add a new airport position to the db. /// </summary> /// <param name="position">The position object to add to the db.</param> /// <returns>True everything went well; False something went wrong.</returns> public bool AddNewPosition(AirportPositions position) { using (var db = new YapbtDbEntities()) { try { db.AirportPositions.Add(position); return true; } catch (System.Exception) { return false; throw; } } }
/// <summary> /// Add a new airport variation by object. /// </summary> /// <param name="airport">The airport object to add.</param> /// <returns>True variation added to db; False something went wrong.</returns> public bool AddNewVariation(AirportVariations airport) { using (var db = new YapbtDbEntities()) { try { db.AirportVariations.Add(airport); } catch (Exception ex) { return false; throw; } } return true; }
/// <summary> /// Set the config value /// </summary> /// <param name="needle">The configuration part.</param> /// <param name="value">The new value.</param> /// <returns>Return an error code.</returns> public ReturnCodes.Codes SetConfig(string needle, string value) { using (var db = new YapbtDbEntities()) { try { var item = db.Configuration.Where(c => c.Setting == needle).FirstOrDefault(); item.Value = value; db.SaveChanges(); return ReturnCodes.Codes.Ok; } catch (Exception ex) { return ReturnCodes.Codes.Error; } } }
/// <summary> /// Remove all positions of an variation. /// </summary> /// <param name="variation"> /// The airport variation. With this object, it will lookup for all positions. /// </param> /// <returns>True everything went well; False something went wrong.</returns> public bool RemoveAllPositions(AirportVariations variation) { using (var db = new YapbtDbEntities()) { Path path = new Path(); // Find a list of all positions, which belongs to this variation. var position = db.AirportPositions.Where(c => c.variationid == variation.variationid).ToList(); // If any position is available. if (position.Any()) { // For each position, remove all pushback path and remove the position. position.ForEach(delegate (AirportPositions pos) { path.RemoveAllPushbackPathOfPosition(pos); db.AirportPositions.Remove(pos); db.SaveChanges(); }); } return true; } }
/// <summary> /// Remove all pushback paths belonging to a position. /// </summary> /// <param name="position">The position it will lookup for.</param> /// <returns>True everything was ok; False something went wrong.</returns> public bool RemoveAllPushbackPathOfPosition(AirportPositions position) { using (var db = new YapbtDbEntities()) { Point point = new Point(); // Find all pushback paths belonging to the position. var pushBackPath = db.AirportPushBackPath.Where(c => c.positionid == position.positionid).ToList(); // If any path was found. if (pushBackPath.Any()) { // For each path remove the positions and the path. pushBackPath.ForEach(delegate (AirportPushBackPath pushpath) { point.RemovePushbackPoint(pushpath); db.AirportPushBackPath.Remove(pushpath); db.SaveChanges(); }); } return true; } }
/// <summary> /// Remove a single pushback point. /// </summary> /// <param name="pushBackPoint">The single pushback point we are looking for.</param> /// <returns>True point deleted; False something went wrong.</returns> public bool RemovePushbackPoint(AirportPushPoints pushBackPoint) { try { // Checking if the point is set. if (pushBackPoint != null) { using (var db = new YapbtDbEntities()) { // Remove the pushback point. db.AirportPushPoints.Remove(pushBackPoint); db.SaveChanges(); return true; } } return false; } catch (Exception) { return false; throw; } }
/// <summary> /// Return a variation list by a specific airport code. /// </summary> /// <param name="icaoCode">The airport we are looking for.</param> /// <returns>Returns a list of variation objects.</returns> public List<AirportVariations> VariationsByAirport(string icaoCode) { using (var db = new YapbtDbEntities()) { try { return db.AirportVariations.Where(c => c.Airport.icao == icaoCode).ToList(); } catch (Exception ex) { return null; throw; } } }
/// <summary> /// Delete a variation and all necessary data (positions, paths). /// </summary> /// <param name="variation">The variation to delete.</param> /// <returns>True variation was delted; False not able.</returns> public bool DeleteVariation(AirportVariations variation) { using (var db = new YapbtDbEntities()) { Position position = new Position(); //TODO Remove Pushbackpath and positions. position.RemoveAllPositions(variation); db.AirportVariations.Remove(variation); db.SaveChanges(); return true; } }
static void AddVariation(string variationName, Airport airport, YapbtDbEntities db) { // Create a new variation, add the data and safe it. AirportVariations variation = new AirportVariations(); variation.Airport = airport; variation.variationname = variationName; variation.cts = DateTime.Now; // Add the new variation to the db. db.AirportVariations.Add(variation); db.SaveChanges(); }
/// <summary> /// Saving all parking positions of the bgl file into the db. /// </summary> /// <returns>Returns a code about the status.</returns> public ReturnCodes.Codes StoreParkingPos() { // Load the xml file. XDocument xmlDoc = XDocument.Load(this.fields.outputFilePath); IEnumerable<XElement> ParkingPositions = null; try { // Get all parking positions. ParkingPositions = from el in xmlDoc .Descendants("FSData") .Descendants("Airport") .Descendants("TaxiwayParking") select el; } catch (Exception) { // Something went wrong, returning an error code. return ReturnCodes.Codes.XmlError; } try { using (var db = new YapbtDbEntities()) { // Reading all xml elements add the data to an object and save into the sqlite db. foreach (XElement ParkingPosition in ParkingPositions) { TempParking parking = new TempParking(); parking.Index = Convert.ToInt64(ParkingPosition.Attribute("index").Value); parking.Name = ParkingPosition.Attribute("name").Value; parking.Number = Convert.ToInt64(ParkingPosition.Attribute("number").Value); // Converting the latitude and longitude to string and double to avoid // system culture problems. string txt = ParkingPosition.Attribute("lat").Value.ToString(CultureInfo.InvariantCulture); //back to a double parking.Latitude = double.Parse(txt, CultureInfo.InvariantCulture); txt = ParkingPosition.Attribute("lon").Value.ToString(CultureInfo.InvariantCulture); //back to a double parking.Longitude = double.Parse(txt, CultureInfo.InvariantCulture); db.TempParking.Add(parking); db.SaveChanges(); } } } catch (Exception) { // Something went wrong, returning an error code. return ReturnCodes.Codes.ImportError; } return ReturnCodes.Codes.ImportOk; }
/// <summary> /// Saving all taxiways of the bgl file into the db. /// </summary> /// <returns>Returns a code about the status.</returns> public ReturnCodes.Codes StoreTaxiway() { // Load the xml file. XDocument xmlDoc = XDocument.Load(this.fields.outputFilePath); IEnumerable<XElement> TaxiwayPaths = null; try { // Get all taxiway paths. TaxiwayPaths = from el in xmlDoc .Descendants("FSData") .Descendants("Airport") .Descendants("TaxiwayPath") select el; } catch (Exception) { // Something went wrong, returning an error code. return ReturnCodes.Codes.XmlError; } try { using (var db = new YapbtDbEntities()) { // Reading all xml elements add the data to an object and save into the sqlite db. foreach (XElement TaxiwayPath in TaxiwayPaths) { var path = new TempTaxiway(); path.FromPoint = Convert.ToInt64(TaxiwayPath.Attribute("start").Value); path.ToPoint = Convert.ToInt64(TaxiwayPath.Attribute("end").Value); db.TempTaxiway.Add(path); db.SaveChanges(); } } } catch (Exception) { // Something went wrong, returning an error code. return ReturnCodes.Codes.ImportError; } return ReturnCodes.Codes.ImportOk; }
/// <summary> /// Saving all points of the bgl file into the db. /// </summary> /// <returns>Returns a code about the status.</returns> public ReturnCodes.Codes StorePoints() { // Load the xml file. XDocument xmlDoc = XDocument.Load(this.fields.outputFilePath); IEnumerable<XElement> TaxiwayPoints = null; try { // Get all taxiway points. TaxiwayPoints = from el in xmlDoc .Descendants("FSData") .Descendants("Airport") .Descendants("TaxiwayPoint") select el; } catch (Exception) { return ReturnCodes.Codes.XmlError; } try { using (var db = new YapbtDbEntities()) { foreach (XElement TaxiPoint in TaxiwayPoints) { var point = new TempPoint(); point.Index = Convert.ToInt64(TaxiPoint.Attribute("index").Value); // Converting the latitude and longitude to string and double to avoid // system culture problems. string txt = TaxiPoint.Attribute("lat").Value.ToString(CultureInfo.InvariantCulture); //back to a double point.Latitude = double.Parse(txt, CultureInfo.InvariantCulture); txt = TaxiPoint.Attribute("lon").Value.ToString(CultureInfo.InvariantCulture); //back to a double point.Longitude = double.Parse(txt, CultureInfo.InvariantCulture); db.TempPoint.Add(point); db.SaveChanges(); } } } catch (Exception) { return ReturnCodes.Codes.ImportError; } return ReturnCodes.Codes.ImportOk; }
// /// <summary> /// Adding a airport variations and all positions, paths and pusback points to the db. /// </summary> /// <param name="variationName">The airport variation name.</param> /// <param name="airport"> The airport object.</param> /// <param name="positionList"> A list of positions.</param> /// <param name="pathList"> A list of posible paths.</param> /// <param name="pointList"> A list of push back points.</param> /// <returns></returns> public bool AddNewVariationList(string variationName, Airport airport, List<AirportPositions> positionList, List<AirportPushBackPath> pathList, List<AirportPushPoints> pointList) { List<AirportPositions> errorPositionList = new List<AirportPositions>(); List<AirportPushBackPath> errorPathList = new List<AirportPushBackPath>(); List<AirportPushPoints> errorPointList = new List<AirportPushPoints>(); // Use the YapbtDbEntities to store the data. using (var db = new YapbtDbEntities()) { try { AddVariation(variationName, airport, db); // Adding the positions to the db. Position position = new Position(); Path path = new Path(); Point point = new Point(); // For each position it will add a path and it's points. errorPositionList = position.AddNewPosition(positionList); foreach (var errorPosition in errorPositionList) { positionList.Remove(errorPosition); var pathToRemoveList = pathList.Where(c => c.AirportPositions == errorPosition).ToList(); foreach (var pathToRemove in pathToRemoveList) { pathList.Remove(pathToRemove); pointList = this.RemovePoints(pointList, pathToRemove); } } //Add the path list data to the db errorPathList = path.AddPathByList(pathList); foreach (var pathToRemove in errorPathList) { pathList.Remove(pathToRemove); pointList = this.RemovePoints(pointList, pathToRemove); } // Add the point list to the data. errorPointList = point.AddPointsByList(pointList); return true; } catch (Exception) { return false; } } }