Exemplo n.º 1
0
        private static Int32 UpdateFormCellDepencencies(List <FormCellDependency> dependencyList, string url, string Username, string Drowssap, out int numberUpdated, out int numberErrored)
        {
            int numberInserted = 0;

            numberUpdated = 0;
            numberErrored = 0;

            foreach (FormCellDependency currentDependency in dependencyList)
            {
                string payload = string.Empty;
                try
                {
                    // get post data.
                    payload = JsonConvert.SerializeObject(currentDependency);
                    byte[] buf = Encoding.UTF8.GetBytes(payload);

                    // create the http web request
                    string query   = currentDependency.FormCellDependencyId > 0 ? string.Format("/api/FormCellDependency/{0}", currentDependency.FormCellDependencyId.ToString()) : "/api/FormCellDependency";
                    var    request = (HttpWebRequest)WebRequest.Create(url + query);
                    request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(Username + ":" + Drowssap)));

                    request.Method        = currentDependency.FormCellDependencyId > 0 ? "PUT" : "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();

                            FormCellDependency responseDependency = responseString.Length > 0 ? JsonConvert.DeserializeObject <FormCellDependency>(responseString) : null;
                            if (currentDependency.FormCellDependencyId > 0)
                            {
                                if (responseDependency.FormCellDependencyId > 0)
                                {
                                    numberUpdated++;
                                }
                            }
                            else
                            {
                                if (responseDependency.FormCellDependencyId > 0)
                                {
                                    numberInserted++;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("UpdateFormCellDepencencies: The following attempted insert/update failed: [{0}].  The unhandled exception that occurred: [{1}]", payload, ex.Message);
                    numberErrored++;
                }
            }

            return(numberInserted);
        }
