Created
August 2, 2012 19:39
-
-
Save jeremyiverson/3240009 to your computer and use it in GitHub Desktop.
Colectica SDK: Import DDI Variables and Concepts from a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To import a CSV using the above code: | |
ImportVariablesAndConcepts importer = new ImportVariablesAndConcepts(); | |
importer.Import(@"d:\tmp\FileToImportInDesigner.csv", "example.org", "en-US", "ImportTest"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Algenta.Colectica.Model.Repository; | |
using Algenta.Colectica.Repository.Client; | |
using Algenta.Colectica.Model.Ddi; | |
using Algenta.Colectica.Model; | |
using Algenta.Colectica.Model.Utility; | |
using System.IO; | |
using System.Collections.ObjectModel; | |
namespace ColecticaSdkSamples.Tools | |
{ | |
public class ImportVariablesAndConcepts | |
{ | |
/// <summary> | |
/// Imports variables and concepts from a CSV file with the following format: | |
/// Column 1: Variable name | |
/// Column 2: Variable label | |
/// Column 3: Concept label | |
/// | |
/// New Variable and Concept items are created and added to new VariableSchemes | |
/// and ConceptSchemes, respectively. | |
/// | |
/// These Schemes are added to a newly created ResourcePackage. | |
/// | |
/// All these items are then published to the specified repository. From there, | |
/// they can be referenced by other projects. | |
/// </summary> | |
/// <param name="fileName">The name of the CSV file from which to import.</param> | |
/// <param name="agency">The DDI agency identifier to assign to newly created items.</param> | |
/// <param name="language">The language of items in the file.</param> | |
/// <param name="label">The label to use for the created ResourcePackage, | |
/// VariableScheme, and ConceptScheme.</param> | |
public void Import(string fileName, string agency, string language, string label) | |
{ | |
// Setting this property ensures all newly instantiated items use the | |
// specified agency identifier. | |
VersionableBase.DefaultAgencyId = agency; | |
// Setting this property let's use use the Property.Current syntax | |
// for assigning strings. | |
MultilingualString.CurrentCulture = language; | |
// Create the parent items: ResourcePackage, VariableScheme, and ConceptScheme. | |
ResourcePackage resourcePackage = new ResourcePackage(); | |
resourcePackage.DublinCoreMetadata.Title.Current = label; | |
VariableScheme variableScheme = new VariableScheme(); | |
variableScheme.Label.Current = label; | |
ConceptScheme conceptScheme = new ConceptScheme(); | |
conceptScheme.Label.Current = label; | |
resourcePackage.VariableSchemes.Add(variableScheme); | |
resourcePackage.ConceptSchemes.Add(conceptScheme); | |
// Read each line of the file | |
foreach (string line in File.ReadAllLines(fileName)) | |
{ | |
// Skip lines with no content. | |
if (string.IsNullOrWhiteSpace(line)) | |
{ | |
continue; | |
} | |
// Grab the three parts of each line. | |
// Note: if any of the fields contain commas, a more robust | |
// method of parsing the CSV should be used. | |
string[] parts = line.Split(new char[] { ',' }); | |
if (parts.Length != 3) | |
{ | |
Console.WriteLine("Skipping line: " + line); | |
continue; | |
} | |
string variableName = parts[0]; | |
string variableLabel = parts[1]; | |
string conceptLabel = parts[2]; | |
// Create the concept and add it to the ConceptScheme. | |
Concept concept = null; | |
if (!string.IsNullOrWhiteSpace(conceptLabel)) | |
{ | |
// Before adding the concept, let's see if we already | |
// created one with the same label. If so, we can just | |
// reuse that. | |
concept = conceptScheme.Concepts | |
.SingleOrDefault(c => c.Label.Current == conceptLabel); | |
// If we didn't find an existing concept with this label, | |
// create it now. | |
if (concept == null) | |
{ | |
Console.WriteLine("Creating concept: " + conceptLabel); | |
concept = new Concept(); | |
concept.Label.Current = conceptLabel; | |
conceptScheme.Concepts.Add(concept); | |
} | |
} | |
// Create the variable and add it to the VariableScheme. | |
Console.WriteLine("Creating variable: " + variableName + " - " + variableLabel); | |
Variable variable = new Variable(); | |
variable.ItemName.Current = variableName; | |
variable.Label.Current = variableLabel; | |
if (concept != null) | |
{ | |
variable.Concept = concept; | |
} | |
variableScheme.Variables.Add(variable); | |
} | |
// Get a list of all the items we have just created, so we can add | |
// each of them to the repository. | |
ItemGathererVisitor gatherer = new ItemGathererVisitor(); | |
resourcePackage.Accept(gatherer); | |
// Add each item to the repository. | |
Console.WriteLine("Adding " + gatherer.FoundItems.Count + " items to the repository."); | |
var client = GetClient(); | |
var itemsToAdd = GetRepositoryItems(gatherer.FoundItems); | |
client.RegisterItems(itemsToAdd, new CommitOptions()); | |
} | |
public static RepositoryClientBase GetClient() | |
{ | |
// The WcfRepositoryClient takes a configation object | |
// detailing how to connect to the Repository. | |
var connectionInfo = new RepositoryConnectionInfo() | |
{ | |
// TODO Replace this with the hostname of your Colectica Repository | |
Url = "localhost", | |
AuthenticationMethod = RepositoryAuthenticationMethod.Windows, | |
TransportMethod = RepositoryTransportMethod.NetTcp, | |
}; | |
// Create the client object, passing in the connection information. | |
WcfRepositoryClient client = new WcfRepositoryClient(connectionInfo); | |
return client; | |
} | |
/// <summary> | |
/// Converts a collection of IVersionable items into RepositoryItems, which | |
/// can be published to Repository with one call. | |
/// </summary> | |
/// <param name="items"></param> | |
/// <returns></returns> | |
static Collection<RepositoryItem> GetRepositoryItems(Collection<IVersionable> items) | |
{ | |
DdiItemFormatter formatter = new DdiItemFormatter(); | |
Collection<RepositoryItem> repositoryItems = new Collection<RepositoryItem>(); | |
Collection<Note> emptyNotes = new Collection<Note>(); | |
foreach (IVersionable item in items) | |
{ | |
RepositoryItem ri = new RepositoryItem() | |
{ | |
CompositeId = item.CompositeId, | |
Item = formatter.GetXmlRepresentation(item).OuterXml, | |
ItemType = item.ItemType, | |
//IsDepricated | |
IsPublished = item.IsPublished, | |
//IsProvisional = true, | |
Notes = emptyNotes, | |
VersionDate = item.VersionDate, | |
VersionRationale = item.VersionRationale, | |
VersionResponsibility = item.VersionResponsibility, | |
Tag = item | |
}; | |
repositoryItems.Add(ri); | |
} | |
return repositoryItems; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment