Example #1
0
        public static ConceptScheme GetByCtid(string ctid)
        {
            ConceptScheme entity = new ConceptScheme();

            entity = Manager.GetByCtid(ctid);
            return(entity);
        }
Example #2
0
        public static ConceptScheme Get(int id)
        {
            ConceptScheme entity = new ConceptScheme();

            entity = Manager.Get(id);
            return(entity);
        }
Example #3
0
        public static List <string> CollectIds(StudyUnit studyUnit)
        {
            List <string> ids = new List <string>();

            //0. StudyUnit itself
            CollectIds(studyUnit, ids);
            //1. Event
            CollectIds(studyUnit.Events, ids);
            //2. Member
            CollectIds(studyUnit.Members, ids);
            //3. Organization
            CollectIds(studyUnit.Organizations, ids);
            //4. Abstract
            CollectIds(studyUnit.Abstract, ids);
            //5. Coverage
            CollectIds(studyUnit.Coverage, ids);
            //6. Funding Agency
            CollectIds(studyUnit.FundingInfos, ids);
            //6-1. Organization including
            CollectIds(FundingInfo.GetOrganizations(studyUnit.FundingInfos), ids);
            //7.Universe
            CollectIds(Sampling.GetUniverses(studyUnit.Samplings), ids);
            //8.Sampling
            CollectIds(studyUnit.Samplings, ids);
            //9. Concept Scheme
            CollectIds(studyUnit.ConceptSchemes, ids);
            //9-1. Concept
            CollectIds(ConceptScheme.GetConcepts(studyUnit.ConceptSchemes), ids);
            //10. Question
            CollectIds(studyUnit.Questions, ids);
            //10-1. Answer
            CollectIds(Question.GetResponses(studyUnit.Questions), ids);
            //11. Category Scheme
            CollectIds(studyUnit.CategorySchemes, ids);
            //11-1. Category
            CollectIds(CategoryScheme.GetCategories(studyUnit.CategorySchemes), ids);
            //12. Code Scheme
            CollectIds(studyUnit.CodeSchemes, ids);
            //12-1. Code
            CollectIds(CodeScheme.GetCodes(studyUnit.CodeSchemes), ids);
            //13. Variable Scheme
            CollectIds(studyUnit.VariableScheme, ids);
            //14. Variable
            CollectIds(studyUnit.Variables, ids);
            //14-1. Answer
            CollectIds(Variable.GetResponses(studyUnit.Variables), ids);
            //15. Dataset
            CollectIds(studyUnit.DataSets, ids);
            //16. Data File
            CollectIds(studyUnit.DataFiles, ids);
            //17. Order of Question
            CollectIds(studyUnit.ControlConstructSchemes, ids);
            //17-1.Sequence
            CollectIds(ControlConstructScheme.GetSequences(studyUnit.ControlConstructSchemes), ids);
            //17-2.Constructs
            CollectIds(ControlConstructScheme.GetConstructs(studyUnit.ControlConstructSchemes), ids);

            return(ids);
        }
Example #4
0
        internal static ConceptSchemeRef Create(ConceptScheme scheme)
        {
            if (scheme == null)
                return null;

            ConceptSchemeRef schemeRef = new ConceptSchemeRef();
            schemeRef.Id = scheme.Id;
            schemeRef.Version = scheme.Version;
            schemeRef.AgencyId = scheme.AgencyId;

            return schemeRef;
        }
Example #5
0
        public ConceptSchemeVM(ConceptScheme conceptScheme)
        {
            this.conceptScheme = conceptScheme;
            concepts           = new ObservableCollection <ConceptVM>();
            foreach (Concept conceptModel in conceptScheme.Concepts)
            {
                ConceptVM conceptVM = new ConceptVM(conceptModel);
                conceptVM.Parent = this;
                concepts.Add(conceptVM);
            }
            modelSyncher = new ModelSyncher <ConceptVM, Concept>(this, concepts, conceptScheme.Concepts);
            string message = Properties.Resources.ConceptFormHelpMessage1;

            TestMessage = message;
        }
