/// <summary> /// Replaces one aircraft for another for the user. The old aircraft is not migrated if any existing flights use it. /// </summary> /// <param name="acNew">The new aircraft</param> /// <param name="acOld">The old aircraft</param> /// <param name="fMigrateFlights">True to migrate any existing flights. If false, the old flights (and the old aircraft) may remain.</param> public void ReplaceAircraftForUser(Aircraft acNew, Aircraft acOld, bool fMigrateFlights) { if (acNew == null) { throw new ArgumentNullException("acNew"); } if (acOld == null) { throw new ArgumentNullException("acOld"); } if (acNew.AircraftID == acOld.AircraftID) { return; } // Add the new aircraft first FAddAircraftForUser(acNew); // Migrate any flights, if necessary... if (fMigrateFlights) { List <Aircraft> lstAc = new List <Aircraft>(GetAircraftForUser()); // make sure we are populated with both old and new so that UpdateFlightAircraftForUser works. // (This can happen if you have one version of an aircraft and you go to add another version of it; // they won't both be there, but the query used in UpdateFlightAircraftForUser wants them both present. if (!lstAc.Exists(ac => ac.AircraftID == acNew.AircraftID)) { lstAc.Add(acNew); } if (!lstAc.Exists(ac => ac.AircraftID == acOld.AircraftID)) { lstAc.Add(acOld); } CachedAircraft = lstAc.ToArray(); // we'll nullify the cache below. LogbookEntry.UpdateFlightAircraftForUser(this.User, acOld.AircraftID, acNew.AircraftID); // Migrate any custom currencies associated with the aircraft foreach (CustomCurrency cc in CustomCurrency.CustomCurrenciesForUser(User)) { List <int> lst = new List <int>(cc.AircraftRestriction); for (int i = 0; i < lst.Count; i++) { if (lst[i] == acOld.AircraftID) { lst[i] = acNew.AircraftID; cc.AircraftRestriction = lst; cc.FCommit(); break; } } } // And migrate any deadlines associated with the aircraft foreach (DeadlineCurrency dc in DeadlineCurrency.DeadlinesForUser(User, acOld.AircraftID)) { dc.AircraftID = acNew.AircraftID; dc.FCommit(); } try { // Then delete the old aircraft. Ignore any errors FDeleteAircraftforUser(acOld.AircraftID); } catch (MyFlightbookException) { InvalidateCache(); } } }