Hello world code: nightly publication import feed

Edited

Introduction

This example code was used in a live demo at the Symplectic User Technical Conference in October 2012 to demonstrate an approach to implementing a nightly publication data import feed from a custom database represented by a single CSV file (a data table file compatible with Excel).

The audience was encouraged to replace the CSV reading code with code that reads data items from whatever database technology you are using.

The code is stripped down to be the minimal code necessary to achieve its basic goal. It is not intended for production use, as it does not contain error handling, or any other features generally expected in production quality programming. You are encouraged to re-use the code as a starting point, with your goal being a properly crafted robust program confirming with the Best Practices section of the API Technical Guide.

Import publications (C#)

//set the endpoint base URL
string apiBaseUrl = "https://localhost:8781/publications-api/v3.7.16";

//prepare credentials for API calls
var client = new WebClient { Credentials = new NetworkCredential("john", "99 red balloons") };

//for the purposes of this demo, the file C:\publications-data.csv is assumed to contain
//the following content:
//Publication  ID,Title,Bibliographic Authors,Resolved Authors
//1,Frog,"Smith, JD; Jones PB",30
//2,Person,"Smith, JD; Jones PB",30
//3,Lamb,"Smith, JD; Jones PB","30, 31"
//4,Tiger,"Smith, JD; Jones PB",

//open the data CSV file
using(CsvReader csvReader = new CsvReader(new StreamReader(@"C:\publications-data.csv"), true, ',')) {
    //loop through the rows in the CSV file
    while(csvReader.ReadNextRecord()) {
        //get the publication data for this row
        var publication = new { 
            MyID = csvReader[0], 
            Title = csvReader[1], 
            BibliographicAuthors = csvReader[2].Split(';').Select(value => value.Trim()), 
            ResolvedAuthors = csvReader[3].Split(',').Where(value => !string.IsNullOrEmpty(value)).Select(int.Parse) 
        };

        //prepare a new publication import instruction for the API in the form
        //of an appropriate XML document
        XElement importXml = new XElement(apiNamespace + "import-record", new XAttribute("type-id", 5), 
            new XElement(apiNamespace + "native", 
                new XElement(apiNamespace + "field", new XAttribute("name", "title"), 
                    new XElement(apiNamespace + "text", publication.Title)), 
                new XElement(apiNamespace + "field", new XAttribute("name", "authors"), 
                    new XElement(apiNamespace + "people", from author in publication.BibliographicAuthors
                        select new XElement(apiNamespace + "person", 
                            new XElement(apiNamespace + "last-name", author))))));

        //send the document to the record import API operation
        //we have decided to "impersonate" the registered data source "manual", meaning that the
        //data we upload will appear as manual records in the Elements system. This isn't recommended, but
        //will suffice for the purposes of this demo.
        client.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
        client.UploadData(apiBaseUrl + string.Format("/publication/records/manual/{0}", publication.MyID), "PUT", Encoding.UTF8.GetBytes(importXml.ToString()));

        //for each resolved author, import a relationship from the publication to the author
        foreach(var resolvedAuthor in publication.ResolvedAuthors) {
            //create a new relationship import XML document
            XElement importRelationshipXml = new XElement(apiNamespace + "import-relationship", 
                new XElement(apiNamespace + "from-object", string.Format("publication(source-manual,pid-{0})", publication.MyID)), 
                new XElement(apiNamespace + "to-object", string.Format("user({0})", resolvedAuthor)), 
                new XElement(apiNamespace + "type-name", "publication-user-authorship"));

            //send the document to the relationship import operation
            client.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
            client.UploadData(apiBaseUrl + "/relationships", "POST", Encoding.UTF8.GetBytes(importRelationshipXml.ToString()));
        }
    }
}

Was this article helpful?

Sorry about that! Care to tell us more?

Thanks for the feedback!

There was an issue submitting your feedback
Please check your connection and try again.