Skip to content
doicontroller.js 8.91 KiB
Newer Older
function DOIController(view) {
    var _this = this;
    // doiServer = the server which provides doi details from a given doi name.
    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;
    this.experimentalReportController = new ExperimentalReportController();
    this.experimentalReportController.onSuccess.attach(function (sender, data) {
        _this.view.renderExperimentalReportData(data);
    });
    this.experimentalReportController.onError.attach(function (sender, data) {
        console.log("Error when retrieving fileList from SMIS.");
        _this.view.renderErrorForExperimentalReport();
    });
/**
 * 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;

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
        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);
        doiData.abstract = jsonExtractor.getAbstract(data);
        doiData.investigationId = jsonExtractor.getInvestigationId(data);
        doiData.resourceType = jsonExtractor.getResourceType(data);
        
        
        // doiData.mintingYear = jsonExtractor.getMintingYear(data);
        //console.log("minting year = " + doiData.mintingYear);
        doiData.proposalType = jsonExtractor.getFieldFromSubject(data.subjects, "Proposal Type Description");
        doiData.resourceTypeGeneral = jsonExtractor.getResourceTypeGeneral(data);
        doiData.sessionDate = jsonExtractor.getSessionDate(data);
        var proposalNames = jsonExtractor.getFieldFromSubject(data.subjects, "Proposal");
        var beamlines = jsonExtractor.getFieldFromSubject(data.subjects, "Instrument");
        _this.experimentalReportController.requestFilenameList(proposalNames[0]);
        doiData.accessibility = doiData.getAccessibilityStatus(doiData.resourceType, doiData.sessionDate, doiData.publiclyAccessibleYear);
        doiData.accessMessage = _this.view.setDataAccessMessage(doiData);
        doiData.citation = _this.createCitation(doiData);
        if (doiData.resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
            doiData.beamlineUrls = _this.getBeamlineUrl(beamlines, doiData.sessionDate, doiData.resourceType);
            doiData.mintingYear = moment(doiData.sessionDate).year();
        }
        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;
    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,
                _this.view.setLoading("Retrieving data from Server");
            },
            complete: function () {
                _this.view.setLoading(false);
            }
        })

            .done(function (data) {
                if (data) {
                    _this.render(doi, 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);
    }
/**
 * 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);
// 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);
    }
/**
 * 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;
* 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) {
Maxime Chaillet's avatar
Maxime Chaillet committed
    var result = [];
    beamlines.forEach(function (beamline) {
        if (date) {
            if (resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
                date = moment(date);
            }
            if (resourceType === CONSTANTS.DC_RESOURCE_TYPE) {
                date = moment(date, "YYYY");
            }
Maxime Chaillet's avatar
Maxime Chaillet committed
            var foundUrl = "";
            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;
                }
            if (foundUrl === "") {
                result.push("noLink");
            } else {
                result.push(foundUrl);
            }

Maxime Chaillet's avatar
Maxime Chaillet committed
        } else {
Maxime Chaillet's avatar
Maxime Chaillet committed
        }
    });
    return result;
 * Create the citation for the current work. Citation proposes a recommendation
 * to the reader on how to cite this work.
 * @param {object} doiData the doiData object we have constructed from JSON datacite
 * @return {string} the citation to be displayed
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.

    if (!citation.isBuildable) {
        return "The citation could not be generated.";

    } else {
        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>";
Maxime Chaillet's avatar
Maxime Chaillet committed
};