Example #6
0
        internal static ConceptSchemeRef Create(ConceptScheme scheme)
        {
            if (scheme == null)
            {
                return(null);
            }

            ConceptSchemeRef schemeRef = new ConceptSchemeRef();

            schemeRef.Id       = scheme.Id;
            schemeRef.Version  = scheme.Version;
            schemeRef.AgencyId = scheme.AgencyId;

            return(schemeRef);
        }
        internal static TempConceptSchemeRef Create(ConceptScheme scheme)
        {
            if (scheme == null)
            {
                return(null);
            }

            TempConceptSchemeRef schemeRef = new TempConceptSchemeRef();

            schemeRef.ID       = scheme.ID;
            schemeRef.Version  = scheme.Version;
            schemeRef.AgencyID = scheme.AgencyID;

            return(schemeRef);
        }
Example #8
0
        private void CreateQuestionGroups()
        {
            //Get concepts that are used, add the implicit ones and except for "0" create question groups from them in the relevant question scheme and put into the working set
            this.usedConcepts = questionsConcepts.Values.Distinct().ToList();

            //add implicit level-1 groups that are parents of level-2 groups
            var implicits = new List <string>();

            foreach (var uc in this.usedConcepts)
            {
                if (uc.Length == 1 + 2 + 2)
                {
                    if (!usedConcepts.Contains(uc.Substring(0, 3)))
                    {
                        implicits.Add(uc.Substring(0, 3));
                    }
                }
            }
            this.usedConcepts.AddRange(implicits.Distinct());

            //get the concept scheme from the repository
            //I assume there is only one
            //if there is none, get concepts from the working set
            ConceptScheme qcgConceptScheme = new ConceptScheme();
            var           client           = Utility.GetClient();
            var           facet            = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.ConceptScheme);
            SearchResponse response = client.Search(facet);
            bool           fromRepo = false;

            if (response.ReturnedResults > 0)
            {
                fromRepo         = true;
                qcgConceptScheme = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate) as ConceptScheme;
            }

            var controlConstructScheme = WorkingSet.OfType <ControlConstructScheme>().Where(x => string.Compare(x.ItemName.Best, this.qcsName, ignoreCase: true) == 0).First();

            foreach (var uc in this.usedConcepts)
            {
                if (uc != "0")
                {
                    ControlConstructGroup qcg = new ControlConstructGroup();
                    qcg.TypeOfGroup = "ConceptGroup";
                    Concept qcgConcept = new Concept();
                    if (fromRepo)
                    {
                        qcgConcept = qcgConceptScheme.Concepts.Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                    }
                    else    //from working set
                    {
                        qcgConcept = WorkingSet.OfType <Concept>().Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                    }
                    qcg.Concept = qcgConcept;
                    qcg.ItemName.Add("en-GB", "Question Construct Group - " + qcgConcept.Label.Best);
                    //Trace.WriteLine("   " + qcg.ItemName.Best);
                    controlConstructScheme.ControlConstructGroups.Add(qcg);
                }
            }
            WorkingSet.AddRange(controlConstructScheme.ControlConstructGroups);
            Trace.WriteLine("  question construct groups: " + controlConstructScheme.ControlConstructGroups.Count().ToString() + " for " + this.qcsName);
        }
        private void CreateVariableGroups()
        {
            //Get concepts that are used, add the implicit ones and except for "0" create variable groups from them in the relevant variable scheme and put into the working set
             this.usedConcepts = variablesConcepts.Values.Distinct().ToList();

             //add implicit level-1 groups that are parents of level-2 groups
             var implicits = new List<string>();
             foreach (var uc in this.usedConcepts)
             {
                 if (uc.Length == 1 + 2 + 2)
                 {
                     if (!usedConcepts.Contains(uc.Substring(0, 3)))
                     {
                         implicits.Add(uc.Substring(0,3));
                     }
                 }
             }
             this.usedConcepts.AddRange(implicits.Distinct());

            //get the concept scheme from the repository
            //I assume there is only one
            //if there is none, get concepts from the working set
            ConceptScheme vgConceptScheme = new ConceptScheme();
            var client = Utility.GetClient();
            var facet = new SearchFacet();
            facet.ItemTypes.Add(DdiItemType.ConceptScheme);
            SearchResponse response = client.Search(facet);
            bool fromRepo = false;
            if (response.ReturnedResults > 0)
            {
                fromRepo = true;
                vgConceptScheme = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate) as ConceptScheme;
            }

             var variableScheme = WorkingSet.OfType<VariableScheme>().Where(x => string.Compare(x.ItemName.Best, this.vsName, ignoreCase: true) == 0).First();
             foreach (var uc in this.usedConcepts)
             {
                 if (uc != "0")
                 {
                     VariableGroup vg = new VariableGroup();
                     vg.TypeOfGroup = "ConceptGroup";
                     Concept vgConcept = new Concept();
                     if (fromRepo)
                     {
                         vgConcept = vgConceptScheme.Concepts.Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                     }
                     else    //from working set
                     {
                         vgConcept = WorkingSet.OfType<Concept>().Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                     }

                     vg.Concept = vgConcept;
                     vg.ItemName.Add("en-GB", "Variable Group - " + vgConcept.Label.Best);
                     //Trace.WriteLine("   " + vg.ItemName.Best);
                     variableScheme.VariableGroups.Add(vg);
                 }
             }
             WorkingSet.AddRange(variableScheme.VariableGroups);
             Trace.WriteLine("  concept groups: " + variableScheme.VariableGroups.Count().ToString() + " for " + this.vsName);
        }
        public void BuildAndRegisterConcepts()
        {
            // Setting the default agency like this means
            // we don't need to manually set it for every
            // item we create.
            VersionableBase.DefaultAgencyId = "int.example";

            // Create a scheme to hold the concepts.
            ConceptScheme scheme = new ConceptScheme();

            scheme.Label.Current = "Transportation Modes";

            // Create 6 concepts, setting up a small hierarchy.
            Concept transportMode = new Concept();

            transportMode.Label.Current = "Transport Mode";

            Concept auto = new Concept();

            auto.SubclassOf.Add(transportMode);
            auto.Label.Current = "Auto";

            Concept car = new Concept();

            car.SubclassOf.Add(auto);
            car.Label.Current = "Car";

            Concept truck = new Concept();

            truck.SubclassOf.Add(auto);
            truck.Label.Current = "Truck";

            Concept bike = new Concept();

            bike.SubclassOf.Add(transportMode);
            bike.Label.Current = "Bike";

            Concept walk = new Concept();

            walk.SubclassOf.Add(transportMode);
            walk.Label.Current = "Walk";

            // Add the concpts to the scheme.
            scheme.Concepts.Add(transportMode);
            scheme.Concepts.Add(auto);
            scheme.Concepts.Add(car);
            scheme.Concepts.Add(truck);
            scheme.Concepts.Add(bike);
            scheme.Concepts.Add(walk);

            var           client  = GetClient();
            CommitOptions options = new CommitOptions();

            // Gather all the scheme and all the items in the scheme,
            // so we can register them with a single call to the repository.
            ItemGathererVisitor gatherer = new ItemGathererVisitor();

            scheme.Accept(gatherer);

            // Register the items with the repository.
            client.RegisterItems(gatherer.FoundItems, options);

            // Alternatively, we could register the items one at a time, like this.

            //client.RegisterItem(scheme, options);
            //client.RegisterItem(transportMode, options);
            //client.RegisterItem(auto, options);
            //client.RegisterItem(car, options);
            //client.RegisterItem(truck, options);
            //client.RegisterItem(bike, options);
            //client.RegisterItem(walk, options);
        }
        public void BuildAndRegisterConcepts()
        {
            // Setting the default agency like this means
            // we don't need to manually set it for every
            // item we create.
            VersionableBase.DefaultAgencyId = "int.example";

            // Create a scheme to hold the concepts.
            ConceptScheme scheme = new ConceptScheme();
            scheme.Label.Current = "Transportation Modes";

            // Create 6 concepts, setting up a small hierarchy.
            Concept transportMode = new Concept();
            transportMode.Label.Current = "Transport Mode";

            Concept auto = new Concept();
            auto.SubclassOf.Add(transportMode);
            auto.Label.Current = "Auto";

            Concept car = new Concept();
            car.SubclassOf.Add(auto);
            car.Label.Current = "Car";

            Concept truck = new Concept();
            truck.SubclassOf.Add(auto);
            truck.Label.Current = "Truck";

            Concept bike = new Concept();
            bike.SubclassOf.Add(transportMode);
            bike.Label.Current = "Bike";

            Concept walk = new Concept();
            walk.SubclassOf.Add(transportMode);
            walk.Label.Current = "Walk";

            // Add the concpts to the scheme.
            scheme.Concepts.Add(transportMode);
            scheme.Concepts.Add(auto);
            scheme.Concepts.Add(car);
            scheme.Concepts.Add(truck);
            scheme.Concepts.Add(bike);
            scheme.Concepts.Add(walk);

            var client = GetClient();
            CommitOptions options = new CommitOptions();

            // Gather all the scheme and all the items in the scheme,
            // so we can register them with a single call to the repository.
            ItemGathererVisitor gatherer = new ItemGathererVisitor();
            scheme.Accept(gatherer);

            // Register the items with the repository.
            client.RegisterItems(gatherer.FoundItems, options);

            // Alternatively, we could register the items one at a time, like this.

            //client.RegisterItem(scheme, options);
            //client.RegisterItem(transportMode, options);
            //client.RegisterItem(auto, options);
            //client.RegisterItem(car, options);
            //client.RegisterItem(truck, options);
            //client.RegisterItem(bike, options);
            //client.RegisterItem(walk, options);
        }
