public override IEnumerable <IVersionable> Build(IEnumerable <IVersionable> ws) { Collection <IVersionable> allItems = getAllItems(); var sus = allItems.OfType <StudyUnit>().ToList(); for (var i = 0; i < sus.Count(); i++) { foreach (var dc in sus[i].DataCollections) { var rp = new ResourcePackage(); rp.DublinCoreMetadata.Title = dc.ItemName; allItems.Add(rp); sus[i].AddChild(rp); rp.AddChild(dc); } } return(allItems); }
/// <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"); }
public void CompareWithRepository() { var client = Utility.GetClient(); var facet = new SearchFacet() { SearchLatestVersion = true }; facet.ItemTypes.Add(DdiItemType.StudyUnit); var response = client.Search(facet); foreach (var result in response.Results) { var su = client.GetItem( result.CompositeId, ChildReferenceProcessing.PopulateLatest) as StudyUnit; foreach (var dc in su.DataCollections) { try { var scope = scopes[dc.ItemName.Best]; scope.su = su; scope.rp = su.ResourcePackages.Where(x => x.ItemName.Best == dc.ItemName.Best).FirstOrDefault(); } catch (KeyNotFoundException) { } } } foreach (var scope in scopes) { if (scope.Value.rp != default(ResourcePackage)) { continue; } var wsRps = workingSet.OfType <ResourcePackage>().Where(x => string.Compare( x.DublinCoreMetadata.Title.Best, scope.Value.name ) == 0 ); if (wsRps.Any()) { scope.Value.rp = wsRps.First(); var bubbleOut = false; foreach (var g in workingSet.OfType <Algenta.Colectica.Model.Ddi.Group>()) { foreach (var su in g.StudyUnits) { if (su.DataCollections.Count(x => x.ItemName.Best == scope.Key) > 0) { scope.Value.su = su; var gatherer = new ItemGathererVisitor(); g.Accept(gatherer); toBeAdded.AddRange(gatherer.FoundItems); bubbleOut = true; } if (bubbleOut) { break; } } if (bubbleOut) { break; } } } else { var rp = new ResourcePackage(); rp.DublinCoreMetadata.Title["en-GB"] = scope.Key; scope.Value.su.AddChild(rp); toBeAdded.Add(scope.Value.su); rp.AddChild(scope.Value.su.DataCollections.First(x => x.ItemName.Best == scope.Key)); scope.Value.rp = rp; } } var ccgs = workingSet.OfType <ControlConstructGroup>(); if (ccgs.Count() > 1) { facet.ItemTypes.Clear(); facet.SearchTargets.Clear(); facet.ItemTypes.Add(DdiItemType.QuestionConstruct); facet.SearchTargets.Add(DdiStringType.UserId); foreach (var ccg in ccgs) { var qcs = ccg.GetChildren().OfType <QuestionActivity>().ToList(); foreach (var qc in ccg.GetChildren().OfType <QuestionActivity>()) { facet.SearchTerms.Clear(); facet.SearchTerms.Add(qc.UserIds.First().Identifier); response = client.Search(facet); if (response.Results.Count > 1) { Console.WriteLine("{0} question constrcuts found during CCG syncing for the question '{1}'", response.Results.Count, qc.UserIds.First().Identifier); } else if (response.Results.Count < 1) { Console.WriteLine("No question constructs were found for the CCG syncing matching '{0}'", qc.UserIds.First().Identifier); } else { var remote_qc = client.GetItem(response.Results.First().CompositeId) as IVersionable; if (remote_qc != default(IVersionable)) { ccg.ReplaceChild(qc.CompositeId, remote_qc); } } } } } var vgs = workingSet.OfType <VariableGroup>(); if (vgs.Count() > 0) { facet.ItemTypes.Clear(); facet.ItemTypes.Add(DdiItemType.Variable); foreach (var vg in vgs) { foreach (var variable in vg.GetChildren().OfType <Variable>()) { facet.SearchTerms.Clear(); facet.SearchTargets.Clear(); bool closer_id_found = false; foreach (var user_id in variable.UserIds) { if (user_id.Type == "closer:id") { closer_id_found = true; facet.SearchTerms.Add(user_id.Identifier); facet.SearchTargets.Add(DdiStringType.UserId); break; } } if (!closer_id_found) { facet.SearchTerms.Add(variable.ItemName.Best); facet.SearchTargets.Add(DdiStringType.Name); } response = client.Search(facet); if (response.Results.Count > 1) { Console.WriteLine("{0} variables found during variable group syncing for the variable '{1}:{2}'", response.Results.Count, name, variable.ItemName.Best); } else if (response.Results.Count < 1) { Console.WriteLine("No variables were found for the variable grouping syncing matching '{0}:{1}'", name, variable.ItemName.Best); } else { var remote_variable = client.GetItem(response.Results.First().CompositeId) as IVersionable; if (remote_variable != default(IVersionable)) { vg.ReplaceChild(variable.CompositeId, remote_variable); } } } } } var progress = new ParallelProgressMonitor(scopes.Count); Parallel.ForEach <KeyValuePair <string, Scope> >(scopes, scope => { string text = String.Format("{0}: Comparing {1}", name, scope.Value.name); progress.StartThread( Thread.CurrentThread.ManagedThreadId, text ); scope.Value.Compare(); progress.FinishThread( Thread.CurrentThread.ManagedThreadId, text.PadRight(40, '-') + "> done." + String.Format("{0} compared.", scope.Value.counter[Counters.Compared]).PadLeft(16) + String.Format("{0} updated.", scope.Value.counter[Counters.Updated]).PadLeft(16) + String.Format("{0} added.", scope.Value.counter[Counters.Added]).PadLeft(16) + String.Format("{0} removed.", scope.Value.counter[Counters.Removed]).PadLeft(16) ); }); }