Skip to content

Instantly share code, notes, and snippets.

@jeremyiverson
Created August 2, 2012 03:02
Show Gist options
  • Save jeremyiverson/3232816 to your computer and use it in GitHub Desktop.
Save jeremyiverson/3232816 to your computer and use it in GitHub Desktop.
Colectica SDK: Find all variables in a study's data files.
/// <summary>
/// Find all variables in a study's data files, and output some information about them.
/// </summary>
public void FindVariablesInStudyDataFiles(Guid identifier, string agency, long version)
{
IdentifierTriple studyID = new IdentifierTriple(identifier, version, agency);
var client = GetClient();
// First, get a list of all PhysicalInstances in the study.
var physicalInstanceIDs = GetReferencedItems(client, studyID, DdiItemType.PhysicalInstance);
// Now, get a list of all the variables in each PhysicalInstance.
Collection<IdentifierTriple> allVariableIDs = new Collection<IdentifierTriple>();
foreach (var physicalInstanceID in physicalInstanceIDs)
{
var variableIDs = GetReferencedItems(client, physicalInstanceID, DdiItemType.Variable);
foreach (var id in variableIDs) allVariableIDs.Add(id);
}
// Grab the descriptions of each variable. The description gives us the
// variable's name, label, and description.
var allVariableDescriptions = client.GetRepositoryItemDescriptions(allVariableIDs);
// If we like, we cant output information about all the variables.
// We'll just write things out to the console, but we could just
// as easily write out a text file or an HTML file.
Console.WriteLine(string.Format("Listing {0} variables.", allVariableDescriptions.Count));
foreach (RepositoryItemMetadata item in allVariableDescriptions)
{
Console.WriteLine(string.Format("{0} - {1}", item.ItemName["en-US"], item.Label["en-US"]));
}
}
/// <summary>
/// Get all items of the specified type that are directly referenced by
/// the item with the specified ID.
/// </summary>
Collection<IdentifierTriple> GetReferencedItems(RepositoryClientBase client, IdentifierTriple subjectId, Guid objectType)
{
GraphSearchFacet facet = new GraphSearchFacet()
{
TargetItem = subjectId,
UseDistinctResultItem = true,
};
facet.ItemTypes.Add(objectType);
return client.GetRelationshipBySubject(facet).ToIdentifierCollection();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment