Skip to content

Instantly share code, notes, and snippets.

@jeremyiverson
Created September 27, 2012 14:00
Show Gist options
  • Save jeremyiverson/3794171 to your computer and use it in GitHub Desktop.
Save jeremyiverson/3794171 to your computer and use it in GitHub Desktop.
Colectica SDK: Find all referencing items. Group different versions of the same item together.
/// <summary>
/// Gets a list of items that reference the item specified by id.
/// This list may contain multiple entries for an individual item if
/// multiple versions of that item reference the base item.
/// We will group these "duplicates" and print each item only once,
/// with a list of matching versions if appropriate.
/// </summary>
/// <param name="id"></param>
public static void PrintReferencingItemInfo(RepositoryClientBase client, IdentifierTriple targetId)
{
SetSearchFacet facet = new SetSearchFacet();
facet.ReverseTraversal = true;
facet.Predicate = RelationshipPredicates.ResourceReference;
// Ask the server for a list of IDs that match our search terms.
Collection<TypedIdTriple> typedIDs = client.SearchTypedSet(targetId, facet);
var allIDs = typedIDs.ToIdentifierCollection();
// Grab descriptions for each item returned.
var descriptions = client.GetRepositoryItemDescriptions(allIDs).OrderBy(x => x.ItemType);
// Group items by their unique identifier.
// Technically this should group by Agency + Identifier, not just Identifier.
// But since our identifiers are globally unique, this is no big deal.
var grouped = descriptions.GroupBy(x => x.Identifier);
Collection<IdentifierTriple> ids = new Collection<IdentifierTriple>();
foreach (var descriptionGroup in grouped)
{
var desc = descriptionGroup.FirstOrDefault();
if (desc == null) continue;
string text = desc.DisplayLabel;
if (string.IsNullOrWhiteSpace(text))
{
text = "Unnamed item";
}
Console.Write(text);
Console.Write(": ");
var allVersions = descriptionGroup.Select(d => d.Version);
var versionStr = string.Join(",", allVersions);
Console.WriteLine(versionStr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment