public static void PopulateDataFlags(this Regulation reg)
        {
            if (reg.Children != null && reg.Children.Where(e => Constants.RegTypes.Contains(e.Type) || e.Type == "Body").Count() > 0)
            {
                ParseDataFlags(reg);

                foreach (var child in reg.Children)
                {
                    PopulateDataFlags(child);
                }
            }
            else
            {
                ParseDataFlags(reg);
            }

            reg.DataFlags = reg.DataFlags.Distinct().ToList();
        }
        public static qm_rclegislation ConvertToTCModel(this Regulation justiceModel)
        {
            var newTcModel = new qm_rclegislation();

            newTcModel.qm_LegislationLbl        = justiceModel.Label;
            newTcModel.qm_rcParentLegislationId = new EntityReference("qm_rclegislation", justiceModel.CrmId);
            newTcModel.qm_InforceDte            = justiceModel.InforceStartDate.ToDateTime("yyyy-M-d");        //TODO: ensure format is consistent on read
            newTcModel.qm_LastAmendedDte        = justiceModel.LastAmendedDate.ToDateTime("yyyy-M-d");
            newTcModel.qm_JusticeId             = justiceModel.JusticeId.ToInt();
            newTcModel.qm_LegislationEtxt       = justiceModel.TextEnglish;
            newTcModel.qm_LegislationFtxt       = justiceModel.TextFrench;
            //newTcModel.qm_TYLegislationSourceCd      = justiceModel.Type;
            newTcModel.qm_OrderNbr   = justiceModel.Order;
            newTcModel.qm_JusticeFId = justiceModel.JusticeFId.ToInt();
            //newTcModel.qm_TYLegislationSectionTypeCd = justiceModel.RegulationType;
            newTcModel.qm_HistoricalNoteEtxt     = justiceModel.HistoricalNote;
            newTcModel.qm_HistoricalNoteFtxt     = justiceModel.HistoricalNote;
            newTcModel.qm_AdditionalMetadataEtxt = justiceModel.AdditionalMetadata;
            newTcModel.qm_AdditionalMetadataFtxt = justiceModel.AdditionalMetadata;
            newTcModel.qm_rcParentLegislationId  = justiceModel.ParentCrmId == null || !justiceModel.ParentCrmId.HasValue ? null : new EntityReference("qm_rclegislation", justiceModel.ParentCrmId.Value);
            newTcModel.qm_name = justiceModel.UniqueId;

            return(newTcModel);
        }
        private static void ParseDataFlags(this Regulation reg)
        {
            if (!string.IsNullOrEmpty(reg?.TextEnglish))
            {
                //TODO French
                var text = reg.TextEnglish;

                //must be utf-8
                byte[] bytes = Encoding.Default.GetBytes(text);
                text = Encoding.UTF8.GetString(bytes);

                text = Regex.Replace(text, "[^ -~]", ""); //strip non ascii char
                text = text.ToUpper();


                foreach (var flag in Constants.DataFlags_Keywords)
                {
                    if (Regex.IsMatch(text, WildCardToRegular($"*{flag}*")))
                    {
                        reg.DataFlags.Add(new KeyValuePair <string, object>(flag, true));
                    }

                    //UN NUMBERS
                    if (Regex.IsMatch(text, @"^.*UN\d{4}.*$"))
                    {
                        var matches      = Regex.Matches(text, @"UN\d{4}");
                        var matchesArray = new List <string>();

                        foreach (Match match in matches)
                        {
                            matchesArray.Add(match.Value); // match csv values outside commas
                            reg.UnNumbers.Add(match.Value);
                        }

                        var matchesCsv = string.Join(",", matchesArray);

                        reg.DataFlags.Add(new KeyValuePair <string, object>(Constants.DataFlags_HasUNNumber, true));
                        reg.DataFlags.Add(new KeyValuePair <string, object>(Constants.DataFlags_UNNumbers, matchesCsv));
                    }

                    //CLASS
                    if (Regex.IsMatch(text, @"^.*CLASS\s*\d\.*\d*.*$"))
                    {
                        var matches      = Regex.Matches(text, @"CLASS\s*\d\.*\d*");
                        var matchesArray = new List <string>();

                        foreach (Match match in matches)
                        {
                            char[] charsToTrim = { ',', '.', ' ' };
                            var    trimmed     = match.Value.TrimEnd(charsToTrim).TrimStart(charsToTrim);

                            matchesArray.Add(trimmed); // match csv values outside commas
                            reg.Classes.Add(trimmed);
                        }

                        var matchesCsv = string.Join(",", matchesArray);

                        reg.DataFlags.Add(new KeyValuePair <string, object>(Constants.DataFlags_HasClasses, true));
                        reg.DataFlags.Add(new KeyValuePair <string, object>(Constants.DataFlags_Classes, matchesCsv));
                    }
                }
            }
        }