// 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); }
public static MARC_Record Get_Record_By_Primary_Identifier(string Primary_Identifier, Z3950_Endpoint Z3950_Server, out string Message, Record_Character_Encoding encoding) { // Initially set the message to empty Message = String.Empty; // http://jai-on-asp.blogspot.com/2010/01/z3950-client-in-cnet-using-zoomnet-and.html // http://www.indexdata.com/yaz/doc/tools.html#PQF // http://www.loc.gov/z3950/agency/defns/bib1.html // http://www.assembla.com/code/wasolic/subversion/nodes/ZOOM.NET // http://lists.indexdata.dk/pipermail/yazlist/2007-June/002080.html // http://fclaweb.fcla.edu/content/z3950-access-aleph const string prefix = "@attrset Bib-1 @attr 1=12 "; try { IConnection connection; // zoom db connector IPrefixQuery query; // zoom query IRecord record; // one zoom record IResultSet records; // collection of records // allocate MARC tools MARC21_Exchange_Format_Parser parser = new MARC21_Exchange_Format_Parser(); // establish connection connection = new Connection(Z3950_Server.URI, Convert.ToInt32(Z3950_Server.Port)); connection.DatabaseName = Z3950_Server.Database_Name; // Any authentication here? if (Z3950_Server.Username.Length > 0) { connection.Username = Z3950_Server.Username; } if (Z3950_Server.Password.Length > 0) { connection.Password = Z3950_Server.Password; } // Set to USMARC connection.Syntax = RecordSyntax.USMARC; // call the Z39.50 server query = new PrefixQuery(prefix + Primary_Identifier); records = connection.Search(query); // If the record count is not one, return a message if (records.Count != 1) { if (records.Count == 0) { Message = "ERROR: No matching record found in Z39.50 endpoint"; } else { Message = "ERROR: More than one matching record found in Z39.50 endpoint by primary identifier"; } return(null); } // capture the byte stream record = records[0]; MemoryStream ms = new MemoryStream(record.Content); // display while debugging //MessageBox.Show(Encoding.UTF8.GetString(record.Content)); try { // feed the record to the parser and add the 955 MARC_Record marcrec = parser.Parse(ms, encoding); parser.Close(); return(marcrec); } catch (Exception error) { Message = "ERROR: Unable to parse resulting record into the MARC Record structure!\n\n" + error.Message; return(null); //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML."); //Trace.WriteLine("Could not convert item " + Primary_Identifier + " to MARCXML."); //Trace.WriteLine("Error details: " + error.Message); } } catch (Exception error) { if (error.Message.IndexOf("The type initializer for 'Zoom.Net.Yaz") >= 0) { Message = "ERROR: The Z39.50 libraries did not correctly initialize.\n\nThese libraries do not currently work in 64-bit environments."; } else { Message = "ERROR: Unable to connect and search provided Z39.50 endpoint.\n\n" + error.Message; } return(null); //MessageBox.Show("Could not convert item " + Primary_Identifier + " to MARCXML."); //Trace.WriteLine("Could not convert item " + Primary_Identifier + " to MARCXML."); //Trace.WriteLine("Error details: " + error.Message); } }
// Retrieves metadata from Z39.50 server private void RetrieveMetadataByBarcodeZ39() { 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; int z39BarcodeField = Settings.Z39BarcodeField; //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 (z39BarcodeField <= 0) { errorText += "Vyhledávací atribut"; } if (!string.IsNullOrEmpty(errorText)) { throw new ArgumentException("V nastaveních chybí následující údaje: " + errorText); } 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 MARC_Record record_from_z3950 = MARC_Record_Z3950_Retriever.Get_Record( z39BarcodeField, this.barcode, endpoint, out errorMessage, z39Encoding); if (record_from_z3950 != null) { Metadata metadata = new Metadata(); // Sysno metadata.Sysno = record_from_z3950.Control_Number; // Custom identifier if (!string.IsNullOrWhiteSpace(metadata.Sysno)) { metadata.Custom = Settings.Base + metadata.Sysno; } // Title foreach (var field in Settings.MetadataTitleFields) { if (!record_from_z3950.has_Field(field.Key)) { continue; } foreach (var subfield in field.Value) { metadata.Title += record_from_z3950.Get_Data_Subfield(field.Key, subfield).TrimEnd('/', ' ') + " "; } } // Authors string authors = ""; foreach (var field in Settings.MetadataAuthorFields) { if (!record_from_z3950.has_Field(field.Key)) { continue; } List <MARC_Field> authorFields = record_from_z3950.Fields[field.Key]; foreach (var authorField in authorFields) { foreach (var subfield in field.Value) { MARC_Subfield authorSubfield = authorField.Subfields_By_Code(subfield).FirstOrDefault(); authors += authorSubfield == null ? "" : authorSubfield.Data; authors += "@"; } authors = authors.Trim() + "|"; } } metadata.Authors = ParseAuthors(authors, '@'); // Publish year metadata.Year = GetZ39Subfields(record_from_z3950, Settings.MetadataPublishYearField.Item1, Settings.MetadataPublishYearField.Item2); // ISBN metadata.ISBN = GetNormalizedISBN(GetZ39Subfields(record_from_z3950, Settings.MetadataIsbnField.Item1, Settings.MetadataIsbnField.Item2)); if (metadata.ISBN.Contains('|')) { Warnings.Add("Záznam obsahuje více než 1 ISBN, vyberte správné, jsou oddělena zvislou čárou."); } // ISSN metadata.ISSN = GetNormalizedISSN(GetZ39Subfields(record_from_z3950, Settings.MetadataIssnField.Item1, Settings.MetadataIssnField.Item2)); if (metadata.ISSN.Contains('|')) { Warnings.Add("Záznam obsahuje více než 1 ISSN, vyberte správné, jsou oddělena zvislou čárou."); } // CNB metadata.CNB = GetZ39Subfields(record_from_z3950, Settings.MetadataCnbField.Item1, Settings.MetadataCnbField.Item2); if (metadata.CNB.Contains('|')) { Warnings.Add("Záznam obsahuje více než 1 ČNB, vyberte správné, jsou oddělena zvislou čárou."); } // OCLC metadata.OCLC = GetZ39Subfields(record_from_z3950, Settings.MetadataOclcField.Item1, Settings.MetadataOclcField.Item2); if (metadata.OCLC.Contains('|')) { Warnings.Add("Záznam obsahuje více než 1 OCLC, vyberte správné, jsou oddělena zvislou čárou."); } // EAN string ean = ""; if (record_from_z3950.has_Field(Settings.MetadataEanField.Item1)) { List <MARC_Field> eanFields = record_from_z3950.Fields[Settings.MetadataEanField.Item1]; foreach (var eanField in eanFields) { if (eanField.Indicator1 != Settings.MetadataEanField.Item3) { continue; } if (eanField.has_Subfield(Settings.MetadataEanField.Item2)) { foreach (var eanSubfield in eanField.Subfields_By_Code(Settings.MetadataEanField.Item2)) { ean += eanSubfield.Data + "|"; } } } } ean = ean.TrimEnd('|'); if (!string.IsNullOrWhiteSpace(ean)) { metadata.EAN = ean; if (metadata.EAN.Contains('|')) { this.Warnings.Add("Záznam obsahuje více než 1 EAN, vyberte správné, jsou oddělena zvislou čárou"); } } metadata.FixedFields = MarcGetFixedFields(record_from_z3950); metadata.VariableFields = MarcGetVariableFields(record_from_z3950); this.Metadata = metadata; } } catch (Exception) { throw new Z39Exception("Nastala neočekávaná chyba během Z39.50 dotazu."); } // Display any error message encountered if (this.Metadata == null) { 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"); } } }