Exemplo n.º 1
0
        private void BtnRunAging_Click(object sender, EventArgs e)
        {
            if (ValidationLogic.DateInvalid(DtpAnchorDate.Value))
            {
                ErrorActions.DateInFuture();
            }
            else if (ValidationLogic.ColumnsNotSelected(LbxDataToAge, LbxDatesToAgeBy))
            {
                ErrorActions.ColumnsNotSelected();
            }
            else
            {
                // simple visibility changes are how the processing status is communicated.
                BtnRunAging.Visible   = false;
                LblProcessing.Visible = true;
                // ensure our accumilator is ready to receive data.
                AgedData.Zero();
                int dateColumn  = LbxDatesToAgeBy.SelectedIndex + 1; // Excel arrays are not 0 based
                int valueColumn = LbxDataToAge.SelectedIndex + 1;    // Excel arrays are not 0 based

                // since our data included a header we skip over the first row (i=2).
                for (int i = 2; i <= ExcelThread.DataSet.Rows.Count; i++)
                {
                    // value2 property doesn’t use the Currency and Date data types returns as Double
                    var rawDate = ExcelThread.DataSet.Cells[i, dateColumn].Value2;
                    var rawVal  = ExcelThread.DataSet.Cells[i, valueColumn].Value2;

                    /* since we are not validating the existance of data within each cell in our dataset
                     * we need to check that we sucessfully returned non-nulls. If either the value
                     * within the 'to age' or the 'to age by' column for a given row are empty the
                     * entire row will be skipped. This application is not intended to clean data only
                     * process an existing proper dataset. */
                    if (rawDate is null || rawVal is null)
                    {
                        continue;
                    }

                    // getting the types of the data returned and also setting flags used in type checking.
                    Type  dateType = rawDate.GetType();
                    Type  valType  = rawVal.GetType();
                    int   bucket   = -1;
                    float value    = (float)-1.0;

                    /* by using the Value2 property we ensure that dates will be read in at Doubles. Given
                     * the stagering diversity in the way date strings are represented the decision was
                     * made to reject any dates not formated as a date, datetime, or number within excel. */
                    if (dateType.Name != "Double")
                    {
                        ErrorActions.InvalidDataType();
                    }
                    else
                    {
                        int daysPast = (DtpAnchorDate.Value - DateTime.FromOADate(rawDate)).Days;
                        bucket = daysPast / (int)30; // explicitly casting denominator at int because I am paranoid.
                    }

                    /* if the 'to age' value read in was a double (because of using Value2) we cast it
                     * to a float and we parse any string received into a float as well. */
                    if (valType.Name == "Double")
                    {
                        value = (float)rawVal;
                    }
                    else if (valType.Name == "String")
                    {
                        try
                        {
                            value = float.Parse(rawVal);
                        }
                        catch
                        {
                            ErrorActions.InvalidDataType();
                        }
                    }
                    else
                    {
                        ErrorActions.InvalidDataType();
                    }

                    // finally we validate our type check flags and update our accumlator
                    if (bucket == -1 || value == (float)-1.0)
                    {
                        ErrorActions.InvalidValue();
                    }
                    else
                    {
                        AgedData.AddAgedData(bucket, value);
                    }
                }
                // simple visibility and text changes are how the processing status is communicated.
                LblProcessing.Text = "Complete"; // text was changed to eliminate a control from the form.
                BtnView.Visible    = true;
                BtnSave.Visible    = true;
            }
        }