public LoadTVLinking(string _filepath)
        {
            Init(_filepath);
            if (vs == default(VariableScheme))
            {
                var client = Utility.GetClient();
                var facet  = new SearchFacet();
                facet.ItemTypes.Add(DdiItemType.VariableScheme);
                facet.SearchTargets.Add(DdiStringType.Name);
                facet.SearchTerms.Add("Topic Variable Groups");
                facet.SearchLatestVersion = true;
                SearchResponse response = client.Search(facet);

                var graphPopulator = new GraphPopulator(client)
                {
                    ChildProcessing = ChildReferenceProcessing.PopulateLatest,
                };
                //graphPopulator.TypesToPopulate.Add(DdiItemType.Variable);
                graphPopulator.TypesToPopulate.Add(DdiItemType.VariableGroup);

                if (response.Results.Count == 1)
                {
                    vs = client.GetItem(
                        response.Results[0].CompositeId,
                        ChildReferenceProcessing.PopulateLatest) as VariableScheme;
                    vs.Accept(new GraphPopulator(client));
                }
            }
        }
        public override IEnumerable <IVersionable> Build(IEnumerable <IVersionable> ws)
        {
            Collection <IVersionable> allItems = getAllItems();

            var rp  = allItems.OfType <ResourcePackage>().First();
            var qcs = new ControlConstructScheme();

            qcs.ItemName.Add("en-GB", "Topic Question Construct Groups");
            qcs.UserIds.Add(new UserId("closerid", "topics-ccs-000001"));
            var vs = new VariableScheme();

            vs.ItemName.Add("en-GB", "Topic Variable Groups");
            vs.UserIds.Add(new UserId("closerid", "topics-vs-000001"));
            rp.ControlConstructSchemes.Add(qcs);
            rp.VariableSchemes.Add(vs);
            allItems.Add(qcs);
            allItems.Add(vs);

            var concept_lookup_q = new Dictionary <Concept, ControlConstructGroup>();
            var concept_lookup_v = new Dictionary <Concept, VariableGroup>();

            foreach (var concept in allItems.OfType <Concept>().ToList())
            {
                var qcg = new ControlConstructGroup();
                qcg.TypeOfGroup = "ConceptGroup";
                qcg.Concept     = concept;
                qcg.Label.Add("en-GB", concept.Label.Best + " Question Construct Group");
                qcg.ItemName.Add("en-GB", concept.ItemName.Best);
                qcg.UserIds.Add(new UserId("closerid", "topics-qcg-" + concept.ItemName.Best.ToLower()));

                concept_lookup_q[concept] = qcg;

                foreach (var parent_concept in concept.SubclassOf)
                {
                    concept_lookup_q[parent_concept].ChildGroups.Add(qcg);
                }

                qcs.ControlConstructGroups.Add(qcg);
                allItems.Add(qcg);

                var vg = new VariableGroup();
                vg.TypeOfGroup = "ConceptGroup";
                vg.Concept     = concept;
                vg.Label.Add("en-GB", concept.Label.Best + " Variable Group");
                vg.ItemName.Add("en-GB", concept.ItemName.Best);

                concept_lookup_v[concept] = vg;

                foreach (var parent_concept in concept.SubclassOf)
                {
                    concept_lookup_v[parent_concept].ChildGroups.Add(vg);
                }

                vs.VariableGroups.Add(vg);
                allItems.Add(vg);
            }

            return(allItems);
        }
        /// <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.");
        }
        /// <summary>
        /// Create a simple VariableScheme and register it with the Repository.
        /// </summary>
        public void RegisterSimpleVariableScheme()
        {
            MultilingualString.CurrentCulture = "en-US";
            VersionableBase.DefaultAgencyId   = "example.org";

            // Create a VariableScheme.
            VariableScheme variableScheme = new VariableScheme();

            variableScheme.ItemName.Current = "My Variables";

            // Add a Variable.
            Variable variable = new Variable();

            variable.ItemName.Current = "var1";
            variableScheme.Variables.Add(variable);

            // Grab a client for our Repository.
            var client = GetClient();

            // Register the item.
            client.RegisterItem(variableScheme, new CommitOptions());
            client.RegisterItem(variable, new CommitOptions());
        }
        public static IEnumerable <IVersionable> FinishedAllBuilds(bool clear_vs_reference = true)
        {
            if (vs == default(VariableScheme))
            {
                return(new List <IVersionable>());
            }
            else
            {
                var gthr = new ItemGathererVisitor();
                gthr.TypesToFind.Add(DdiItemType.VariableScheme);
                gthr.TypesToFind.Add(DdiItemType.VariableGroup);
                vs.Accept(gthr);

                var foundItems = gthr.FoundItems;

                if (clear_vs_reference)
                {
                    vs = default(VariableScheme);
                }

                return(foundItems);
            }
        }
        /// <summary>
        /// Create a simple VariableScheme and register it with the Repository.
        /// </summary>
        public void RegisterSimpleVariableScheme()
        {
            MultilingualString.CurrentCulture = "en-US";
            VersionableBase.DefaultAgencyId = "example.org";

            // Create a VariableScheme.
            VariableScheme variableScheme = new VariableScheme();
            variableScheme.ItemName.Current = "My Variables";

            // Add a Variable.
            Variable variable = new Variable();
            variable.ItemName.Current = "var1";
            variableScheme.Variables.Add(variable);

            // Grab a client for our Repository.
            var client = GetClient();

            // Register the item.
            client.RegisterItem(variableScheme, new CommitOptions());
            client.RegisterItem(variable, new CommitOptions());
        }