private static Int64 InsertFormFilingSystem(FormFilingSystem filingSystem, string url, string Username, string Drowssap) { string payload = string.Empty; Int64 formFilingSystemId = -1; try { // get post data. payload = JsonConvert.SerializeObject(filingSystem); byte[] buf = Encoding.UTF8.GetBytes(payload); // create the http web request string query = "/api/FormFilingSystem"; var request = (HttpWebRequest)WebRequest.Create(url + query); request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(Username + ":" + Drowssap))); request.Method = "POST"; request.ContentLength = buf.Length; request.ContentType = "application/json; charset=utf-8"; request.GetRequestStream().Write(buf, 0, buf.Length); // get response and deserialize it. using (var response = (HttpWebResponse)request.GetResponse()) { if (response != null) { var responseStream = response.GetResponseStream(); var streamReader = new System.IO.StreamReader(responseStream, Encoding.UTF8); var responseString = streamReader.ReadToEnd(); FormFilingSystem responseDependency = responseString.Length > 0 ? JsonConvert.DeserializeObject <FormFilingSystem>(responseString) : null; formFilingSystemId = responseDependency.FormFilingSystemId; } } } catch (Exception ex) { Console.WriteLine("9: InsertFormFilingSystem: The following attempted insert/update failed: [{0}]. The unhandled exception that occurred: [{1}]", payload, ex.Message); } return(formFilingSystemId); }
/// <summary> /// /// </summary> /// <param name="args"></param> /// <remarks>args[0] = File path and name</remarks> /// <remarks>args[1] = url for Tax Form Catalog</remarks> /// <remarks>args[2] = user id for Tax Form Catalog</remarks> /// <remarks>args[3] = password for Tax Form Catalog</remarks> static void Main(string[] args) { List <InputData> filingSystems = new List <InputData>(); // = null; //Populate object with source file using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fs, false)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); SheetData sheetData = worksheetPart.Worksheet.Elements <SheetData>().First(); foreach (Row row in sheetData.Elements <Row>()) { InputData filingSystem = new InputData(); int rowcolumn = 1; foreach (Cell c in row.Elements <Cell>()) { switch (rowcolumn) { case 1: filingSystem.TaxFormCode = ReadExcelCell(c, workbookPart); break; case 2: filingSystem.State = ReadExcelCell(c, workbookPart); break; case 3: filingSystem.FilingSystem = ReadExcelCell(c, workbookPart) ?? null; break; } rowcolumn++; } filingSystems.Add(filingSystem); } } } int totalRecords = filingSystems.Count; Int64 formMasterId = 0; int recordsInserted = 0; int recordsErrored = 0; // Create List of FormCellDependency records to add foreach (InputData item in filingSystems) { formMasterId = GetFormMasterId(item.TaxFormCode, args[1], args[2], args[3]); if (formMasterId <= 0) { Console.WriteLine(string.Format("1: Unable to find a formmaster record for taxformcode [{0}].", item.TaxFormCode)); } else { try { Int64 filingSystemId = 0; switch (item.FilingSystem.ToLower()) { case "transmission": filingSystemId = 1; break; case "skyscraper": filingSystemId = 2; break; case "onetransmission": filingSystemId = 3; break; default: filingSystemId = 0; break; } List <FormFilingSystem> existingFilingSystems = GetFormFilingSystems(formMasterId, args[1], args[2], args[3]); FormFilingSystem formFilingSystem = existingFilingSystems.Any() ? existingFilingSystems.Where <FormFilingSystem>(x => x.FilingSystemId == filingSystemId).FirstOrDefault() : null; if (formFilingSystem == null || formFilingSystem == new FormFilingSystem()) { FormFilingSystem filingSystemToAdd = new FormFilingSystem(); filingSystemToAdd.FormFilingSystemId = -1; filingSystemToAdd.FormMasterId = formMasterId; filingSystemToAdd.FilingSystemId = filingSystemId; filingSystemToAdd.Disabled = false; filingSystemToAdd.IsDefault = existingFilingSystems.Count > 0 ? false : true; filingSystemToAdd.GenerateTransmissionFile = filingSystemId == 1 || filingSystemId == 3 ? true : false; Int64 insertedFilingSystemId = -1; insertedFilingSystemId = InsertFormFilingSystem(filingSystemToAdd, args[1], args[2], args[3]); if (insertedFilingSystemId > 0) { recordsInserted++; } else { Console.WriteLine(string.Format("2: Insert to FormFilingSystem did not return a valid FormFilingSystemId for TaxFormCode [{0}].", item.TaxFormCode)); recordsErrored++; } } else { Console.WriteLine(string.Format("3: An Existing FormFilingSystemRecord exists for TaxFormCode [{0}] and Filing System [{1}].", item.TaxFormCode, item.FilingSystem)); } } catch (Exception ex) { Console.WriteLine(string.Format("4: LoadFormFilingSystem: An error occurred [{0}] while processing TaxFormCode [{1}] and filing system [{2}].", ex.Message, item.TaxFormCode, item.FilingSystem)); recordsErrored++; } } } Console.WriteLine(string.Format("5: LoadFormFilingSystem: {0} records inserted and {1} records errored out of {2} total records.", recordsInserted.ToString(), recordsErrored.ToString(), totalRecords.ToString())); Console.ReadKey(); }