public ValueViewComponent(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
        }
        public SeriesController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            IEnumerable<int> validIds = (from d in _context.tblDetail
                                         where d.Releasable.HasValue && d.Releasable.Value == true
                                         select d.Follow_Up.Study.Program_ID)
                                        .ToList();
            _series = from p in _context.tblProgram
                      where validIds.Contains(p.Program_ID) && this.IsStringValid(p.Program_Name)
                      select new SeriesViewModel()
                      {
                          Id = p.Program_ID,
                          Name = p.Program_Name,
                          Symbol = this.StringFormatter(p.Program_Abbreviation),
                          OldName1 = this.StringFormatter(p.Program_Old_Name_1),
                          OldName1Symbol = this.StringFormatter(p.Program_Old_Name_1_Abbr),
                          OldName1Duration = this.StringFormatter(p.Program_Old_Name_1_Duration),
                          OldName2 = this.StringFormatter(p.Program_Old_Name_2),
                          OldName2Symbol = this.StringFormatter(p.Program_Old_Name_2_Abbr),
                          OldName2Duration = this.StringFormatter(p.Program_Old_Name_2_Duration),
                          ParentOrganization = "U.S. Department of Education" +
                              (this.IsStringValid(p.Division.Unit.POC.Principal_Office_Name) ? $", {p.Division.Unit.POC.Principal_Office_Name}" : string.Empty +
                              (this.IsStringValid(p.Division.Unit.Unit_Name) ? $", {p.Division.Unit.Unit_Name}" : string.Empty +
                              (this.IsStringValid(p.Division.Division_Name) ? $", {p.Division.Division_Name}" : string.Empty))),
                          Description = this.StringFormatter(p.Program_Description)
                      };
            _studyStubGenerator = id =>
                from d in _context.tblDetail
                where d.Releasable.HasValue && d.Releasable.Value == true
                let fu = d.Follow_Up
                let s = fu.Study
                where s.Program_ID == id && (this.IsStringValid(s.Study_Name) || this.IsStringValid(fu.Follow_Up_Name))
                select new StudyStubViewModel()
                {
                    Id = d.Detail_ID,
                    Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                    Parent = new SeriesStubViewModel()
                    {
                        Id = s.Program_ID,
                        Name = this.StringFormatter(s.Program.Program_Name)
                    }
                };
        }
        public InventoryListController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            _collectionStubs = from d in _context.tblDetail
                               where d.Releasable.HasValue && d.Releasable.Value == true
                               let fu = d.Follow_Up
                               let s = fu.Study
                               where this.IsStringValid(s.Study_Name) || this.IsStringValid(fu.Follow_Up_Name)
                               select new StudyStubViewModel()
                               {
                                   Id = d.Detail_ID,
                                   Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                                   Parent = new SeriesStubViewModel()
                                   {
                                       Id = s.Program_ID,
                                       Name = this.StringFormatter(s.Program.Program_Name)
                                   }
                               };
        }
        public CollectionController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            _collections = from d in _context.tblDetail
                           where d.Releasable.HasValue && d.Releasable.Value == true
                           join c in _context.tblCollection on d.Detail_ID equals c.Detail_ID.Value
                           where this.IsStringValid(c.Collection_Name)
                           let fu = d.Follow_Up
                           let s = fu.Study
                           where this.IsStringValid(s.Study_Name) || this.IsStringValid(fu.Follow_Up_Name)
                           select new CollectionViewModel()
                           {
                               Id = c.Collection_ID,
                               Name = this.StringFormatter(c.Collection_Name),
                               Parent = new StudyStubViewModel()
                               {
                                   Id = d.Detail_ID,
                                   Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                                   Parent = new SeriesStubViewModel()
                                   {
                                       Id = s.Program_ID,
                                       Name = this.StringFormatter(s.Program.Program_Name)
                                   }
                               },
                               CollectionType = this.StringFormatter(c.Collection_Type),
                               Cost = this.IntegerFormatter(c.Cost),
                               CostYears = this.StringFormatter(c.Collection_Cost_Years),
                               RecruitmentStartDateEstimated = this.DateTimeFormatter(c.Recruitment_Start_Date),
                               CollectionStartDateEstimated = this.DateTimeFormatter(c.Collection_Start_Date__Planned_),
                               CollectionStartDate = this.DateTimeFormatter(c.Collection_Start_Date__Actual_),
                               CollectionEndDateEstimated = this.DateTimeFormatter(c.Collection_End_Date__Planned_),
                               CollectionEndDate = this.DateTimeFormatter(c.Collection_End_Date__Actual_),
                               DateDescription = this.StringFormatter(c.Date_Detail),
                               DataCollectionAgentType = this.StringFormatter(c.Collector_Type),
                               DataCollectionAgentPrimary = this.StringFormatter(c.Collectors_Names),
                               DataCollectionAgentNonPrimary = this.StringFormatter(c.Subcollectors_Names),
                               ConfidentialityLaw = this.StringFormatter(c.Relevant_Conf_Law),
                               VoluntaryConfidentialStatement = this.StringFormatter(c.Confidentiality_Language),
                               VoluntaryConfidentialStatementRespondent = this.StringFormatter(c.VC_Language_A10),
                               ExperimentDescription = this.StringFormatter(c.Experiments_Description),
                               ExperimentResults = this.StringFormatter(c.Experiment_Results)
                           };
            _respondentStubGenerator = id =>
                from r in _context.tblRespondent
                let c = r.Collection
                join d in _context.tblDetail on c.Detail_ID.Value equals d.Detail_ID
                let fu = d.Follow_Up
                let s = fu.Study
                where d.Releasable.HasValue && d.Releasable.Value == true && r.Collection_ID.HasValue && r.Collection_ID.Value == id && this.IsStringValid(r.Respondent_Type)
                select new RespondentStubViewModel()
                {
                    Id = r.Respondent_ID,
                    Description = this.StringFormatter(r.Respondent_Type) + (this.IsStringValid(r.Respondent_Type_Detail) ? " – " + this.StringFormatter(r.Respondent_Type_Detail) : string.Empty),
                    Parent = new CollectionStubViewModel()
                    {
                        Id = r.Collection_ID.Value,
                        Name = this.StringFormatter(r.Collection.Collection_Name),
                        Parent = new StudyStubViewModel()
                        {
                            Id = r.Collection.Detail_ID.Value,
                            Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                            Parent = new SeriesStubViewModel()
                            {
                                Id = s.Program_ID,
                                Name = this.StringFormatter(s.Program.Program_Name)
                            }
                        }
                    }
                };
            _packageStubGenerator = id =>
            {
                IQueryable<PackageStubViewModel> ret =
                    from l in _context.tblLink
                    where l.Collection_ID == id
                    from p in _context.tblPackage
                    where p.Package_ID == l.Package_ID && (this.IsStringValid(p.ICR_ID_Main) || this.IsStringValid(p.EDICS__) || this.IsStringValid(p.ICR_Ref_No) || (this.IsStringValid(p.Office_OMB__) && this.IsStringValid(p.Study_OMB__)))
                    select new PackageStubViewModel()
                    {
                        Id = p.Package_ID,
                        ReferenceNumber = this.IsStringValid(p.ICR_ID_Main) && this.IsStringValid(p.ICR_ID_Version) ? $"ICRAS {p.ICR_ID_Main}.{p.ICR_ID_Version}"
                            : (this.IsStringValid(p.EDICS__) ? $"EDICS {p.EDICS__}"
                            : (this.IsStringValid(p.ICR_Ref_No) ? p.ICR_Ref_No
                            : (this.IsStringValid(p.Office_OMB__) && this.IsStringValid(p.Study_OMB__) ? $"{p.Office_OMB__}-{p.Study_OMB__} v." + (this.IsStringValid(p.Version_OMB__) ? p.Version_OMB__ : "NEW")
                            : string.Empty)))
                    };
                if (ret.Any())
                {
                    return ret.Select(item =>
                        new PackageStubViewModel()
                        {
                            Id = item.Id,
                            ReferenceNumber = item.ReferenceNumber,
                            Collections = from l in _context.tblLink
                                          where l.Package_ID == item.Id
                                          join d in _context.tblDetail on l.Collection.Detail_ID.Value equals d.Detail_ID
                                          let fu = d.Follow_Up
                                          let s = fu.Study
                                          select new CollectionStubViewModel()
                                          {
                                              Id = l.Collection_ID,
                                              Name = this.StringFormatter(l.Collection.Collection_Name),
                                              Parent = new StudyStubViewModel()
                                              {
                                                  Id = d.Detail_ID,
                                                  Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                                                  Parent = new SeriesStubViewModel()
                                                  {
                                                      Id = s.Program_ID,
                                                      Name = this.StringFormatter(s.Program.Program_Name)
                                                  }
                                              }
                                          }
                        });
                }
                else
                {
                    return ret;
                }
            };
        }
        public FileController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            _files = from d in _context.tblDetail
                     where d.Releasable.HasValue && d.Releasable.Value == true
                     join sd in _context.tblSDLink_ on d.Follow_Up.Study_ID equals sd.Study_ID
                     join t in _context.tblDataset_ on sd.Dataset_ID equals t.Dataset_ID
                     join df in _context.tblDFLink_ on sd.SD_Link_ID equals df.SD_Link_ID
                     join f in _context.tblFile_ on df.File_ID equals f.File_ID
                     where this.IsStringValid(f.File_Name)
                     select new FileViewModel()
                     {
                         Id = df.DF_Link_ID,
                         Name = this.StringFormatter(f.File_Name),
                         Format = this.StringFormatter(f.File_Format),
                         Dataset = this.StringFormatter(t.Dataset_Title),
                         Restriction = !t.Dataset_Restriction.HasValue || t.Dataset_Restriction.Value == 2 ? "Private"
                             : (t.Dataset_Restriction.Value == 1 ? "Restricted Public"
                             : "Public"),
                         Location = this.StringFormatter(t.Dataset_Location),
                         LocationDescription = this.StringFormatter(t.Dataset_Location_Detail)
                     };
            _studyStubGenerator = id =>
                from df in _context.tblDFLink_
                where df.DF_Link_ID == id
                join sd in _context.tblSDLink_ on df.SD_Link_ID equals sd.SD_Link_ID
                join d in _context.tblDetail on sd.Study_ID equals d.Follow_Up.Study_ID
                where d.Releasable.HasValue && d.Releasable.Value == true
                let fu = d.Follow_Up
                let s = fu.Study
                where this.IsStringValid(s.Study_Name) || this.IsStringValid(fu.Follow_Up_Name)
                select new StudyStubViewModel()
                {
                    Id = d.Detail_ID,
                    Name = this.IsStringValid(fu.Follow_Up_Name) ? fu.Follow_Up_Name : this.StringFormatter(s.Study_Name)
                };
            _elementGenerator = id =>
            {
                Predicate<string> isElementStringValid = str => this.IsStringValid(str) && str != "<null>";
                return from df in _context.tblDFLink_
                       where df.DF_Link_ID == id
                       join fe in _context.tblFELink_ on df.DF_Link_ID equals fe.DF_Link_ID
                       join e in _context.tblElement_ on fe.Element_ID equals e.Element_ID
                       where isElementStringValid(e.Element_Name) || isElementStringValid(e.Element_Label)
                       select new ElementViewModel()
                       {
                           Id = fe.FE_Link_ID,
                           Name = isElementStringValid(e.Element_Name) ? this.StringFormatter(e.Element_Name) : string.Empty,
                           Type = isElementStringValid(e.Element_Type) ? this.StringFormatter(e.Element_Type) : string.Empty,
                           Label = isElementStringValid(e.Element_Label) ? this.StringFormatter(e.Element_Label) : string.Empty,
                           LabelExtended = isElementStringValid(e.Element_Extended_Definition) ? this.StringFormatter(e.Element_Extended_Definition) : string.Empty,
                           Question = isElementStringValid(e.Element_Question) ? this.StringFormatter(e.Element_Question) : string.Empty,
                           Values = Enumerable.Empty<ValueViewModel>()
                       };
            };
        }
        public RespondentController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            var validActivities = from d in _context.tblDetail
                                  where d.Releasable.HasValue && d.Releasable.Value == true
                                  join c in _context.tblCollection on d.Detail_ID equals c.Detail_ID.Value
                                  where this.IsStringValid(c.Collection_Name)
                                  select new
                                  {
                                      Id = c.Collection_ID,
                                      Name = c.Collection_Name
                                  };
            _respondents = from r in _context.tblRespondent
                           where this.IsStringValid(r.Respondent_Type) && r.Collection_ID.HasValue && validActivities.ToList().Any(va => va.Id == r.Collection_ID.Value)
                           join d in _context.tblDetail on r.Collection.Detail_ID.Value equals d.Detail_ID
                           let fu = d.Follow_Up
                           let s = fu.Study
                           select new RespondentViewModel()
                           {
                               Id = r.Respondent_ID,
                               Parent = new CollectionStubViewModel()
                               {
                                   Id = r.Collection_ID.Value,
                                   Name = this.StringFormatter(validActivities.FirstOrDefault(va => va.Id == r.Collection_ID.Value).Name),
                                   Parent = new StudyStubViewModel()
                                   {
                                       Id = r.Collection.Detail_ID.Value,
                                       Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                                       Parent = new SeriesStubViewModel()
                                       {
                                           Id = s.Program_ID,
                                           Name = this.StringFormatter(s.Program.Program_Name)
                                       }
                                   }
                               },
                               Type = this.StringFormatter(r.Respondent_Type),
                               Description = this.StringFormatter(r.Respondent_Type_Detail),
                               Keywords = this.StringFormatter(r.Topics),
                               ResponseVoluntary = this.BooleanFormatter(r.Voluntary_Response),
                               ResponseMandatory = this.BooleanFormatter(r.Mandatory_Response),
                               ResponseRequiredForBenefits = this.BooleanFormatter(r.Required_for_Benefits_Response),
                               ResponseRequirementDescription = this.StringFormatter(r.Required_Response_Detail),
                               ResponseRequirementReason = this.StringFormatter(r.Response_Requirement_Reason),
                               PopulationSizeEstimated = this.IntegerFormatter(r.Expected_Sample_Size),
                               PopulationSize = this.IntegerFormatter(r.Actual_Sample_Size),
                               PopulationSizeDescription = this.StringFormatter(r.Actual_Sample_Size_Detail),
                               ResponseSizeEstimated = this.IntegerFormatter(r.Respondent_Size_Estimate),
                               ResponseSize = this.IntegerFormatter(r.Respondent_Size_Actual),
                               ResponseRateEstimated = this.DoubleFormatter(r.Response_Rate_Estimated),
                               ResponseRate = this.DoubleFormatter(r.Response_Rate_Actual),
                               ResponseRateDescription = this.StringFormatter(r.Response_Rate_Actual_Detail),
                               Burden = this.IntegerFormatter(r.Burden_Time_Total),
                               BurdenPerRespondent = this.IntegerFormatter(r.Burden_Time_Per_Respondent),
                               BurdenPerRespondentSurvey = this.IntegerFormatter(r.Survey_Burden_Per_Respondent),
                               BurdenPerRespondentAssessment = this.IntegerFormatter(r.Assessment_Burden_Per_Respondent),
                               ConsentExplicit = this.BooleanFormatter(r.Explicit_Consent),
                               ConsentImplicit = this.BooleanFormatter(r.Implicit_Consent),
                               ConsentNotApplicable = this.BooleanFormatter(r.Consent_Not_Applicable) || this.BooleanFormatter(r.Participatory_Consent),
                               ConsentForm = this.StringFormatter(r.Consent_Form),
                               Population = string.Join("; ",
                                   new[] {
                                        r.Respondent_Age_0_2.HasValue && r.Respondent_Age_0_2.Value == true ? "Age 0–2" : string.Empty,
                                        r.Respondent_Age_3_5.HasValue && r.Respondent_Age_3_5.Value == true ? "Age 3–5" : string.Empty,
                                        r.Respondent_Age_6_21.HasValue && r.Respondent_Age_6_21.Value == true ? "Age 6–21" : string.Empty,
                                        r.Respondent_Age_Older_Than_21.HasValue && r.Respondent_Age_Older_Than_21.Value == true ? "Age Older Than 21" : string.Empty,
                                        r.Respondent_Age_NA.HasValue && r.Respondent_Age_NA.Value == true ? "Age Not Applicable" : string.Empty,
                                        r.Respondent_Pre_K.HasValue && r.Respondent_Pre_K.Value == true ? "Pre-Kindergarten" : string.Empty,
                                        r.Respondent_Elementary.HasValue && r.Respondent_Elementary.Value == true ? "Elementary School" : string.Empty,
                                        r.Respondent_Middle.HasValue && r.Respondent_Middle.Value == true ? "Middle School" : string.Empty,
                                        r.Respondent_High_School.HasValue && r.Respondent_High_School.Value == true ? "High School" : string.Empty,
                                        r.Respondent_Postsecondary.HasValue && r.Respondent_Postsecondary.Value == true ? "Postsecondary Education" : string.Empty,
                                        r.Respondent_Graduate.HasValue && r.Respondent_Graduate.Value == true ? "Graduate Education" : string.Empty,
                                        r.Respondent_Continued_Technical_Ed.HasValue && r.Respondent_Continued_Technical_Ed.Value == true ? "Continued/Technical Education" : string.Empty,
                                        r.Respondent_Adult_Education.HasValue && r.Respondent_Adult_Education.Value == true ? "General Adult Population" : string.Empty,
                                        r.Respondent_Education_Level_NA.HasValue && r.Respondent_Education_Level_NA.Value == true ? "Education Level Not Applicable" : string.Empty,
                                        this.StringFormatter(r.Respondent_Other_Population)
                                   }
                                   .Where(str => !string.IsNullOrWhiteSpace(str))),
                               PopulationDescription = this.StringFormatter(r.Respondent_Population_Detail),
                               ResponseType = string.Join("; ",
                                   new[] {
                                        r.Administrative_Records.HasValue && r.Administrative_Records.Value == true ? "Administrative Records" : string.Empty,
                                        r.Address_Update.HasValue && r.Address_Update.Value == true ? "Address Update" : string.Empty,
                                        r.List_Data.HasValue && r.List_Data.Value == true ? "List Data" : string.Empty,
                                        r.Recruitment.HasValue && r.Recruitment.Value == true ? "Recruitment" : string.Empty,
                                        r.Coordination_Help.HasValue && r.Coordination_Help.Value == true ? "Coordination Assistance" : string.Empty,
                                        r.Screener.HasValue && r.Screener.Value == true ? "Screener" : string.Empty,
                                        r.Assessment.HasValue && r.Assessment.Value == true ? "Assessment" : string.Empty,
                                        r.Survey.HasValue && r.Survey.Value == true ? "Survey" : string.Empty,
                                        r.Abbreviated_Survey.HasValue && r.Abbreviated_Survey.Value == true ? "Abbreviated Survey" : string.Empty,
                                        r.Program_Reporting.HasValue && r.Program_Reporting.Value == true ? "Program Reporting" : string.Empty,
                                        r.EDFacts.HasValue && r.EDFacts.Value == true ? "EDFacts" : string.Empty,
                                        r.CPS.HasValue && r.CPS.Value == true ? "Central Processing System (CPS)" : string.Empty,
                                        r.NSLDS.HasValue && r.NSLDS.Value == true ? "National Student Loan Data System (NSLDS)" : string.Empty,
                                        this.StringFormatter(r.Other_Response_Type)
                                   }
                                   .Where(str => !string.IsNullOrWhiteSpace(str))),
                               ResponseTypeDescription = this.StringFormatter(r.Respondent_Type_Detail),
                               ResponseMode = string.Join("; ",
                                   new[] {
                                        r.Paper.HasValue && r.Paper.Value != 0 ? "Paper" : string.Empty,
                                        r.Phone__Not_CATI_.HasValue && r.Phone__Not_CATI_.Value != 0 ? "Telephone" : string.Empty,
                                        r.CATI.HasValue && r.CATI.Value != 0 ? "Computer-assisted telephone interview (CATI)" : string.Empty,
                                        r.Web.HasValue && r.Web.Value != 0 ? "Internet" : string.Empty,
                                        r.Email.HasValue && r.Email.Value != 0 ? "Email" : string.Empty,
                                        r.F2F__Not_CAPI_.HasValue && r.F2F__Not_CAPI_ != 0 ? "Personal interview" : string.Empty,
                                        r.CAPI.HasValue && r.CAPI.Value != 0 ? "Computer-assisted personal interview (CAPI)" : string.Empty,
                                        r.Spreadsheet.HasValue && r.Spreadsheet.Value != 0 ? "Spreadsheet" : string.Empty,
                                        r.PRS.HasValue && r.PRS.Value != 0 ? "Personnel Response System (PRS)" : string.Empty,
                                        this.StringFormatter(r.Other_Collection_Mode)
                                   }
                                   .Where(str => !string.IsNullOrWhiteSpace(str))),
                               ResponseModeDescription = this.StringFormatter(r.Collection_Mode_Detail),
                               AdditionalLanguageInstrument = this.StringFormatter(r.Other_Languages),
                               AdditionalLanguageInterpreter = this.StringFormatter(r.Interpreters),
                               IncentiveCashValue = this.StringFormatter(r.Cash_Range_Amount),
                               IncentiveCashDescription = this.StringFormatter(r.Cash_Detail),
                               IncentiveNonCashValue = this.StringFormatter(r.Non_Cash_Range_Amount),
                               IncentiveNonCashDescription = this.StringFormatter(r.Non_Cash_Detail),
                               IncentiveJustification = this.StringFormatter(r.Justification_for_Incentive),
                               ConfidentialityLaw = this.StringFormatter(r.Confidentiality_Law_Cited),
                               VoluntaryConfidentialStatementInstrument = this.StringFormatter(r.VC_Language_IC),
                               VoluntaryConfidentialStatementContactMaterial = this.StringFormatter(r.VC_Language_Letters),
                               VoluntaryConfidentialStatementFaq = this.StringFormatter(r.VC_Language_FAQ),
                               VoluntaryConfidentialStatementBrochure = this.StringFormatter(r.VC_Language_Brochure),
                               FollowUpInformedConsentStatement = this.StringFormatter(r.FIC_Language),
                               FollowUpInformedConsentLocation = this.StringFormatter(r.FIC_Language_Locations),
                               PaperworkReductionActStatement = this.StringFormatter(r.PRA_Statement),
                               PaperworkReductionActLocation = this.StringFormatter(r.PRA_Statement_Locations)
                           };
        }
        public StudyController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            _studies = from d in _context.tblDetail
                       where d.Releasable.HasValue && d.Releasable.Value == true
                       let fu = d.Follow_Up
                       let s = fu.Study
                       where this.IsStringValid(s.Study_Name) || this.IsStringValid(fu.Follow_Up_Name)
                       select new StudyViewModel()
                       {
                           Id = d.Detail_ID,
                           Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                           Symbol = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Abbreviation) : this.StringFormatter(s.Study_Abbreviation),
                           OldName1 = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_1) : default(string),
                           OldName1Symbol = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_1_Abbr) : default(string),
                           OldName1Duration = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_1_Duration) : default(string),
                           OldName2 = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_2) : default(string),
                           OldName2Symbol = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_2_Abbr) : default(string),
                           OldName2Duration = !this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(s.Study_Old_Name_2_Duration) : default(string),
                           Parent = this.IsStringValid(s.Program.Program_Name)
                               ? new SeriesStubViewModel()
                               {
                                   Id = s.Program.Program_ID,
                                   Name = this.StringFormatter(s.Program.Program_Name)
                               }
                               : default(SeriesStubViewModel),
                           Investigator = this.StringFormatter(d.Steward),
                           BureauCode = this.StringFormatter(d.Bureau_Code),
                           ProgramCode = this.StringFormatter(d.Program_Code),
                           Website = this.IsStringValid(s.Study_Website) && Uri.IsWellFormedUriString(s.Study_Website, UriKind.Absolute) ? new Uri(s.Study_Website) : default(Uri),
                           Keywords = this.StringFormatter(d.Keywords),
                           Description = this.IsStringValid(fu.Follow_up_Abstract) ? this.StringFormatter(fu.Follow_up_Abstract) : this.StringFormatter(s.Study_Abstract),
                           AuthorizingLaw = this.StringFormatter(d.Authorizing_Law),
                           TotalCost = this.IntegerFormatter(d.Total_Cost.Value),
                           TotalCostYears = this.StringFormatter(d.Full_Scale_Years),
                           TotalCostDescription = this.StringFormatter(d.Total_Cost_Detail),
                           CollectionUniverse = this.BooleanFormatter(d.Universe_Survey),
                           CollectionSample = this.BooleanFormatter(d.Sample_Survey),
                           CollectionLongitudinal = this.BooleanFormatter(d.Longitudinal),
                           CollectionCrossSectional = this.BooleanFormatter(d.Cross_Sectional),
                           CollectionProgramMonitoring = this.BooleanFormatter(d.Program_Monitoring),
                           CollectionGranteeReporting = this.BooleanFormatter(d.Grantee_Reports),
                           CollectionVoluntary = this.BooleanFormatter(d.Voluntary),
                           CollectionMandatory = this.BooleanFormatter(d.Mandatory),
                           CollectionRequiredForBenefits = this.BooleanFormatter(d.Required_for_Benefits),
                           CollectionRequirementDescription = this.StringFormatter(d.Requirement_Detail),
                           CollectionRequirementReason = this.StringFormatter(d.Requirement_Reason),
                           Sorn = this.StringFormatter(d.SORN),
                           SornUrl = this.IsStringValid(d.SORN_URL) && Uri.IsWellFormedUriString(d.SORN_URL, UriKind.Absolute) ? new Uri(d.SORN_URL) : default(Uri),
                           ConfidentialityRestrictions = this.StringFormatter(d.Confidentiality_Restrictions),
                           PII_DI = this.BooleanFormatter(d.PII_DI),
                           PIA = this.BooleanFormatter(d.PIA),
                           DataCouldBePublic = this.BooleanFormatter(d.Could_Be_Public),
                           PublicationStatisticsType = this.StringFormatter(d.Publication_Type__Stats_),
                           PublicationStatisticsUrl = this.IsStringValid(d.Publication_URL__Stats_) && Uri.IsWellFormedUriString(d.Publication_URL__Stats_, UriKind.Absolute) ? new Uri(d.Publication_URL__Stats_) : default(Uri),
                           PublicationStatisticsDate = this.DateTimeFormatter(d.Publication_Date__Stats_),
                           PublicationDataUrl = this.IsStringValid(d.Publication_URL__Data_) && Uri.IsWellFormedUriString(d.Publication_URL__Data_, UriKind.Absolute) ? new Uri(d.Publication_URL__Data_) : default(Uri),
                           PublicationDataDate = this.DateTimeFormatter(d.Publication_Date__Data_),
                           PublicationRestrictedUseDataDate = this.DateTimeFormatter(d.Restricted_Use_Date),
                           SubjectPopulation = string.Join("; ",
                               new[] {
                                    d.Students.HasValue && d.Students.Value != 0 ? "Students" : string.Empty,
                                    d.Staff.HasValue && d.Staff.Value != 0 ? "Staff" : string.Empty,
                                    d.Institutions.HasValue && d.Institutions.Value != 0 ? "Institutions" : string.Empty,
                                    d.Programs.HasValue && d.Programs.Value != 0 ? "Programs" : string.Empty,
                                    d.Age_0_2.HasValue && d.Age_0_2.Value != 0 ? "Age 0–2" : string.Empty,
                                    d.Age_3_5.HasValue && d.Age_3_5.Value != 0 ? "Age 3–5" : string.Empty,
                                    d.Age_6_21.HasValue && d.Age_6_21.Value != 0 ? "Age 6–21" : string.Empty,
                                    d.Age_Older_Than_21.HasValue && d.Age_Older_Than_21.Value != 0 ? "Age Older Than 21" : string.Empty,
                                    d.Age_NA.HasValue && d.Age_NA.Value != 0 ? "Age Not Applicable" : string.Empty,
                                    d.Pre_K.HasValue && d.Pre_K.Value != 0 ? "Pre-Kindergarten" : string.Empty,
                                    d.Elementary.HasValue && d.Elementary.Value != 0 ? "Elementary School" : string.Empty,
                                    d.Middle.HasValue && d.Middle.Value != 0 ? "Middle School" : string.Empty,
                                    d.High_School.HasValue && d.High_School.Value != 0 ? "High School" : string.Empty,
                                    d.Postsecondary.HasValue && d.Postsecondary.Value != 0 ? "Postsecondary Education" : string.Empty,
                                    d.Graduate.HasValue && d.Graduate.Value != 0 ? "Graduate Education" : string.Empty,
                                    d.Continued_Technical_Ed.HasValue && d.Continued_Technical_Ed.Value != 0 ? "Continued/Technical Education" : string.Empty,
                                    d.General_Adult.HasValue && d.General_Adult.Value != 0 ? "General Adult Population" : string.Empty,
                                    d.Education_Level_NA.HasValue && d.Education_Level_NA.Value != 0 ? "Education Level Not Applicable" : string.Empty,
                                    this.StringFormatter(d.Other_Subject),
                                    this.StringFormatter(d.Other_Population)
                               }
                               .Where(str => !string.IsNullOrWhiteSpace(str))),
                           SubjectPopulationDescription = this.StringFormatter(d.Subject_Population_Detail),
                           DataLevelsAvailable = string.Join("; ",
                               new[] {
                                    d.Individual_Data.HasValue && d.Individual_Data.Value == true ? "Individual" : string.Empty,
                                    d.Classroom_Data.HasValue && d.Classroom_Data.Value == true ? "Classroom" : string.Empty,
                                    d.Grade_Level_Data.HasValue && d.Grade_Level_Data.Value == true ? "Grade Level" : string.Empty,
                                    d.School_Institution_Data.HasValue && d.School_Institution_Data.Value == true ? "School/Institution" : string.Empty,
                                    d.LEA_Data.HasValue && d.LEA_Data.Value == true ? "Local education agency (LEA)" : string.Empty,
                                    d.State_Data.HasValue && d.State_Data.Value == true ? "State" : string.Empty,
                                    d.Region_Data.HasValue && d.Region_Data.Value == true ? "Region" : string.Empty,
                                    d.National_Data.HasValue && d.National_Data.Value == true ? "United States" : string.Empty
                               }
                               .Where(str => !string.IsNullOrWhiteSpace(str))),
                           DataLevelPublic = this.StringFormatter(d.Public_Access_Level),
                           DataLevelDescription = this.StringFormatter(d.Data_Level_Detail)
                       };
            _previousStudyStubGenerator = id =>
                (from d in _context.tblDetail
                 where d.Detail_ID == id && d.Follow_Up.Preceding_Follow_Up_ID.HasValue
                 join fu in _context.tblFollow_Up on d.Follow_Up.Preceding_Follow_Up_ID.Value equals fu.Follow_Up_ID
                 select new StudyStubViewModel()
                 {
                     Id = fu.tblDetail.FirstOrDefault().Detail_ID,
                     Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(fu.Study.Study_Name)
                 })
                .FirstOrDefault();
            _contactGenerator = id =>
                from d in _context.tblDetail
                where d.Detail_ID == id
                let p = d.Follow_Up.Study.Program
                where p.Primary_Contact_ID.HasValue || p._2nd_Contact_ID.HasValue || p._3rd_Contact_ID.HasValue || p.Supervisor_ID.HasValue
                from contact_id in new Dictionary<int, int?>()
                {
                    { 1, p.Primary_Contact_ID },
                    { 2, p._2nd_Contact_ID },
                    { 3, p._3rd_Contact_ID },
                    { 4, p.Supervisor_ID }
                }
                .Where(cid => cid.Value.HasValue)
                join c in _context.tblContact on contact_id.Value.Value equals c.Contact_ID
                where this.IsStringValid(c.Contact_First_Name) && this.IsStringValid(c.Contact_Last_Name)
                select new ContactViewModel()
                {
                    Id = c.Contact_ID,
                    SortOrder = contact_id.Key,
                    FirstName = c.Contact_First_Name,
                    LastName = c.Contact_Last_Name,
                    EmailAddress = c.Contact_Email,
                    TelephoneNumber = c.Contact_Phone
                };
            _collectionStubGenerator = id =>
                from d in _context.tblDetail
                where d.Detail_ID == id
                join c in _context.tblCollection on d.Detail_ID equals c.Detail_ID.Value
                where this.IsStringValid(c.Collection_Name)
                select new CollectionStubViewModel()
                {
                    Id = c.Collection_ID,
                    Name = this.StringFormatter(c.Collection_Name),
                    Parent = new StudyStubViewModel()
                    {
                        Id = d.Detail_ID,
                        Name = this.IsStringValid(d.Follow_Up.Follow_Up_Name) ? this.StringFormatter(d.Follow_Up.Follow_Up_Name) : this.StringFormatter(d.Follow_Up.Study.Study_Name),
                        Parent = new SeriesStubViewModel()
                        {
                            Id = d.Follow_Up.Study.Program_ID,
                            Name = this.StringFormatter(d.Follow_Up.Study.Program.Program_Name)
                        }
                    }
                };
            _fileStubGenerator = id =>
            {
                IQueryable<FileStubViewModel> ret =
                    from d in _context.tblDetail
                    where d.Detail_ID == id
                    join sd in _context.tblSDLink_ on d.Follow_Up.Study.Study_ID equals sd.Study_ID
                    join set in _context.tblDataset_ on sd.Dataset_ID equals set.Dataset_ID
                    join df in _context.tblDFLink_ on sd.SD_Link_ID equals df.SD_Link_ID
                    join f in _context.tblFile_ on df.File_ID equals f.File_ID
                    where this.IsStringValid(f.File_Name)
                    select new FileStubViewModel()
                    {
                        Id = df.DF_Link_ID,
                        Name = this.StringFormatter(f.File_Name),
                        Format = this.StringFormatter(f.File_Format),
                        Restriction =
                            !set.Dataset_Restriction.HasValue || set.Dataset_Restriction.Value == 2
                            ? "Private"
                            : (set.Dataset_Restriction.Value == 1 ? "Restricted Public" : "Public")
                    };
                if (ret.Any())
                {
                    return ret.Select(item =>
                        new FileStubViewModel()
                        {
                            Id = item.Id,
                            Name = item.Name,
                            Format = item.Format,
                            Restriction = item.Restriction,
                            Studies = from df in _context.tblDFLink_
                                      where df.DF_Link_ID == item.Id
                                      join sd in _context.tblSDLink_ on df.SD_Link_ID equals sd.SD_Link_ID
                                      join s in _context.tblStudy on sd.Study_ID equals s.Study_ID
                                      from fu in s.tblFollow_Up
                                      from d in fu.tblDetail
                                      select new StudyStubViewModel()
                                      {
                                          Id = d.Detail_ID,
                                          Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                                          Parent = new SeriesStubViewModel()
                                          {
                                              Id = s.Program_ID,
                                              Name = this.StringFormatter(s.Program.Program_Name)
                                          }
                                      }
                        });
                }
                else
                {
                    return ret;
                }
            };
        }
        public PackageController(DataContext context)
        {
            if (context == default(DataContext))
            {
                throw new ArgumentNullException();
            }

            _context = context;
            _packages = from p in _context.tblPackage
                        where this.IsStringValid(p.ICR_ID_Main) || this.IsStringValid(p.EDICS__) || this.IsStringValid(p.ICR_Ref_No) || (this.IsStringValid(p.Office_OMB__) && this.IsStringValid(p.Study_OMB__))
                        select new PackageViewModel()
                        {
                            Id = p.Package_ID,
                            ICRAS = this.IsStringValid(p.ICR_ID_Main) && this.IsStringValid(p.ICR_ID_Version) ? $"{p.ICR_ID_Main}.{p.ICR_ID_Version}" : null,
                            EDICS = this.StringFormatter(p.EDICS__),
                            ICRReferenceNumber = this.StringFormatter(p.ICR_Ref_No),
                            OMBControlNumber = this.IsStringValid(p.Office_OMB__) && this.IsStringValid(p.Study_OMB__)
                                ? $"{p.Office_OMB__}-{p.Study_OMB__} v." + (this.IsStringValid(p.Version_OMB__) ? p.Version_OMB__ : "NEW")
                                : string.Empty,
                            CFDA = this.StringFormatter(p.CFDA__),
                            Title = this.StringFormatter(p.Package_Title),
                            Keywords = this.StringFormatter(p.Package_Keywords),
                            Abstract = this.StringFormatter(p.Package_Abstract),
                            IssueDate = this.DateTimeFormatter(p.Date_Notice_Issued),
                            ExpirationDate = this.DateTimeFormatter(p.Expiration),
                            NoticeType = this.StringFormatter(p.Type_of_Notice),
                            TermsOfClearance = this.StringFormatter(p.TOC),
                            NumberRespondents = this.IntegerFormatter(p.__Respondents),
                            NumberResponses = this.IntegerFormatter(p.__Responses),
                            PercentCollectedElectronically = this.DoubleFormatter(p.Percent_Collected_Electronically),
                            BurdenTotal = this.IntegerFormatter(p.Total_Burden_Hours),
                            BurdenChange = this.IntegerFormatter(p.Burden_Hours_Change),
                            BurdenAdjustment = this.IntegerFormatter(p.Burden_Hours_Adjustment),
                            BurdenExplanation = this.StringFormatter(p.Change_Explanation),
                            PublicComment = this.StringFormatter(p.Public_Comment_Doc),
                            PublicCommentResponse = this.StringFormatter(p.Public_Response_Doc),
                            OMBPassback = this.StringFormatter(p.OMB_Response_Doc),
                            AuthorizingLawCited = this.StringFormatter(p.Authorizing_Law_Cited),
                            AuthorizingLawText = this.StringFormatter(p.Authorizing_Law_Text),
                            ContractorConfidentialityFormLocation = this.StringFormatter(p.Contractor_Confidentiality_Form),
                        };
            _collectionStubGenerator = id =>
                from l in _context.tblLink
                where l.Package_ID == id
                join c in _context.tblCollection on l.Collection_ID equals c.Collection_ID
                join d in _context.tblDetail on c.Detail_ID.Value equals d.Detail_ID
                let fu = d.Follow_Up
                let s = fu.Study
                where d.Releasable.HasValue && d.Releasable.Value == true && this.IsStringValid(c.Collection_Name)
                select new CollectionStubViewModel()
                {
                    Id = c.Collection_ID,
                    Name = this.StringFormatter(c.Collection_Name),
                    Parent = new StudyStubViewModel()
                    {
                        Id = c.Detail_ID.Value,
                        Name = this.IsStringValid(fu.Follow_Up_Name) ? this.StringFormatter(fu.Follow_Up_Name) : this.StringFormatter(s.Study_Name),
                        Parent = new SeriesStubViewModel()
                        {
                            Id = s.Program_ID,
                            Name = this.StringFormatter(s.Program.Program_Name)
                        }
                    }
                };
        }