public string Deserialize(ProducerException rules)
        {
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var exceptionJson = jsonSerializer.Serialize(rules);

            return(exceptionJson.ToString());
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var spreadSheets = Infrastructure.Importer.GetSpreadSheetsToProcess();
            var import       = new ExcelImport.Infrastructure.Producers();

            foreach (var spreadSheet in spreadSheets)
            {
                var producerImport = new List <Producer>();
                import.Open(spreadSheet);

                // we get all the producers
                var allProducers = import.GetProducersForImport();

                var distinctProducers = allProducers.Where(x => !string.IsNullOrEmpty(x.ProducerName)).OrderBy(x => x.ProducerName).Select(dp => new { ProductType = dp.ProductType, ProducerName = dp.ProducerName }).Distinct().ToList();

                var producerExceptionList = new List <ProducerException>();

                foreach (var producer in distinctProducers)
                {
                    var pe = new ProducerException {
                        ProducerName = producer.ProducerName
                    };

                    var exceptionRules = new List <ExceptionRule>();
                    var producers      = allProducers.Where(p => p.ProducerName == producer.ProducerName).ToList();

                    foreach (var p in producers)
                    {
                        if (!string.IsNullOrEmpty(p.Exceptions))
                        {
                            // build the exceptions
                            var exceptions = p.Exceptions.Split(',');

                            foreach (var exceptionType in exceptions)
                            {
                                RuleType ruleType;
                                if (Enum.TryParse(exceptionType.TrimEnd(' '), out ruleType))
                                {
                                    // build the exception
                                    if (ruleType == RuleType.Winemaker)
                                    {
                                        exceptionRules.Add(new ExceptionRule {
                                            RuleType = ruleType, WineMakerId = p.FirstName.Replace(" ", "") + p.LastName.Replace(" ", ""), ProductId = p.Variation
                                        });
                                    }

                                    if (ruleType == RuleType.Address)
                                    {
                                        try
                                        {
                                            if (p.CoOrdinates.Count == 2)
                                            {
                                                exceptionRules.Add(new ExceptionRule {
                                                    RuleType = ruleType, WineMakerId = p.FirstName.Replace(" ", "") + p.LastName.Replace(" ", ""), ProductId = p.Variation, Address = new ProducerAddress {
                                                        Latitude = p.CoOrdinates[0], Longitude = p.CoOrdinates[1], PostCode = p.PostCode, StreetAddress = p.AddressLine1, Suburb = p.AddressLine2
                                                    }
                                                });
                                            }
                                            else
                                            {
                                                exceptionRules.Add(new ExceptionRule {
                                                    RuleType = ruleType, WineMakerId = p.FirstName.Replace(" ", "") + p.LastName.Replace(" ", ""), ProductId = p.Variation, Address = new ProducerAddress {
                                                        Latitude = string.Empty, Longitude = string.Empty, PostCode = p.PostCode, StreetAddress = p.AddressLine1, Suburb = p.AddressLine2
                                                    }
                                                });
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                        }
                                    }

                                    // build the exception
                                    if (ruleType == RuleType.Biography)
                                    {
                                        exceptionRules.Add(new ExceptionRule {
                                            RuleType = ruleType, ProductBiography = p.Biography, ProductId = p.Variation
                                        });
                                    }

                                    // build the exception
                                    if (ruleType == RuleType.Brand)
                                    {
                                        exceptionRules.Add(new ExceptionRule {
                                            RuleType = ruleType, Brand = p.Brand, ProductId = p.Variation
                                        });
                                    }

                                    // build the exception
                                    if (ruleType == RuleType.ProductType)
                                    {
                                        exceptionRules.Add(new ExceptionRule {
                                            RuleType = ruleType, ProductType = p.ProductType, ProductId = p.Variation
                                        });
                                    }
                                }
                            }
                        }
                    }


                    pe.ExceptionRules = exceptionRules;
                    producerExceptionList.Add(pe);
                }

                var finalListWithExceptions = new List <Producer>();


                foreach (var producer in distinctProducers)
                {
                    var perl = allProducers.Where(er => er.ProducerName == producer.ProducerName).FirstOrDefault();

                    if (perl != null)
                    {
                        var pexceptions = producerExceptionList.Where(ex => ex.ProducerName == perl.ProducerName).FirstOrDefault();

                        if (pexceptions != null)
                        {
                            perl.Exceptions = Deserialize(pexceptions);
                        }

                        finalListWithExceptions.Add(perl);
                    }
                }

                rptRecipes.DataSource = finalListWithExceptions;
                rptRecipes.DataBind();
            }
        }