Commit 14cd3832 authored by Maxime Chaillet's avatar Maxime Chaillet

Refactor the code to remove global function which are extracting information...

Refactor the code to remove global function which are extracting information from the data json object. Create the class jsonExtractor for that purpose.
parent b684fd36
......@@ -12,6 +12,7 @@
<script src="src/beamlineUrl.js"></script>
<script src="src/doicontroller.js"></script>
<script src="src/doiview.js"></script>
<script src="src/jsonExtractor.js"></script>
<!-- Using grunt-wiredep, the proper js files from bower components are included below -->
<!-- bower:js -->
......
This diff is collapsed.
......@@ -28,14 +28,14 @@ DOIController.prototype.getData = function(doi) {
success : function(doiData) {
if (doiData) {
var jsonExtractor = new JsonExtractor();
// extract dates for doi landing page
extractDates(doiData);
// extract specific fields from doiData
extractFields(doiData);
//extractInvestigationId from doiData.doi
extractFieldsFromDoi(doiData);
// create citation
createCitation(doiData);
extractDates(doiData);
jsonExtractor.extractFromSubject("Proposal Type Description",doiData,"proposalType");
jsonExtractor.extractFromSubject("Proposal",doiData,"proposalNumber");
jsonExtractor.extractFromSubject("instrument",doiData,"beamline");
jsonExtractor.extractInvestigationId(doiData);
_this.createCitation(doiData);
// Retrieve the URL corresponding to the beamline name.
var experimentalDate = new Date(doiData.date_collected + "T00:00+00:00");
......@@ -123,16 +123,54 @@ DOIController.prototype.findsUrl = function(beamline, date){
return result;
};
/**
* Create the citation for the current work. Citation proposes a recommendation to the reader on how to cite this work.
*@param {string} data: the data object, it is a reference
*/
DOIController.prototype. createCitation = function(data) {
// Authors sent in json can be in several format. The following call find first name and last name when possible.
// It writes a message in the console in case it fails identifying them in which case the string containing first name and last name will
// be displayed as it came from json.
var authorList = _.transform(data.creator, function(result, obj) {
var author = null;
if (obj.givenName) {
author = new Author();
author.setFamilyName(obj.familyName);
author.setFirstName(obj.givenName);
result.push(author.getTitleCaseFamilyName() + ' ' + author.getFirstNameInitials());
} else {
// Here first name and last name are in a single string field creator.name
author = new Author();
author.setSingleName(obj.name);
if (!author.getRawFamilyName() || !author.getRawFirstName() ){
result.push(obj.name);
} else {
// FirstName and family name are not in a single string
result.push(author.getTitleCaseFamilyName() + " " + author.getFirstNameInitials());
}
}
},[]);
var authorString = _.join(authorList,', ');
var citation = authorString +
' (' +
new Date(data.date_registered).getFullYear() +
'). ' +
data.title +
'. ' +
data.publisher +
' (ESRF). ' +
" <a href='https://doi.esrf.fr/" + data.DOI + "'> doi:" + data.doi.toUpperCase() + "</a>";
data.citation = citation;
};
//HELPER FUNCTIONS
// Finds a regular expression in a text
// @regExp : the regExp, must not be surrounded by ""
// @text : a string to match against
// @return: the position where the pattern was found
function findRegExpInText(regExp, text) {
return text.search(regExp);
}
//HELPER FUNCTIONS
// extract dates as required for the DOI landing page
function extractDates(data){
......@@ -155,74 +193,9 @@ function extractDates(data){
}
// Extract beamline, proposal number and proposal type from the data json object
// Add the corresponding fields in the data object
// @data: the data object, it is a reference
function extractFields(data) {
_.forEach(data.subject[0], function(value){
switch (value.subject_scheme ) {
case "Proposal Type Description":
data.proposalType = value.text;
break;
case "Proposal":
data.proposalNumber = value.text;
break;
case "instrument":
data.beamline = value.text;
}
});
}
//Extract investigationId from the data.doi field as received from the json response.
//Add the corresponding fields in the data object
//@data: the data object, it is a reference
function extractFieldsFromDoi(data) {
//extract the investigationId from the doi.
var regExp = RegExp(/\/\w*-*[Ee][Ss][Rr][Ff]-[Ee][Ss]-(\d+)$/);
data.investigationId = regExp.exec(data.doi)[1];
}
// Create the citation for the current work. Citation proposes a recommendation to the reader
// on how to cite this work.
//@data: the data object, it is a reference
function createCitation(data) {
// Authors sent in json can be in several format. The following call find first name and last name when possible.
// It writes a message in the console in case it fails identifying them in which case the string containing first name and last name will
// be displayed as it came from json.
var authorList = _.transform(data.creator, function(result, obj) {
var author = null;
if (obj.givenName) {
author = new Author();
author.setFamilyName(obj.familyName);
author.setFirstName(obj.givenName);
result.push(author.getTitleCaseFamilyName() + ' ' + author.getFirstNameInitials());
} else {
// Here first name and last name are in a single string field creator.name
author = new Author();
author.setSingleName(obj.name);
if (!author.getRawFamilyName() || !author.getRawFirstName() ){
result.push(obj.name);
} else {
// FirstName and family name are not in a single string
result.push(author.getTitleCaseFamilyName() + " " + author.getFirstNameInitials());
}
}
},[]);
var authorString = _.join(authorList,', ');
var citation = authorString +
' (' +
new Date(data.date_registered).getFullYear() +
'). ' +
data.title +
'. ' +
data.publisher +
' (ESRF). ' +
" <a href='https://doi.esrf.fr/" + data.DOI + "'> doi:" + data.doi.toUpperCase() + "</a>";
data.citation = citation;
}
function JsonExtractor() {
}
/**
* Extract investigationId from the data.doi field as received from the json
* response. Add the corresponding fields in the data object
*
* @param {string}
* data The data object reference which is sent to the dustjs
* template
*/
JsonExtractor.prototype.extractInvestigationId = function(data) {
var regExp = RegExp(/\/\w*-*[Ee][Ss][Rr][Ff]-[Ee][Ss]-(\d+)$/);
data.investigationId = regExp.exec(data.doi)[1];
};
/**
* Extract fields value (beamline, proposal number and proposal type) from the
* data json object. Handles the possibility that the this fields are not found.
* Store the found fields in a new field which eases the use in the template.
*
* @param {object}
* data The data object reference which is sent to the dustjs
* template
* @param {string}
* field The text field which is searched
* @param {string}
* newField The new field name the result is stored in.
*/
JsonExtractor.prototype.extractFromSubject = function(field, data, newField) {
var counter = 0;
var found = false;
while (counter < data.subject[0].length && found === false) {
if (data.subject[0][counter].subject_scheme === field) {
data[newField] = data.subject[0][counter].text;
found = true;
}
counter++;
}
if (!found) {
data[newField] = "Not available";
}
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment