/// <summary>
        /// Update an existing CallStackPattern
        /// </summary>
        /// <param name="entity"></param>
        public void Update(CallStackPattern entity)
        {
            var set   = _entityContext.Set <CallStackPattern>();
            var entry = set.Local.SingleOrDefault(f => f.id == entity.id);

            if (entry != null)
            {
                var attachedFeature = _entityContext.Entry(entry);
                attachedFeature.CurrentValues.SetValues(entity);
                attachedFeature.State = EntityState.Modified;
            }
            else
            {
                _entityContext.CallStackPatterns.Attach(entity);
                _entityContext.Entry(entity).State = EntityState.Modified;
            }
        }
        /// <summary>
        /// Create a new crash data model object and insert it into the database
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        private Crash CreateCrash(CrashDescription description)
        {
            var newCrash = new Crash
            {
                Branch            = description.BranchName,
                BaseDir           = description.BaseDir,
                BuildVersion      = description.EngineVersion,
                EngineVersion     = description.BuildVersion,
                ChangeListVersion = description.BuiltFromCL.ToString(),
                CommandLine       = description.CommandLine,
                EngineMode        = description.EngineMode,
                ComputerName      = description.MachineGuid
            };

            //if there's a valid username assign the associated UserNameId else use "anonymous".
            var userName = (!string.IsNullOrEmpty(description.UserName)) ? description.UserName : UserNameAnonymous;
            var user     = _unitOfWork.UserRepository.GetByUserName(userName);

            if (user != null)
            {
                newCrash.UserNameId = user.Id;
            }
            else
            {
                newCrash.User = new User()
                {
                    UserName = description.UserName, UserGroupId = 5
                };
            }

            //If there's a valid EpicAccountId assign that.
            if (!string.IsNullOrEmpty(description.EpicAccountId))
            {
                newCrash.EpicAccountId = description.EpicAccountId;
            }

            newCrash.Description = "";
            if (description.UserDescription != null)
            {
                newCrash.Description = string.Join(Environment.NewLine, description.UserDescription);
            }

            newCrash.EngineMode   = description.EngineMode;
            newCrash.GameName     = description.GameName;
            newCrash.LanguageExt  = description.Language;    //Converted by the crash process.
            newCrash.PlatformName = description.Platform;

            if (description.ErrorMessage != null)
            {
                newCrash.Summary = string.Join("\n", description.ErrorMessage);
            }

            if (description.CallStack != null)
            {
                newCrash.RawCallStack = string.Join("\n", description.CallStack);
            }

            if (description.SourceContext != null)
            {
                newCrash.SourceContext = string.Join("\n", description.SourceContext);
            }

            newCrash.TimeOfCrash = description.TimeofCrash;
            newCrash.Processed   = description.bAllowToBeContacted;

            newCrash.Jira            = "";
            newCrash.FixedChangeList = "";
            newCrash.ProcessFailed   = description.bProcessorFailed;

            // Set the crash type
            newCrash.CrashType = 1;

            //if we have a crash type set the crash type
            if (!string.IsNullOrEmpty(description.CrashType))
            {
                switch (description.CrashType.ToLower())
                {
                case "crash":
                    newCrash.CrashType = 1;
                    break;

                case "assert":
                    newCrash.CrashType = 2;
                    break;

                case "ensure":
                    newCrash.CrashType = 3;
                    break;

                case "":
                case null:
                default:
                    newCrash.CrashType = 1;
                    break;
                }
            }
            else     //else fall back to the old behavior and try to determine type from RawCallStack
            {
                if (newCrash.RawCallStack != null)
                {
                    if (newCrash.RawCallStack.Contains("FDebug::AssertFailed"))
                    {
                        newCrash.CrashType = 2;
                    }
                    else if (newCrash.RawCallStack.Contains("FDebug::Ensure"))
                    {
                        newCrash.CrashType = 3;
                    }
                    else if (newCrash.RawCallStack.Contains("FDebug::OptionallyLogFormattedEnsureMessageReturningFalse"))
                    {
                        newCrash.CrashType = 3;
                    }
                    else if (newCrash.RawCallStack.Contains("NewReportEnsure"))
                    {
                        newCrash.CrashType = 3;
                    }
                }
            }

            // As we're adding it, the status is always new
            newCrash.Status = "New";

            /*
             * Unused Crashes' fields.
             *
             * Title				nchar(20)
             * Selected			bit
             * Version				int
             * AutoReporterID		int
             * Processed			bit	-> renamed to AllowToBeContacted
             * HasDiagnosticsFile	bit	always true
             * HasNewLogFile		bit
             * HasMetaData			bit	always true
             */

            // Set the unused fields to the default values.
            //NewCrash.Title = "";			removed from dbml
            //NewCrash.Selected = false;	removed from dbml
            //NewCrash.Version = 4;			removed from dbml
            //NewCrash.AutoReporterID = 0;	removed from dbml
            //NewCrash.HasNewLogFile = false;removed from dbml

            //NewCrash.HasDiagnosticsFile = true;
            //NewCrash.HasMetaData = true;
            newCrash.UserActivityHint = description.UserActivityHint;

            BuildPattern(newCrash);

            if (newCrash.CommandLine == null)
            {
                newCrash.CommandLine = "";
            }

            _unitOfWork.Dispose();
            _unitOfWork = new UnitOfWork(new CrashReportEntities());
            var callStackRepository = _unitOfWork.CallstackRepository;

            try
            {
                var crashRepo = _unitOfWork.CrashRepository;
                //if we don't have any callstack data then insert the crash and return
                if (string.IsNullOrEmpty(newCrash.Pattern))
                {
                    crashRepo.Save(newCrash);
                    _unitOfWork.Save();
                    return(newCrash);
                }

                //If this isn't a new pattern then link it to our crash data model
                if (callStackRepository.Any(data => data.Pattern == newCrash.Pattern))
                {
                    var callstackPattern = callStackRepository.First(data => data.Pattern == newCrash.Pattern);
                    newCrash.PatternId = callstackPattern.id;
                }
                else
                {
                    //if this is a new callstack pattern then insert into data model and create a new bugg.
                    var callstackPattern = new CallStackPattern {
                        Pattern = newCrash.Pattern
                    };
                    callStackRepository.Save(callstackPattern);
                    _unitOfWork.Save();
                    newCrash.PatternId = callstackPattern.id;
                }

                //Mask out the line number and File path from our error message.
                var errorMessageString = description.ErrorMessage != null?String.Join("", description.ErrorMessage) : "";

                //Create our masking regular expressions
                var fileRegex = new Regex(@"(\[File:).*?(])"); //Match the filename out the file name
                var lineRegex = new Regex(@"(\[Line:).*?(])"); //Match the line no.

                /**
                 * Regex to match ints of two characters or longer
                 * First term ((?<=\s)|(-)) : Positive look behind, match if preceeded by whitespace or if first character is '-'
                 * Second term (\d{3,}) match three or more decimal chracters in a row.
                 * Third term (?=(\s|$)) positive look ahead match if followed by whitespace or end of line/file.
                 */
                var intRegex = new Regex(@"-?\d{3,}");

                /**
                 * Regular expression for masking out floats
                 */
                var floatRegex = new Regex(@"-?\d+\.\d+");

                /**
                 * Regular expression for masking out hexadecimal numbers
                 */
                var hexRegex = new Regex(@"0x[\da-fA-F]+");

                //mask out terms matches by our regex's
                var trimmedError = fileRegex.Replace(errorMessageString, "");
                trimmedError = lineRegex.Replace(trimmedError, "");
                trimmedError = floatRegex.Replace(trimmedError, "");
                trimmedError = hexRegex.Replace(trimmedError, "");
                trimmedError = intRegex.Replace(trimmedError, "");

                //Check to see if the masked error message is unique
                ErrorMessage errorMessage = null;
                if (_unitOfWork.ErrorMessageRepository.Any(data => data.Message.Contains(trimmedError)))
                {
                    errorMessage =
                        _unitOfWork.ErrorMessageRepository.First(data => data.Message.Contains(trimmedError));
                }
                else
                {
                    //if it's a new message then add it to the database.
                    errorMessage = new ErrorMessage()
                    {
                        Message = trimmedError
                    };
                    _unitOfWork.ErrorMessageRepository.Save(errorMessage);
                    _unitOfWork.Save();
                }

                //Check for an existing bugg with this pattern and error message / no error message
                if (_unitOfWork.BuggRepository.Any(data =>
                                                   (data.PatternId == newCrash.PatternId || data.Pattern == newCrash.Pattern) && (newCrash.CrashType == 3 ||
                                                                                                                                  (data.ErrorMessageId == errorMessage.Id || data.ErrorMessageId == null))))
                {
                    //if a bugg exists for this pattern update the bugg data
                    var bugg = _unitOfWork.BuggRepository.First(data => data.PatternId == newCrash.PatternId) ??
                               _unitOfWork.BuggRepository.First(data => data.Pattern == newCrash.Pattern);
                    bugg.PatternId = newCrash.PatternId;

                    if (newCrash.CrashType != 3)
                    {
                        bugg.CrashType      = newCrash.CrashType;
                        bugg.ErrorMessageId = errorMessage.Id;
                    }

                    //also update the bugg data while we're here
                    bugg.TimeOfLastCrash = newCrash.TimeOfCrash;

                    if (String.Compare(newCrash.BuildVersion, bugg.BuildVersion,
                                       StringComparison.Ordinal) != 1)
                    {
                        bugg.BuildVersion = newCrash.BuildVersion;
                    }

                    _unitOfWork.Save();

                    //if a bugg exists update this crash from the bugg
                    //buggs are authoritative in this case
                    newCrash.Buggs.Add(bugg);
                    newCrash.Jira            = bugg.TTPID;
                    newCrash.FixedChangeList = bugg.FixedChangeList;
                    newCrash.Status          = bugg.Status;
                    _unitOfWork.CrashRepository.Save(newCrash);
                    _unitOfWork.Save();
                }
                else
                {
                    //if there's no bugg for this pattern create a new bugg and insert into the data store.
                    var bugg = new Bugg();
                    bugg.TimeOfFirstCrash       = newCrash.TimeOfCrash;
                    bugg.TimeOfLastCrash        = newCrash.TimeOfCrash;
                    bugg.TTPID                  = newCrash.Jira;
                    bugg.Pattern                = newCrash.Pattern;
                    bugg.PatternId              = newCrash.PatternId;
                    bugg.NumberOfCrashes        = 1;
                    bugg.NumberOfUsers          = 1;
                    bugg.NumberOfUniqueMachines = 1;
                    bugg.BuildVersion           = newCrash.BuildVersion;
                    bugg.CrashType              = newCrash.CrashType;
                    bugg.Status                 = newCrash.Status;
                    bugg.FixedChangeList        = newCrash.FixedChangeList;
                    bugg.ErrorMessageId         = errorMessage.Id;
                    newCrash.Buggs.Add(bugg);
                    _unitOfWork.BuggRepository.Save(bugg);
                    _unitOfWork.CrashRepository.Save(newCrash);
                    _unitOfWork.Save();
                }
            }
            catch (DbEntityValidationException dbentEx)
            {
                var messageBuilder = new StringBuilder();
                messageBuilder.AppendLine("Db Entity Validation Exception Exception was:");
                messageBuilder.AppendLine(dbentEx.ToString());

                var innerEx = dbentEx.InnerException;
                while (innerEx != null)
                {
                    messageBuilder.AppendLine("Inner Exception : " + innerEx.Message);
                    innerEx = innerEx.InnerException;
                }

                if (dbentEx.EntityValidationErrors != null)
                {
                    messageBuilder.AppendLine("Validation Errors : ");
                    foreach (var valErr in dbentEx.EntityValidationErrors)
                    {
                        messageBuilder.AppendLine(valErr.ValidationErrors.Select(data => data.ErrorMessage).Aggregate((current, next) => current + "; /n" + next));
                    }
                }
                FLogger.Global.WriteException(messageBuilder.ToString());
            }
            catch (Exception ex)
            {
                var messageBuilder = new StringBuilder();
                messageBuilder.AppendLine("Create Crash Exception : ");
                messageBuilder.AppendLine(ex.Message.ToString());
                var innerEx = ex.InnerException;
                while (innerEx != null)
                {
                    messageBuilder.AppendLine("Inner Exception : " + innerEx.Message);
                    innerEx = innerEx.InnerException;
                }

                FLogger.Global.WriteException("Create Crash Exception : " +
                                              messageBuilder.ToString());
                _slackWriter.Write("Create Crash Exception : " + messageBuilder.ToString());
                throw;
            }

            return(newCrash);
        }
 /// <summary>
 /// Remove a CallStackPattern from the data store
 /// </summary>
 /// <param name="entity"></param>
 public void Delete(CallStackPattern entity)
 {
     _entityContext.CallStackPatterns.Remove(entity);
 }
 /// <summary>
 /// Add a new CallStackPattern to the data store
 /// </summary>
 /// <param name="entity"></param>
 public void Save(CallStackPattern entity)
 {
     _entityContext.CallStackPatterns.Add(entity);
 }
        /// <summary>
        /// Update an existing CallStackPattern
        /// </summary>
        /// <param name="entity"></param>
        public void Update(CallStackPattern entity)
        {
            var set = _entityContext.Set<CallStackPattern>();
            var entry = set.Local.SingleOrDefault(f => f.id == entity.id);

            if (entry != null)
            {
                var attachedFeature = _entityContext.Entry(entry);
                attachedFeature.CurrentValues.SetValues(entity);
                attachedFeature.State = EntityState.Modified;
            }
            else
            {
                _entityContext.CallStackPatterns.Attach(entity);
                _entityContext.Entry(entity).State = EntityState.Modified;
            }
        }
 /// <summary>
 /// Remove a CallStackPattern from the data store
 /// </summary>
 /// <param name="entity"></param>
 public void Delete(CallStackPattern entity)
 {
     _entityContext.CallStackPatterns.Remove(entity);
 }
 /// <summary>
 /// Add a new CallStackPattern to the data store
 /// </summary>
 /// <param name="entity"></param>
 public void Save(CallStackPattern entity)
 {
     _entityContext.CallStackPatterns.Add(entity);
 }