/// <summary> Reads the bibliographic information from an existing MARC XML file into the current object </summary>
 /// <param name="fileName">Name of the MARC XML file</param>
 public bool Read_From_MARC_XML(string fileName)
 {
     string errorMessage;
     MarcXML_File_ReaderWriter reader = new MarcXML_File_ReaderWriter();
     return reader.Read_Metadata(fileName, this, null, out errorMessage);
 }
 /// <summary> Reads the bibliographic information from an existing MARC XML file and returns a new object </summary>
 /// <param name="fileName">Name of the MARC XML file</param>
 /// <returns>Built SobekCM_Item object</returns>
 public static SobekCM_Item Read_MARC_XML(string fileName)
 {
     SobekCM_Item returnVal = new SobekCM_Item();
     string errorMessage;
     MarcXML_File_ReaderWriter reader = new MarcXML_File_ReaderWriter();
     reader.Read_Metadata(fileName, returnVal, null, out errorMessage);
     return returnVal;
 }
        /// <summary> Do the bulk of the work of stepping through the input file and
        /// copying the data from the MARC record to the Tracking data object. </summary>
        public void Do_Work()
        {
            string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            errors.Clear();
            errorCnt = 0;
            //int error = 0;

            // Declare the marc xml reader
            MarcXML_File_ReaderWriter marcReader = new MarcXML_File_ReaderWriter();

            try
            {
                // Read the first record
                MARC_Record result = parser.Parse( inputFile );

                // Loop through all the records
                while (parser.EOF_Flag == false)
                {
                    // Fire the event that one item is complete
                    OnNewProgress(recordsProcessed);

                    try
                    {
                        // check if the record is null or has errors
                        if (result == null)
                        {
                            // increment counters
                            errorCnt++;
                            recordsProcessed++;

                            // get next record
                            result = parser.Next();
                            continue;
                        }

                        // Save the MARC XML file
                        string marc_xml_file = Save_MARC_XML( result );

                        // If we should only save these, move on
                        if (JustSaveMarcXML)
                        {
                            recordsProcessed++;

                            // get next record
                            result = parser.Next();
                            continue;
                        }

                        // If there was no marc xml file saved, skip this and call it an error
                        if ((marc_xml_file.Length == 0) || (!File.Exists(marc_xml_file)))
                        {
                            // increment counters
                            errorCnt++;
                            recordsProcessed++;

                            // get next record
                            result = parser.Next();
                            continue;
                        }

                        // Load the information from the MARC XML file
                        SobekCM_Item newItem = new SobekCM_Item();
                        string error_message = String.Empty;
                        marcReader.Read_Metadata( marc_xml_file, newItem, null, out error_message );

                        // FOR THE MARC IMPORTER ONLY.. THE MAIN TITLE SHOULD BE COPIED TO THE BIB TITLE
                        if (newItem != null)
                        {
                            newItem.Behaviors.GroupTitle = newItem.Bib_Info.Main_Title.Title.Trim();
                        }

                        // try adding some data if the error flag is true
                        if ((result.Error_Flag) || ( newItem == null ))
                        {
                            // Add this to the result table being built
                            base.report.Add_Item(newItem, marc_xml_file, "Error reading MARC record");

                            // increment counters
                            errorCnt++;
                            recordsProcessed++;
                        }
                        else
                        {
                            // Copy all user settings to this package
                            base.Copy_User_Settings_To_Package(newItem);

                            // Save this to the tracking database
                            bool success = base.Check_For_Existence_And_Save(newItem, null, marc_xml_file, matching_message, "MARC Importer GUI", preview_mode);

                            // If that was successful, save a new METS file and add to item list
                            if (success)
                            {
                                // Save the METS
                                newItem.METS_Header.Creator_Software = "Spreadsheet Importer";
                                newItem.METS_Header.Creator_Individual = username;
                                save_to_mets(newItem, preview_mode);
                            }
                        }
                    }
                    catch (Exception ee)
                    {
                        DLC.Tools.Forms.ErrorMessageBox.Show(ee.Message, "DLC Importer Error", ee);
                        errorCnt++;
                    }

                    // Fire the event that one item is complete
                    OnNewProgress(recordsProcessed);

                    // Get the next one
                    result = parser.Next();
                }

                // display messagebox that import is complete
                if (preview_mode)
                {
                    MessageBox.Show("records processed:\t\t[ " + recordsProcessed.ToString("#,##0;") + " ]" +
                                    "\n\n records saved:\t\t[ " + recordsSavedToDB.ToString("#,##0;") + " ]" +
                                    "\n\n records skipped:\t\t[ " + recordsSkipped.ToString("#,##0;") + " ]" +
                                    "\n\n records with errors:\t[ " + errorCnt.ToString("#,##0;") + " ]", "Preview Complete!");
                }
                else
                {
                    MessageBox.Show("records processed:\t\t[ " + recordsProcessed.ToString("#,##0;") + " ]" +
                                    "\n\n records saved:\t\t[ " + recordsSavedToDB.ToString("#,##0;") + " ]" +
                                    "\n\n records skipped:\t\t[ " + recordsSkipped.ToString("#,##0;") + " ]" +
                                    "\n\n records with errors:\t[ " + errorCnt.ToString("#,##0;") + " ]", "Import Complete!");
                }

                recordsSavedToDB = 0;
            }
            catch (Exception ee)
            {
                MessageBox.Show("Error encountered while processing!     \n\nOnly " + recordsProcessed + " records processed.\n\n" + ee.ToString(), "Did not reach EOF Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
            }

            // Fire the event that the entire work is complete
            OnComplete(999999);
        }