/// <summary>
        /// Reads a text file with one mapping per line:
        ///   [VariableName]|[QuestionName]
        ///
        /// </summary>
        /// <remarks>
        /// See below for a method that automatically maps variables and questions based on matching names,
        /// without the need for an extra file to specify the mapping.
        /// </remarks>
        /// <param name="filePath"></param>
        /// <param name="variableSchemeId"></param>
        /// <param name="questionSchemeId"></param>
        /// <param name="onlyTest"></param>
        static void MapVariablesToQuestions(string filePath, IdentifierTriple variableSchemeId, IdentifierTriple questionSchemeId, bool onlyTest)
        {
            var client = RepositoryIntro.GetClient();

            VariableScheme vs = client.GetItem(variableSchemeId, ChildReferenceProcessing.Populate) as VariableScheme;
            QuestionScheme qs = client.GetItem(questionSchemeId, ChildReferenceProcessing.Populate) as QuestionScheme;

            // Read each line of the mapping file.
            int matches = 0;

            foreach (string line in File.ReadLines(filePath))
            {
                // Grab the variable name and question name.
                string[] parts = line.Split(new char[] { '|' });
                if (parts.Length != 2)
                {
                    Console.WriteLine("Invalid line: " + line);
                    continue;
                }

                string variableName = parts[0];
                string questionName = parts[1];

                // Grab the corresponding variable and question objects.
                Variable variable = vs.Variables.SingleOrDefault(v => v.ItemName.Current == variableName);
                Question question = qs.Questions.SingleOrDefault(q => q.ItemName.Current == questionName);
                if (variable != null && question != null)
                {
                    // Add the question as SourceQuestion of the variable.
                    variable.SourceQuestions.Add(question);
                    variable.Version++;

                    Console.WriteLine(string.Format("Assigning {0} to {1}", question.ItemName.Current, variable.ItemName.Current));

                    if (!onlyTest)
                    {
                        client.RegisterItem(variable, new CommitOptions());
                    }

                    matches++;
                }
                else
                {
                    Console.WriteLine(string.Format("No match for {0} or {1}", variableName, questionName));
                }
            }

            vs.Version++;
            if (!onlyTest)
            {
                client.RegisterItem(vs, new CommitOptions());
            }

            Console.WriteLine("Done. Found " + matches.ToString() + " matches.");
        }
        /// <summary>
        /// Maps Variables to Questions where the name of the Variable matches the name of
        /// the Question.
        /// </summary>
        /// <param name="variableSchemeId"></param>
        /// <param name="questionSchemeId"></param>
        /// <param name="onlyTest"></param>
        static void MapVariablesToQuestionsAuto(IdentifierTriple variableSchemeId, IdentifierTriple questionSchemeId, bool onlyTest)
        {
            var client = RepositoryIntro.GetClient();

            CommitOptions commitOptions = new CommitOptions();

            VariableScheme vs = client.GetItem(variableSchemeId, ChildReferenceProcessing.Populate) as VariableScheme;
            QuestionScheme qs = client.GetItem(questionSchemeId, ChildReferenceProcessing.Populate) as QuestionScheme;

            // Grab all variable names and question names.
            var variableNameList = vs.Variables.Select(v => v.ItemName.Current.Substring(2)).ToList();
            var questionNameList = qs.Questions.Select(q => q.ItemName.Current).ToList();

            int matches = 0;

            foreach (string varName in variableNameList)
            {
                string foundQuestionName = questionNameList.Where(q => string.Compare(q, varName, true) == 0).FirstOrDefault();
                if (foundQuestionName != null)
                {
                    // If there is a question with the same name as this variable,
                    // grab the Question and Variable objects, and assign the question
                    // as a SourceQuestion.
                    Variable variable = vs.Variables.SingleOrDefault(v => v.ItemName.Current == varName);
                    Question question = qs.Questions.SingleOrDefault(q => q.ItemName.Current == foundQuestionName);

                    if (variable != null && question != null)
                    {
                        variable.SourceQuestions.Add(question);
                        variable.Version++;
                        Console.WriteLine(string.Format("Assigning {0} to {1}", question.ItemName.Current, variable.ItemName.Current));

                        if (!onlyTest)
                        {
                            client.RegisterItem(variable, commitOptions);
                        }

                        matches++;
                    }
                    else
                    {
                        Console.WriteLine(string.Format("No match for {0} or {1}", varName, foundQuestionName));
                    }
                }
            }

            vs.Version++;
            if (!onlyTest)
            {
                client.RegisterItem(vs, commitOptions);
            }

            Console.WriteLine("Done. Found " + matches.ToString() + " matches.");
        }
Пример #3
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");
        }
Пример #4
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");
        }