Exemplo n.º 2
0
        /// <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> newDependencies = 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     newDependency = new InputData();
                        int           rowcolumn     = 1;
                        int           tempMonths    = -1;
                        List <String> firstRow      = new List <String>();

                        foreach (Cell c in row.Elements <Cell>())
                        {
                            switch (rowcolumn)
                            {
                            //New Format
                            case 1: newDependency.TaxFormCode = ReadExcelCell(c, workbookPart); break;

                            case 2: newDependency.SourceTaxFormCode = ReadExcelCell(c, workbookPart); break;

                            case 3: newDependency.SummaryLabel = ReadExcelCell(c, workbookPart); break;

                            case 4: newDependency.FieldName = ReadExcelCell(c, workbookPart) ?? null; break;

                            case 5: newDependency.CurrentPeriod = ReadExcelCell(c, workbookPart) ?? null; break;

                            case 6: newDependency.Months = int.TryParse(ReadExcelCell(c, workbookPart), out tempMonths) ? tempMonths : 0; break;
                            }

                            rowcolumn++;
                        }

                        if (newDependency.TaxFormCode != "TaxFormCode")
                        {
                            newDependencies.Add(newDependency);
                        }
                    }
                }
            }

            List <FormCellDependency> recordsToAdd          = new List <FormCellDependency>();
            int                       totalRecords          = newDependencies.Count;
            string                    prevTaxFormCode       = string.Empty;
            string                    prevSourceTaxFormCode = string.Empty;
            long                      formMasterId          = 0;
            long                      updateFormMasterId    = 0;
            List <Annotation>         formAnnotations       = new List <Annotation>();
            List <SummaryLabel>       summaryLabels         = GetSummaryLabels(args[1], args[2], args[3]);
            List <FormCellDependency> existingDependencies  = new List <FormCellDependency>();


            // Create List of FormCellDependency records to add
            foreach (InputData item in newDependencies)
            {
                if (!item.TaxFormCode.Equals(prevTaxFormCode))
                {
                    prevTaxFormCode      = item.TaxFormCode;
                    updateFormMasterId   = GetFormMasterId(item.TaxFormCode, args[1], args[2], args[3]);
                    existingDependencies = GetFormCellDependencies(updateFormMasterId, args[1], args[2], args[3]);
                }

                if (!item.SourceTaxFormCode.Equals(prevSourceTaxFormCode))
                {
                    formMasterId          = GetFormMasterId(item.SourceTaxFormCode, args[1], args[2], args[3]);
                    prevSourceTaxFormCode = item.SourceTaxFormCode;
                    if (formMasterId > 0)
                    {
                        formAnnotations = GetAnnotations(formMasterId, args[1], args[2], args[3]);
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Unable to find an annotation record for taxformcode [{0}].", item.TaxFormCode));
                    }
                }

                if (formMasterId > 0)
                {
                    try
                    {
                        FormCellDependency dependency = null;
                        var annotation   = formAnnotations.Any() ? formAnnotations.Where(a => a.AnnotationName == item.FieldName).FirstOrDefault() : null;
                        var summaryLabel = summaryLabels.Any() ? summaryLabels.Where(a => a.SummaryLabelCode == item.SummaryLabel).FirstOrDefault() : null;
                        if (annotation == null)
                        {
                            if (summaryLabel == null)
                            {
                                Console.WriteLine(string.Format("AddLocalFormCellDependencyFromExcel:  Unable to find summary label [{0}] or field name [{1}] on tax form [{2}].", item.SummaryLabel, item.FieldName, item.TaxFormCode));
                            }
                            else
                            {
                                dependency = existingDependencies.Any() ? existingDependencies.Where(d => d.SummaryLabelId == summaryLabel.SummaryLabelId && !d.AnnotationId.HasValue && d.FormMasterOptionId == formMasterId).FirstOrDefault() : null;
                            }
                        }
                        else
                        {
                            if (summaryLabel == null)
                            {
                                dependency = existingDependencies.Any() ? existingDependencies.Where(d => d.AnnotationId == annotation.AnnotationId && !d.SummaryLabelId.HasValue && d.FormMasterOptionId == formMasterId).FirstOrDefault() : null;
                            }
                            else
                            {
                                dependency = existingDependencies.Any() ? existingDependencies.Where(d => d.SummaryLabelId == summaryLabel.SummaryLabelId && d.AnnotationId == annotation.AnnotationId && d.FormMasterOptionId == formMasterId).FirstOrDefault() : null;
                            }
                        }
                        if (summaryLabel != null || annotation != null)
                        {
                            FormCellDependency dependencyToAdd = new FormCellDependency();
                            dependencyToAdd.FormCellDependencyId = dependency == null ? -1 : dependency.FormCellDependencyId;
                            dependencyToAdd.FormMasterId         = updateFormMasterId;
                            dependencyToAdd.FormMasterOptionId   = formMasterId;
                            dependencyToAdd.AnnotationId         = annotation == null ? null : annotation.AnnotationId;
                            dependencyToAdd.SummaryLabelId       = summaryLabel == null ? null : summaryLabel.SummaryLabelId; //null;  // needs to be added if we plan to use this for other forms
                            dependencyToAdd.IncludeCurrentPeriod = item.CurrentPeriod == "1" ? true : false;
                            dependencyToAdd.MonthsAgo            = item.Months;
                            dependencyToAdd.PriorYearEndFiscal   = false; // needs to be added if we plan to use this for other forms
                            dependencyToAdd.CreatedDate          = System.DateTime.UtcNow;
                            dependencyToAdd.CreatedUserId        = 0;
                            dependencyToAdd.ModifiedDate         = System.DateTime.UtcNow;
                            dependencyToAdd.ModifiedUserId       = 0;
                            recordsToAdd.Add(dependencyToAdd);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("AddFormCellDependencyFromExcel: An error occurred [{0}] while processing TaxFormCode [{1}] and summary label [{2}] and field [{3}].", ex.Message, item.TaxFormCode, item.SummaryLabel, item.FieldName));
                    }
                }
            }


            // insert records to FormCellDependency
            int recordsInserted = 0;
            int recordsUpdated  = 0;
            int recordsErrored  = 0;

            if (recordsToAdd.Count > 0)
            {
                recordsInserted = UpdateFormCellDepencencies(recordsToAdd, args[1], args[2], args[3], out recordsUpdated, out recordsErrored);
            }
            Console.WriteLine(string.Format("AddFormCellDependencyFromExcel: {0} records inserted and {1} records updated out of {2} total records.  There were {3} records that resulted in an error. ", recordsInserted.ToString(), recordsUpdated.ToString(), totalRecords.ToString(), recordsErrored.ToString()));
            Console.ReadKey();
        }