public void RewriteCCS() { MultilingualString.CurrentCulture = "en-GB"; VersionableBase.DefaultAgencyId = "example.org"; var client = RepositoryIntro.GetClient(); var instance = client.GetItem(new Guid("b9ee3aa5-5bc5-43ed-a24e-f560abb30801"), "example.org", 2) as DdiInstance; //DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer(); //var instance = deserializer.LoadDdiFile(@"D:\Downloads\filesforMinneapolis\filesforMinneapolis\bcs08v08.xml"); GraphPopulator populator = new GraphPopulator(client); populator.ChildProcessing = ChildReferenceProcessing.Populate; instance.Accept(populator); var resourcePackage = instance.ResourcePackages[0]; var instrument = instance.ResourcePackages[0].DataCollections[0].Instruments[0]; //var topLevelSequence = client.GetLatestItem(new Guid("ceaa9acf-2b2f-4c41-b298-b9f419412586"), "cls") // as CustomSequenceActivity; var topLevelSequence = instrument.Sequence; var moduleSequences = topLevelSequence.Activities; foreach (CustomSequenceActivity module in moduleSequences.OfType <CustomSequenceActivity>()) { DirtyItemGatherer gatherer = new DirtyItemGatherer(true); module.Accept(gatherer); var allChildren = gatherer.DirtyItems; ControlConstructScheme ccs = new ControlConstructScheme(); ccs.ItemName.Copy(module.ItemName); foreach (var child in allChildren.OfType <ActivityBase>()) { ccs.ControlConstructs.Add(child); } //client.RegisterItem(ccs, new CommitOptions()); resourcePackage.ControlConstructSchemes.Add(ccs); } DDIWorkflowSerializer serializer = new DDIWorkflowSerializer(); serializer.UseConciseBoundedDescription = false; var doc = serializer.Serialize(instance); doc.Save(@"d:\ColecticaOutput\bcsv8.xml"); }
/// <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> /// Counts the number of Categories with the same label that are /// found in a particular ResourcePackage in the Repository. /// For each unique category label, output the number of categories with that label. /// This is useful for determining whether harmonizing a repository's Category items /// would be a worthwhile effort. /// </summary> /// <remarks> /// This will provide output like this: /// Yes: 1 /// No: 1 /// Don't know: 12 /// Refused: 12 /// </remarks> public void CountUniqueCategoryLabels() { MultilingualString.CurrentCulture = "en-GB"; var client = RepositoryIntro.GetClient(); // To search within a certain set of items, we create a SetSearchFacet object. // We can set some properties on this object to tell the repository what // items we want to find. SetSearchFacet facet = new SetSearchFacet(); // Find all Category items. facet.ItemTypes.Add(DdiItemType.Category); // Search under the item with the provided identification. // This method returns a collection of identifiers of all matching items. var categoryIDs = client.SearchTypedSet( new IdentifierTriple(new Guid("e92ac0d9-9f2f-4891-9c42-75bfeafc6d23"), 3, "example.org"), facet); // The identifiers by themselves don't give us much information. // Ask the repository for descriptions of all the categories. // This is a very fast call, compared to retrieving the fully populated Category objects, // and it has the information we need: each category's name, label, and description. var categoryDescriptions = client.GetRepositoryItemDescriptions(categoryIDs.ToIdentifierCollection()); // Group the categories by label. The GroupBy method returns a list of lists, looking something like this: // "Yes" -> [cat1Desc, cat2Desc, cat3Desc] // "No" -> [cat4Desc, cat5Desc, cat6Desc] // ... var groupedCategories = categoryDescriptions.GroupBy(cat => cat.Label.Current); // For each unique category label, output the number of categories with that label. foreach (var group in groupedCategories.OrderBy(g => g.Count())) { Console.WriteLine(group.Key + ": " + group.Count().ToString()); } }
static void ExtractAndCollapseMissingCategories() { MultilingualString.CurrentCulture = "en-GB"; VersionableBase.DefaultAgencyId = "cls"; string[] missingCategories = { "Refusal", "Don't Know", "Item not applicable", "Schedule not applicable", "Not applicable", }; var client = RepositoryIntro.GetClient(); //CategoryScheme newMissingScheme = new CategoryScheme(); //newMissingScheme.ItemName.Current = "Missing"; Dictionary <string, Category> categoryMap = new Dictionary <string, Category>(); //foreach (string catLabel in missingCategories) //{ // Category cat = new Category(); // cat.IsMissing = true; // cat.Label.Current = catLabel; // newMissingScheme.Categories.Add(cat); // categoryMap.Add(catLabel, cat); // client.RegisterItem(cat, new CommitOptions()); //} //client.RegisterItem(newMissingScheme, new CommitOptions()); CategoryScheme newMissingScheme = client.GetItem(new Guid("12aaf470-953d-4ebe-9d51-15de00a97921"), "cls", 1, ChildReferenceProcessing.Populate) as CategoryScheme; foreach (var cat in newMissingScheme.Categories) { categoryMap.Add(cat.Label.Current, cat); } // Get all codeSchemes in the repository, so we can update the category references // to the new, harmonized categories that we created above. //SetSearchFacet facet = new SetSearchFacet(); //facet.ItemTypes.Add(DdiItemType.CodeScheme); //var codeSchemeIDs = client.SearchTypedSet(new IdentifierTriple(new Guid("e92ac0d9-9f2f-4891-9c42-75bfeafc6d23"), 3, "ucl.ac.uk"), facet); //var codeSchemes = client.GetItems(codeSchemeIDs.ToIdentifierCollection()); ResourcePackage resourcePackage = client.GetItem(new IdentifierTriple(new Guid("e92ac0d9-9f2f-4891-9c42-75bfeafc6d23"), 3, "ucl.ac.uk")) as ResourcePackage; resourcePackage.CategorySchemes.Add(newMissingScheme); //foreach (CodeScheme cs in resourcePackage.CodeSchemes) //{ // client.PopulateItem(cs, false, ChildReferenceProcessing.Populate); // foreach (Code code in cs.Codes) // { // if (categoryMap.ContainsKey(code.Category.Label.Current)) // { // var category = categoryMap[code.Category.Label.Current]; // code.Category = category; // } // } // cs.Version++; // client.RegisterItem(cs, new CommitOptions()); //} // TODO go through all CategorySchemes and remove the missing categories. // Codes will point to the new categories instead. // Also, rename the category schemes based on the new contents. foreach (CategoryScheme cs in resourcePackage.CategorySchemes) { if (cs == newMissingScheme) { continue; } client.PopulateItem(cs, false, ChildReferenceProcessing.Populate); var toRemove = cs.Categories.Where(cat => categoryMap.ContainsKey(cat.Label.Current)).ToList(); foreach (var remove in toRemove) { cs.Categories.Remove(remove); } cs.ItemName.Current = string.Join(", ", cs.Categories .Select(cat => cat.Label.Current) .Take(Math.Min(cs.Categories.Count, 3)) .ToArray()); cs.Version++; client.RegisterItem(cs, new CommitOptions()); } // Don't forget to publish the new ResourcePackage. resourcePackage.Version++; client.RegisterItem(resourcePackage, new CommitOptions()); }