Created
January 10, 2013 03:20
-
-
Save jeremyiverson/4499147 to your computer and use it in GitHub Desktop.
Colectica SDK: General samples from Copenhagen training
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 Algenta.Colectica.Model; | |
using Algenta.Colectica.Model.Ddi; | |
using Algenta.Colectica.Model.Ddi.Serialization; | |
using Algenta.Colectica.Model.Repository; | |
using Algenta.Colectica.Model.Utility; | |
using Algenta.Colectica.Repository.Client; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Xml; | |
namespace CopenhagenSamples | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// It's useful to set these static properties at the beginning of your program. | |
VersionableBase.DefaultAgencyId = "int.example"; | |
MultilingualString.CurrentCulture = "en-US"; | |
//BuildSomeDdi(); | |
//ReadSomeDdi(); | |
//RepositoryDemo(); | |
//ImportCodesFromSpreadsheets(); | |
//AddSomeThingsToExistingResourcePackage(); | |
MakeAVariable(); | |
Console.WriteLine("Press Enter to exit"); | |
Console.ReadLine(); | |
} | |
static void BuildSomeDdi() | |
{ | |
// First, create a DDIInstance and set the Title in two languages. | |
DdiInstance instance = new DdiInstance(); | |
instance.DublinCoreMetadata.Title.Current = "My First Instance"; | |
instance.DublinCoreMetadata.Title["da-DK"] = "I don't know."; | |
// Add a ResourcePackage to the instance. | |
ResourcePackage resourcePackage = GetResourcePackageForDemo(); | |
instance.ResourcePackages.Add(resourcePackage); | |
// EnsureCompliance makes sure the item has all the | |
// necessary information to be DDI 3.1 compliant. | |
DDIWorkflowSerializer.EnsureCompliance(instance); | |
// Create a serializer to write the DDI XML. | |
DDIWorkflowSerializer serializer = new DDIWorkflowSerializer(); | |
serializer.UseConciseBoundedDescription = false; | |
XmlDocument doc = serializer.Serialize(instance); | |
// Save the file to disk. | |
doc.Save("sample.xml"); | |
} | |
static void ReadSomeDdi() | |
{ | |
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
// Create a deserializer to read the DDI XML file and put it into the object model. | |
DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer(); | |
DdiInstance instance = deserializer.LoadDdiFile(Path.Combine(directory, "sample.xml")); | |
Console.WriteLine("Resource Packages: " + instance.ResourcePackages.Count); | |
// Use LINQ to Find a category with the label "Car". | |
Category carCat = (from rp in instance.ResourcePackages | |
from cs in rp.CategorySchemes | |
from cat in cs.Categories | |
where cat.Label.ContainsKey("en-US") && cat.Label["en-US"] == "Car" | |
select cat).FirstOrDefault(); | |
if (carCat != null) | |
{ | |
Console.WriteLine("Found the car"); | |
} | |
else | |
{ | |
Console.WriteLine("We did not find it"); | |
} | |
} | |
static RepositoryClientBase GetClient() | |
{ | |
// Get a client that can be used to interact with Colectica Repository. | |
RepositoryConnectionInfo connectionInfo = new RepositoryConnectionInfo() | |
{ | |
Url = "localhost", | |
AuthenticationMethod = RepositoryAuthenticationMethod.Windows, | |
TransportMethod = RepositoryTransportMethod.NetTcp | |
}; | |
WcfRepositoryClient client = new WcfRepositoryClient(connectionInfo); | |
return client; | |
} | |
static void RepositoryDemo() | |
{ | |
RepositoryClientBase client = GetClient(); | |
client.CreateRepository("dk.dst", "Statistics Denmark"); | |
RepositoryInfo info = client.GetRepositoryInfo(); | |
foreach (var repo in info.Repositories) | |
{ | |
Console.WriteLine(repo.Key + " - " + repo.Value); | |
} | |
} | |
static void ImportCodesFromSpreadsheets() | |
{ | |
ResourcePackage codeResources = GetFromSpreadsheet(); | |
var client = GetClient(); | |
// A visitor can be "accepted" by an instance of one of the | |
// DDI item types. | |
// In this case, the visitor will gather every item underneath | |
// the codeResources ResourcePackage, so we can add each | |
// item to the Repository. | |
ItemGathererVisitor gatherer = new ItemGathererVisitor(); | |
codeResources.Accept(gatherer); | |
// Register all the items with the Repository. | |
CommitOptions options = new CommitOptions(); | |
client.RegisterItems(gatherer.FoundItems, options); | |
} | |
static void AddSomeThingsToExistingResourcePackage() | |
{ | |
var client = GetClient(); | |
// Search for an existing CategoryScheme. | |
SearchFacet facet = new SearchFacet(); | |
facet.ItemTypes.Add(DdiItemType.CategoryScheme); | |
facet.ResultOrdering = SearchResultOrdering.MetadataRank; | |
facet.SearchTargets.Add(DdiStringType.Label); | |
facet.Cultures.Add("en-US"); | |
SearchResponse response = client.Search(facet); | |
SearchResult result = response.Results[0]; | |
// Retrieve the item. | |
CategoryScheme categoryList = client.GetItem(result.Identifier, result.AgencyId, result.Version) | |
as CategoryScheme; | |
// Add some custom fields to the category list. | |
CustomField validFromField = new CustomField(); | |
validFromField.StringValue = "2008"; | |
validFromField.Title.Current = "Valid From"; | |
CustomField validToField = new CustomField(); | |
validToField.StringValue = "2010"; | |
validToField.Title.Current = "Valid To"; | |
categoryList.CustomFields.Add(validFromField); | |
categoryList.CustomFields.Add(validToField); | |
categoryList.Version++; | |
// Search for a specific ResourcePackage. | |
SearchFacet rpFacet = new SearchFacet(); | |
rpFacet.ItemTypes.Add(DdiItemType.ResourcePackage); | |
rpFacet.SearchTerms.Add("Demographic"); | |
SearchResponse rpResponse = client.Search(rpFacet); | |
SearchResult rpResult = rpResponse.Results[0]; | |
ResourcePackage rp = client.GetItem(rpResult.CompositeId, | |
ChildReferenceProcessing.Instantiate) | |
as ResourcePackage; | |
// Use the new version of the category list in this ResourcePackage. | |
rp.ReplaceChild(result.CompositeId, categoryList); | |
rp.Version++; | |
// Register the new versions of the category list and the ResourcePackage. | |
client.RegisterItem(categoryList, new CommitOptions()); | |
client.RegisterItem(rp, new CommitOptions()); | |
} | |
static void MakeAVariable() | |
{ | |
var client = GetClient(); | |
Variable variable = new Variable(); | |
variable.ItemName.Current = "TRAN"; | |
variable.Label.Current = "Transportation Method Used"; | |
variable.RepresentationType = RepresentationType.Code; | |
// Search for a CodeList to use with this variable. | |
SearchFacet facet = new SearchFacet(); | |
facet.ItemTypes.Add(DdiItemType.CodeScheme); | |
facet.SearchTerms.Add("Transportation"); | |
facet.SearchLatestVersion = true; | |
var response = client.Search(facet); | |
CodeScheme codeScheme = client.GetItem(response.Results[0].CompositeId) | |
as CodeScheme; | |
variable.CodeRepresentation.Codes = codeScheme; | |
// Save the variable to the Repository. | |
client.RegisterItem(variable, new CommitOptions()); | |
} | |
static ResourcePackage GetFromSpreadsheet() | |
{ | |
// We'll fake it for now. | |
return GetResourcePackageForDemo(); | |
} | |
static ResourcePackage GetResourcePackageForDemo() | |
{ | |
// Create a new ResourcePackage | |
ResourcePackage resourcePackage = new ResourcePackage(); | |
resourcePackage.DublinCoreMetadata.Title.Current = "My Resource Package"; | |
// Create a new concept list. | |
ConceptScheme conceptScheme = new ConceptScheme(); | |
conceptScheme.Label.Current = "My Concepts"; | |
resourcePackage.ConceptSchemes.Add(conceptScheme); | |
string[] conceptLabels = { "Education", "Educational attainment", "school", "university" }; | |
foreach (string label in conceptLabels) | |
{ | |
Concept concept = new Concept(); | |
concept.Label.Current = label; | |
conceptScheme.Concepts.Add(concept); | |
} | |
// Category and Code Lists | |
CategoryScheme catScheme = new CategoryScheme(); | |
catScheme.Label.Current = "Transportation Categories"; | |
resourcePackage.CategorySchemes.Add(catScheme); | |
CodeScheme codeScheme = new CodeScheme(); | |
resourcePackage.CodeSchemes.Add(codeScheme); | |
// Add the first category and code: Airplane | |
Category airplaneCategory = new Category(); | |
airplaneCategory.Label.Current = "Airplane"; | |
Code airplaneCode = new Code(); | |
airplaneCode.Value = "0"; | |
airplaneCode.Category = airplaneCategory; | |
catScheme.Categories.Add(airplaneCategory); | |
codeScheme.Codes.Add(airplaneCode); | |
// Car | |
Category carCategory = new Category(); | |
carCategory.Label.Current = "Car"; | |
Code carCode = new Code | |
{ | |
Value = "1", | |
Category = carCategory | |
}; | |
catScheme.Categories.Add(carCategory); | |
codeScheme.Codes.Add(carCode); | |
// Train | |
Category trainCategory = new Category(); | |
trainCategory.Label.Current = "Train"; | |
Code trainCode = new Code | |
{ | |
Value = "2", | |
Category = trainCategory | |
}; | |
catScheme.Categories.Add(trainCategory); | |
codeScheme.Codes.Add(trainCode); | |
return resourcePackage; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment