/// <summary> Reads metadata from an open stream and saves to the provided item/package </summary>
        /// <param name="Input_Stream"> Open stream to read metadata from </param>
        /// <param name="Return_Package"> Package into which to read the metadata </param>
        /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param>
        /// <param name="Error_Message">[OUTPUT] Explanation of the error, if an error occurs during reading </param>
        /// <returns>TRUE if successful, otherwise FALSE </returns>
        public bool Read_Metadata(Stream Input_Stream, SobekCM_Item Return_Package, Dictionary <string, object> Options, out string Error_Message)
        {
            // Set default error outpt message
            Error_Message = String.Empty;

            // Create a XML reader and read the metadata
            XmlTextReader nodeReader  = null;
            bool          returnValue = true;

            try
            {
                // create the node reader
                nodeReader = new XmlTextReader(Input_Stream);

                DC_METS_dmdSec_ReaderWriter.Read_Simple_Dublin_Core_Info(nodeReader, Return_Package.Bib_Info);
            }
            catch (Exception ee)
            {
                Error_Message = "Error reading DC from stream: " + ee.Message;
                returnValue   = false;
            }
            finally
            {
                if (nodeReader != null)
                {
                    nodeReader.Close();
                }
            }

            return(returnValue);
        }
        /// <summary> Writes the formatted metadata from the provided item to a TextWriter (usually to an output stream) </summary>
        /// <param name="Output_Stream"></param>
        /// <param name="Item_To_Save"> Package with all the metadata to save </param>
        /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param>
        /// <param name="Error_Message">[OUTPUT] Explanation of the error, if an error occurs during write </param>
        /// <returns>TRUE if successful, otherwise FALSE </returns>
        public bool Write_Metadata(TextWriter Output_Stream, SobekCM_Item Item_To_Save, Dictionary <string, object> Options, out string Error_Message)
        {
            // Set default error outpt message
            Error_Message = String.Empty;

            // Check for OAI set and OAI data in the options
            string   OAI_Set  = String.Empty;
            DateTime OAI_Date = DateTime.Now;

            if (Options.ContainsKey("OAI_File_ReaderWriter:OAI_Set"))
            {
                OAI_Set = Options["OAI_File_ReaderWriter:OAI_Set"].ToString();
            }
            if (Options.ContainsKey("OAI_File_ReaderWriter:OAI_Date"))
            {
                DateTime.TryParse(Options["OAI_File_ReaderWriter:OAI_Date"].ToString(), out OAI_Date);
            }

            StringBuilder returnValue = new StringBuilder();

            // Add the header for this OAI
            Output_Stream.WriteLine("<xml>");

            Output_Stream.WriteLine("<record><header><identifier>oai:www.uflib.ufl.edu.ufdc:" + Item_To_Save.BibID + "</identifier>");
            Output_Stream.WriteLine("<datestamp>" + OAI_Date.Year + "-" + OAI_Date.Month.ToString().PadLeft(2, '0') + "-" + OAI_Date.Day.ToString().PadLeft(2, '0') + "</datestamp>");
            Output_Stream.WriteLine("<setSpec>" + OAI_Set + "</setSpec></header>");

            // Start the metadata section with the namespace references
            Output_Stream.WriteLine("<metadata>");
            Output_Stream.WriteLine("<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ");
            Output_Stream.WriteLine("xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ");
            Output_Stream.WriteLine("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
            Output_Stream.WriteLine("xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ ");
            Output_Stream.WriteLine("http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">");

            // Add the dublin core
            DC_METS_dmdSec_ReaderWriter.Write_Simple_Dublin_Core(Output_Stream, Item_To_Save.Bib_Info);

            // Add the URL as the identifier
            if (Item_To_Save.Bib_Info.Location.PURL.Length > 0)
            {
                Output_Stream.WriteLine("<dc:identifer>" + Item_To_Save.Bib_Info.Location.PURL + "</dc:identifier>");
            }
            else if (Item_To_Save.Web.Service_URL.Length > 0)
            {
                Output_Stream.WriteLine("<dc:identifer>" + Item_To_Save.Web.Service_URL + "</dc:identifier>");
            }

            // Finish this OAI
            Output_Stream.WriteLine("</oai_dc:dc>");
            Output_Stream.WriteLine("</metadata>");
            Output_Stream.WriteLine("</record>");

            Output_Stream.WriteLine("</xml>");

            // Return the built OAI string
            return(true);
        }
예제 #3
0
        /// <summary> Returns the OAI-PMH metadata in dublin core (OAI-flavored) for this item </summary>
        /// <param name="Item_To_Save"> Package with all the metadata to save </param>
        /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param>
        /// <param name="Error_Message">[OUTPUT] Explanation of the error, if an error occurs during write </param>
        /// <returns> Metadata for a OAI-PMH record of a particular metadata format/type </returns>
        public override string Create_OAI_PMH_Metadata(SobekCM_Item Item_To_Save, Dictionary <string, object> Options, out string Error_Message)
        {
            // Set default error outpt message
            Error_Message = String.Empty;

            StringBuilder results = new StringBuilder();
            StringWriter  writer  = new StringWriter(results);

            writer.WriteLine("<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ");
            writer.WriteLine("xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ");
            writer.WriteLine("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
            writer.WriteLine("xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ ");
            writer.WriteLine("http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">");

            // Add the dublin core
            DC_METS_dmdSec_ReaderWriter.Write_Simple_Dublin_Core(writer, Item_To_Save.Bib_Info);

            // Add the URL as the identifier
            if (Item_To_Save.Bib_Info.Location.PURL.Length > 0)
            {
                writer.WriteLine("<dc:identifier>" + Item_To_Save.Bib_Info.Location.PURL + "</dc:identifier>");
            }
            else if (Item_To_Save.Web.Service_URL.Length > 0)
            {
                writer.WriteLine("<dc:identifier>" + Item_To_Save.Web.Service_URL + "</dc:identifier>");
            }

            // Finish this OAI
            writer.WriteLine("</oai_dc:dc>");

            string resultsString = results.ToString();

            writer.Close();

            return(resultsString);
        }
        /// <summary> Writes the formatted metadata from the provided item to a TextWriter (usually to an output stream) </summary>
        /// <param name="Output_Stream"></param>
        /// <param name="Item_To_Save"> Package with all the metadata to save </param>
        /// <param name="Options"> Dictionary of any options which this metadata reader/writer may utilize </param>
        /// <param name="Error_Message">[OUTPUT] Explanation of the error, if an error occurs during write </param>
        /// <returns>TRUE if successful, otherwise FALSE </returns>
        /// <remarks>This writer accepts one option value.  'DC_File_ReaderWriter:RDF_Style' is a boolean value which indicates
        /// if the Dublin Core metadata file should be written in RDF format.  (Default is FALSE).</remarks>
        public bool Write_Metadata(TextWriter Output_Stream, SobekCM_Item Item_To_Save, Dictionary <string, object> Options, out string Error_Message)
        {
            // Set default error outpt message
            Error_Message = String.Empty;

            // Check for RDF style in the options
            bool RDF_Style = false;

            if (Options != null)
            {
                if (Options.ContainsKey("DC_File_ReaderWriter:RDF_Style"))
                {
                    bool.TryParse(Options["DC_File_ReaderWriter:RDF_Style"].ToString(), out RDF_Style);
                }
            }

            if (RDF_Style)
            {
                try
                {
                    // Start to build the XML result
                    Output_Stream.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                    Output_Stream.WriteLine("<!DOCTYPE rdf:RDF SYSTEM \"http://purl.org/dc/schemas/dcmes-xml-20000714.dtd\">");
                    Output_Stream.WriteLine("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
                    Output_Stream.WriteLine("         xmlns:dc=\"http://purl.org/dc/elements/1.1/\">");
                    Output_Stream.WriteLine("<rdf:Description>");

                    DC_METS_dmdSec_ReaderWriter.Write_Simple_Dublin_Core(Output_Stream, Item_To_Save.Bib_Info);

                    Output_Stream.WriteLine("</rdf:Description>");
                    Output_Stream.WriteLine("</rdf:RDF>");

                    return(true);
                }
                catch (Exception ee)
                {
                    Error_Message = "Error caught while saving RDF-style Dublin Core to stream: " + ee.Message;
                    return(false);
                }
            }
            else
            {
                try
                {
                    // Start to build the XML result
                    StringBuilder results = new StringBuilder();
                    Output_Stream.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n");
                    Output_Stream.WriteLine("<records>\r\n");
                    Output_Stream.WriteLine("<record xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\">\r\n");

                    DC_METS_dmdSec_ReaderWriter.Write_Simple_Dublin_Core(Output_Stream, Item_To_Save.Bib_Info);

                    Output_Stream.WriteLine("</record>\r\n");
                    Output_Stream.WriteLine("</records>\r\n");

                    return(true);
                }
                catch (Exception ee)
                {
                    Error_Message = "Error caught while saving Dublin Core to stream: " + ee.Message;
                    return(false);
                }
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            // Try to read the metadata configuration file
            string app_start_config = Application.StartupPath + "\\config";

            if ((Directory.Exists(app_start_config)) && (File.Exists(app_start_config + "\\sobekCM_metadata.config")))
            {
                Metadata_Configuration.Read_Metadata_Configuration(app_start_config + "\\sobekCM_metadata.config");
            }


            bool   complete_static_rebuild = false;
            bool   marc_rebuild            = false;
            bool   run_preloader           = true;
            bool   run_background          = false;
            bool   show_help = false;
            bool   build_production_marcxml_feed = false;
            bool   build_test_marcxml_feed       = false;
            string invalid_arg = String.Empty;
            bool   refresh_oai = false;
            bool   verbose     = false;

            // Get values from the arguments
            foreach (string thisArgs in args)
            {
                bool arg_handled = false;

                // Check for the config flag
                if (thisArgs == "--config")
                {
                    if (File.Exists(app_start_config + "\\SobekCM_Builder_Configuration.exe"))
                    {
                        Process.Start(app_start_config + "\\SobekCM_Builder_Configuration.exe");
                        return;
                    }
                    Console.WriteLine("ERROR: Unable to find configuration executable file!!");
                    return;
                }

                // Check for versioning option
                if (thisArgs == "--version")
                {
                    Console.WriteLine("You are running version " + SobekCM_Library_Settings.CURRENT_BUILDER_VERSION + " of the SobekCM Builder.");
                    return;
                }

                // Check for no loader flag
                if (thisArgs == "--background")
                {
                    run_background = true;
                    arg_handled    = true;
                }

                // Check for verbose flag
                if (thisArgs == "--verbose")
                {
                    verbose     = true;
                    arg_handled = true;
                }

                // Check for no loader flag
                if (thisArgs == "--refresh_oai")
                {
                    refresh_oai = true;
                    arg_handled = true;
                }


                // Check for no oading flag
                if (thisArgs == "--noload")
                {
                    run_preloader = false;
                    arg_handled   = true;
                }

                // Check for static rebuild
                if (thisArgs == "--staticrebuild")
                {
                    complete_static_rebuild = true;
                    arg_handled             = true;
                }

                // Check for static rebuild
                if (thisArgs == "--createmarc")
                {
                    marc_rebuild = true;
                    arg_handled  = true;
                }

                // Check for marc xml feed creation flags
                if (thisArgs.IndexOf("--marcxml") == 0)
                {
                    build_production_marcxml_feed = true;
                    arg_handled = true;
                }
                if (thisArgs.IndexOf("--testmarcxml") == 0)
                {
                    build_test_marcxml_feed = true;
                    arg_handled             = true;
                }

                // Check for help
                if ((thisArgs == "--help") || (thisArgs == "?") || (thisArgs == "-help"))
                {
                    show_help   = true;
                    arg_handled = true;
                }

                // If not handled, set as error
                if (!arg_handled)
                {
                    invalid_arg = thisArgs;
                    break;
                }
            }

            // Was there an invalid argument or was help requested
            if ((invalid_arg.Length > 0) || (show_help))
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("\nThis application is used to bulk load SobekCM items, perform post-processing\n");
                builder.Append("for items loaded through the web, and perform some regular maintenance activities\n");
                builder.Append("in support of a SobekCM web application.\n\n");
                builder.Append("Usage: SobekCM_Builder [options]\n\n");
                builder.Append("Options:\n\n");
                builder.Append("  --config\tRuns the configuration tool\n\n");
                builder.Append("  --version\tDisplays the current version of the SobekCM Builder\n\n");
                builder.Append("  --verbose\tFlag indicates to be verbose in the logs and console\n\n");
                builder.Append("  --background\tContinues to run in the background\n\n");
                builder.Append("  --help\t\tShows these instructions\n\n");
                builder.Append("  --noload\t\tSupresses the loading portion of the SobekCM Builder\n\n");
                builder.Append("  --staticrebuild\tPerform a complete rebuild on static pages\n\n");
                builder.Append("  --marcxml\tBuild the marcxml production feed\n\n");
                builder.Append("  --testmarcxml\tBuild the marcxml test feed\n\n");
                builder.Append("  --createmarc\tRecreate all of the MARC.xml files\n\n");
                builder.Append("  --refresh_oai\tResave the OAI-PMH DC data for every item in the library\n\n");
                builder.Append("Examples:\n\n");
                builder.Append("  1. To just rebuild all the static pages:\n");
                builder.Append("       SobekCM_Builder --nopreload --noload --staticrebuild");
                builder.Append("  2. To have the SobekCM Builder constantly run in the background");
                builder.Append("       SobekCM_Builder --background");

                // If invalid arg, save to log file
                if (invalid_arg.Length > 0)
                {
                    // Show INVALID ARGUMENT error in console
                    Console.WriteLine("\nINVALID ARGUMENT PROVIDED ( " + invalid_arg + " )");
                }

                Console.WriteLine(builder.ToString());
                return;
            }

            // Now, veryify the configuration file exists
            string config_file = Application.StartupPath + "\\config\\sobekcm.config";

            if (!File.Exists(config_file))
            {
                Console.WriteLine("The configuration file is missing!!\n");
                Console.Write("Would you like to run the configuration tool? [Y/N]: ");
                string result = Console.ReadLine().ToUpper();
                if ((result == "Y") || (result == "YES"))
                {
                    // Does the config app exist?
                    if (File.Exists(Application.StartupPath + "\\config\\SobekCM_Builder_Configuration.exe"))
                    {
                        // Run the config app
                        Process configProcess = new Process {
                            StartInfo = { FileName = Application.StartupPath + "\\config\\SobekCM_Builder_Configuration.exe" }
                        };
                        configProcess.Start();
                        configProcess.WaitForExit();

                        // If still no config file, just abort
                        if (!File.Exists(config_file))
                        {
                            Console.WriteLine("Execution aborted due to missing configuration file.");
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("ERROR: Unable to find configuration executable file!!");
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("Execution aborted due to missing configuration file.");
                    return;
                }
            }

            // Should be a config file now, so read it
            SobekCM_Library_Settings.Read_Configuration_File(config_file);
            if ((SobekCM_Library_Settings.Database_Connections.Count == 0) || (SobekCM_Library_Settings.Database_Connections[0].Connection_String.Length == 0))
            {
                Console.WriteLine("Missing database connection string!!\n");
                Console.Write("Would you like to run the configuration tool? [Y/N]: ");
                string result = Console.ReadLine().ToUpper();
                if ((result == "Y") || (result == "YES"))
                {
                    // Does the config app exist?
                    if (File.Exists(Application.StartupPath + "\\config\\SobekCM_Builder_Configuration.exe"))
                    {
                        // Run the config app
                        Process configProcess = new Process {
                            StartInfo = { FileName = Application.StartupPath + "\\config\\SobekCM_Builder_Configuration.exe" }
                        };
                        configProcess.Start();
                        configProcess.WaitForExit();

                        // If still no config file, just abort
                        if (!File.Exists(config_file))
                        {
                            Console.WriteLine("Execution aborted due to missing configuration file.");
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("ERROR: Unable to find configuration executable file!!");
                        return;
                    }
                }
                else
                {
                    Console.WriteLine("Execution aborted due to missing configuration file.");
                    return;
                }
            }

            // Assign the connection string and test the connection (if only a single connection listed)
            if (SobekCM_Library_Settings.Database_Connections.Count == 1)
            {
                SobekCM_Database.Connection_String = SobekCM_Library_Settings.Database_Connections[0].Connection_String;
                if (!SobekCM_Database.Test_Connection())
                {
                    Console.WriteLine("Unable to connect to the database using provided connection string:");
                    Console.WriteLine();
                    Console.WriteLine(SobekCM_Database.Connection_String);
                    Console.WriteLine();
                    Console.WriteLine("Run this application with an argument of '--config' to launch the configuration tool.");
                    return;
                }
            }

            // Verify connectivity and rights on the logs subfolder
            SobekCM_Library_Settings.Local_Log_Directory = Application.StartupPath + "\\logs";
            if (!Directory.Exists(SobekCM_Library_Settings.Local_Log_Directory))
            {
                try
                {
                    Directory.CreateDirectory(SobekCM_Library_Settings.Local_Log_Directory);
                }
                catch
                {
                    Console.WriteLine("Error creating necessary logs subfolder under the application folder.\n");
                    Console.WriteLine("Please create manually.\n");
                    Console.WriteLine(SobekCM_Library_Settings.Local_Log_Directory);
                    return;
                }
            }
            try
            {
                StreamWriter testWriter = new StreamWriter(SobekCM_Library_Settings.Local_Log_Directory + "\\test.log", false);
                testWriter.WriteLine("TEST");
                testWriter.Flush();
                testWriter.Close();

                File.Delete(SobekCM_Library_Settings.Local_Log_Directory + "\\test.log");
            }
            catch
            {
                Console.WriteLine("The service account needs modify rights on the logs subfolder.\n");
                Console.WriteLine("Please correct manually.\n");
                Console.WriteLine(SobekCM_Library_Settings.Local_Log_Directory);
                return;
            }

            // Look for Ghostscript from the registry, if not provided in the config file
            if (SobekCM_Library_Settings.Ghostscript_Executable.Length == 0)
            {
                // LOOK FOR THE GHOSTSCRIPT DIRECTORY
                string possible_ghost = Look_For_Variable_Registry_Key("SOFTWARE\\GPL Ghostscript", "GS_DLL");
                if (!String.IsNullOrEmpty(possible_ghost))
                {
                    SobekCM_Library_Settings.Ghostscript_Executable = possible_ghost;
                }
            }

            // Look for Imagemagick from the registry, if not provided in the config file
            string possible_imagemagick = Look_For_Variable_Registry_Key("SOFTWARE\\ImageMagick", "BinPath");

            if (!String.IsNullOrEmpty(possible_imagemagick))
            {
                SobekCM_Library_Settings.ImageMagick_Executable = possible_imagemagick;
            }

            // If this is to refresh the OAI, don't use the worker controller
            if ((refresh_oai) && (SobekCM_Library_Settings.Database_Connections.Count == 1))
            {
                // Set the item for the current mode
                int successes = 0;

                DataTable item_list_table = SobekCM_Database.Get_All_Groups_First_VID( );
                foreach (DataRow thisRow in item_list_table.Rows)
                {
                    string bibid   = thisRow["BibID"].ToString();
                    string vid     = thisRow["VID"].ToString();
                    int    groupid = Convert.ToInt32(thisRow["groupid"]);

                    string directory = SobekCM_Library_Settings.Image_Server_Network + bibid.Substring(0, 2) + "\\" + bibid.Substring(2, 2) + "\\" + bibid.Substring(4, 2) + "\\" + bibid.Substring(6, 2) + "\\" + bibid.Substring(8, 2) + "\\" + vid;
                    string mets      = directory + "\\" + bibid + "_" + vid + ".mets.xml";

                    try
                    {
                        SobekCM_Item thisItem = SobekCM_Item.Read_METS(mets);
                        if (thisItem != null)
                        {
                            // Get the OAI-PMH dublin core information
                            StringBuilder oaiDataBuilder = new StringBuilder(1000);
                            StringWriter  writer         = new StringWriter(oaiDataBuilder);
                            DC_METS_dmdSec_ReaderWriter.Write_Simple_Dublin_Core(writer, thisItem.Bib_Info);
                            // Also add the URL as identifier
                            oaiDataBuilder.AppendLine("<dc:identifier>" + SobekCM_Library_Settings.System_Base_URL + bibid + "</dc:identifier>");
                            Resource_Object.Database.SobekCM_Database.Save_Item_Group_OAI(groupid, oaiDataBuilder.ToString(), "oai_dc", true);
                            writer.Flush();
                            writer.Close();

                            successes++;
                            if (successes % 1000 == 0)
                            {
                                Console.WriteLine(@"{0} complete", successes);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                return;
            }

            // Two ways to run this... constantly in background or once
            Worker_Controller controller = new Worker_Controller(verbose);

            if (!run_background)
            {
                controller.Execute_Immediately(build_production_marcxml_feed, build_test_marcxml_feed, run_preloader, complete_static_rebuild, marc_rebuild);
            }
            else
            {
                controller.Execute_In_Background();
            }

            // If this was set to aborting, set to last execution aborted
            Builder_Operation_Flag_Enum operationFlag = Abort_Database_Mechanism.Builder_Operation_Flag;

            if ((operationFlag == Builder_Operation_Flag_Enum.ABORTING) || (operationFlag == Builder_Operation_Flag_Enum.ABORT_REQUESTED))
            {
                Abort_Database_Mechanism.Builder_Operation_Flag = Builder_Operation_Flag_Enum.LAST_EXECUTION_ABORTED;
            }
        }