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

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

            mfbImportAircraft1.CandidatesForImport = Array.Empty <AircraftImportMatchRow>(); // start fresh every time.

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

            int cOriginalSize = rgb.Length;

            pnlConverted.Visible = pnlAudit.Visible = false;

            ExternalFormatConvertResults results = ExternalFormatConvertResults.ConvertToCSV(rgb);

            lblAudit.Text       = results.AuditResult;
            hdnAuditState.Value = results.ResultString;
            CurrentCSVSource    = results.GetConvertedBytes();

            int cConvertedSize = CurrentCSVSource.Length;

            IsPendingOnly = IsPendingOnly || results.IsPendingOnly; // results can change between first preview and import, so if it's true anywhere along the way, preserve that.

            if (!String.IsNullOrEmpty(results.ConvertedName))
            {
                lblFileWasConverted.Text = String.Format(CultureInfo.CurrentCulture, Resources.LogbookEntry.importLabelFileWasConverted, results.ConvertedName);
                pnlConverted.Visible     = true;
            }

            pnlAudit.Visible = (results.IsFixedOrBroken);
            if (results.IsBroken)
            {
                lblAudit.CssClass = "error";
                ExpandoAudit.ExpandoControl.Collapsed   = false;
                ExpandoAudit.ExpandoControl.ClientState = "false";
            }
            else
            {
                lblAudit.CssClass = string.Empty;
                ExpandoAudit.ExpandoControl.Collapsed   = true;
                ExpandoAudit.ExpandoControl.ClientState = "true";
            }

            pnlAudit.Visible = pnlConverted.Visible || !String.IsNullOrEmpty(lblAudit.Text);

            ErrorContext.Clear();
            CSVImporter csvimporter = CurrentImporter = new CSVImporter(mfbImportAircraft1.ModelMapping);

            csvimporter.InitWithBytes(CurrentCSVSource, User.Identity.Name, AddSuccessRow, AddErrorRow, ckAutofill.Checked);

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

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

            if (wzImportFlights.ActiveStep == wsMissingAircraft)
            {
                EventRecorder.LogCall("Import Preview - User: {user}, Upload size {cbin}, converted size {cbconvert}, flights found: {flightcount}", User.Identity.Name, cOriginalSize, cConvertedSize, csvimporter.FlightsToImport.Count);
            }

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

            if (csvimporter.FlightsToImport.Count > 0)
            {
                if (csvimporter.HasErrors)
                {
                    if (!IsPendingOnly)
                    {
                        lblError.Text = Resources.LogbookEntry.ImportPreviewNotSuccessful;
                    }

                    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;
            }
        }
Esempio n. 2
0
        public static ExternalFormatConvertResults ConvertToCSV(byte[] rgb)
        {
            ExternalFormatConvertResults result = new ExternalFormatConvertResults
            {
                // issue #280: some files have \r\r\n as line separators!
                _newRGB = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rgb).Replace("\r\r\n", "\r\n")),

                InputStatus = CSVAnalyzer.CSVStatus.OK
            };

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

            if (efi != null)
            {
                try
                {
                    rgb = efi.PreProcess(rgb);
                    result.IsPendingOnly = efi.IsPendingOnly;
                }
                catch (Exception ex) when(ex is MyFlightbookException || ex is MyFlightbookValidationException)
                {
                    result.InputStatus = CSVAnalyzer.CSVStatus.Broken;
                    result.AuditResult = ex.Message;
                }
            }

            if (result.InputStatus != CSVAnalyzer.CSVStatus.Broken)
            {
                using (DataTable dt = new DataTable()
                {
                    Locale = CultureInfo.CurrentCulture
                })
                {
                    CSVAnalyzer csvAnalyzer;
                    using (MemoryStream ms = new MemoryStream(rgb))
                    {
                        csvAnalyzer = new CSVAnalyzer(ms, dt);
                    }
                    result.InputStatus = csvAnalyzer.Status;

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

                        if (szCSV != null)
                        {
                            result._newRGB = rgb = Encoding.UTF8.GetBytes(szCSV);
                        }

                        // And show conversion, if it was converted
                        if (efi != null)
                        {
                            result.ConvertedName = efi.Name;
                        }
                    }

                    result.AuditResult = csvAnalyzer.Audit;
                }
            }

            return(result);
        }