public static StaticTextFieldDependency GetStaticTextDependencies(List <SheetField> listSheetFields)
        {
            StaticTextFieldDependency staticTextDependencies = StaticTextFieldDependency.None;

            foreach (SheetField sheetField in listSheetFields.FindAll(x => x.FieldType == SheetFieldType.StaticText))
            {
                Regex staticTextPattern = new Regex(@"(?<=)\[.+?\](?=)");              //Matches on static text field replacement strings. e.g. [PatLName]
                var   matches           = staticTextPattern.Matches(sheetField.FieldValue);
                foreach (Match match in matches)
                {
                    string strMatch = match.ToString().Replace("[", "").Replace("]", "");                //Pull out just the replacement string.
                    if (!Enum.TryParse(strMatch, out StaticTextField enumStaticText))
                    {
                        continue;
                    }
                    if (_dictDependencies.TryGetValue(enumStaticText, out StaticTextFieldDependency sheetFieldDependencies))
                    {
                        staticTextDependencies |= sheetFieldDependencies;
                    }
                    else if (ODBuild.IsDebug())
                    {
                        //Programmer must add [staticTextFieldValue] to the dictionary linking these text fields to the data in StaticTextData.
                        //Only throwing in Debug in case a customer has some other [notAnActualStaticTextField] type string entered in their sheet.
                        //We don't want to crash Sheets for an otherwise perfectly valid Sheet, this exception is intended to force a programmer to implement
                        //the proper StaticTextFieldDependency for a new StaticTextField.
                        throw new NotImplementedException("StaticTextFieldDependencies not implemented for " + match.Value + ".");
                    }
                }
            }
            return(staticTextDependencies);
        }
 ///<summary>Gets the data necessary for Static Text Field string replacements.  Returns a StaticTextData object so that
 ///references are preserved across Middle Tier.</summary>
 public static StaticTextData GetStaticTextData(StaticTextFieldDependency staticTextDependencies, Patient pat, Family fam
                                                , List <long> listProcCodeNums, StaticTextData data = null)
 {
     if (RemotingClient.RemotingRole == RemotingRole.ClientWeb)
     {
         return(Meth.GetObject <StaticTextData>(MethodBase.GetCurrentMethod(), staticTextDependencies, pat, fam, listProcCodeNums, data));
     }
     data = data ?? new StaticTextData();
     data.LoadData(staticTextDependencies, pat, fam, listProcCodeNums);
     return(data);
 }
        ///<summary>Runs the required queries to populate the necessary StaticTextData fields corresponding to staticTextDependencies.</summary>
        private void LoadData(StaticTextFieldDependency staticTextDependencies, Patient pat, Family fam, List <long> listProcCodeNums)
        {
            bool isMiddleTier = (RemotingClient.RemotingRole == RemotingRole.ServerWeb);

            System.Diagnostics.Stopwatch timer = null;
            if (ODBuild.IsDebug())
            {
                timer = new System.Diagnostics.Stopwatch();
                timer.Start();
            }
            if (staticTextDependencies.HasFlag(StaticTextFieldDependency.Pat))
            {
                //patient should already be loaded.
            }
            if (fam == null && staticTextDependencies.HasFlag(StaticTextFieldDependency.Fam))
            {
                fam = Patients.GetFamily(pat.PatNum);
            }
            if (PatNote == null)
            {
                if (staticTextDependencies.HasFlag(StaticTextFieldDependency.PatNote))
                {
                    PatNote = PatientNotes.Refresh(pat.PatNum, pat.Guarantor);
                }
                else
                {
                    PatNote = new PatientNote();
                }
            }
            bool IsQueryNeeded <T>(ref List <T> list, StaticTextFieldDependency dependency)
            {
                if (list == null || (isMiddleTier && list.Count == 0))             //Middle Tier deserializes null lists to empty lists.
                {
                    if (staticTextDependencies.HasFlag(dependency))
                    {
                        return(true);
                    }
                    else
                    {
                        list = new List <T>();
                    }
                }
                return(false);
            }

            if (IsQueryNeeded(ref ListRefAttaches, StaticTextFieldDependency.ListRefAttaches))
            {
                ListRefAttaches = RefAttaches.Refresh(pat.PatNum);
            }
            if (IsQueryNeeded(ref ListInsSubs, StaticTextFieldDependency.ListInsSubs))
            {
                ListInsSubs = InsSubs.RefreshForFam(fam);
            }
            if (IsQueryNeeded(ref ListInsPlans, StaticTextFieldDependency.ListInsPlans))
            {
                ListInsPlans = InsPlans.RefreshForSubList(ListInsSubs);
            }
            if (IsQueryNeeded(ref ListPatPlans, StaticTextFieldDependency.ListPatPlans))
            {
                ListPatPlans = PatPlans.Refresh(pat.PatNum);
            }
            if (IsQueryNeeded(ref ListBenefits, StaticTextFieldDependency.ListBenefits))
            {
                ListBenefits = Benefits.Refresh(ListPatPlans, ListInsSubs);
            }
            if (IsQueryNeeded(ref HistList, StaticTextFieldDependency.HistList))
            {
                HistList = ClaimProcs.GetHistList(pat.PatNum, ListBenefits, ListPatPlans, ListInsPlans, DateTime.Today, ListInsSubs);
            }
            if (IsQueryNeeded(ref ListTreatPlans, StaticTextFieldDependency.ListTreatPlans))
            {
                ListTreatPlans = TreatPlans.Refresh(pat.PatNum);
            }
            if (IsQueryNeeded(ref ListRecallsForFam, StaticTextFieldDependency.ListRecallsForFam))
            {
                ListRecallsForFam = Recalls.GetList(fam.ListPats.Select(x => x.PatNum).ToList());
            }
            if (IsQueryNeeded(ref ListAppts, StaticTextFieldDependency.ListAppts))
            {
                ListAppts = Appointments.GetListForPat(pat.PatNum);
            }
            if (IsQueryNeeded(ref ListFutureApptsForFam, StaticTextFieldDependency.ListFutureApptsForFam))
            {
                ListFutureApptsForFam = Appointments.GetFutureSchedApts(fam.ListPats.Select(x => x.PatNum).ToList());
            }
            if (IsQueryNeeded(ref ListDiseases, StaticTextFieldDependency.ListDiseases))
            {
                ListDiseases = Diseases.Refresh(pat.PatNum, true);
            }
            if (IsQueryNeeded(ref ListAllergies, StaticTextFieldDependency.ListAllergies))
            {
                ListAllergies = Allergies.GetAll(pat.PatNum, false);
            }
            if (IsQueryNeeded(ref ListMedicationPats, StaticTextFieldDependency.ListMedicationPats))
            {
                ListMedicationPats = MedicationPats.Refresh(pat.PatNum, false);
            }
            if (IsQueryNeeded(ref ListFamPopups, StaticTextFieldDependency.ListFamPopups))
            {
                ListFamPopups = Popups.GetForFamily(pat);
            }
            if (IsQueryNeeded(ref ListProceduresSome, StaticTextFieldDependency.ListProceduresSome))
            {
                ListProceduresSome = Procedures.RefreshForProcCodeNums(pat.PatNum, listProcCodeNums);
            }
            if (IsQueryNeeded(ref ListProceduresPat, StaticTextFieldDependency.ListProceduresPat))
            {
                ListProceduresPat = Procedures.Refresh(pat.PatNum);
            }
            if (IsQueryNeeded(ref ListPlannedAppts, StaticTextFieldDependency.ListPlannedAppts))
            {
                ListPlannedAppts = new List <PlannedAppt>();
                PlannedAppt plannedAppt = PlannedAppts.GetOneOrderedByItemOrder(pat.PatNum);
                if (plannedAppt != null)
                {
                    ListPlannedAppts.Add(plannedAppt);
                }
            }
            if (ODBuild.IsDebug())
            {
                timer.Stop();
                Console.WriteLine("Static text field query time (ms): " + timer.ElapsedMilliseconds);
            }
        }