public static string GetHighWaterMarks(int idAircraft) { if (!HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("Unauthenticated call to GetHighWaterMarks"); } if (idAircraft <= 0) { return(String.Empty); } decimal hwHobbs = AircraftUtility.HighWaterMarkHobbsForUserInAircraft(idAircraft, HttpContext.Current.User.Identity.Name); decimal hwTach = AircraftUtility.HighWaterMarkTachForUserInAircraft(idAircraft, HttpContext.Current.User.Identity.Name); if (hwTach == 0) { return(hwHobbs == 0 ? String.Empty : String.Format(CultureInfo.CurrentCulture, Resources.Aircraft.HighWaterMarkHobbsOnly, hwHobbs)); } else if (hwHobbs == 0) { return(String.Format(CultureInfo.CurrentCulture, Resources.Aircraft.HighWaterMarkTachOnly, hwTach)); } else { return(String.Format(CultureInfo.CurrentCulture, Resources.Aircraft.HighWaterMarkTachAndHobbs, hwTach, hwHobbs)); } }
protected void btnRefreshInvalid_Click(object sender, EventArgs e) { mvAircraftIssues.SetActiveView(vwInvalidAircraft); gvInvalidAircraft.DataSource = AircraftUtility.AdminAllInvalidAircraft(); gvInvalidAircraft.DataBind(); }
protected void btnMigrateSim_Click(object sender, EventArgs e) { Aircraft acOriginal = new Aircraft(MfbEditAircraft1.AircraftID); if (!CouldBeSim(acOriginal)) { lblErr.Text = Resources.Aircraft.AdminNotASim; return; } // detect likely sim type AircraftInstanceTypes ait = PseudoSimTypeFromTail(acOriginal.TailNumber); // see if the specified sim exists string szSimTail = Aircraft.SuggestSims(acOriginal.ModelID, ait)[0].TailNumber; Aircraft acNew = new Aircraft(szSimTail); if (acNew.IsNew) { acNew.TailNumber = szSimTail; acNew.ModelID = acOriginal.ModelID; acNew.InstanceType = ait; acNew.Commit(); } // set the original's instance type so that merge works. acOriginal.InstanceType = ait; AircraftUtility.AdminMergeDupeAircraft(acNew, acOriginal); Response.Redirect(Request.Url.PathAndQuery.Replace(String.Format(CultureInfo.InvariantCulture, "id={0}", acOriginal.AircraftID), String.Format(CultureInfo.InvariantCulture, "id={0}", acNew.AircraftID))); }
public static void MigrateGeneric(int idAircraft) { if (!HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("Unauthenticated call to MigrateGeneric"); } Aircraft ac = new Aircraft(idAircraft); if (String.IsNullOrWhiteSpace(ac.TailNumber) || ac.AircraftID <= 0) { throw new MyFlightbookValidationException(String.Format(CultureInfo.CurrentCulture, "No aircraft with ID {0}", idAircraft)); } Aircraft acOriginal = new Aircraft(ac.AircraftID); // See if there is a generic for the model string szTailNumGeneric = Aircraft.AnonymousTailnumberForModel(acOriginal.ModelID); Aircraft acGeneric = new Aircraft(szTailNumGeneric); if (acGeneric.IsNew) { acGeneric.TailNumber = szTailNumGeneric; acGeneric.ModelID = acOriginal.ModelID; acGeneric.InstanceType = AircraftInstanceTypes.RealAircraft; acGeneric.Commit(); } AircraftUtility.AdminMergeDupeAircraft(acGeneric, acOriginal); }
protected void gvOrphanedAircraft_RowCommand(object sender, CommandEventArgs e) { if (e != null && e.CommandName.CompareCurrentCultureIgnoreCase("_Delete") == 0) { AircraftUtility.DeleteOrphanAircraft(Convert.ToInt32(e.CommandArgument, CultureInfo.InvariantCulture)); } btnOrphans_Click(sender, e); }
/// <summary> /// Commits the edits, returns the id of the resulting row, -1 if it failed. /// </summary> /// <returns>Non-negative flight ID, -1 for failure</returns> protected int CommitChanges() { int idResult = -1; if (!Page.IsValid) { return(idResult); } LogbookEntry le = InitLogbookEntryFromForm(); FlightWillBeSaved?.Invoke(this, new LogbookEventArgs(le)); if (le.IsValid()) { // if a new flight and hobbs > 0, save it for the next flight if (le.IsNewFlight && le.HobbsEnd > 0) { SetLastHobbs(le.HobbsEnd); } le.FlightData = mfbFlightInfo1.Telemetry; try { if (le.FCommit(le.HasFlightData)) { AircraftUtility.SaveLastTail(le.AircraftID); ProcessImages(le.FlightID); if (FlightID == LogbookEntry.idFlightNew) // new flight - save the date { Session[keyLastEntryDate] = le.Date; } idResult = le.FlightID; // this must be >= 0 if the commit succeeded // Badge computation may be wrong MyFlightbook.Profile.GetUser(le.User).SetAchievementStatus(MyFlightbook.Achievements.Achievement.ComputeStatus.NeedsComputing); } else { lblError.Text = HttpUtility.HtmlEncode(le.ErrorString); } } catch (MyFlightbookException ex) { lblError.Text = !String.IsNullOrEmpty(le.ErrorString) ? le.ErrorString : (ex.InnerException == null ? ex.Message : ex.InnerException.Message); } } else { lblError.Text = HttpUtility.HtmlEncode(le.ErrorString); } return(idResult); }
public static string HighWaterMarkTachForAircraft(int idAircraft) { if (HttpContext.Current == null || HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("You must be authenticated to make this call"); } System.Threading.Thread.CurrentThread.CurrentCulture = util.SessionCulture ?? CultureInfo.CurrentCulture; return(AircraftUtility.HighWaterMarkTachForUserInAircraft(idAircraft, HttpContext.Current.User.Identity.Name).ToString("0.0#", CultureInfo.CurrentCulture)); }
protected void Page_Load(object sender, EventArgs e) { this.Master.SelectedTab = tabID.tabAircraft; this.Master.Title = String.Format(CultureInfo.CurrentCulture, Resources.LocalizedText.TitleAircraft, Branding.CurrentBrand.AppName); if (!IsPostBack) { int id = util.GetIntParam(Request, "id", Aircraft.idAircraftUnknown); bool fAdminMode = AdminMode = id > 0 && (util.GetIntParam(Request, "a", 0) != 0) && MyFlightbook.Profile.GetUser(Page.User.Identity.Name).CanSupport; bool fCanMigrate = !String.IsNullOrEmpty(util.GetStringParam(Request, "genCandidate")); MfbEditAircraft1.AircraftID = id = AircraftTombstone.MapAircraftID(id); MfbEditAircraft1.AdminMode = lblAdminMode.Visible = pnlAdminUserFlights.Visible = fAdminMode; if (fAdminMode) { sqlDSFlightsPerUser.SelectParameters.Add(new Parameter("idaircraft", TypeCode.Int32, id.ToString(CultureInfo.InvariantCulture))); gvFlightsPerUser.DataSource = sqlDSFlightsPerUser; try { gvFlightsPerUser.DataBind(); } catch (MySql.Data.MySqlClient.MySqlException) { } List <Aircraft> lst = Aircraft.AircraftMatchingTail(new Aircraft(id).TailNumber); if (lst.Count > 1) { btnMakeDefault.Visible = true; btnMakeDefault.Enabled = (lst[0].AircraftID != id); // only enable it if we aren't the default. } } btnMigrateGeneric.Visible = fAdminMode && fCanMigrate && !MfbEditAircraft1.Aircraft.IsAnonymous && MfbEditAircraft1.Aircraft.InstanceType == AircraftInstanceTypes.RealAircraft; btnMigrateSim.Visible = btnMigrateGeneric.Visible && AircraftUtility.CouldBeSim(MfbEditAircraft1.Aircraft); if (MfbEditAircraft1.AircraftID == Aircraft.idAircraftUnknown) { lblAddEdit1.Text = Resources.Aircraft.AircraftEditAdd; } else { lblAddEdit1.Text = Resources.Aircraft.AircraftEditEdit; lblTail.Text = MfbEditAircraft1.Aircraft.DisplayTailnumberWithModel; } // Remember the return URL, but only if it is relative (for security) string szReturnURL = util.GetStringParam(Request, "Ret"); if (Uri.IsWellFormedUriString(szReturnURL, UriKind.Relative)) { hdnReturnURL.Value = szReturnURL; } } }
protected async void btnDeleteAllOrphans_Click(object sender, EventArgs e) { await Task.Run(() => { foreach (DataKey dk in gvOrphanedAircraft.DataKeys) { AircraftUtility.DeleteOrphanAircraft(Convert.ToInt32(dk.Value, CultureInfo.InvariantCulture)); } }); await RefreshOrphans(); }
protected void gvPseudoGeneric_RowDataBound(object sender, GridViewRowEventArgs e) { if (e != null && e.Row.RowType == DataControlRowType.DataRow) { HyperLink h = (HyperLink)e.Row.FindControl("lnkViewFixedTail"); HyperLink l = (HyperLink)e.Row.FindControl("lblTailnumber"); GroupCollection gc = regexPseudoSim.Match(l.Text).Groups; string szTailnumFixed = l.Text; if (gc != null && gc.Count > 1) { szTailnumFixed = String.Format(CultureInfo.InvariantCulture, "N{0}", gc[1].Value); } else if (regexOOrI.IsMatch(l.Text)) { szTailnumFixed = l.Text.ToUpper(CultureInfo.CurrentCulture).Replace('I', '1').Replace('O', '0'); } else if (szTailnumFixed.StartsWith("N0", StringComparison.CurrentCultureIgnoreCase)) { szTailnumFixed = "N" + szTailnumFixed.Substring(2); } h.Text = HttpUtility.HtmlEncode(String.Format(CultureInfo.CurrentCulture, Resources.Admin.ViewRegistrationTemplate, szTailnumFixed)); h.NavigateUrl = Aircraft.LinkForTailnumberRegistry(szTailnumFixed); h.Visible = !String.IsNullOrEmpty(h.NavigateUrl); int idAircraft = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "idaircraft"), CultureInfo.InvariantCulture); HyperLink hLeadingN = (HyperLink)e.Row.FindControl("lnkRemoveLeadingN"); hLeadingN.Visible = l.Text.StartsWith("N0", StringComparison.CurrentCultureIgnoreCase) || l.Text.StartsWith("NN", StringComparison.CurrentCultureIgnoreCase); hLeadingN.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:trimLeadingN('{0}',{1});", hLeadingN.ClientID, idAircraft); HyperLink hConvertOandI = (HyperLink)e.Row.FindControl("lnkConvertOandI"); hConvertOandI.Visible = regexOOrI.IsMatch(l.Text); hConvertOandI.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:convertOandI('{0}',{1});", hConvertOandI.ClientID, idAircraft); HyperLink hMigrateGeneric = (HyperLink)e.Row.FindControl("lnkMigrateGeneric"); hMigrateGeneric.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:migrateGeneric('{0}',{1});", hMigrateGeneric.ClientID, idAircraft); HyperLink hN0ToN = (HyperLink)e.Row.FindControl("lnkN0ToN"); hN0ToN.Visible = l.Text.StartsWith("N0", StringComparison.CurrentCultureIgnoreCase); hN0ToN.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:trimN0('{0}', {1});", hN0ToN.ClientID, idAircraft); HyperLink hMigSim = (HyperLink)e.Row.FindControl("lnkMigrateSim"); hMigSim.Visible = AircraftUtility.PseudoSimTypeFromTail(l.Text) != AircraftInstanceTypes.RealAircraft; hMigSim.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:migrateSim('{0}', {1});", hMigSim.ClientID, idAircraft); HyperLink hViewFlights = (HyperLink)e.Row.FindControl("lnkFlights"); hViewFlights.NavigateUrl = String.Format(CultureInfo.InvariantCulture, "javascript:viewFlights({0}, '{1}');", idAircraft, l.Text); } }
protected void btnMigrateSim_Click(object sender, EventArgs e) { Aircraft acOriginal = new Aircraft(MfbEditAircraft1.AircraftID); int idNew = AircraftUtility.MapToSim(acOriginal); if (idNew == Aircraft.idAircraftUnknown) { lblErr.Text = Resources.Aircraft.AdminNotASim; } else { Response.Redirect(Request.Url.PathAndQuery.Replace(String.Format(CultureInfo.InvariantCulture, "id={0}", acOriginal.AircraftID), String.Format(CultureInfo.InvariantCulture, "id={0}", idNew))); } }
protected void gvDupeAircraft_RowCommand(object sender, CommandEventArgs e) { if (e == null) { throw new ArgumentNullException(nameof(e)); } int rowClicked = Convert.ToInt32(e.CommandArgument, CultureInfo.InvariantCulture); GridViewRow gvr = gvDupeAircraft.Rows[rowClicked]; string szTail = gvr.Cells[0].Text; List <Aircraft> lstDupes = Aircraft.AircraftMatchingTail(szTail); // Sort by instance type, then model ID. We'll be grouping by these two. lstDupes.Sort((ac1, ac2) => { return((ac1.InstanceTypeID == ac2.InstanceTypeID) ? ac1.ModelID - ac2.ModelID : ac1.InstanceTypeID - ac2.InstanceTypeID); }); // Now go through the list. If adjacent aircraft are the same, merge them. // TODO: This actually has a bug where if we have 3 aircraft to merge, it does the 1st two then exits the loop. for (int i = 0; i < lstDupes.Count - 1; i++) { Aircraft acThis = lstDupes[i]; Aircraft acNext = lstDupes[i + 1]; if (acThis.InstanceTypeID == acNext.InstanceTypeID && acThis.ModelID == acNext.ModelID) { AircraftUtility.AdminMergeDupeAircraft(acThis, acNext); lstDupes.RemoveAt(i + 1); } if (acThis.Version != i) { acThis.Version = i; acThis.Commit(); } if (acNext.Version != i + 1) { acNext.Version = i + 1; acNext.Commit(); } } // Now hide each row that matched - just for speed; much quicker than rebinding the data string szNormalTail = Aircraft.NormalizeTail(szTail); foreach (GridViewRow gvrow in gvDupeAircraft.Rows) { if (String.Compare(Aircraft.NormalizeTail(gvrow.Cells[0].Text), szNormalTail, StringComparison.OrdinalIgnoreCase) == 0) { gvrow.Visible = false; } } }
protected void MergeClone(Aircraft ac, Aircraft acClone) { if (ac == null) { throw new ArgumentNullException(nameof(ac)); } if (acClone == null) { throw new ArgumentNullException(nameof(acClone)); } if (!acClone.IsNew) { AircraftUtility.AdminMergeDupeAircraft(ac, acClone); Response.Redirect(String.Format(CultureInfo.InvariantCulture, "~/Member/EditAircraft.aspx?id={0}", ac.AircraftID)); } }
protected void gvDupeSims_RowCommand(object sender, CommandEventArgs e) { if (e == null) { throw new ArgumentNullException(nameof(e)); } int rowClicked = Convert.ToInt32(e.CommandArgument, CultureInfo.InvariantCulture); GridViewRow gvr = gvDupeSims.Rows[rowClicked]; int idInstanceTypeKeep = Convert.ToInt32(gvr.Cells[0].Text, CultureInfo.InvariantCulture); int idModelKeep = Convert.ToInt32(gvr.Cells[1].Text, CultureInfo.InvariantCulture); int idAircraftKeep = Convert.ToInt32(gvr.Cells[2].Text, CultureInfo.InvariantCulture); Aircraft acMaster = new Aircraft(idAircraftKeep); List <int> lstDupesToMerge = new List <int>(); foreach (GridViewRow gvrow in gvDupeSims.Rows) { int idInstanceType = Convert.ToInt32(gvrow.Cells[0].Text, CultureInfo.InvariantCulture); int idModel = Convert.ToInt32(gvrow.Cells[1].Text, CultureInfo.InvariantCulture); int idAircraft = Convert.ToInt32(gvrow.Cells[2].Text, CultureInfo.InvariantCulture); if (idAircraft != idAircraftKeep && idInstanceType == idInstanceTypeKeep && idModel == idModelKeep) { lstDupesToMerge.Add(idAircraft); } } try { // Merge each of the dupes to the one we want to keep foreach (int acID in lstDupesToMerge) { Aircraft ac = new Aircraft(acID); AircraftUtility.AdminMergeDupeAircraft(acMaster, ac); } // refresh the list. gvDupeSims.DataBind(); } catch (MyFlightbookException ex) { Label lblErr = (Label)gvr.FindControl("lblError"); lblErr.Text = ex.Message; return; } }
public static void MigrateSim(int idAircraft) { if (!HttpContext.Current.User.Identity.IsAuthenticated || String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { throw new MyFlightbookException("Unauthenticated call to MigrateSim"); } Aircraft ac = new Aircraft(idAircraft); if (String.IsNullOrWhiteSpace(ac.TailNumber) || ac.AircraftID <= 0) { throw new MyFlightbookValidationException(String.Format(CultureInfo.CurrentCulture, "No aircraft with ID {0}", idAircraft)); } if (AircraftUtility.MapToSim(ac) < 0) { throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Unable to map aircraft {0}", ac.TailNumber)); } }
protected void btnMigrateGeneric_Click(object sender, EventArgs e) { Aircraft acOriginal = new Aircraft(MfbEditAircraft1.AircraftID); // See if there is a generic for the model string szTailNumGeneric = Aircraft.AnonymousTailnumberForModel(acOriginal.ModelID); Aircraft acGeneric = new Aircraft(szTailNumGeneric); if (acGeneric.IsNew) { acGeneric.TailNumber = szTailNumGeneric; acGeneric.ModelID = acOriginal.ModelID; acGeneric.InstanceType = AircraftInstanceTypes.RealAircraft; acGeneric.Commit(); } AircraftUtility.AdminMergeDupeAircraft(acGeneric, acOriginal); Response.Redirect(Request.Url.PathAndQuery.Replace(String.Format(CultureInfo.InvariantCulture, "id={0}", acOriginal.AircraftID), String.Format(CultureInfo.InvariantCulture, "id={0}", acGeneric.AircraftID))); }
protected void btnDeleteDupeMake_Click(object sender, EventArgs e) { StringBuilder sbAudit = new StringBuilder("<br /><br /><b>Audit of changes made:</b><br />"); int idModelToDelete = Convert.ToInt32(cmbModelToDelete.SelectedValue, CultureInfo.InvariantCulture); int idModelToMergeInto = Convert.ToInt32(cmbModelToMergeInto.SelectedValue, CultureInfo.InvariantCulture); // Before we migrate old aircraft, see if there are generics. Aircraft acGenericSource = new Aircraft(Aircraft.AnonymousTailnumberForModel(idModelToDelete)); Aircraft acGenericTarget = new Aircraft(Aircraft.AnonymousTailnumberForModel(idModelToMergeInto)); if (acGenericSource.AircraftID != Aircraft.idAircraftUnknown) { // if the generic for the target doesn't exist, then no problem - just rename it and remap it! if (acGenericTarget.AircraftID == Aircraft.idAircraftUnknown) { acGenericSource.ModelID = idModelToMergeInto; acGenericSource.TailNumber = Aircraft.AnonymousTailnumberForModel(idModelToMergeInto); acGenericSource.Commit(); } else { // if the generic for the target also exists, need to merge the aircraft (creating a tombstone). AircraftUtility.AdminMergeDupeAircraft(acGenericTarget, acGenericSource); } } using (IDataReader idr = (IDataReader)sqlAirplanesToReMap.Select(DataSourceSelectArguments.Empty)) { // migrate the aircraft on the old model to the new model while (idr.Read()) { string szIdAircraft = idr["idaircraft"].ToString(); Aircraft ac = new Aircraft(Convert.ToInt32(szIdAircraft, CultureInfo.InvariantCulture)) { ModelID = idModelToMergeInto }; ac.Commit(); sbAudit.Append(String.Format(CultureInfo.CurrentCulture, "Updated aircraft {0} to model {1}<br />", szIdAircraft, idModelToMergeInto)); } } // Update any custom currency references to the old model DBHelper dbhCCR = new DBHelper("UPDATE CustCurrencyRef SET value=?newID WHERE value=?oldID AND type=?modelsType"); dbhCCR.DoNonQuery((comm) => { comm.Parameters.AddWithValue("newID", idModelToMergeInto); comm.Parameters.AddWithValue("oldID", idModelToDelete); comm.Parameters.AddWithValue("modelsType", (int)MyFlightbook.Currency.CustomCurrency.CurrencyRefType.Models); }); // Then delete the old model string szQ = String.Format(CultureInfo.InvariantCulture, "DELETE FROM models WHERE idmodel={0}", idModelToDelete); DBHelper dbh = new DBHelper(szQ); if (!dbh.DoNonQuery()) { throw new MyFlightbookException("Error deleting model: " + szQ + "\r\n" + dbh.LastError); } sbAudit.Append(szQ + "<br />"); lblPreview.Text = sbAudit.ToString(); RefreshDupeModels(); gvOrphanMakes.DataBind(); cmbModelToDelete.DataBind(); cmbModelToMergeInto.DataBind(); pnlPreview.Visible = false; }
protected void AircraftUpdated(object sender, EventArgs e) { AircraftUtility.SaveLastTail(MfbEditAircraft1.AircraftID); Response.Redirect(!String.IsNullOrEmpty(hdnReturnURL.Value) ? hdnReturnURL.Value : "Aircraft.aspx"); }