public  DataCleanEvent ValidateAddress(InputStreetAddress input)
        {

            DataCleanEvent e;
            OutputStreetAddress output;
            if (_criteria.ForceValidation == false)
            {
                e = _dataCleanRepository.GetEvent(input.ID);
                if (e != null) return e;
            }

            var b = _dataCleaner.VerifyAndCleanAddress(input, out output);
            e = new DataCleanEvent() {Input = input, DataCleanDate = DateTime.Now, Output = output};
            _dataCleanRepository.SaveEvent(e);
            return e;
        }
        public static VoucherImportWrapper ToVoucherImportWrapper(DataCleanEvent e, VoucherImportWrapper src)
        {
            VoucherImportWrapper tgt = null;

            try
            {
                tgt = ToVoucherImportWrapper(e);
                tgt.JobNumber = src.JobNumber;
                tgt.Amount = src.Amount;
                return tgt;
            }
            catch (Exception x)
            {

                throw new Exception(x.Message + " - No matching wrapper for event tgtId =" + tgt.ID + " - Src ID = " + src.ID);
            }
        }
 public void CantSaveOutputDoesNotMatchInputTest()
 {
     var db = new DataCleanRespository();
     var e = new DataCleanEvent()
     {
         Input = TestData.GoodAddresstoClean,
         DataCleanDate = DateTime.Now,
          Output = _goodOutputStreetAddress
     };
     // output id does not match input
     var id = e.ID;
     try
     {
         db.SaveEvent(e);
     }
     catch (Exception ex)
     {
         Assert.IsTrue(ex.Message.Contains("id"));
     }
 }
        public void CantSaveUncleanEventTest()
        {
            var db = new DataCleanRespository();
            var e = new DataCleanEvent()
            {
                Input = TestData.GoodAddresstoClean
                //DataCleanDate = DateTime.Now  

               // We dont want to save an event without a data clean date! 
            };
            var id = e.ID;
            try
            {
                db.SaveEvent(e);
            }
            catch( Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("date"));
            }
        }
        public static VoucherImportWrapper ToVoucherImportWrapper(DataCleanEvent e)
        {
            var n = new VoucherImport();
            n.AddressLine1 = e.Input.AddressLine1;
            n.AddressLine2 = e.Input.AddressLine2;
            n.Company = e.Input.CompanyName;
            n.Country = e.Input.Country;
            n.EmailAddress = e.Input.EmailAddress;
            n.First = e.Input.FirstName;
            n.Last = e.Input.LastName;
            n.Municipality = e.Input.City;
            n.PhoneNumber = e.Input.PhoneNumber;
            n.PostalCode = e.Input.PostalCode;
            n.Region = e.Input.State;
            n.RecordID = e.Input.RecordID;
            n.ID = e.Input.ID;

            var v = new VoucherImportWrapper(n);
            v.DataCleanDate = e.DataCleanDate;
            v.AltAddress = e.Output;
            return v;
        }
        public void CanSaveEventTest()
        {
            var testTime = DateTime.Now;
            var db = new DataCleanRespository();
            var e = new DataCleanEvent()
            {
                Input = TestData.GoodAddresstoClean,
                DataCleanDate = testTime,
                Output = _goodOutputStreetAddress

            };
            // if the output id does not match it will not resolve correctly when we combine the two after datacleaner is done
            //
            e.Output.ID = e.ID;

            var id = e.ID;
            db.SaveEvent(e);
            var d = db.GetEvent(e.ID);
            Assert.IsTrue(d.ID == e.ID);
            Assert.IsTrue(d.Output.ID == e.ID);
            Assert.IsTrue(d.Output.ID == e.Input.ID);
            Assert.IsTrue(d.DataCleanDate == testTime);
        }
        // save the results of the dataclean operation to the database 
        // convert the event and its related addresses and parse  results to json and store em 
        // if a datacleanevent exists the contents are updated 
        public void SaveEvent(DataCleanEvent e)
        {
            try
            {
                if (e == null) throw new Exception("null DataClean event cant be saved ");
                if (e.DataCleanDate == DateTime.MinValue) throw new Exception("Bad DataClean date - only clean events can be saved");
                if (e.Input == null) throw new Exception("Bad DataClean input - input address needed");
                if (e.Output == null) throw new Exception("Bad DataClean output - dataclean output needed for event to be saved");
                if (e.Output.Results== null) throw new Exception("Bad DataClean output results - dataclean output must contain results to be saved. was DataClener run on the event? ");
                if (e.Output.ID != e.Input.ID) throw new Exception("Bad address match - dataclean output id must match input id");

                var a = new DataCleanEventLog()
                {
                    ID = e.ID,
                    DataCleanDate = e.DataCleanDate,
                    DataCleanEventJson = JsonConvert.SerializeObject(e)
                };

                var v = _ctx.DataCleanEventLogs.Find(a.ID);
                if (v != null)
                {
                    v.DataCleanDate = a.DataCleanDate;
                    v.DataCleanEventJson = a.DataCleanEventJson;
                }
                else
                {
                    _ctx.DataCleanEventLogs.Add(a);
                }
                _ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(e.Input?.EmailAddress);
                Logger.Error(ex.Message);
                Logger.Error(ex.InnerException.Message);
            }
        }
 private DataCleanEvent MergeInandOutReq(InputStreetAddress inAddress, DataCleanEvent outEvent)
 {
     var e = new DataCleanEvent
     {
         DataCleanDate = outEvent.DataCleanDate,
         Input = inAddress,
         Output = outEvent.Output
     };
     e.Output.RecordID = e.Input.RecordID;
     return e;
 }
        private List<DataCleanEvent> CombineOutputsAndInputs(List<InputStreetAddress> toBeCleaned,OutputStreetAddress[] outArray)
        {

            var convertedList = new List<DataCleanEvent>();

            foreach (var i in toBeCleaned)
            {
                var e = new DataCleanEvent();
                //find related output assign to local var
                var oc = outArray.First(obj => obj.RecordID == i.RecordID);
                if (oc != null)
                {
                    e.DataCleanDate = DateTime.Now;
                    e.Input = i;
                    e.Output = oc;
                    convertedList.Add(e);
                }
            }
            return convertedList;
        }
 public void CantSaveEmptyEventTest()
 {
     var db = new DataCleanRespository();
     var e = new DataCleanEvent();
     var id = e.ID;
     try
     {
         db.SaveEvent(e);
     }
     catch (Exception ex)
     {
         Assert.IsTrue(ex.Message.Contains("event"));
     }
 }
        public void CantSaveWithoutOutputTest()
        {
            var db = new DataCleanRespository();
            var e = new DataCleanEvent()
            {
                DataCleanDate = DateTime.Now,
                Input = TestData.GoodAddresstoClean
            };
            var id = e.ID;
            try
            {

                db.SaveEvent(e);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("output"));
            }
        }