예제 #1
0
        /// <summary> DEMO 1 : Read a MARC DAT file completely using the IEnumerator interface. and adding it to  a MarcXML output file </summary>
        private static void demo1()
        {
            Console.WriteLine("Performing demo1");

            // Create the marc21 exchange reader
            MARC21_Exchange_Format_Parser parser1 = new MARC21_Exchange_Format_Parser("AgricNewCat02.dat");

            // Create the marc xml writer
            MARCXML_Writer writer1 = new MARCXML_Writer("AgricNewCat02.xml");

            try
            {
                // Step through each record from the Marc21 dat file and output to the XML file
                foreach (MARC_Record thisRecord in parser1)
                {
                    writer1.AppendRecord(thisRecord);
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.StackTrace);
            }
            finally
            {
                // Close all the streams
                parser1.Close();
                writer1.Close();
            }
        }
        // Retrieves metadata from Z39.50 server
        private static IList <Metadata> RetrieveMetadataByZ39(IdentifierType identifierType, string value, bool isUnion)
        {
            string z39Server   = Settings.Z39ServerUrl;
            int    z39Port     = Settings.Z39Port;
            string z39Base     = Settings.Z39Base;
            string z39UserName = Settings.Z39UserName;
            string z39Password = Settings.Z39Password;
            Record_Character_Encoding z39Encoding = Settings.Z39Encoding;

            //Set union Z39.50
            if (isUnion)
            {
                z39Server   = Settings.Z39UnionServerUrl;
                z39Port     = Settings.Z39UnionPort;
                z39Base     = Settings.Z39UnionBase;
                z39Encoding = Settings.Z39UnionEncoding;
                z39UserName = null;
                z39Password = null;
            }

            int identifierFieldNumber;

            switch (identifierType)
            {
            case IdentifierType.BARCODE:
                identifierFieldNumber = Settings.Z39BarcodeField;
                break;

            case IdentifierType.CNB:
                if (isUnion)
                {
                    identifierFieldNumber = Settings.DEFAULT_NKP_CNB_FIELD;
                }
                else
                {
                    identifierFieldNumber = Settings.Z39CnbField;
                }
                break;

            case IdentifierType.EAN:
                identifierFieldNumber = Settings.Z39EanField;
                break;

            case IdentifierType.ISBN:
                identifierFieldNumber = Settings.Z39IsbnField;
                break;

            case IdentifierType.ISSN:
                identifierFieldNumber = Settings.Z39IssnField;
                break;

            case IdentifierType.ISMN:
                identifierFieldNumber = Settings.Z39IsmnField;
                break;

            case IdentifierType.OCLC:
                identifierFieldNumber = Settings.Z39OclcField;
                break;

            default:
                identifierFieldNumber = Settings.DEFAULT_FIELD;
                break;
            }

            //validate
            string errorText = "";

            if (string.IsNullOrEmpty(z39Server))
            {
                errorText += "Z39.50 Server URL; ";
            }
            if (z39Port <= 0)
            {
                errorText += "Z39.50 Sever Port; ";
            }
            if (string.IsNullOrEmpty(z39Base))
            {
                errorText += "Z39.50 Databáze; ";
            }
            if (identifierFieldNumber <= 0)
            {
                errorText += "Vyhledávací atribut";
            }

            if (!string.IsNullOrEmpty(errorText))
            {
                throw new ArgumentException("V nastaveních chybí následující údaje: " + errorText);
            }

            List <Metadata> metadataList = new List <Metadata>();
            string          errorMessage = "";

            try
            {
                Z3950_Endpoint endpoint;
                if (string.IsNullOrEmpty(z39UserName))
                {
                    endpoint = new Z3950_Endpoint("Z39.50",
                                                  z39Server, (uint)z39Port, z39Base);
                }
                else
                {
                    endpoint = new Z3950_Endpoint("Z39.50",
                                                  z39Server, (uint)z39Port, z39Base, z39UserName);
                    endpoint.Password = z39Password ?? "";
                }

                // Retrieve the record by primary identifier
                IEnumerable <MARC_Record> recordsFromZ3950 = MARC_Record_Z3950_Retriever.Get_Record(
                    identifierFieldNumber, '"' + value + '"', endpoint, out errorMessage, z39Encoding);

                MARCXML_Writer marcXmlWriter = new MARCXML_Writer(System.IO.Path.Combine(Settings.TemporaryFolder, "marcxml.xml"));

                if (recordsFromZ3950 != null)
                {
                    foreach (MARC_Record record in recordsFromZ3950)
                    {
                        Metadata metadata = new Metadata();
                        // Sysno
                        metadata.Sysno          = record.Control_Number;
                        metadata.FixedFields    = MarcGetFixedFields(record);
                        metadata.VariableFields = MarcGetVariableFields(record);

                        // Get identifiers list (ISBNs + 902a yearbooks)
                        List <MetadataIdentifier> Identifiers = getIdentifiersList(metadata);
                        metadata.Identifiers = Identifiers;

                        // Add to list
                        metadataList.Add(metadata);

                        // Write to MARC XML file
                        marcXmlWriter.AppendRecord(record);
                    }

                    // Finish MARC XML file
                    marcXmlWriter.Close();
                }
            }
            catch (Exception e)
            {
                throw new Z39Exception("Nastala chyba během stahování MARC záznamu pomocí Z39.50, nebo zápisu výsledného MARC záznamu na disk. Detailnější popis problému: " + e.Message);
            }

            // Display any error message encountered
            if (metadataList.Count == 0)
            {
                if (errorMessage.Length > 0)
                {
                    if (errorMessage.Contains("No matching record found in Z39.50 endpoint"))
                    {
                        errorMessage = "Nenalezen vhodný záznam.";
                    }

                    else if (errorMessage.Contains("Connection could not be made to"))
                    {
                        errorMessage = "Nebylo možné navázat spojení s " +
                                       errorMessage.Substring(errorMessage.LastIndexOf(' '));
                    }

                    throw new Z39Exception(errorMessage);
                }
                else
                {
                    throw new Z39Exception("Nastala neznámá chyba během Z39.50 dotazu");
                }
            }
            return(metadataList);
        }