Пример #1
0
        /// <summary> Saves the MarcXML file, used for creating MARC feeds, for this incoming digital resource </summary>
        /// <param name="Collection_Codes"> Collection codes to include in the resultant MarcXML file </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public bool Save_MARC_XML(DataTable Collection_Codes)
        {
            try
            {
                // Set the image location
                string greenstoneLink = SobekCM_Library_Settings.Image_URL;
                bibPackage.Web.Image_Root = greenstoneLink + bibPackage.Web.File_Root.Replace("\\", "/");
                if (bibPackage.Web.Image_Root.IndexOf("/" + vid) < 0)
                {
                    bibPackage.Web.Image_Root = bibPackage.Web.Image_Root + "/" + vid;
                }
                bibPackage.Web.Set_BibID_VID(bibPackage.BibID, bibPackage.VID);


                List <string> collectionnames = new List <string>();
                // Get the collection names
                if ((bibPackage.Behaviors.Aggregation_Count > 0) && (Collection_Codes != null))
                {
                    collectionnames.AddRange(from aggregation in bibPackage.Behaviors.Aggregations select aggregation.Code into altCollection select Collection_Codes.Select("collectioncode = '" + altCollection + "'") into altCode where altCode.Length > 0 select altCode[0]["ShortName"].ToString());
                }

                // Save the marc xml file
                MarcXML_File_ReaderWriter marcWriter = new MarcXML_File_ReaderWriter();
                string Error_Message;
                Dictionary <string, object> options = new Dictionary <string, object>();
                options["MarcXML_File_ReaderWriter:Additional_Tags"] = bibPackage.MARC_Sobek_Standard_Tags(collectionnames, true, SobekCM_Library_Settings.System_Name, SobekCM_Library_Settings.System_Abbreviation);
                return(marcWriter.Write_Metadata(bibPackage.Source_Directory + "\\marc.xml", bibPackage, options, out Error_Message));
            }
            catch
            {
                return(false);
            }
        }
Пример #2
0
        /// <summary> Saves the MarcXML file, used for creating MARC feeds, for this incoming digital resource </summary>
        /// <param name="CollectionCodes"> Collection codes to include in the resultant MarcXML file </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public bool Save_MARC_XML(DataTable CollectionCodes, InstanceWide_Settings Settings)
        {
            try
            {
                // Set the image location
                Metadata.Web.Image_Root = Settings.Servers.Image_URL + Metadata.Web.File_Root.Replace("\\", "/");
                Metadata.Web.Set_BibID_VID(Metadata.BibID, Metadata.VID);


                List <string> collectionnames = new List <string>();
                // Get the collection names
                if ((Metadata.Behaviors.Aggregation_Count > 0) && (CollectionCodes != null))
                {
                    collectionnames.AddRange(from aggregation in Metadata.Behaviors.Aggregations select aggregation.Code into altCollection select CollectionCodes.Select("collectioncode = '" + altCollection + "'") into altCode where altCode.Length > 0 select altCode[0]["ShortName"].ToString());
                }

                // Create the options dictionary used when saving information to the database, or writing MarcXML
                Dictionary <string, object> options = new Dictionary <string, object>();
                if (Settings.MarcGeneration != null)
                {
                    options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] = Settings.MarcGeneration.Cataloging_Source_Code;
                    options["MarcXML_File_ReaderWriter:MARC Location Code"]          = Settings.MarcGeneration.Location_Code;
                    options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"]    = Settings.MarcGeneration.Reproduction_Agency;
                    options["MarcXML_File_ReaderWriter:MARC Reproduction Place"]     = Settings.MarcGeneration.Reproduction_Place;
                    options["MarcXML_File_ReaderWriter:MARC XSLT File"] = Settings.MarcGeneration.XSLT_File;
                }
                options["MarcXML_File_ReaderWriter:System Name"]         = Settings.System.System_Name;
                options["MarcXML_File_ReaderWriter:System Abbreviation"] = Settings.System.System_Abbreviation;

                // Save the marc xml file
                MarcXML_File_ReaderWriter marcWriter = new MarcXML_File_ReaderWriter();
                string errorMessage;
                return(marcWriter.Write_Metadata(Metadata.Source_Directory + "\\marc.xml", Metadata, options, out errorMessage));
            }
            catch
            {
                return(false);
            }
        }
Пример #3
0
        /// <summary> Saves a MarcXML file within the digital resource folder </summary>
        /// <param name="Resource"> Incoming digital resource object </param>
        /// <returns> TRUE if processing can continue, FALSE if a critical error occurred which should stop all processing </returns>
        public override bool DoWork(Incoming_Digital_Resource Resource)
        {
            try
            {
                // Set the image location
                Resource.Metadata.Web.Image_Root = Settings.Servers.Image_URL + Resource.Metadata.Web.File_Root.Replace("\\", "/");
                Resource.Metadata.Web.Set_BibID_VID(Resource.Metadata.BibID, Resource.Metadata.VID);

                // Create the options dictionary used when saving information to the database, or writing MarcXML
                Dictionary <string, object> options = new Dictionary <string, object>();
                if (Engine_ApplicationCache_Gateway.Settings.MarcGeneration != null)
                {
                    options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Cataloging_Source_Code;
                    options["MarcXML_File_ReaderWriter:MARC Location Code"]          = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Location_Code;
                    options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"]    = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Agency;
                    options["MarcXML_File_ReaderWriter:MARC Reproduction Place"]     = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Place;
                    options["MarcXML_File_ReaderWriter:MARC XSLT File"] = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.XSLT_File;
                }
                options["MarcXML_File_ReaderWriter:System Name"]         = Engine_ApplicationCache_Gateway.Settings.System.System_Name;
                options["MarcXML_File_ReaderWriter:System Abbreviation"] = Engine_ApplicationCache_Gateway.Settings.System.System_Abbreviation;

                // Save the marc xml file
                MarcXML_File_ReaderWriter marcWriter = new MarcXML_File_ReaderWriter();
                string errorMessage;
                if (!marcWriter.Write_Metadata(Resource.Metadata.Source_Directory + "\\marc.xml", Resource.Metadata, options, out errorMessage))
                {
                    OnError("Error while saving the MarcXML : " + errorMessage, Resource.BibID + ":" + Resource.VID, Resource.METS_Type_String, Resource.BuilderLogId);
                }
            }
            catch (Exception ee)
            {
                OnError("Exception caught while saving the MarcXML : " + ee.Message, Resource.BibID + ":" + Resource.VID, Resource.METS_Type_String, Resource.BuilderLogId);
            }

            return(true);
        }
        private void complete_item_submission(SobekCM_Item Item_To_Complete, Custom_Tracer Tracer)
        {
            // If this is a newspaper type, and the pubdate has a value, try to use that for the serial heirarchy
            if ((Item_To_Complete.Behaviors.Serial_Info.Count == 0) && (Item_To_Complete.Bib_Info.Origin_Info.Date_Issued.Length > 0) && (Item_To_Complete.Bib_Info.SobekCM_Type == TypeOfResource_SobekCM_Enum.Newspaper))
            {
                DateTime asDateTime;
                if (DateTime.TryParse(Item_To_Complete.Bib_Info.Origin_Info.Date_Issued, out asDateTime))
                {
                    hierarchyCopiedFromDate = true;
                    Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(1, asDateTime.Year, asDateTime.Year.ToString());
                    switch (asDateTime.Month)
                    {
                    case 1:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "January");
                        break;

                    case 2:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "February");
                        break;

                    case 3:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "March");
                        break;

                    case 4:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "April");
                        break;

                    case 5:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "May");
                        break;

                    case 6:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "June");
                        break;

                    case 7:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "July");
                        break;

                    case 8:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "August");
                        break;

                    case 9:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "September");
                        break;

                    case 10:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "October");
                        break;

                    case 11:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "November");
                        break;

                    case 12:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "December");
                        break;
                    }

                    Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(3, asDateTime.Day, asDateTime.Day.ToString());
                }
            }

            // Determine the in process directory for this
            string user_in_process_directory = UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location + "\\" + RequestSpecificValues.Current_User.UserName.Replace(".", "").Replace("@", "") + "\\newitem";

            if (RequestSpecificValues.Current_User.ShibbID.Trim().Length > 0)
            {
                user_in_process_directory = UI_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location + "\\" + RequestSpecificValues.Current_User.ShibbID + "\\newitem";
            }

            // Ensure this directory exists
            if (!Directory.Exists(user_in_process_directory))
            {
                Directory.CreateDirectory(user_in_process_directory);
            }

            // Now, delete all the files in the processing directory
            string[] all_files = Directory.GetFiles(user_in_process_directory);
            foreach (string thisFile in all_files)
            {
                File.Delete(thisFile);
            }

            // Save to the database
            Item_To_Complete.Web.File_Root = Item_To_Complete.BibID.Substring(0, 2) + "\\" + Item_To_Complete.BibID.Substring(2, 2) + "\\" + Item_To_Complete.BibID.Substring(4, 2) + "\\" + Item_To_Complete.BibID.Substring(6, 2) + "\\" + Item_To_Complete.BibID.Substring(8, 2);
            SobekCM_Item_Database.Save_New_Digital_Resource(Item_To_Complete, false, false, RequestSpecificValues.Current_User.UserName, String.Empty, -1);

            // Assign the file root and assoc file path
            Item_To_Complete.Web.AssocFilePath = Item_To_Complete.Web.File_Root + "\\" + Item_To_Complete.VID + "\\";

            // Create the static html pages
            string base_url = RequestSpecificValues.Current_Mode.Base_URL;

            // Save the rest of the metadata
            Item_To_Complete.Source_Directory = user_in_process_directory;
            Item_To_Complete.Save_SobekCM_METS();

            Database.SobekCM_Database.Add_Item_To_User_Folder(RequestSpecificValues.Current_User.UserID, "Submitted Items", Item_To_Complete.BibID, Item_To_Complete.VID, 0, String.Empty, Tracer);

            // Save Bib_Level METS?
            //SobekCM.Resource_Object.Writers.OAI_Writer oaiWriter = new SobekCM.Resource_Object.Writers.OAI_Writer();
            //oaiWriter.Save_OAI_File(bibPackage, resource_folder + "\\oai_dc.xml", bibPackage.Processing_Parameters.Collection_Primary.ToLower(), createDate);

            // If there was no match, try to save to the tracking database
            Engine_Database.Tracking_Online_Submit_Complete(Item_To_Complete.Web.ItemID, RequestSpecificValues.Current_User.Full_Name, String.Empty);

            // Create the options dictionary used when saving information to the database, or writing MarcXML
            Dictionary <string, object> options = new Dictionary <string, object>();

            if (UI_ApplicationCache_Gateway.Settings.MarcGeneration != null)
            {
                options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] = UI_ApplicationCache_Gateway.Settings.MarcGeneration.Cataloging_Source_Code;
                options["MarcXML_File_ReaderWriter:MARC Location Code"]          = UI_ApplicationCache_Gateway.Settings.MarcGeneration.Location_Code;
                options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"]    = UI_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Agency;
                options["MarcXML_File_ReaderWriter:MARC Reproduction Place"]     = UI_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Place;
                options["MarcXML_File_ReaderWriter:MARC XSLT File"] = UI_ApplicationCache_Gateway.Settings.MarcGeneration.XSLT_File;
            }
            options["MarcXML_File_ReaderWriter:System Name"]         = UI_ApplicationCache_Gateway.Settings.System.System_Name;
            options["MarcXML_File_ReaderWriter:System Abbreviation"] = UI_ApplicationCache_Gateway.Settings.System.System_Abbreviation;

            // Save the MARC file
            MarcXML_File_ReaderWriter marcWriter = new MarcXML_File_ReaderWriter();
            string errorMessage;

            marcWriter.Write_Metadata(Item_To_Complete.Source_Directory + "\\marc.xml", Item_To_Complete, options, out errorMessage);

            // Copy all the files over to the server
            string serverNetworkFolder = UI_ApplicationCache_Gateway.Settings.Servers.Image_Server_Network + Item_To_Complete.Web.AssocFilePath;

            // Create the folder
            if (!Directory.Exists(serverNetworkFolder))
            {
                Directory.CreateDirectory(serverNetworkFolder);
            }
            if (!Directory.Exists(serverNetworkFolder + "\\" + UI_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name))
            {
                Directory.CreateDirectory(serverNetworkFolder + "\\" + UI_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name);
            }

            // Copy the static HTML page over first
            if (File.Exists(user_in_process_directory + "\\" + currentItem.BibID + "_" + currentItem.VID + ".html"))
            {
                File.Copy(user_in_process_directory + "\\" + currentItem.BibID + "_" + currentItem.VID + ".html", serverNetworkFolder + "\\" + UI_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name + "\\" + currentItem.BibID + "_" + currentItem.VID + ".html", true);
                File.Delete(user_in_process_directory + "\\" + currentItem.BibID + "_" + currentItem.VID + ".html");
            }

            // Copy all the files
            string[] allFiles = Directory.GetFiles(user_in_process_directory);
            foreach (string thisFile in allFiles)
            {
                string destination_file = serverNetworkFolder + "\\" + (new FileInfo(thisFile)).Name;
                File.Copy(thisFile, destination_file, true);
            }

            // Incrememnt the count of number of items submitted by this user
            RequestSpecificValues.Current_User.Items_Submitted_Count++;

            // Delete any remaining items
            all_files = Directory.GetFiles(user_in_process_directory);
            foreach (string thisFile in all_files)
            {
                File.Delete(thisFile);
            }
        }
Пример #5
0
        /// <summary> Update the exsting digital resource, by saving the changes to the database and rewriting metadata files </summary>
        /// <param name="Item"> Digital resource object with all the updated metadata </param>
        /// <param name="User"> User who performed the update, for the item milestones </param>
        /// <param name="Error_Message"> [OUT] Return an error message if an exception is encountered </param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool Update_Item(SobekCM_Item Item, User_Object User, out string Error_Message)
        {
            Error_Message = String.Empty;

            // Determine the in process directory for this
            string user_bib_vid_process_directory = Path.Combine(Engine_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, User.ShibbID + "\\metadata_updates\\" + Item.BibID + "_" + Item.VID);

            if (User.ShibbID.Trim().Length == 0)
            {
                user_bib_vid_process_directory = Path.Combine(Engine_ApplicationCache_Gateway.Settings.Servers.In_Process_Submission_Location, User.UserName.Replace(".", "").Replace("@", "") + "\\metadata_updates\\" + Item.BibID + "_" + Item.VID);
            }

            // Ensure the folder exists and is empty to start with
            if (!Directory.Exists(user_bib_vid_process_directory))
            {
                Directory.CreateDirectory(user_bib_vid_process_directory);
            }
            else
            {
                // Anything older than a day should be deleted
                string[] files = Directory.GetFiles(user_bib_vid_process_directory);
                foreach (string thisFile in files)
                {
                    try
                    {
                        File.Delete(thisFile);
                    }
                    catch (Exception)
                    {
                        // Not much to do here
                    }
                }
            }

            // Update the METS file with METS note and name
            Item.METS_Header.Creator_Individual = User.UserName;
            Item.METS_Header.Modify_Date        = DateTime.Now;
            Item.METS_Header.RecordStatus_Enum  = METS_Record_Status.METADATA_UPDATE;

            // Create the options dictionary used when saving information to the database, or writing MarcXML
            Dictionary <string, object> options = new Dictionary <string, object>();

            if (Engine_ApplicationCache_Gateway.Settings.MarcGeneration != null)
            {
                options["MarcXML_File_ReaderWriter:MARC Cataloging Source Code"] = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Cataloging_Source_Code;
                options["MarcXML_File_ReaderWriter:MARC Location Code"]          = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Location_Code;
                options["MarcXML_File_ReaderWriter:MARC Reproduction Agency"]    = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Agency;
                options["MarcXML_File_ReaderWriter:MARC Reproduction Place"]     = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.Reproduction_Place;
                options["MarcXML_File_ReaderWriter:MARC XSLT File"] = Engine_ApplicationCache_Gateway.Settings.MarcGeneration.XSLT_File;
            }
            options["MarcXML_File_ReaderWriter:System Name"]         = Engine_ApplicationCache_Gateway.Settings.System.System_Name;
            options["MarcXML_File_ReaderWriter:System Abbreviation"] = Engine_ApplicationCache_Gateway.Settings.System.System_Abbreviation;
            //  options["MarcXML_File_ReaderWriter:Additional_Tags"] = Item.MARC_Sobek_Standard_Tags(true, Engine_ApplicationCache_Gateway.Settings.System.System_Name, Engine_ApplicationCache_Gateway.Settings.System.System_Abbreviation);


            // Save the METS file and related Items
            bool db_successful_save = true;

            try
            {
                SobekCM_Item_Database.Save_Digital_Resource(Item, options, DateTime.Now, true);
            }
            catch
            {
                db_successful_save = false;
            }

            // Save the data to SOLR
            bool solr_successful_save = true;

            try
            {
                // Save this to the Solr/Lucene database
                if (!String.IsNullOrEmpty(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL))
                {
                    Solr_Controller.Update_Index(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL, Engine_ApplicationCache_Gateway.Settings.Servers.Page_Solr_Index_URL, Item, true);
                }
            }
            catch
            {
                solr_successful_save = false;
            }


            //// Create the static html pages
            //string base_url = RequestSpecificValues.Current_Mode.Base_URL;
            //try
            //{
            //    Static_Pages_Builder staticBuilder = new Static_Pages_Builder(Engine_AppliationCache_Gateway.Settings.Servers.System_Base_URL, Engine_AppliationCache_Gateway.Settings.Servers.Base_Data_Directory, RequestSpecificValues.HTML_Skin.Skin_Code);
            //    string filename = user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html";
            //    staticBuilder.Create_Item_Citation_HTML(Item, filename, Engine_AppliationCache_Gateway.Settings.Servers.Image_Server_Network + Item.Web.AssocFilePath);

            //    // Copy the static HTML file to the web server
            //    try
            //    {
            //        if (!Directory.Exists(Engine_AppliationCache_Gateway.Settings.Servers.Static_Pages_Location + Item.BibID.Substring(0, 2) + "\\" + Item.BibID.Substring(2, 2) + "\\" + Item.BibID.Substring(4, 2) + "\\" + Item.BibID.Substring(6, 2) + "\\" + Item.BibID.Substring(8)))
            //            Directory.CreateDirectory(Engine_AppliationCache_Gateway.Settings.Servers.Static_Pages_Location + Item.BibID.Substring(0, 2) + "\\" + Item.BibID.Substring(2, 2) + "\\" + Item.BibID.Substring(4, 2) + "\\" + Item.BibID.Substring(6, 2) + "\\" + Item.BibID.Substring(8));
            //        if (File.Exists(user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html"))
            //            File.Copy(user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html", Engine_AppliationCache_Gateway.Settings.Servers.Static_Pages_Location + Item.BibID.Substring(0, 2) + "\\" + Item.BibID.Substring(2, 2) + "\\" + Item.BibID.Substring(4, 2) + "\\" + Item.BibID.Substring(6, 2) + "\\" + Item.BibID.Substring(8) + "\\" + Item.BibID + "_" + Item.VID + ".html", true);
            //    }
            //    catch
            //    {
            //        // This is not critical
            //    }
            //}
            //catch
            //{
            //    // Failing to make the static page is not the worst thing in the world...
            //}
            //RequestSpecificValues.Current_Mode.Base_URL = base_url;

            Item.Source_Directory = user_bib_vid_process_directory;
            Item.Save_SobekCM_METS();

            // If this was not able to be saved in the database, try it again
            if (!db_successful_save)
            {
                SobekCM_Item_Database.Save_Digital_Resource(Item, options, DateTime.Now, false);
            }

            // If this was not able to be saved to solr, try it again
            if (!solr_successful_save)
            {
                try
                {
                    // Save this to the Solr/Lucene database
                    if (!String.IsNullOrEmpty(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL))
                    {
                        Solr_Controller.Update_Index(Engine_ApplicationCache_Gateway.Settings.Servers.Document_Solr_Index_URL, Engine_ApplicationCache_Gateway.Settings.Servers.Page_Solr_Index_URL, Item, true);
                    }
                }
                catch
                {
                    solr_successful_save = false;
                }
            }

            // Make sure the progress has been added to this Item's work log
            try
            {
                Engine_Database.Tracking_Online_Edit_Complete(Item.Web.ItemID, User.Full_Name, String.Empty);
            }
            catch (Exception)
            {
                // This is not critical
            }

            // Save the MARC file
            MarcXML_File_ReaderWriter marcWriter = new MarcXML_File_ReaderWriter();
            string errorMessage;

            marcWriter.Write_Metadata(Item.Source_Directory + "\\marc.xml", Item, options, out errorMessage);

            // Determine the server folder
            string serverNetworkFolder = Engine_ApplicationCache_Gateway.Settings.Servers.Image_Server_Network + Item.Web.AssocFilePath;

            // Create the folder
            if (!Directory.Exists(serverNetworkFolder))
            {
                Directory.CreateDirectory(serverNetworkFolder);
                if (!Directory.Exists(serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name))
                {
                    Directory.CreateDirectory(serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name);
                }
            }
            else
            {
                if (!Directory.Exists(serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name))
                {
                    Directory.CreateDirectory(serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name);
                }

                // Rename any existing standard mets to keep a backup
                if (File.Exists(serverNetworkFolder + "\\" + Item.BibID + "_" + Item.VID + ".mets.xml"))
                {
                    FileInfo currentMetsFileInfo = new FileInfo(serverNetworkFolder + "\\" + Item.BibID + "_" + Item.VID + ".mets.xml");
                    DateTime lastModDate         = currentMetsFileInfo.LastWriteTime;
                    File.Copy(serverNetworkFolder + "\\" + Item.BibID + "_" + Item.VID + ".mets.xml", serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name + "\\" + Item.BibID + "_" + Item.VID + "_" + lastModDate.Year + "_" + lastModDate.Month + "_" + lastModDate.Day + ".mets.bak", true);
                }
            }

            // Copy the static HTML page over first
            if (File.Exists(user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html"))
            {
                File.Copy(user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html", serverNetworkFolder + "\\" + Engine_ApplicationCache_Gateway.Settings.Resources.Backup_Files_Folder_Name + "\\" + Item.BibID + "_" + Item.VID + ".html", true);
                File.Delete(user_bib_vid_process_directory + "\\" + Item.BibID + "_" + Item.VID + ".html");
            }

            // Copy all the files
            string[] allFiles = Directory.GetFiles(user_bib_vid_process_directory);
            foreach (string thisFile in allFiles)
            {
                string destination_file = serverNetworkFolder + "\\" + (new FileInfo(thisFile)).Name;
                File.Copy(thisFile, destination_file, true);
            }

            // Now, delete all the files here
            string[] all_files = Directory.GetFiles(user_bib_vid_process_directory);
            foreach (string thisFile in all_files)
            {
                try
                {
                    File.Delete(thisFile);
                }
                catch
                {
                }
            }

            // Clear the User-specific and global cache of this Item
            CachedDataManager.Items.Remove_Digital_Resource_Object(User.UserID, Item.BibID, Item.VID, null);
            CachedDataManager.Items.Remove_Digital_Resource_Object(Item.BibID, Item.VID, null);
            CachedDataManager.Items.Remove_Items_In_Title(Item.BibID, null);

            // Also clear any searches or browses ( in the future could refine this to only remove those
            // that are impacted by this save... but this is good enough for now )
            CachedDataManager.Clear_Search_Results_Browses();

            return(true);
        }
        private void complete_item_submission(SobekCM_Item Item_To_Complete, Custom_Tracer Tracer)
        {
            // If this is a newspaper type, and the pubdate has a value, try to use that for the serial heirarchy
            if ((Item_To_Complete.Behaviors.Serial_Info.Count == 0) && (Item_To_Complete.Bib_Info.Origin_Info.Date_Issued.Length > 0) && (Item_To_Complete.Bib_Info.SobekCM_Type == TypeOfResource_SobekCM_Enum.Newspaper))
            {
                DateTime asDateTime;
                if (DateTime.TryParse(Item_To_Complete.Bib_Info.Origin_Info.Date_Issued, out asDateTime))
                {
                    hierarchyCopiedFromDate = true;
                    Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(1, asDateTime.Year, asDateTime.Year.ToString());
                    switch (asDateTime.Month)
                    {
                    case 1:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "January");
                        break;

                    case 2:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "February");
                        break;

                    case 3:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "March");
                        break;

                    case 4:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "April");
                        break;

                    case 5:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "May");
                        break;

                    case 6:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "June");
                        break;

                    case 7:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "July");
                        break;

                    case 8:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "August");
                        break;

                    case 9:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "September");
                        break;

                    case 10:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "October");
                        break;

                    case 11:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "November");
                        break;

                    case 12:
                        Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(2, asDateTime.Month, "December");
                        break;
                    }

                    Item_To_Complete.Behaviors.Serial_Info.Add_Hierarchy(3, asDateTime.Day, asDateTime.Day.ToString());
                }
            }

            // Determine the in process directory for this
            string user_in_process_directory = SobekCM_Library_Settings.In_Process_Submission_Location + "\\" + user.UserName.Replace(".", "").Replace("@", "") + "\\newitem";

            if (user.UFID.Trim().Length > 0)
            {
                user_in_process_directory = SobekCM_Library_Settings.In_Process_Submission_Location + "\\" + user.UFID + "\\newitem";
            }

            // Ensure this directory exists
            if (!Directory.Exists(user_in_process_directory))
            {
                Directory.CreateDirectory(user_in_process_directory);
            }

            // Now, delete all the files in the processing directory
            string[] all_files = Directory.GetFiles(user_in_process_directory);
            foreach (string thisFile in all_files)
            {
                File.Delete(thisFile);
            }

            // Save to the database
            Item_To_Complete.Web.File_Root = Item_To_Complete.BibID.Substring(0, 2) + "\\" + Item_To_Complete.BibID.Substring(2, 2) + "\\" + Item_To_Complete.BibID.Substring(4, 2) + "\\" + Item_To_Complete.BibID.Substring(6, 2) + "\\" + Item_To_Complete.BibID.Substring(8, 2);
            SobekCM_Database.Save_New_Digital_Resource(Item_To_Complete, false, false, user.UserName, String.Empty, -1);

            // Assign the file root and assoc file path
            Item_To_Complete.Web.AssocFilePath = Item_To_Complete.Web.File_Root + "\\" + Item_To_Complete.VID + "\\";

            // Create the static html pages
            string base_url = currentMode.Base_URL;

            try
            {
                Static_Pages_Builder staticBuilder = new Static_Pages_Builder(SobekCM_Library_Settings.System_Base_URL, SobekCM_Library_Settings.Base_Data_Directory, Translator, codeManager, itemList, iconList, webSkin);
                string filename = user_in_process_directory + "\\" + Item_To_Complete.BibID + "_" + Item_To_Complete.VID + ".html";
                staticBuilder.Create_Item_Citation_HTML(Item_To_Complete, filename, String.Empty);
            }
            catch (Exception ee)
            {
                message = message + "<br /><span style=\"color: red\"><strong>" + ee.Message + "<br />" + ee.StackTrace.Replace("\n", "<br />") + "</strong></span>";
            }

            currentMode.Base_URL = base_url;

            // Save the rest of the metadata
            Item_To_Complete.Source_Directory = user_in_process_directory;
            Item_To_Complete.Save_SobekCM_METS();
            Item_To_Complete.Save_Citation_Only_METS();

            // Add this to the cache
            itemList.Add_SobekCM_Item(Item_To_Complete);

            Database.SobekCM_Database.Add_Item_To_User_Folder(user.UserID, "Submitted Items", Item_To_Complete.BibID, Item_To_Complete.VID, 0, String.Empty, Tracer);

            // Save Bib_Level METS?
            //SobekCM.Resource_Object.Writers.OAI_Writer oaiWriter = new SobekCM.Resource_Object.Writers.OAI_Writer();
            //oaiWriter.Save_OAI_File(bibPackage, resource_folder + "\\oai_dc.xml", bibPackage.Processing_Parameters.Collection_Primary.ToLower(), createDate);

            // If there was no match, try to save to the tracking database
            Database.SobekCM_Database.Tracking_Online_Submit_Complete(Item_To_Complete.Web.ItemID, user.Full_Name, String.Empty);



            List <string>             collectionnames = new List <string>();
            MarcXML_File_ReaderWriter marcWriter      = new MarcXML_File_ReaderWriter();
            string Error_Message;
            Dictionary <string, object> options = new Dictionary <string, object>();

            options["MarcXML_File_ReaderWriter:Additional_Tags"] = Item_To_Complete.MARC_Sobek_Standard_Tags(collectionnames, true, SobekCM_Library_Settings.System_Name, SobekCM_Library_Settings.System_Abbreviation);
            marcWriter.Write_Metadata(Item_To_Complete.Source_Directory + "\\marc.xml", Item_To_Complete, options, out Error_Message);


            // Copy this to all the image servers
            SobekCM_Library_Settings.Refresh(Database.SobekCM_Database.Get_Settings_Complete(Tracer));
            string[] allFiles = Directory.GetFiles(user_in_process_directory);

            // Copy all the files over to the server
            string serverNetworkFolder = SobekCM_Library_Settings.Image_Server_Network + Item_To_Complete.Web.AssocFilePath;

            // Create the folder
            if (!Directory.Exists(serverNetworkFolder))
            {
                Directory.CreateDirectory(serverNetworkFolder);
            }

            foreach (string thisFile in allFiles)
            {
                string destination_file = serverNetworkFolder + "\\" + (new FileInfo(thisFile)).Name;
                File.Copy(thisFile, destination_file, true);
            }

            // Add this to the cache
            itemList.Add_SobekCM_Item(Item_To_Complete);

            // Incrememnt the count of number of items submitted by this user
            user.Items_Submitted_Count++;

            // Delete any remaining items
            all_files = Directory.GetFiles(user_in_process_directory);
            foreach (string thisFile in all_files)
            {
                File.Delete(thisFile);
            }
        }
        /// <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 = WindowsIdentity.GetCurrent().Name;

            errors.Clear();
            errorCnt = 0;
            //int error = 0;

            // Declare the marc xml reader
            MarcXML_File_ReaderWriter marcReader = new MarcXML_File_ReaderWriter();
            string marcread_error = String.Empty;


            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 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();
                        marcReader.Read_Metadata(marc_xml_file, newItem, null, out marcread_error);


                        // 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, "Error reading MARC record");

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

                            // Make sure there is a title
                            if (newItem.Bib_Info.Main_Title.ToString().Trim().Length == 0)
                            {
                                newItem.Bib_Info.Main_Title.Title = "Missing Title";
                            }

                            // Save the METS
                            newItem.METS_Header.Creator_Software = "SobekCM METS Editor";
                            if (MetaTemplate_UserSettings.Individual_Creator.Length > 0)
                            {
                                newItem.METS_Header.Creator_Individual = MetaTemplate_UserSettings.Individual_Creator;
                            }
                            else
                            {
                                newItem.METS_Header.Creator_Individual = username;
                            }
                            newItem.METS_Header.Add_Creator_Individual_Notes("Imported from MARC records");
                            newItem.VID = "00001";
                            newItem.METS_Header.Creator_Software = "SobekCM METS Editor";

                            // Set the next BIBID
                            string next_bibid = next_bibid_counter.ToString();
                            next_bibid_counter++;
                            newItem.BibID = (bibid_start + next_bibid.PadLeft(10 - bibid_start.Length, '0')).ToUpper();

                            if (!save_to_mets(newItem, destination_folder))
                            {
                                errorCnt++;
                                report.Add_Item(newItem, "ERROR while writing METS");
                            }
                            else
                            {
                                report.Add_Item(newItem, "New METS file written");
                            }

                            recordsProcessed++;
                        }
                    }
                    catch (Exception ee)
                    {
                        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
                MessageBox.Show("records processed:\t\t[ " + recordsProcessed.ToString("#,##0;") + " ]" +
                                "\n\n records skipped:\t\t[ " + recordsSkipped.ToString("#,##0;") + " ]" +
                                "\n\n records with errors:\t\t[ " + errorCnt.ToString("#,##0;") + " ]", "Batch METS File Creation Complete!");
            }
            catch (Exception ee)
            {
                MessageBox.Show("Error encountered while processing!     \n\nOnly " + recordsProcessed + " records processed.\n\n" + ee, "Did not reach EOF Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            // Fire the event that the entire work is complete
            OnComplete(999999);
        }
        private void Complete_Item_Save()
        {
            if (isProject)
            {
                // Save the new project METS
                item.Save_METS();

                // Clear the cache of this item
                Cached_Data_Manager.Remove_Project(user.UserID, item.BibID, null);

                // Redirect
                currentMode.Mode             = Display_Mode_Enum.Administrative;
                currentMode.Admin_Type       = Admin_Type_Enum.Projects;
                currentMode.My_Sobek_SubMode = String.Empty;
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
            }
            else
            {
                // Determine the in process directory for this
                string user_bib_vid_process_directory = SobekCM_Library_Settings.In_Process_Submission_Location + "\\" + user.UFID + "\\metadata_updates\\" + item.BibID + "_" + item.VID;
                if (user.UFID.Trim().Length == 0)
                {
                    user_bib_vid_process_directory = SobekCM_Library_Settings.In_Process_Submission_Location + "\\" + user.UserName.Replace(".", "").Replace("@", "") + "\\metadata_updates\\" + item.BibID + "_" + item.VID;
                }

                // Ensure the folder exists and is empty to start with
                if (!Directory.Exists(user_bib_vid_process_directory))
                {
                    Directory.CreateDirectory(user_bib_vid_process_directory);
                }
                else
                {
                    // Anything older than a day should be deleted
                    string[] files = Directory.GetFiles(user_bib_vid_process_directory);
                    foreach (string thisFile in files)
                    {
                        try
                        {
                            File.Delete(thisFile);
                        }
                        catch (Exception)
                        {
                            // Not much to do here
                        }
                    }
                }

                // Update the METS file with METS note and name
                item.METS_Header.Creator_Individual = user.UserName;
                item.METS_Header.Modify_Date        = DateTime.Now;
                item.METS_Header.RecordStatus_Enum  = METS_Record_Status.METADATA_UPDATE;

                // Save the METS file and related items
                bool successful_save = true;
                try
                {
                    SobekCM_Database.Save_Digital_Resource(item, DateTime.Now, true);
                }
                catch
                {
                    successful_save = false;
                }

                // Create the static html pages
                string base_url = currentMode.Base_URL;
                try
                {
                    Static_Pages_Builder staticBuilder = new Static_Pages_Builder(SobekCM_Library_Settings.System_Base_URL, SobekCM_Library_Settings.Base_Data_Directory, Translator, codeManager, itemList, iconList, webSkin);
                    string filename = user_bib_vid_process_directory + "\\" + item.BibID + "_" + item.VID + ".html";
                    staticBuilder.Create_Item_Citation_HTML(item, filename, SobekCM_Library_Settings.Image_Server_Network + item.Web.AssocFilePath);
                }
                catch (Exception)
                {
                    // Failing to make the static page is not the worst thing in the world...
                }

                currentMode.Base_URL = base_url;

                item.Source_Directory = user_bib_vid_process_directory;
                item.Save_SobekCM_METS();
                item.Save_Citation_Only_METS();


                // If this was not able to be saved in the UFDC database, try it again
                if (!successful_save)
                {
                    SobekCM_Database.Save_Digital_Resource(item, DateTime.Now, false);
                }

                // Make sure the progress has been added to this item's work log
                try
                {
                    Database.SobekCM_Database.Tracking_Online_Edit_Complete(item.Web.ItemID, user.Full_Name, String.Empty);
                }
                catch (Exception)
                {
                    // This is not critical
                }

                List <string>             collectionnames = new List <string>();
                MarcXML_File_ReaderWriter marcWriter      = new MarcXML_File_ReaderWriter();
                string Error_Message;
                Dictionary <string, object> options = new Dictionary <string, object>();
                options["MarcXML_File_ReaderWriter:Additional_Tags"] = item.MARC_Sobek_Standard_Tags(collectionnames, true, SobekCM_Library_Settings.System_Name, SobekCM_Library_Settings.System_Abbreviation);
                marcWriter.Write_Metadata(item.Source_Directory + "\\marc.xml", item, options, out Error_Message);

                // Copy this to all the image servers
                SobekCM_Library_Settings.Refresh(Database.SobekCM_Database.Get_Settings_Complete(null));
                string[] allFiles = Directory.GetFiles(user_bib_vid_process_directory);

                string serverNetworkFolder = SobekCM_Library_Settings.Image_Server_Network + item.Web.AssocFilePath;

                // Create the folder
                if (!Directory.Exists(serverNetworkFolder))
                {
                    Directory.CreateDirectory(serverNetworkFolder);
                }
                else
                {
                    // Rename any existing standard mets to keep a backup
                    if (File.Exists(serverNetworkFolder + "\\" + item.BibID + "_" + item.VID + ".mets.xml"))
                    {
                        FileInfo currentMetsFileInfo = new FileInfo(serverNetworkFolder + "\\" + item.BibID + "_" + item.VID + ".mets.xml");
                        DateTime lastModDate         = currentMetsFileInfo.LastWriteTime;
                        File.Copy(serverNetworkFolder + "\\" + item.BibID + "_" + item.VID + ".mets.xml", serverNetworkFolder + "\\" + item.BibID + "_" + item.VID + "_" + lastModDate.Year + "_" + lastModDate.Month + "_" + lastModDate.Day + ".mets.bak", true);
                    }
                }

                foreach (string thisFile in allFiles)
                {
                    string destination_file = serverNetworkFolder + "\\" + (new FileInfo(thisFile)).Name;
                    File.Copy(thisFile, destination_file, true);
                }

                // Copy the static HTML file as well
                try
                {
                    if (!Directory.Exists(SobekCM_Library_Settings.Static_Pages_Location + item.BibID.Substring(0, 2) + "\\" + item.BibID.Substring(2, 2) + "\\" + item.BibID.Substring(4, 2) + "\\" + item.BibID.Substring(6, 2) + "\\" + item.BibID.Substring(8)))
                    {
                        Directory.CreateDirectory(SobekCM_Library_Settings.Static_Pages_Location + item.BibID.Substring(0, 2) + "\\" + item.BibID.Substring(2, 2) + "\\" + item.BibID.Substring(4, 2) + "\\" + item.BibID.Substring(6, 2) + "\\" + item.BibID.Substring(8));
                    }
                    if (File.Exists(user_bib_vid_process_directory + "\\" + item.BibID + "_" + item.VID + ".html"))
                    {
                        File.Copy(user_bib_vid_process_directory + "\\" + item.BibID + "_" + item.VID + ".html", SobekCM_Library_Settings.Static_Pages_Location + item.BibID.Substring(0, 2) + "\\" + item.BibID.Substring(2, 2) + "\\" + item.BibID.Substring(4, 2) + "\\" + item.BibID.Substring(6, 2) + "\\" + item.BibID.Substring(8) + "\\" + item.BibID + "_" + item.VID + ".html", true);
                    }
                }
                catch (Exception)
                {
                    // This is not critical
                }

                // Add this to the cache
                itemList.Add_SobekCM_Item(item, false);

                // Now, delete all the files here
                string[] all_files = Directory.GetFiles(user_bib_vid_process_directory);
                foreach (string thisFile in all_files)
                {
                    File.Delete(thisFile);
                }

                // Clear the user-specific and global cache of this item
                Cached_Data_Manager.Remove_Digital_Resource_Object(user.UserID, item.BibID, item.VID, null);
                Cached_Data_Manager.Remove_Digital_Resource_Object(item.BibID, item.VID, null);
                Cached_Data_Manager.Remove_Items_In_Title(item.BibID, null);


                // Forward to the display item again
                currentMode.Mode       = Display_Mode_Enum.Item_Display;
                currentMode.ViewerCode = "citation";
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
            }
        }
        /// <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);
        }
        public void Do_Work()
        {
            string        username = WindowsIdentity.GetCurrent().Name;
            List <string> directories_to_process = new List <string>();

            try
            {
                recurse_through_directories(directory, directories_to_process);
            }
            catch
            {
            }

            // Now, iterate through all the directories
            int current_directory  = 1;
            int errors_encountered = 0;

            foreach (string thisDirectory in directories_to_process)
            {
                try
                {
                    DirectoryInfo thisDirectoryInfo = new DirectoryInfo(thisDirectory);

                    string folder_name_for_progress = thisDirectoryInfo.Name;
                    if (folder_name_for_progress.Length <= 5)
                    {
                        folder_name_for_progress = (thisDirectoryInfo.Parent.Name) + "\\" + folder_name_for_progress;
                    }
                    OnNewFolder(folder_name_for_progress + " ( " + current_directory + " of " + directories_to_process.Count + " )");

                    // Get the metadata file
                    string[] metadata = Directory.GetFiles(thisDirectory, filter);
                    if (metadata.Length > 0)
                    {
                        SobekCM_Item newItem = null;
                        if (metadata_type.IndexOf("METS") >= 0)
                        {
                            newItem = SobekCM_Item.Read_METS(metadata[0]);
                        }
                        else
                        {
                            newItem = new SobekCM_Item();
                            newItem.Source_Directory = thisDirectory;

                            newItem = new SobekCM_Item();
                            List <string> addOns = MetaTemplate_UserSettings.AddOns_Enabled;

                            // Set the initial agents
                            if (MetaTemplate_UserSettings.Individual_Creator.Length > 0)
                            {
                                newItem.METS_Header.Creator_Individual = MetaTemplate_UserSettings.Individual_Creator;
                            }
                            else
                            {
                                newItem.METS_Header.Creator_Individual = username;
                            }
                            newItem.METS_Header.Creator_Software = "SobekCM METS Editor";

                            // Add FCLA add-on defaults
                            if (addOns.Contains("FCLA"))
                            {
                                PALMM_Info palmmInfo = newItem.Get_Metadata_Module(GlobalVar.PALMM_METADATA_MODULE_KEY) as PALMM_Info;
                                if (palmmInfo == null)
                                {
                                    palmmInfo = new PALMM_Info();
                                    newItem.Add_Metadata_Module(GlobalVar.PALMM_METADATA_MODULE_KEY, palmmInfo);
                                }
                                palmmInfo.PALMM_Project = MetaTemplate_UserSettings.PALMM_Code;
                                palmmInfo.toPALMM       = MetaTemplate_UserSettings.FCLA_Flag_PALMM;


                                DAITSS_Info daitssInfo = newItem.Get_Metadata_Module(GlobalVar.DAITSS_METADATA_MODULE_KEY) as DAITSS_Info;
                                if (daitssInfo == null)
                                {
                                    daitssInfo = new DAITSS_Info();
                                    newItem.Add_Metadata_Module(GlobalVar.DAITSS_METADATA_MODULE_KEY, daitssInfo);
                                }
                                daitssInfo.toArchive  = MetaTemplate_UserSettings.FCLA_Flag_FDA;
                                daitssInfo.Account    = MetaTemplate_UserSettings.FDA_Account;
                                daitssInfo.SubAccount = MetaTemplate_UserSettings.FDA_SubAccount;
                                daitssInfo.Project    = MetaTemplate_UserSettings.FDA_Project;
                            }

                            // Add SobekCM add-on defaults
                            if (addOns.Contains("SOBEKCM"))
                            {
                                // Add any wordmarks
                                List <string> wordmarks = MetaTemplate_UserSettings.SobekCM_Wordmarks;
                                foreach (string thisWordmark in wordmarks)
                                {
                                    newItem.Behaviors.Add_Wordmark(thisWordmark);
                                }

                                // Add any aggregations
                                List <string> aggregations = MetaTemplate_UserSettings.SobekCM_Aggregations;
                                foreach (string thisAggregation in aggregations)
                                {
                                    newItem.Behaviors.Add_Aggregation(thisAggregation);
                                }

                                // Add any web skins
                                List <string> webskins = MetaTemplate_UserSettings.SobekCM_Web_Skins;
                                foreach (string thisWebSkin in webskins)
                                {
                                    newItem.Behaviors.Add_Web_Skin(thisWebSkin);
                                }

                                // Add any viewers
                                List <string> viewers = MetaTemplate_UserSettings.SobekCM_Viewers;
                                foreach (string thisViewer in viewers)
                                {
                                    if (String.Compare(thisViewer, "Page Image (JPEG)") == 0)
                                    {
                                        newItem.Behaviors.Add_View(View_Enum.JPEG);
                                    }
                                    if (String.Compare(thisViewer, "Zoomable (JPEG2000)") == 0)
                                    {
                                        newItem.Behaviors.Add_View(View_Enum.JPEG2000);
                                    }
                                    if (String.Compare(thisViewer, "Page Turner") == 0)
                                    {
                                        newItem.Behaviors.Add_View(View_Enum.PAGE_TURNER);
                                    }
                                    if (String.Compare(thisViewer, "Text") == 0)
                                    {
                                        newItem.Behaviors.Add_View(View_Enum.TEXT);
                                    }
                                    if (String.Compare(thisViewer, "Thumbnails") == 0)
                                    {
                                        newItem.Behaviors.Add_View(View_Enum.RELATED_IMAGES);
                                    }
                                }
                            }

                            // Add all other defaults
                            newItem.Bib_Info.Source.Code      = MetaTemplate_UserSettings.Default_Source_Code;
                            newItem.Bib_Info.Source.Statement = MetaTemplate_UserSettings.Default_Source_Statement;
                            if (MetaTemplate_UserSettings.Default_Funding_Note.Length > 0)
                            {
                                newItem.Bib_Info.Add_Note(MetaTemplate_UserSettings.Default_Funding_Note, Note_Type_Enum.funding);
                            }
                            if (MetaTemplate_UserSettings.Default_Rights_Statement.Length > 0)
                            {
                                newItem.Bib_Info.Access_Condition.Text = MetaTemplate_UserSettings.Default_Rights_Statement;
                            }

                            // Set some final values
                            newItem.Bib_Info.Type.MODS_Type = TypeOfResource_MODS_Enum.Text;

                            // Assign an ObjectID
                            switch (next_bibid_counter)
                            {
                            case -1:
                                newItem.METS_Header.ObjectID = thisDirectoryInfo.Name;
                                if ((newItem.METS_Header.ObjectID.Length == 16) && (newItem.METS_Header.ObjectID[10] == '_'))
                                {
                                    newItem.VID   = newItem.BibID.Substring(11);
                                    newItem.BibID = newItem.BibID.Substring(0, 10).ToUpper();
                                }
                                break;

                            case -2:
                                newItem.METS_Header.ObjectID = (new FileInfo(metadata[0])).Name;
                                if ((newItem.METS_Header.ObjectID.Length == 16) && (newItem.METS_Header.ObjectID[10] == '_'))
                                {
                                    newItem.VID   = newItem.BibID.Substring(11);
                                    newItem.BibID = newItem.BibID.Substring(0, 10).ToUpper();
                                }
                                break;

                            default:
                                string next_bibid = next_bibid_counter.ToString();
                                next_bibid_counter++;
                                newItem.BibID = (bibid_start + next_bibid.PadLeft(10 - bibid_start.Length, '0')).ToUpper();
                                newItem.VID   = "00001";
                                break;
                            }

                            string errors = String.Empty;
                            if (metadata_type.IndexOf("DUBLIN CORE") >= 0)
                            {
                                // Open a stream to read the indicated import file
                                Stream reader = new FileStream(metadata[0], FileMode.Open, FileAccess.Read);
                                DC_File_ReaderWriter dcreader = new DC_File_ReaderWriter();
                                dcreader.Read_Metadata(reader, newItem, null, out errors);
                            }

                            if (metadata_type.IndexOf("MODS") >= 0)
                            {
                                // Open a stream to read the indicated import file
                                Stream reader = new FileStream(metadata[0], FileMode.Open, FileAccess.Read);
                                MODS_File_ReaderWriter dcreader = new MODS_File_ReaderWriter();
                                dcreader.Read_Metadata(reader, newItem, null, out errors);
                            }

                            if (metadata_type.IndexOf("MARCXML") >= 0)
                            {
                                // Open a stream to read the indicated import file
                                Stream reader = new FileStream(metadata[0], FileMode.Open, FileAccess.Read);
                                MarcXML_File_ReaderWriter dcreader = new MarcXML_File_ReaderWriter();
                                dcreader.Read_Metadata(reader, newItem, null, out errors);
                            }
                        }

                        // Make sure there is a title
                        if (newItem.Bib_Info.Main_Title.ToString().Trim().Length == 0)
                        {
                            newItem.Bib_Info.Main_Title.Title = "Missing Title";
                        }

                        // We now have a good METS file, so let's look for files to add
                        string[]      existing_files    = Directory.GetFiles(thisDirectory);
                        bool          image_files_found = false;
                        List <string> otherFiles        = new List <string>();
                        foreach (string thisFile in existing_files)
                        {
                            string upper_case = new FileInfo(thisFile).Name.ToUpper();
                            if ((upper_case.IndexOf(".TIF") > 0) || (upper_case.IndexOf(".JPG") > 0) || (upper_case.IndexOf(".JP2") > 0))
                            {
                                image_files_found = true;
                            }
                            else if ((upper_case.IndexOf(".METS") < 0) && (upper_case.IndexOf(".TXT") < 0) && (upper_case.IndexOf(".PRO") < 0) && (upper_case.IndexOf(".XML") < 0) && (upper_case.IndexOf(".MODS") < 0) && (upper_case.IndexOf(".DC") < 0))
                            {
                                otherFiles.Add((new FileInfo(thisFile)).Name);
                            }
                        }

                        // Add the image files first
                        newItem.Source_Directory = thisDirectory;
                        Bib_Package_Builder.Add_All_Files(newItem, "*.tif|*.jpg|*.jp2|*.txt|*.pro|*.gif", MetaTemplate_UserSettings.Always_Recurse_Through_Subfolders_On_New, MetaTemplate_UserSettings.Page_Images_In_Seperate_Folders_Can_Be_Same_Page);

                        // Add any downloads next
                        foreach (string thisFile in otherFiles)
                        {
                            newItem.Divisions.Download_Tree.Add_File(thisFile);
                        }

                        // Now, save this METS file
                        // Prepare to save the enw METS

                        // Determine the filename
                        string mets_file = thisDirectory + "\\" + newItem.METS_Header.ObjectID + MetaTemplate_UserSettings.METS_File_Extension;

                        // Save the actual file
                        newItem.Divisions.Suppress_Checksum = !MetaTemplate_UserSettings.Include_Checksums;

                        // Save the actual file
                        METS_File_ReaderWriter metsWriter = new METS_File_ReaderWriter();
                        string writing_error = String.Empty;
                        metsWriter.Write_Metadata(mets_file, newItem, null, out writing_error);
                    }
                }
                catch (Exception ee)
                {
                    errors_encountered++;
                }

                OnNewProgress(current_directory, directories_to_process.Count);
                current_directory++;
            }

            OnComplete(current_directory, errors_encountered);
        }