Esempio n. 1
0
    protected void PreviewData()
    {
        lblError.Text = "";

        mvPreviewResults.SetActiveView(vwPreviewResults);                       // default to showing results.

        mfbImportAircraft1.CandidatesForImport = new AircraftImportMatchRow[0]; // start fresh every time.

        byte[] rgb = CurrentCSVSource;
        if (rgb == null || rgb.Length == 0)
        {
            lblFileRequired.Text = Resources.LogbookEntry.errImportInvalidCSVFile;
            SetWizardStep(wsUpload);
            return;
        }

        // issue #280: some files have \r\r\n as line separators!
        rgb = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rgb).Replace("\r\r\n", "\r\n"));

        // Validate the file
        ExternalFormatImporter efi = ExternalFormatImporter.GetImporter(rgb);

        if (efi != null)
        {
            rgb           = efi.PreProcess(rgb);
            IsPendingOnly = efi.IsPendingOnly;
        }

        MemoryStream ms = new MemoryStream(rgb);

        try
        {
            pnlConverted.Visible = pnlAudit.Visible = false;
            lblAudit.Text        = string.Empty;
            using (CSVAnalyzer csvAnalyzer = new CSVAnalyzer(ms))
            {
                ms = null;  // Avoid CA2202
                CSVAnalyzer.CSVStatus result = csvAnalyzer.Status;
                hdnAuditState.Value = result.ToString();

                if (result != CSVAnalyzer.CSVStatus.Broken)
                {
                    string szCSV = null;
                    if (efi == null)    // was already CSV - only update it if it was fixed (vs. broken)
                    {
                        if (result == CSVAnalyzer.CSVStatus.Fixed)
                        {
                            szCSV = csvAnalyzer.DataAsCSV;
                        }
                    }
                    else  // But if it was converted, ALWAYS update the CSV.
                    {
                        szCSV = efi.CSVFromDataTable(csvAnalyzer.Data);
                    }

                    if (szCSV != null)
                    {
                        CurrentCSVSource = rgb = System.Text.Encoding.UTF8.GetBytes(szCSV);
                    }

                    // And show conversion, if it was converted
                    if (efi != null)
                    {
                        lblFileWasConverted.Text = String.Format(CultureInfo.CurrentCulture, Resources.LogbookEntry.importLabelFileWasConverted, efi.Name);
                        pnlConverted.Visible     = true;
                    }
                }

                if (result != CSVAnalyzer.CSVStatus.OK)
                {
                    pnlAudit.Visible = true;
                }
                lblAudit.Text = csvAnalyzer.Audit;

                pnlAudit.Visible = pnlConverted.Visible || !String.IsNullOrEmpty(lblAudit.Text);
            }
        }
        finally
        {
            if (ms != null)
            {
                ms.Dispose();
            }
        }

        ErrorContext.Clear();
        CSVImporter csvimporter = CurrentImporter = new CSVImporter()
        {
            ModelNameMappings = mfbImportAircraft1.ModelMapping
        };
        AutoFillOptions afo = ckAutofill.Checked ? new AutoFillOptions(Request.Cookies)
        {
            IncludeHeliports = true
        } : null;

        using (MemoryStream ms2 = new MemoryStream(rgb))
            csvimporter.FInitFromStream(ms2, User.Identity.Name, AddSuccessRow, AddErrorRow, afo);

        if (csvimporter.FlightsToImport == null)
        {
            lblFileRequired.Text = csvimporter.ErrorMessage;
            SetWizardStep(wsUpload);
            return;
        }

        rptPreview.DataSource = csvimporter.FlightsToImport;
        rptPreview.DataBind();
        mvPreview.SetActiveView(csvimporter.FlightsToImport.Count > 0 ? vwPreview : vwNoResults);

        mvMissingAircraft.SetActiveView(vwNoMissingAircraft); // default state.

        if (csvimporter.FlightsToImport.Count > 0)
        {
            if (csvimporter.HasErrors)
            {
                if (!IsPendingOnly)
                {
                    lblError.Text = String.Format(CultureInfo.InvariantCulture, "<p>{0}</p><p>{1}</p>", Resources.LogbookEntry.ImportPreviewNotSuccessful, lblError.Text);
                }

                List <AircraftImportMatchRow> missing = new List <AircraftImportMatchRow>(csvimporter.MissingAircraft);
                if (missing.Count > 0)
                {
                    mfbImportAircraft1.CandidatesForImport = missing;
                    mvMissingAircraft.SetActiveView(vwAddMissingAircraft);
                }

                ((Button)wzImportFlights.FindControl("FinishNavigationTemplateContainerID$btnNewFile")).Visible = true;
            }

            ((AjaxControlToolkit.ConfirmButtonExtender)wzImportFlights.FindControl("FinishNavigationTemplateContainerID$confirmImportWithErrors")).Enabled = csvimporter.HasErrors;
        }
    }