Example #12
0
        public bool ImportVariables(string path, StudyUnitVM studyUnit)
        {
            Debug.Assert(!string.IsNullOrEmpty(path));
            StudyUnit       studyUnitModel = studyUnit.StudyUnitModel;
            FileStream      stream         = File.OpenRead(path);
            SavFileParser   parser         = new SavFileParser(stream);
            List <Variable> variables      = new List <Variable>();

            foreach (SpssLib.SpssDataset.Variable v in parser.Variables)
            {
                Variable variable = new Variable();
                variable.Title = v.Name;
                variable.Label = v.Label;
                variables.Add(variable);
                if (v.Type == SpssLib.SpssDataset.DataType.Text)
                {
                    variable.Response.TypeCode = Options.RESPONSE_TYPE_FREE_CODE;
                }
                else
                {
                    if (v.ValueLabels.Count > 0)
                    {
                        CategoryScheme categoryScheme = new CategoryScheme();
                        categoryScheme.Title = v.Label;
                        studyUnitModel.CategorySchemes.Add(categoryScheme);
                        CodeScheme codeScheme = new CodeScheme();
                        codeScheme.Title = v.Label;;
                        studyUnitModel.CodeSchemes.Add(codeScheme);

                        variable.Response.TypeCode     = Options.RESPONSE_TYPE_CHOICES_CODE;
                        variable.Response.CodeSchemeId = codeScheme.Id;
                        // Create Category
                        foreach (KeyValuePair <double, string> pair in v.ValueLabels)
                        {
                            Category category = new Category();
                            categoryScheme.Categories.Add(category);
                            category.Title            = pair.Value;
                            category.CategorySchemeId = categoryScheme.Id;

                            Code code = new Code();
                            codeScheme.Codes.Add(code);
                            code.CategoryId   = category.Id;
                            code.CodeSchemeId = codeScheme.Id;
                            code.Value        = pair.Key.ToString();
                        }
                        // Add Missing value
                        if (v.MissingValues.Count > 0)
                        {
                            foreach (double missingValue in v.MissingValues)
                            {
                                string missingValueStr = missingValue.ToString();
                                if (ExistValue(codeScheme, missingValueStr))
                                {
                                    continue;
                                }
                                Category category = new Category();
                                categoryScheme.Categories.Add(category);
                                category.Title            = Resources.MissingValue; //Missing value
                                category.IsMissingValue   = true;
                                category.CategorySchemeId = categoryScheme.Id;

                                Code code = new Code();
                                codeScheme.Codes.Add(code);
                                code.CategoryId   = category.Id;
                                code.CodeSchemeId = codeScheme.Id;
                                code.Value        = missingValueStr;
                            }
                        }
                    }
                    else
                    {
                        variable.Response.TypeCode = Options.RESPONSE_TYPE_NUMBER_CODE;
                    }
                }
            }

            if (variables.Count > 0)
            {
                ConceptScheme conceptScheme = new ConceptScheme();
                conceptScheme.Title = EDOUtils.UniqueLabel(ConceptScheme.GetTitles(studyUnitModel.ConceptSchemes), Resources.ImportVariable);                        //Imported Variable
                string name = Path.GetFileName(path);
                conceptScheme.Memo = EDOUtils.UniqueLabel(ConceptScheme.GetMemos(studyUnitModel.ConceptSchemes), string.Format(Resources.ImportVariableFrom, name)); //Imported variable from {0}

                Concept concept = new Concept();
                concept.Title   = EDOUtils.UniqueLabel(ConceptScheme.GetConceptTitles(studyUnitModel.ConceptSchemes), Resources.ImportVariable);                            //Imported Variable
                concept.Content = EDOUtils.UniqueLabel(ConceptScheme.GetConceptContents(studyUnitModel.ConceptSchemes), string.Format(Resources.ImportVariableFrom, name)); //Imported variable from {0}
                conceptScheme.Concepts.Add(concept);
                studyUnitModel.ConceptSchemes.Add(conceptScheme);

                foreach (Variable variable in variables)
                {
                    Question question = new Question();
                    question.Title                  = variable.Label;
                    question.ConceptId              = concept.Id;
                    question.Response.TypeCode      = variable.Response.TypeCode;
                    question.Response.CodeSchemeId  = variable.Response.CodeSchemeId;
                    question.VariableGenerationInfo = question.CreateVariableGenerationInfo();
                    studyUnitModel.Questions.Add(question);
                    variable.ConceptId  = concept.Id;
                    variable.QuestionId = question.Id;
                    studyUnitModel.Variables.Add(variable);
                }
            }
            return(true);
        }
