public int StudyType   = 0; // Intrinsic or Extrinsic

        /// <summary>
        /// Create mapping datasets to use in the sas scripts
        /// </summary>
        /// <param name="noCache"> If true send a timestamp to avoid caching by the server</param>
        /// <returns></returns>
        public DataSet ToMappingDataSet(bool noCache = false)
        {
            var data = new DataSet();


            // Study information
            var studyInfo = new DataTable("study");

            studyInfo.Columns.Add("Submission", typeof(string));
            studyInfo.Columns.Add("Supplement", typeof(string));
            studyInfo.Columns.Add("StudyCode", typeof(string));
            studyInfo.Columns.Add("Type", typeof(int));
            studyInfo.Columns.Add("Design", typeof(int));
            studyInfo.Columns.Add("Cumulative", typeof(int));
            studyInfo.Columns.Add("SubjectCTCorrelation", typeof(int));
            studyInfo.Columns.Add("ScatterPlot", typeof(int));
            studyInfo.Columns.Add("Demographictable", typeof(int));
            studyInfo.Columns.Add("UseEx", typeof(int));
            studyInfo.Columns.Add("UseSuppdm", typeof(int));
            studyInfo.Columns.Add("DisablePcCleanup", typeof(int));
            studyInfo.Columns.Add("UseCustomArms", typeof(int));
            studyInfo.Columns.Add("UseCustomPcVisit", typeof(int));
            studyInfo.Columns.Add("UseCustomPcPctptnum", typeof(int));
            studyInfo.Columns.Add("UseCustomPpVisit", typeof(int));
            studyInfo.Rows.Add(
                this.NDAName,
                this.quote(this.SupplementNumber),
                this.StudyCode,
                this.StudyType,
                this.StudyDesign,
                this.Cumulative ? 1 : 0,
                this.SubjectCTCorrelation ? 1 : 0,
                this.ScatterPlot ? 1 : 0,
                this.Demographictable ? 1 : 0,
                this.UseEx ? 1:0,
                this.UseSuppdm ? 1:0,
                this.DisablePcCleanup ? 1:0,
                this.UseCustomArms ? 1:0,
                this.UseCustomPcVisit ? 1:0,
                this.UseCustomPcPctptnum ? 1:0,
                this.UseCustomPpVisit ? 1:0);

            // Study mappings
            var studyMapping = new DataTable("mapping");

            studyMapping.Columns.Add("StudyCode", typeof(string));
            studyMapping.Columns.Add("Domain", typeof(string));
            studyMapping.Columns.Add("StdmVar", typeof(string));
            studyMapping.Columns.Add("FileVar", typeof(string));

            foreach (var domain in this.StudyMappings)
            {
                // Skip EX if UseEx is not checked
                if (!UseEx && domain.Type.ToUpper() == "EX")
                {
                    continue;
                }

                // Skip Suppdm if Usesuppdm is not checked
                if (!UseSuppdm && domain.Type.ToUpper() == "SUPPDM")
                {
                    continue;
                }

                // Copy each variable mapping to the table
                foreach (var mapping in domain.DomainMappings)
                {
                    studyMapping.Rows.Add(
                        this.StudyCode,
                        domain.Type,
                        mapping.SdtmVariable,
                        mapping.FileVariable);
                }
            }

            // Arm mappings
            if (this.UseCustomArms)
            {
                var armMapping = new DataTable("customDmArms");
                armMapping.Columns.Add("OldArm", typeof(string));
                armMapping.Columns.Add("NewArm", typeof(string));
                if (this.ArmMappings != null)
                {
                    foreach (var arm in this.ArmMappings)
                    {
                        armMapping.Rows.Add(arm.OldArm, arm.NewArm);
                    }
                }
                data.Tables.Add(armMapping);
            }

            // PC Visit Mapping
            if (this.UseCustomPcVisit)
            {
                var pcVisitMapping = new DataTable("customPcVisit");
                pcVisitMapping.Columns.Add("OldValue", typeof(string));
                pcVisitMapping.Columns.Add("NewValue", typeof(string));
                if (this.PcVisitMappings != null)
                {
                    foreach (var mapping in this.PcVisitMappings)
                    {
                        pcVisitMapping.Rows.Add(mapping.Original, mapping.New);
                    }
                }
                data.Tables.Add(pcVisitMapping);
            }

            // PC Tptnum Mapping
            if (this.UseCustomPcPctptnum)
            {
                var pcTptnumMapping = new DataTable("customPcPctptnum");
                pcTptnumMapping.Columns.Add("OldValue", typeof(string));
                pcTptnumMapping.Columns.Add("NewValue", typeof(string));
                if (this.PcPctptnumMappings != null)
                {
                    foreach (var mapping in this.PcPctptnumMappings)
                    {
                        pcTptnumMapping.Rows.Add(mapping.Original, mapping.New);
                    }
                }
                data.Tables.Add(pcTptnumMapping);
            }

            // PP Visit Mapping
            if (this.UseCustomPpVisit)
            {
                var ppVisitMapping = new DataTable("customPpVisit");
                ppVisitMapping.Columns.Add("OldValue", typeof(string));
                ppVisitMapping.Columns.Add("NewValue", typeof(string));
                if (this.PpVisitMappings != null)
                {
                    foreach (var mapping in this.PpVisitMappings)
                    {
                        ppVisitMapping.Rows.Add(mapping.Original, mapping.New);
                    }
                }
                data.Tables.Add(ppVisitMapping);
            }

            // Study references
            var references = new DataTable("references");

            references.Columns.Add("Cohort", typeof(string));
            references.Columns.Add("Number", typeof(int));
            references.Columns.Add("Reference", typeof(string));
            if (this.Cohorts != null)
            {
                foreach (var cohort in this.Cohorts)
                {
                    references.Rows.Add(
                        cohort.Name,
                        cohort.Number,
                        cohort.Reference);
                }
            }

            // User settings
            var userName = HttpContext.Current.User.Identity.Name ?? "DEFAULT";

            userName = userName.Substring(userName.LastIndexOfAny(new[] { '/', '\\' }) + 1);
            var userConfig = new DataTable("userConfig");

            userConfig.Columns.Add("Name", typeof(string));
            userConfig.Columns.Add("Value", typeof(string));
            userConfig.Rows.Add("ProfileName", this.quote(this.ProfileName));
            userConfig.Rows.Add("Username", this.quote(userName));
            if (noCache) // If caching is not allowed, send timestamp, otherwise send study settings Hash
            {
                userConfig.Rows.Add("Timestamp", this.quote(DateTime.Now.Ticks.ToString()));
            }
            else
            {
                userConfig.Rows.Add("Hash", this.quote(NDAHash.GetStudyHash(this.NDAName, this.StudyCode).ToString()));
            }

            data.Tables.Add(studyInfo);
            data.Tables.Add(studyMapping);
            data.Tables.Add(references);
            data.Tables.Add(userConfig);

            return(data);
        }
        /// <summary>
        /// Create Variability Meta mapping datasets to use in the sas scripts
        /// </summary>
        /// <param name="noCache"> If true send a timestamp to avoid caching by the server</param>
        /// <returns></returns>
        public DataSet ToVariabilityMetaMappingDataSet(bool noCache = false)
        {
            var data = new DataSet();
            // meta analysis format information
            var metaVariabilityformat = new DataTable("metaVariabilityformat");

            metaVariabilityformat.Columns.Add("PlotType", typeof(String));
            metaVariabilityformat.Columns.Add("NormalizedType", typeof(String));
            metaVariabilityformat.Columns.Add("upperbound", typeof(float));
            metaVariabilityformat.Columns.Add("lowerbound", typeof(float));
            metaVariabilityformat.Columns.Add("showdistributionornot", typeof(String));
            metaVariabilityformat.Rows.Add(
                this.PlotType,
                this.NormalizedType,
                this.Cutoffupperbound,
                this.Cutofflowerbound,
                this.showdistributionornot);

            // Study information
            var studyInfo = new DataTable("study");

            for (int i = 1; i <= 1; i++)
            {
                studyInfo.Columns.Add("Submission", typeof(string));
                studyInfo.Columns.Add("Supplement", typeof(string));
                studyInfo.Columns.Add("StudyCode", typeof(string));
                studyInfo.Columns.Add("Type", typeof(int));
                studyInfo.Columns.Add("Design", typeof(int));
                studyInfo.Columns.Add("Cumulative", typeof(int));
                studyInfo.Columns.Add("SubjectCTCorrelation", typeof(int));
                studyInfo.Columns.Add("ScatterPlot", typeof(int));
                studyInfo.Columns.Add("Demographictable", typeof(int));
                studyInfo.Columns.Add("UseEx", typeof(int));
                studyInfo.Columns.Add("UseSuppdm", typeof(int));
                studyInfo.Columns.Add("DisablePcCleanup", typeof(int));
                studyInfo.Columns.Add("UseCustomArms", typeof(int));
                studyInfo.Columns.Add("UseCustomPcVisit", typeof(int));
                studyInfo.Columns.Add("UseCustomPcPctptnum", typeof(int));
                studyInfo.Columns.Add("UseCustomPpVisit", typeof(int));
                studyInfo.Rows.Add(
                    this.NDAName,
                    this.quote(this.SupplementNumber),
                    this.StudyCode,
                    this.StudyType,
                    this.StudyDesign,
                    this.Cumulative ? 1 : 0,
                    this.SubjectCTCorrelation ? 1 : 0,
                    this.ScatterPlot ? 1 : 0,
                    this.Demographictable ? 1 : 0,
                    this.UseEx ? 1 : 0,
                    this.UseSuppdm ? 1 : 0,
                    this.DisablePcCleanup ? 1 : 0,
                    this.UseCustomArms ? 1 : 0,
                    this.UseCustomPcVisit ? 1 : 0,
                    this.UseCustomPcPctptnum ? 1 : 0,
                    this.UseCustomPpVisit ? 1 : 0);
            }



            // Study mappings
            var studyMapping = new DataTable("mapping");

            studyMapping.Columns.Add("StudyCode", typeof(string));
            studyMapping.Columns.Add("Domain", typeof(string));
            studyMapping.Columns.Add("StdmVar", typeof(string));
            studyMapping.Columns.Add("FileVar", typeof(string));

            foreach (var domain in this.StudyMappings)
            {
                // Skip EX if UseEx is not checked
                if (!UseEx && domain.Type.ToUpper() == "EX")
                {
                    continue;
                }

                // Skip Suppdm if Usesuppdm is not checked
                if (!UseSuppdm && domain.Type.ToUpper() == "SUPPDM")
                {
                    continue;
                }

                // Copy each variable mapping to the table
                foreach (var mapping in domain.DomainMappings)
                {
                    studyMapping.Rows.Add(
                        this.StudyCode,
                        domain.Type,
                        mapping.SdtmVariable,
                        mapping.FileVariable);
                }
            }

            // Arm mappings
            if (this.UseCustomArms)
            {
                var armMapping = new DataTable("customDmArms");
                armMapping.Columns.Add("OldArm", typeof(string));
                armMapping.Columns.Add("NewArm", typeof(string));
                armMapping.Columns.Add("StudyCode", typeof(string));

                if (this.ArmMappings != null)
                {
                    foreach (var arm in this.ArmMappings)
                    {
                        armMapping.Rows.Add(arm.OldArm, arm.NewArm, this.StudyCode);
                    }
                }

                data.Tables.Add(armMapping);
            }

            // PC Visit Mapping
            if (this.UseCustomPcVisit)
            {
                var pcVisitMapping = new DataTable("customPcVisit");
                pcVisitMapping.Columns.Add("OldValue", typeof(string));
                pcVisitMapping.Columns.Add("NewValue", typeof(string));
                pcVisitMapping.Columns.Add("StudyCode", typeof(string));

                if (this.PcVisitMappings != null)
                {
                    foreach (var mapping in this.PcVisitMappings)
                    {
                        pcVisitMapping.Rows.Add(mapping.Original, mapping.New, this.StudyCode);
                    }
                }

                data.Tables.Add(pcVisitMapping);
            }

            // PC Tptnum Mapping
            if (this.UseCustomPcPctptnum)
            {
                var pcTptnumMapping = new DataTable("customPcPctptnum");
                pcTptnumMapping.Columns.Add("OldValue", typeof(string));
                pcTptnumMapping.Columns.Add("NewValue", typeof(string));
                pcTptnumMapping.Columns.Add("StudyCode", typeof(string));

                if (this.PcPctptnumMappings != null)
                {
                    foreach (var mapping in this.PcPctptnumMappings)
                    {
                        pcTptnumMapping.Rows.Add(mapping.Original, mapping.New, this.StudyCode);
                    }
                }
                data.Tables.Add(pcTptnumMapping);
            }

            // PP Visit Mapping
            if (this.UseCustomPpVisit)
            {
                var ppVisitMapping = new DataTable("customPpVisit");
                ppVisitMapping.Columns.Add("OldValue", typeof(string));
                ppVisitMapping.Columns.Add("NewValue", typeof(string));
                ppVisitMapping.Columns.Add("StudyCode", typeof(string));

                if (this.PpVisitMappings != null)
                {
                    foreach (var mapping in this.PpVisitMappings)
                    {
                        ppVisitMapping.Rows.Add(mapping.Original, mapping.New, this.StudyCode);
                    }
                }
                data.Tables.Add(ppVisitMapping);
            }

            // Study references
            var references = new DataTable("cohort");

            references.Columns.Add("StudyCode", typeof(string));
            references.Columns.Add("Number", typeof(int));
            references.Columns.Add("Cohort", typeof(string));

            if (this.Cohorts != null)
            {
                foreach (var cohort in this.Cohorts)
                {
                    references.Rows.Add(
                        this.StudyCode,
                        cohort.Number,
                        cohort.Name);
                }
            }

            // Merge with study cohorts list to obtain the cohort number

            // Study meta references
            var metareferences = new DataTable("trtdose");

            metareferences.Columns.Add("StudyCode", typeof(string));
            metareferences.Columns.Add("TrtGrp", typeof(string));
            metareferences.Columns.Add("Dose", typeof(string));
            metareferences.Columns.Add("SelectedCohort", typeof(string));
            metareferences.Columns.Add("Number", typeof(int));


            if (this.Cohorts != null)
            {
                for (int i = 0; i < this.Cohorts.Count; i++)
                {
                    var MetaReferences = this.Reports[0].Settings.References[i].MetaCohorts;
                    var Dose           = this.Reports[0].Settings.References[i].Doses;
                    //var Dose1 = this.Reports[0].Settings.References[i].MetaCohortsAndDoses[0].Dose;
                    //var Dose555 = this.Doses[i];
                    foreach (var Reference in MetaReferences)
                    {
                        foreach (var Dosevalue in Dose)
                        {
                            if (Dosevalue.MetaCohort == Reference)
                            {
                                metareferences.Rows.Add(
                                    this.StudyCode,
                                    Dosevalue.MetaCohort,
                                    Dosevalue.value,
                                    this.Cohorts[i].Name,
                                    this.Cohorts[i].Number
                                    );
                            }
                        }
                    }
                }
            }

            // User settings
            var userName = HttpContext.Current.User.Identity.Name ?? "DEFAULT";

            userName = userName.Substring(userName.LastIndexOfAny(new[] { '/', '\\' }) + 1);
            var userConfig = new DataTable("userConfig");

            userConfig.Columns.Add("Name", typeof(string));
            userConfig.Columns.Add("Value", typeof(string));
            userConfig.Columns.Add("StudyCode", typeof(string));
            userConfig.Rows.Add("ProfileName", this.quote(this.ProfileName), this.StudyCode);
            userConfig.Rows.Add("Username", this.quote(userName), this.StudyCode);


            if (noCache) // If caching is not allowed, send timestamp, otherwise send study settings Hash
            {
                userConfig.Rows.Add("Timestamp", this.quote(DateTime.Now.Ticks.ToString()), this.StudyCode);
            }
            else
            {
                userConfig.Rows.Add("Hash", this.quote(NDAHash.GetStudyHash(this.NDAName, this.StudyCode).ToString()), this.StudyCode);
            }

            data.Tables.Add(studyInfo);
            data.Tables.Add(studyMapping);
            data.Tables.Add(references);
            data.Tables.Add(metareferences);
            data.Tables.Add(userConfig);
            data.Tables.Add(metaVariabilityformat);

            return(data);
        }