Exemple #1
0
    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)));
    }
Exemple #2
0
        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 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 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;
            }
        }
        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 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;
        }