Example #13
0
        public static void RenameIds(StudyUnit orgStudyUnit, StudyUnit newStudyUnit)
        {
            // renumber the ID of newStudyUnit to avoid duplication between ID of newStudyUnit ID and orgStudyUnit

            List <string> ids = CollectIds(orgStudyUnit);

            //0. StudyUnit itself
            RenameIds(newStudyUnit, ids);
            //1. Event
            RenameIds(newStudyUnit.Events, ids);
            //2. Member
            RenameIds(newStudyUnit.Members, ids, (result) =>
            {
                Sampling.ChangeMemberId(newStudyUnit.Samplings, result.OldId, result.NewId);
            });

            //3. Organization
            RenameIds(newStudyUnit.Organizations, ids, (result) =>
            {
                Member.ChangeOrganizationId(newStudyUnit.Members, result.OldId, result.NewId);
                Sampling.ChangeMemberId(newStudyUnit.Samplings, result.OldId, result.NewId);
            });
            //4. Abstract
            RenameIds(newStudyUnit.Abstract, ids);
            //5. Coverage
            RenameIds(newStudyUnit.Coverage, ids);
            //6. Funding Agency
            RenameIds(newStudyUnit.FundingInfos, ids);
            RenameIds(newStudyUnit.FundingInfoOrganizations, ids); //Organization of Funding Agency
            ////7.Universe
            RenameIds(newStudyUnit.AllUniverses, ids, (result) =>
            {
                Variable.ChangeUniverseId(newStudyUnit.Variables, result.OldId, result.NewId);
            });
            //8.Sampling
            RenameIds(newStudyUnit.Samplings, ids);
            //9. Concept Scheme
            RenameIds(newStudyUnit.ConceptSchemes, ids);
            //9-1. Concept
            RenameIds(ConceptScheme.GetConcepts(newStudyUnit.ConceptSchemes), ids, (result) =>
            {
                Question.ChangeConceptId(newStudyUnit.Questions, result.OldId, result.NewId);
                Variable.ChangeConceptId(newStudyUnit.Variables, result.OldId, result.NewId);
                Book.ChangeMetaDataId(newStudyUnit.Books, result.OldId, result.NewId);
            });
            //10. Question
            RenameIds(newStudyUnit.Questions, ids, (result) =>
            {
                ControlConstructScheme.ChangeQuestionId(newStudyUnit.ControlConstructSchemes, result.OldId, result.NewId);
                Variable.ChangeQuestionId(newStudyUnit.Variables, result.OldId, result.NewId);
                Book.ChangeMetaDataId(newStudyUnit.Books, result.OldId, result.NewId);
            });
            //11. Category Scheme
            RenameIds(newStudyUnit.CategorySchemes, ids, (result) =>
            {
                CategoryScheme.ChangeCategorySchemeId(newStudyUnit.CategorySchemes, result.OldId, result.NewId);
            });
            //11-1. Category
            RenameIds(CategoryScheme.GetCategories(newStudyUnit.CategorySchemes), ids, (result) =>
            {
                CodeScheme.ChangeCategoryId(newStudyUnit.CodeSchemes, result.OldId, result.NewId);
            });
            //12. Code Scheme
            RenameIds(newStudyUnit.CodeSchemes, ids, (result) =>
            {
                CodeScheme.ChangeCodeSchemeId(newStudyUnit.CodeSchemes, result.OldId, result.NewId);
            });
            //12-1. Code
            RenameIds(CodeScheme.GetCodes(newStudyUnit.CodeSchemes), ids);
            //13. Variable Scheme
            RenameIds(newStudyUnit.VariableScheme, ids);
            //14. Variable
            RenameIds(newStudyUnit.Variables, ids, (result) =>
            {
                DataSet.ChangeVariableId(newStudyUnit.DataSets, result.OldId, result.NewId);
                Book.ChangeMetaDataId(newStudyUnit.Books, result.OldId, result.NewId);
                StatisticsInfo.ChangeVariableId(newStudyUnit.StatisticsInfos, result.OldId, result.NewId);
            });
            //15. Dataset
            RenameIds(newStudyUnit.DataSets, ids, (result) =>
            {
                DataFile.ChangeDataSetId(newStudyUnit.DataFiles, result.OldId, result.NewId);
            });
            //16. Data File
            RenameIds(newStudyUnit.DataFiles, ids);
            //17. Order of Question
            RenameIds(newStudyUnit.ControlConstructSchemes, ids);
            //17-1.Sequence
            RenameIds(ControlConstructScheme.GetSequences(newStudyUnit.ControlConstructSchemes), ids);
            //17-2.Constructs
            RenameIds(ControlConstructScheme.GetConstructs(newStudyUnit.ControlConstructSchemes), ids, (result) =>
            {
                ControlConstructScheme.ChangeControlConstructId(newStudyUnit.ControlConstructSchemes, result.OldId, result.NewId);
            });

            //Related materials
            RenameIds(newStudyUnit.Books, ids);
        }
