public List<SalesLead> ConvertToType(string filePath)
        {
            List<SalesLead> salesLeads = new List<SalesLead>();

            using (ICsvParser csvParser = _csvReaderFactory.CreateCsvParser(filePath))
            {
                Row row = new Row();
                int rowNumber = 1;
                try
                {
                    row.RowNumber = rowNumber;
                    while (csvParser.ReadRow(row))
                    {
                        try
                        {
                            AddUpdateSalesLead(ref salesLeads, row, rowNumber);
                            rowNumber++;
                            row.RowNumber = rowNumber;
                        }
                        catch (InvalidRowInputException invalidRowException)
                        {
                            if (invalidRowException.InvalidColumns.Count > 0)
                            {
                                PrintInvalidRowMessage(invalidRowException, rowNumber);
                            }
                            else
                            {
                                Console.WriteLine(invalidRowException.Message);
                            }
                            return null;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                            return null;
                        }
                    }
                }
                catch (InvalidRowInputException invalidRowException)
                {
                    Console.WriteLine("Row {0} contains no data. Please make sure the csv file has been formatted correctly.", rowNumber);
                    return null;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return null;
                }
            }

            if (salesLeads.Count > 0)
            {
                salesLeads.ForEach(sl => sl.EventScoreSum = sl.EventScores.Sum(es => es.WeightedScore));
                double min = salesLeads.Min(x => x.EventScoreSum);
                double max = salesLeads.Max(x => x.EventScoreSum);

                foreach (SalesLead sl in salesLeads)
                {
                    sl.EventScoreNormalized = _salesLeadCalculationUtils.CalculateNormalizedValue(min, max, sl.EventScoreSum);
                    sl.ContactQuartile = _salesLeadCalculationUtils.DetermineQuartile(sl.EventScoreNormalized);
                }
            }

            return salesLeads.OrderBy(x => x.ContactId).ToList();
        }
Esempio n. 2
0
        public bool ReadRow(Row row)
        {
            row.LineStr = ReadLine();

            if (String.IsNullOrEmpty(row.LineStr))
            {
                if (row.RowNumber < _totalRows)
                {
                    throw new InvalidRowInputException("Row is empty");
                }
                return false;
            }

            int position = 0;
            int rows = 0;

            while (position < row.LineStr.Length)
            {
                string value;

                if (row.LineStr[position] == '"')
                {
                    position++;
                    int start = position;
                    while (position < row.LineStr.Length)
                    {
                        if (row.LineStr[position] == '"')
                        {
                            position++;

                            if (position >= row.LineStr.Length || row.LineStr[position] != '"')
                            {
                                position--;
                                break;
                            }
                        }
                        position++;
                    }
                    value = row.LineStr.Substring(start, position - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    int start = position;
                    while (position < row.LineStr.Length && row.LineStr[position] != ',')
                        position++;
                    value = row.LineStr.Substring(start, position - start);
                }

                if (rows < row.Columns.Count)
                {
                    row.Columns[rows] = value;
                }
                else
                {
                    row.Columns.Add(value);
                }
                rows++;

                while (position < row.LineStr.Length && row.LineStr[position] != ',')
                    position++;
                if (position < row.LineStr.Length) position++;
            }

            while (row.Columns.Count > rows) row.Columns.RemoveAt(rows);

            return (row.Columns.Count > 0);
        }
        private void AddUpdateSalesLead(ref List<SalesLead> salesLeads, Row row, int rowNumber)
        {
            SalesLead salesLead = new SalesLead();
            if (row.Columns.Count == 3)
            {
                int contactId;
                bool contactIdValid = int.TryParse(row.Columns[0], out contactId);
                EventType eventType;
                bool eventTypeValid = _eventTypeConverter.TryParseString(row.Columns[1], out eventType);
                double score;
                bool scoreValid = double.TryParse(row.Columns[2], out score);
                if (!contactIdValid || !eventTypeValid || !scoreValid)
                {
                    List<int> invalidColumns = new List<int>(3);
                    if (!contactIdValid) invalidColumns.Add(1);
                    if (!eventTypeValid) invalidColumns.Add(2);
                    if (!scoreValid) invalidColumns.Add(3);
                    throw new InvalidRowInputException(invalidColumns);
                }
                SalesLead existing = salesLeads.FirstOrDefault(sl => sl.ContactId == contactId);
                if (existing == null)
                {
                    salesLead.ContactId = contactId;
                    salesLead.EventScores = new List<EventScore>
                    {
                        new EventScore
                        {
                            Type = eventType,
                            Score = score,
                            WeightedScore = _salesLeadCalculationUtils.CalculateWeightedValue(eventType, score)
                        }
                    };
                    salesLeads.Add(salesLead);
                }
                else
                {
                    existing.EventScores.Add(new EventScore
                    {
                        Type = eventType,
                        Score = score,
                        WeightedScore = _salesLeadCalculationUtils.CalculateWeightedValue(eventType, score)
                    });
                }

            }
            else
            {
                throw new InvalidRowInputException("Invalid number of columns for row " + rowNumber + ". " +
                                                   "Each row must contain comma separated columns for " +
                                                   "contactId, event, and score.");
            }
        }