Newer
Older
var _this = this;
// doiServer = the server which provides doi details from a given doi name.
Maxime Chaillet
committed
this.doiServer = "https://data.datacite.org/";
// 10.5072 is the test esrf doi prefix;
// 10.15151 is the production esrf doi prefix;
this.acceptedPrefix = ["10.5072", "10.15151"];
this.view = view;
Maxime Chaillet
committed
this.experimentalReportController = new ExperimentalReportController();
Maxime Chaillet
committed
this.experimentalReportController.onSuccess.attach(function (sender, data) {
_this.view.renderExperimentalReportData(data);
});
Maxime Chaillet
committed
this.experimentalReportController.onError.attach(function (sender, data) {
console.log("Error when retrieving fileList from SMIS.");
_this.view.renderErrorForExperimentalReport();
});
Maxime Chaillet
committed
Maxime Chaillet
committed
/**
* For unknown reason, datacite can send an incomplete JSON object containing 3
* fields when the doi does not exist. In this case the following code is
* executed. Reloading the page is treated by .fail as it should be.
*/
DOIController.prototype.isValidDOIData = function (doiData) {
// console.log(doiData)
// if (_.keysIn(doiData).length > 3) {
// return _.find(_.keysIn(doiData), function (value) {
// return value === "title";
// }).length > 0;
// }
// return false;
return true;
Maxime Chaillet
committed
};
DOIController.prototype.render = function (doi, data) {
var _this = this;
// Test abnormal length of the json object
if (!_this.isValidDOIData(data)) {
_this.view.renderError("Not Found", _this.origin);
} else {
// Extract data and fill doiData with it
Maxime Chaillet
committed
var doiData = new DoiData();
// var doiData = new DoiData(data.doi.toUpperCase(), _this.doiServer + doi, data.publisher, data.creator, data.publication_year);
var jsonExtractor = new JsonExtractor();
doiData.title = jsonExtractor.getTitle(data);
doiData.doi = jsonExtractor.getDOI(data);
doiData.dataciteLink = _this.doiServer + doi;
doiData.publisher = jsonExtractor.getPublisher(data);
doiData.creators = jsonExtractor.getCreators(data);
doiData.publiclyAccessibleYear = jsonExtractor.getPubliclyAccessibleYear(data);
Maxime Chaillet
committed
doiData.abstract = jsonExtractor.getAbstract(data);
doiData.investigationId = jsonExtractor.getInvestigationId(data);
doiData.resourceType = jsonExtractor.getResourceType(data);
// doiData.mintingYear = jsonExtractor.getMintingYear(data);
Maxime Chaillet
committed
//console.log("minting year = " + doiData.mintingYear);
Maxime Chaillet
committed
doiData.proposalType = jsonExtractor.getFieldFromSubject(data.subjects, "Proposal Type Description");
Maxime Chaillet
committed
doiData.resourceTypeGeneral = jsonExtractor.getResourceTypeGeneral(data);
doiData.sessionDate = jsonExtractor.getSessionDate(data);
Maxime Chaillet
committed
var proposalNames = jsonExtractor.getFieldFromSubject(data.subjects, "Proposal");
var beamlines = jsonExtractor.getFieldFromSubject(data.subjects, "Instrument");
Maxime Chaillet
committed
_this.experimentalReportController.requestFilenameList(proposalNames[0]);
doiData.accessibility = doiData.getAccessibilityStatus(doiData.resourceType, doiData.sessionDate, doiData.publiclyAccessibleYear);
doiData.accessMessage = _this.view.setDataAccessMessage(doiData);
Maxime Chaillet
committed
doiData.citation = _this.createCitation(doiData);
Maxime Chaillet
committed
if (doiData.resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
doiData.beamlineUrls = _this.getBeamlineUrl(beamlines, doiData.sessionDate, doiData.resourceType);
doiData.mintingYear = moment(doiData.sessionDate).year();
}
Maxime Chaillet
committed
if (doiData.resourceType === CONSTANTS.DC_RESOURCE_TYPE) {
doiData.beamlineUrls = _this.getBeamlineUrl(beamlines, doiData.publiclyAccessibleYear, doiData.resourceType);
doiData.mintingYear = doiData.publiclyAccessibleYear;
}
// Create a table containing unique tuples [proposal, beamline, beamlineUrl]
doiData.mergedProposalBeamlines = jsonExtractor.getMergedProposalAndBeamline(proposalNames, beamlines, doiData.beamlineUrls);
// send to dust render
_this.view.renderDOIData(doiData);
}
DOIController.prototype.getData = function (doi) {
var _this = this;
this.view.setDOIInTopBanner(doi);
if (doi.toUpperCase() == "10.15151/ESRF-DC-142893590") {
this.render(doi, ESRF_DC_142893590);
return;
}
if (doi.toUpperCase() == "10.15151/ESRF-DC-142915526") {
this.render(doi, ESRF_DC_142915526);
return;
}
if (this.hasAcceptedPrefix(doi)) {
$.ajax({
headers: {
Accept: "application/vnd.datacite.datacite+json"
},
type: "GET",
timeout: 15000, // triggers timeout when request pends longer than
// 5000ms
url: this.doiServer + doi,
Maxime Chaillet
committed
beforeSend: function () {
_this.view.setLoading("Retrieving data from Server");
},
complete: function () {
_this.view.setLoading(false);
}
})
.done(function (data) {
if (data) {
}
}).fail(function (jqXHR, textStatus, errorThrown) {
_this.view.renderError(jqXHR, errorThrown, _this.origin);
_this.view.setLoading(false);
});
} else {
_this.view.renderError("Bad Prefix", _this.origin);
}
Maxime Chaillet
committed
/**
* Checks whether the requested doi prefix is an esrf prefix
*
* @param{string} doi The DOI number
* @return true if the prefix is an esrf prefix, false otherwise
*
*/
DOIController.prototype.hasAcceptedPrefix = function (doi) {
var prefix = doi.split('/')[0];
return (_.findIndex(this.acceptedPrefix, function (o) {
return prefix == o;
}) != -1);
Maxime Chaillet
committed
};
// Selects what will be displayed in the main id div
DOIController.prototype.displayMainContent = function (doi) {
var isDOIProvided = false;
if (this.origin === "welcome-page") {
dust.render('welcome_tpl', {}, function (err, out) {
$("#main").html(out);
});
}
if (this.origin === "index") {
this.getData(doi);
}
Maxime Chaillet
committed
};
/**
* Set the origin from where the DOI controller is created. This affects the
* content of error messages for example depending on whether they are displayed
* in the "welcome-page" content or the "doi landing page" content.
*
* @param {string}
* origin The origin page this call is made from
*/
DOIController.prototype.setOrigin = function (origin) {
this.origin = origin;
Maxime Chaillet
committed
};
Maxime Chaillet
committed
/**
Maxime Chaillet
committed
* Get the corresponding url for a given beamline name
* @param {array} beamlines The beamlines
* @param {momentJS} date The date the experiment was performed.
* @param {String} resourceType the data resource type
* @return {array} an array containing urls of the beamLine(s).
*/
DOIController.prototype.getBeamlineUrl = function (beamlines, date, resourceType) {
var result = [];
beamlines.forEach(function (beamline) {
Maxime Chaillet
committed
if (date) {
if (resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
date = moment(date);
}
if (resourceType === CONSTANTS.DC_RESOURCE_TYPE) {
date = moment(date, "YYYY");
}
Maxime Chaillet
committed
for (var i = 0; i < BEAMLINEURL.length; i++) {
var startDate = moment(BEAMLINEURL[i].startDate);
var endDate = moment(BEAMLINEURL[i].endDate);
if (BEAMLINEURL[i].name.toLowerCase() === beamline.toLowerCase() && date >= startDate && date <= endDate) {
foundUrl = BEAMLINEURL[i].url;
break;
}
Maxime Chaillet
committed
if (foundUrl === "") {
result.push("noLink");
} else {
result.push(foundUrl);
}
Maxime Chaillet
committed
result.push("noLink");
Maxime Chaillet
committed
};
Maxime Chaillet
committed
/**
* Create the citation for the current work. Citation proposes a recommendation
* to the reader on how to cite this work.
Maxime Chaillet
committed
* @param {object} doiData the doiData object we have constructed from JSON datacite
* @return {string} the citation to be displayed
Maxime Chaillet
committed
DOIController.prototype.createCitation = function (doiData) {
// Authors sent in json can be in several format. The following call find
// first name and last name when possible.
Maxime Chaillet
committed
var citation = new Citation(doiData);
if (!citation.isBuildable) {
return "The citation could not be generated.";
} else {
Maxime Chaillet
committed
var authorInCitation = citation.getAuthorsForCitation(doiData.creators);
var doiRegistrationYear = citation.getDOIRegistrationYear(doiData);
var fullCitation = authorInCitation + ' (' + doiRegistrationYear + '). ' + doiData.title + '. ' + doiData.publisher + ' (ESRF). ' +
" <a href='https://doi.esrf.fr/" + doiData.doi + "'> doi:" + doiData.doi.toUpperCase() + "</a>";
return fullCitation;
}