Example #14
0
        /// <summary>
        /// This method builds up a DdiInstance and writes it to a
        /// valid DDI 3.1. XML file.
        /// </summary>
        public void BuildSomeDdiAndWriteToXml()
        {
            // It is helpful to set some default properties before
            // working with the SDK's model. These two properties
            // determine the default language and agency identifier
            // for every item.
            MultilingualString.CurrentCulture = "en-US";
            VersionableBase.DefaultAgencyId   = "example.org";

            // Start out by creating a new DDIInstance.
            // The DdiInstance can hold StudyUnits, Groups, and ResourcePackages.
            DdiInstance instance = new DdiInstance();

            Instance = instance;
            instance.DublinCoreMetadata.Title.Current = "My First Instance";

            // Since we set the CurrentCulture to "en-US", that last line is
            // equivalent to this next one.
            instance.DublinCoreMetadata.Title["en-US"] = "My First Instance";

            // We can set multiple languages, if we want to.
            instance.DublinCoreMetadata.Title["fr"] = "TODO";

            // Add a ResourcePackage to the DdiInstance. There are three things to do.
            // 1. First, create it.
            // 2. Then, set whatever properties you like. Here, we just set the Title.
            // 3. Add the item to it's parent. In this case, that's the DdiInstance.
            ResourcePackage resourcePackage = new ResourcePackage();

            resourcePackage.DublinCoreMetadata.Title.Current = "RP1";
            instance.AddChild(resourcePackage);

            // Now let's add a ConceptScheme to the ResourcePackage. We'll do this
            // using the same three steps we used to create the ResourcePackage.
            ConceptScheme conceptScheme = new ConceptScheme();

            conceptScheme.ItemName.Current    = "My Concepts";
            conceptScheme.Description.Current = "Just some concepts for testing.";
            resourcePackage.AddChild(conceptScheme);

            // Let's add some Concepts to the ConceptScheme.
            string[] conceptLabels = { "Pet", "Dog", "Cat", "Bird", "Fish", "Monkey" };
            foreach (string label in conceptLabels)
            {
                // Again, for each Concept we create, we want to perform the
                // same three steps as above:
                // 1. instantiate, 2. assign properties, 3. add to parent.
                Concept concept = new Concept();
                concept.Label.Current = label;
                conceptScheme.AddChild(concept);
            }

            // Let's create a collection of questions.
            QuestionScheme questionScheme = new QuestionScheme();

            questionScheme.ItemName.Current = "Sample Questions";
            resourcePackage.QuestionSchemes.Add(questionScheme);


            // First, we can ask for a name. This will just collect textual data.
            Question q1 = new Question();

            q1.QuestionText.Current = "What is your name?";
            q1.ResponseDomains.Add(new TextDomain());
            questionScheme.Questions.Add(q1);

            // Next, we can ask what method of transportation somebody used to get to Minneapolis.
            Question transportationQuestion = new Question();

            transportationQuestion.QuestionText.Current = "How did you get to Minneapolis?";

            // For this question, the respondent will choose from a list of answers.
            // Let's make that list.
            CategoryScheme catScheme = new CategoryScheme();

            resourcePackage.CategorySchemes.Add(catScheme);

            var codeScheme = new CodeList();

            resourcePackage.CodeSchemes.Add(codeScheme);

            // Add the first category and code: Airplane
            Category airplaneCategory = new Category();

            airplaneCategory.Label.Current = "Airplane";
            Code airplaneCode = new Code
            {
                Value    = "0",
                Category = airplaneCategory
            };

            catScheme.Categories.Add(airplaneCategory);
            codeScheme.Codes.Add(airplaneCode);

            // Car
            Category carCategory = new Category();

            carCategory.ItemName.Current = "Car";
            Code carCode = new Code
            {
                Value    = "1",
                Category = carCategory
            };

            catScheme.Categories.Add(carCategory);
            codeScheme.Codes.Add(carCode);

            // Train
            Category trainCategory = new Category();

            trainCategory.ItemName.Current = "Train";
            Code trainCode = new Code
            {
                Value    = "2",
                Category = trainCategory
            };

            catScheme.Categories.Add(trainCategory);
            codeScheme.Codes.Add(trainCode);

            // Now that we have a Category and CodeScheme, we can create
            // a CodeDomain and assign this as the type of data the transportation
            // question will collect.
            CodeDomain codeDomain = new CodeDomain();

            codeDomain.Codes = codeScheme;
            transportationQuestion.ResponseDomains.Add(codeDomain);
            questionScheme.Questions.Add(transportationQuestion);

            // We have created a DdiInstance, a ResourcePackage, some concepts,
            // and some questions.
            //
            // Now what?
            //
            // Let's save all this to a DDI 3.1 XML file.
            //
            // First, we can call EnsureCompliance to make sure
            // our objects have all fields that are required
            // by the DDI 3.1 schemas. If we missed anything,
            // this method will fill in some defaults for us.
            DDIWorkflowSerializer.EnsureCompliance(instance);

            // Now, create the serializer object that will save our items to XML.
            // Setting UseConciseBoundedDescription to false makes sure we
            // write every item, and not just references to items.
            DDIWorkflowSerializer serializer = new DDIWorkflowSerializer();

            serializer.UseConciseBoundedDescription = false;

            // Getting a valid XML representation of our model is just one method call.
            XmlDocument xmlDoc = serializer.Serialize(instance);

            // Finally, save the XML document to a file.
            xmlDoc.Save("sample.xml");
        }
