/// <summary> /// Select values from MarcImportBatch by primary key(s). /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="connectionKeyName">Connection key name located in config file.</param> /// <param name="marcImportBatchID"></param> /// <returns>Object of type MarcImportBatch.</returns> public MarcImportBatch MarcImportBatchSelectAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, string connectionKeyName, int marcImportBatchID) { SqlConnection connection = CustomSqlHelper.CreateConnection(CustomSqlHelper.GetConnectionStringFromConnectionStrings(connectionKeyName), sqlConnection); SqlTransaction transaction = sqlTransaction; using (SqlCommand command = CustomSqlHelper.CreateCommand("MarcImportBatchSelectAuto", connection, transaction, CustomSqlHelper.CreateInputParameter("MarcImportBatchID", SqlDbType.Int, null, false, marcImportBatchID))) { using (CustomSqlHelper <MarcImportBatch> helper = new CustomSqlHelper <MarcImportBatch>()) { CustomGenericList <MarcImportBatch> list = helper.ExecuteReader(command); if (list.Count > 0) { MarcImportBatch o = list[0]; list = null; return(o); } else { return(null); } } } }
/// <summary> /// Manage MarcImportBatch object. /// If the object is of type CustomObjectBase, /// then either insert values into, delete values from, or update values in MarcImportBatch. /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="value">Object of type MarcImportBatch.</param> /// <returns>Object of type CustomDataAccessStatus<MarcImportBatch>.</returns> public CustomDataAccessStatus <MarcImportBatch> MarcImportBatchManageAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, MarcImportBatch value) { return(MarcImportBatchManageAuto(sqlConnection, sqlTransaction, "BHL", value)); }
/// <summary> /// Update values in MarcImportBatch. Returns an object of type MarcImportBatch. /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="value">Object of type MarcImportBatch.</param> /// <returns>Object of type MarcImportBatch.</returns> public MarcImportBatch MarcImportBatchUpdateAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, MarcImportBatch value) { return(MarcImportBatchUpdateAuto(sqlConnection, sqlTransaction, "BHL", value)); }
/// <summary> /// Update values in MarcImportBatch. Returns an object of type MarcImportBatch. /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="connectionKeyName">Connection key name located in config file.</param> /// <param name="marcImportBatchID"></param> /// <param name="fileName"></param> /// <param name="institutionCode"></param> /// <returns>Object of type MarcImportBatch.</returns> public MarcImportBatch MarcImportBatchUpdateAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, string connectionKeyName, int marcImportBatchID, string fileName, string institutionCode) { SqlConnection connection = CustomSqlHelper.CreateConnection(CustomSqlHelper.GetConnectionStringFromConnectionStrings(connectionKeyName), sqlConnection); SqlTransaction transaction = sqlTransaction; using (SqlCommand command = CustomSqlHelper.CreateCommand("MarcImportBatchUpdateAuto", connection, transaction, CustomSqlHelper.CreateInputParameter("MarcImportBatchID", SqlDbType.Int, null, false, marcImportBatchID), CustomSqlHelper.CreateInputParameter("FileName", SqlDbType.NVarChar, 500, false, fileName), CustomSqlHelper.CreateInputParameter("InstitutionCode", SqlDbType.NVarChar, 10, true, institutionCode), CustomSqlHelper.CreateReturnValueParameter("ReturnCode", SqlDbType.Int, null, false))) { using (CustomSqlHelper <MarcImportBatch> helper = new CustomSqlHelper <MarcImportBatch>()) { CustomGenericList <MarcImportBatch> list = helper.ExecuteReader(command); if (list.Count > 0) { MarcImportBatch o = list[0]; list = null; return(o); } else { return(null); } } } }
/// <summary> /// Insert values into MarcImportBatch. Returns an object of type MarcImportBatch. /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="connectionKeyName">Connection key name located in config file.</param> /// <param name="value">Object of type MarcImportBatch.</param> /// <returns>Object of type MarcImportBatch.</returns> public MarcImportBatch MarcImportBatchInsertAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, string connectionKeyName, MarcImportBatch value) { return(MarcImportBatchInsertAuto(sqlConnection, sqlTransaction, connectionKeyName, value.FileName, value.InstitutionCode)); }
/// <summary> /// Manage MarcImportBatch object. /// If the object is of type CustomObjectBase, /// then either insert values into, delete values from, or update values in MarcImportBatch. /// </summary> /// <param name="sqlConnection">Sql connection or null.</param> /// <param name="sqlTransaction">Sql transaction or null.</param> /// <param name="connectionKeyName">Connection key name located in config file.</param> /// <param name="value">Object of type MarcImportBatch.</param> /// <returns>Object of type CustomDataAccessStatus<MarcImportBatch>.</returns> public CustomDataAccessStatus <MarcImportBatch> MarcImportBatchManageAuto( SqlConnection sqlConnection, SqlTransaction sqlTransaction, string connectionKeyName, MarcImportBatch value) { if (value.IsNew && !value.IsDeleted) { MarcImportBatch returnValue = MarcImportBatchInsertAuto(sqlConnection, sqlTransaction, connectionKeyName, value.FileName, value.InstitutionCode); return(new CustomDataAccessStatus <MarcImportBatch>( CustomDataAccessContext.Insert, true, returnValue)); } else if (!value.IsNew && value.IsDeleted) { if (MarcImportBatchDeleteAuto(sqlConnection, sqlTransaction, connectionKeyName, value.MarcImportBatchID)) { return(new CustomDataAccessStatus <MarcImportBatch>( CustomDataAccessContext.Delete, true, value)); } else { return(new CustomDataAccessStatus <MarcImportBatch>( CustomDataAccessContext.Delete, false, value)); } } else if (value.IsDirty && !value.IsDeleted) { MarcImportBatch returnValue = MarcImportBatchUpdateAuto(sqlConnection, sqlTransaction, connectionKeyName, value.MarcImportBatchID, value.FileName, value.InstitutionCode); return(new CustomDataAccessStatus <MarcImportBatch>( CustomDataAccessContext.Update, true, returnValue)); } else { return(new CustomDataAccessStatus <MarcImportBatch>( CustomDataAccessContext.NA, false, value)); } }
protected void importButton_Click(object sender, EventArgs e) { try { if (fileUpload.HasFile) { BHLProvider provider = new BHLProvider(); List <String> files = new List <String>(); // Get the identifier for this import batch MarcImportBatch batch = provider.MarcImportBatchInsertAuto(fileUpload.FileName, listInstitutions.SelectedValue); String filePath = String.Format(ConfigurationManager.AppSettings["MarcUploadPath"], batch.MarcImportBatchID.ToString(), fileUpload.FileName); String xmlFilePath = filePath + ".xml"; // Save the file fileUpload.SaveAs(filePath); // --- Process the file --- int result = 0; if (rdoType.SelectedValue == "1") // MRC file { // Convert MARC21 to MARCXML MARCEngine5.MARC21 marcEngine = new MARCEngine5.MARC21(); result = marcEngine.MARC2MARC21XML(filePath, xmlFilePath, true); } else // MARCXML file { xmlFilePath = filePath; } if (result >= 0) { // Marc conversion OK // Extract the MARC record(s) from the MARCXML file into individual MARCXML // files. Even if the original fine contains only a single MARC record, // this action will remove any extra headers (OAI, etc) that might be // contained in the file. String xmlString = File.ReadAllText(xmlFilePath); String recordStartTag = String.Empty; String recordEndTag = String.Empty; if (xmlString.Contains("<marc:record")) { recordStartTag = "<marc:record"; recordEndTag = "</marc:record>"; } else { recordStartTag = "<record"; recordEndTag = "</record>"; } // Get the endpoints of the first record int recordCount = 1; int startPos = xmlString.IndexOf(recordStartTag, StringComparison.InvariantCultureIgnoreCase); int endPos = xmlString.IndexOf(recordEndTag, StringComparison.InvariantCultureIgnoreCase); while (startPos != -1) { // Get the record String recordString = xmlString.Substring(startPos, endPos + recordEndTag.Length - startPos); // Add namespace declarations recordString = recordString.Replace("<record>", "<record xmlns=\"http://www.loc.gov/MARC21/slim\">"); recordString = recordString.Replace("<marc:record>", "<marc:record xmlns:marc=\"http://www.loc.gov/MARC21/slim\">"); // Write record to a file File.WriteAllText(filePath + "." + recordCount.ToString() + ".xml", recordString); files.Add(filePath + "." + recordCount.ToString() + ".xml"); recordCount++; // Find the endpoints of the next record startPos = xmlString.IndexOf(recordStartTag, endPos + recordEndTag.Length, StringComparison.InvariantCultureIgnoreCase); endPos = xmlString.IndexOf(recordEndTag, endPos + recordEndTag.Length, StringComparison.InvariantCultureIgnoreCase); } } else { // Present error message to user errorControl.AddErrorText("Error converting MARC: " + result.ToString()); errorControl.Visible = true; } // For each of the files in the "files" list, parse the MARCXML // into database records. foreach (String file in files) { this.HarvestMarcData(file, batch.MarcImportBatchID, listInstitutions.SelectedValue); } // Try to match the just imported MARC records to existing BHL titles if (provider.MarcResolveTitles(batch.MarcImportBatchID)) { // Redirect to page that summarizes the records being imported Response.Redirect("TitleImport.aspx?id=" + batch.MarcImportBatchID.ToString()); } else { // Present error message to user errorControl.AddErrorText("Error resolving uploaded titles"); errorControl.Visible = true; } } else { // Present error message to user errorControl.AddErrorText("Please select a file to import"); errorControl.Visible = true; } } catch (Exception ex) { // Present error message to user errorControl.AddErrorText("Error importing MARC: " + ex.Message); errorControl.Visible = true; } }