Example #15
0
        /// <summary>
        /// This method builds up a DdiInstance and writes it to a 
        /// valid DDI 3.1. XML file.
        /// </summary>
        public void BuildSomeDdiAndWriteToXml()
        {
            // It is helpful to set some default properties before
            // working with the SDK's model. These two properties
            // determine the default language and agency identifier
            // for every item.
            MultilingualString.CurrentCulture = "en-US";
            VersionableBase.DefaultAgencyId = "example.org";

            // Start out by creating a new DDIInstance.
            // The DdiInstance can hold StudyUnits, Groups, and ResourcePackages.
            DdiInstance instance = new DdiInstance();
            Instance = instance;
            instance.DublinCoreMetadata.Title.Current = "My First Instance";

            // Since we set the CurrentCulture to "en-US", that last line is
            // equivalent to this next one.
            instance.DublinCoreMetadata.Title["en-US"] = "My First Instance";

            // We can set multiple languages, if we want to.
            instance.DublinCoreMetadata.Title["fr"] = "TODO";

            // Add a ResourcePackage to the DdiInstance. There are three things to do.
            // 1. First, create it.
            // 2. Then, set whatever properties you like. Here, we just set the Title.
            // 3. Add the item to it's parent. In this case, that's the DdiInstance.
            ResourcePackage resourcePackage = new ResourcePackage();
            resourcePackage.DublinCoreMetadata.Title.Current = "RP1";
            instance.AddChild(resourcePackage);

            // Now let's add a ConceptScheme to the ResourcePackage. We'll do this
            // using the same three steps we used to create the ResourcePackage.
            ConceptScheme conceptScheme = new ConceptScheme();
            conceptScheme.ItemName.Current = "My Concepts";
            conceptScheme.Description.Current = "Just some concepts for testing.";
            resourcePackage.AddChild(conceptScheme);

            // Let's add some Concepts to the ConceptScheme.
            string[] conceptLabels = { "Pet", "Dog", "Cat", "Bird", "Fish", "Monkey" };
            foreach (string label in conceptLabels)
            {
                // Again, for each Concept we create, we want to perform the
                // same three steps as above:
                // 1. instantiate, 2. assign properties, 3. add to parent.
                Concept concept = new Concept();
                concept.Label.Current = label;
                conceptScheme.AddChild(concept);
            }

            // Let's create a collection of questions.
            QuestionScheme questionScheme = new QuestionScheme();
            questionScheme.ItemName.Current = "Sample Questions";
            resourcePackage.QuestionSchemes.Add(questionScheme);

            // First, we can ask for a name. This will just collect textual data.
            Question q1 = new Question();
            q1.QuestionText.Current = "What is your name?";
            q1.ResponseDomains.Add(new TextDomain());
            questionScheme.Questions.Add(q1);

            // Next, we can ask what method of transportation somebody used to get to Minneapolis.
            Question transportationQuestion = new Question();
            transportationQuestion.QuestionText.Current = "How did you get to Minneapolis?";

            // For this question, the respondent will choose from a list of answers.
            // Let's make that list.
            CategoryScheme catScheme = new CategoryScheme();
            resourcePackage.CategorySchemes.Add(catScheme);

            var codeScheme = new CodeList();
            resourcePackage.CodeSchemes.Add(codeScheme);

            // Add the first category and code: Airplane
            Category airplaneCategory = new Category();
            airplaneCategory.Label.Current = "Airplane";
            Code airplaneCode = new Code
            {
                Value = "0",
                Category = airplaneCategory
            };
            catScheme.Categories.Add(airplaneCategory);
            codeScheme.Codes.Add(airplaneCode);

            // Car
            Category carCategory = new Category();
            carCategory.ItemName.Current = "Car";
            Code carCode = new Code
            {
                Value = "1",
                Category = carCategory
            };
            catScheme.Categories.Add(carCategory);
            codeScheme.Codes.Add(carCode);

            // Train
            Category trainCategory = new Category();
            trainCategory.ItemName.Current = "Train";
            Code trainCode = new Code
            {
                Value = "2",
                Category = trainCategory
            };
            catScheme.Categories.Add(trainCategory);
            codeScheme.Codes.Add(trainCode);

            // Now that we have a Category and CodeScheme, we can create
            // a CodeDomain and assign this as the type of data the transportation
            // question will collect.
            CodeDomain codeDomain = new CodeDomain();
            codeDomain.Codes = codeScheme;
            transportationQuestion.ResponseDomains.Add(codeDomain);
            questionScheme.Questions.Add(transportationQuestion);

            // We have created a DdiInstance, a ResourcePackage, some concepts,
            // and some questions.
            //
            // Now what?
            //
            // Let's save all this to a DDI 3.1 XML file.
            //
            // First, we can call EnsureCompliance to make sure
            // our objects have all fields that are required
            // by the DDI 3.1 schemas. If we missed anything,
            // this method will fill in some defaults for us.
            DDIWorkflowSerializer.EnsureCompliance(instance);

            // Now, create the serializer object that will save our items to XML.
            // Setting UseConciseBoundedDescription to false makes sure we
            // write every item, and not just references to items.
            DDIWorkflowSerializer serializer = new DDIWorkflowSerializer();
            serializer.UseConciseBoundedDescription = false;

            // Getting a valid XML representation of our model is just one method call.
            XmlDocument xmlDoc = serializer.Serialize(instance);

            // Finally, save the XML document to a file.
            xmlDoc.Save("sample.xml");
        }