Skip to content

Commit

Permalink
Merge pull request #1416 from thostetler/1415-orcid-bugfixes
Browse files Browse the repository at this point in the history
1415 orcid bugfixes
  • Loading branch information
ehenneken authored Mar 27, 2018
2 parents 90f72a3 + 88d5aec commit 6202d94
Show file tree
Hide file tree
Showing 7 changed files with 946 additions and 630 deletions.
9 changes: 9 additions & 0 deletions grunt/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ module.exports = function (grunt) {
return dest + src.replace('FileSaver.min', 'index');
}
},
{
cwd: 'node_modules/file-saver',
src: 'FileSaver.min.js',
dest: 'src/libs/file-saver/',
expand: true,
rename: function (dest, src) {
return dest + src.replace('FileSaver.min', 'index');
}
},
{
src: 'bower_components/lodash/dist/*',
dest: 'src/libs/lodash/',
Expand Down
75 changes: 44 additions & 31 deletions src/js/modules/orcid/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,25 @@ define([
if (model) {
model.set('orcid', actions);
}

if (rInfo.children) {
_.forEach(rInfo.children, function (putcode) {
var childModel = _.find(self.hiddenCollection.models, function (m) {
return m.get('_work').getPutCode() === putcode;
});

if (childModel) {
self.removeModel(childModel);
}
});
}
}

if (counter === 0) {
self.trigger('orcid-update-finished');
}
});

recInfo.fail(function (data) {
counter -= 1;

Expand Down Expand Up @@ -206,10 +219,6 @@ define([
}
});

if (counter === 0) {
self.trigger('orcid-update-finished', docs);
}

return docs;
};

Expand Down Expand Up @@ -247,7 +256,7 @@ define([

if (exIds.bibcode === wIds.bibcode || doi) {
m.set({
'source_name': w.getSourceName(),
'source_name': w.sources.join('; '),
'_work': w
});
}
Expand Down Expand Up @@ -423,6 +432,34 @@ define([
return $dd.promise();
};

WidgetClass.prototype.removeModel = function (model) {
var idx = model.resultsIndex;
this.hiddenCollection.remove(model);
var models = this.hiddenCollection.models;
_.forEach(_.rest(models, idx), function (m) {
m.set('resultsIndex', m.get('resultsIndex') - 1);
m.set('indexToShow', m.get('indexToShow') - 1);
});

var showRange = this.model.get('showRange');
var range = _.range(showRange[0], showRange[1] + 1);
var visible = [];
_.forEach(range, function (i) {
if (models[i] && models[i].set) {
models[i].set('visible', true);
models[i].resultsIndex = i;
models[i].set('resultsIndex', i);
models[i].set('indexToShow', i + 1);
visible.push(models[i]);
}
});
this.hiddenCollection.reset(models);
this.collection.reset(visible);

// reset the total number of papers
this.model.set('totalPapers', models.length);
};

WidgetClass.prototype.onAllInternalEvents = function(ev, arg1, arg2) {
if (ev === 'childview:OrcidAction') {
var self = this;
Expand Down Expand Up @@ -450,7 +487,7 @@ define([

model.set({
orcid: self._getOrcidInfo(recInfo),
'source_name': work.getSourceName()
'source_name': work.sources.join('; ')
});

self.trigger('orcidAction:' + action, model);
Expand Down Expand Up @@ -496,31 +533,7 @@ define([

// Remove entry from collection after delete
if (self.orcidWidget) {
var idx = model.resultsIndex;
self.hiddenCollection.remove(model);
var models = self.hiddenCollection.models;
_.forEach(_.rest(models, idx), function (m) {
m.set('resultsIndex', m.get('resultsIndex') - 1);
m.set('indexToShow', m.get('indexToShow') - 1);
});

var showRange = self.model.get('showRange');
var range = _.range(showRange[0], showRange[1] + 1);
var visible = [];
_.forEach(range, function (i) {
if (models[i] && models[i].set) {
models[i].set('visible', true);
models[i].resultsIndex = i;
models[i].set('resultsIndex', i);
models[i].set('indexToShow', i + 1);
visible.push(models[i]);
}
});
self.hiddenCollection.reset(models);
self.collection.reset(visible);

// reset the total number of papers
self.model.set('totalPapers', models.length);
self.removeModel(model);
} else {
// reset orcid actions
model.set('orcid', self._getOrcidInfo({}));
Expand Down
155 changes: 144 additions & 11 deletions src/js/modules/orcid/orcid_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ define([

request.done(function (profile) {
_.forEach(cache, function (promise) {
promise.resolve(new Profile(profile));
promise.resolve(self._reconcileProfileWorks(profile));
});
});

Expand All @@ -384,6 +384,85 @@ define([
});
},

/**
* Reconcile the works contained in the incoming profile.
* Since it's possible for an ORCiD record to contain multiple sources,
* we have to figure out the best one to pick.
*
* The user can selected a "preferred" source, but since we can only match
* on items that have enough information (bibcode, doi, etc), we have to search
* through them all to find the best one.
*
* @param {object} rawProfile - the incoming profile
* @returns {Profile} - the new profile (with reconciled works)
*/
_reconcileProfileWorks: function (rawProfile) {
/*
1. Source is ADS
2. Has Bibcode
3. Has DOI
4. Other
*/
var self = this;
var profile = new Profile(rawProfile);
var works = _.map(profile.getWorksDeep(), function (work, idx) {
var w;

// only operate on arrays > 1
if (work.length > 1) {
var workWithBibcode, workWithDoi;
_.forEach(work, function (item) {

// check if the source is ADS
var isADS = self.isSourcedByADS(item);

// grab an array of external ids ['bibcode', 'doi', '...']
var exIds = item.getExternalIdType();
var hasBibcode = exIds.indexOf('bibcode') > -1;
var hasDoi = exIds.indexOf('doi') > -1;

// if it's sourced by ADS, use that one and break out of loop
if (isADS) {
w = item;
return false;
}

// grab the first one that has a bibcode
if (hasBibcode && !workWithBibcode) {
workWithBibcode = item;
}

// grab the first one that has a doi
if (hasDoi && !workWithDoi) {
workWithDoi = item;
}
});

// w will be defined if we found an ADS-sourced work
// otherwise, set the work accordingly below
if (!w && workWithBibcode) {
w = workWithBibcode;
} else if (!w && workWithDoi) {
w = workWithDoi;
} else if (!w) {
w = work[0];
}

// set the work's list of sources based on the full list from orcid
w.sources = _.map(work, function (_w) {
return _w.getSourceName();
});
}

// take the first work if we haven't found an array to process
return w ? w : work[0];
});

// set the new works
profile.setWorks(works);
return profile;
},

/**
* Retrieves user profile
* Must have scope: /orcid-profile/read-limited
Expand Down Expand Up @@ -643,12 +722,27 @@ define([
});

// on fail, reject the promises
prom.fail(function () {
self.addCache = _.reduce(self.addCache, function (res, entry) {
entry.promise.state() === 'pending' ?
entry.promise.reject() : res.push(entry);
return res;
}, []);
// this should receive a list of ids which we can finish up with
prom.fail(function (ids) {
var args = arguments;
_.forEach(ids, function (id) {

// find the cache entry
var idx = _.findIndex(self.addCache, { id: id });
if (idx >= 0) {

// grab reference to promise
var promise = self.addCache[idx].promise;

// remove entry from cache
self.addCache.splice(idx, idx + 1);

// if it is still pending, reject it now
if (promise.state() === 'pending') {
promise.reject.apply(promise, args);
}
}
});
});
},

Expand Down Expand Up @@ -710,9 +804,9 @@ define([
}, {});

$dd.resolve(obj);
}, function () {
}, function (xhr) {
self.setDirty();
$dd.reject.apply($dd, arguments);
$dd.reject.apply($dd, [xhr.cacheIds].concat(arguments));
});

return $dd.promise();
Expand Down Expand Up @@ -1006,8 +1100,7 @@ define([
* unset (-1) so that it won't be counted as an orcid record.
*
*/
var querySuccess = function () {
var ids = _.flatten(arguments);
var querySuccess = function (ids) {

// Update each orcid record with identifier info gained from ADS
_.each(db, function (v, key) {
Expand All @@ -1021,6 +1114,7 @@ define([
}
});

self._combineDatabaseWorks(db);
finishUpdate(db);
};

Expand All @@ -1047,6 +1141,41 @@ define([
return self.dbUpdatePromise.promise();
},

/**
* Looks at the identifier of the work and attempts to
* detect if a bibcode has a child within the other entries
* of the database.
*
* @param {object} db - the database object
* @returns {object} db - the update database object
*/
_combineDatabaseWorks: function (db) {

// loop through each entry of the database
_.forEach(db, function (data, identifier) {

// we can only do this for entries with data and bibcodes
if (_.isUndefined(data) || _.isUndefined(data.bibcode)) {
return true;
}

// remove 'identifier:' from front of key
var key = identifier.split(':')[1];

// add an children property to the current (parent entry)
_.forEach(db, function (entry, subKey) {

// excluding our parent, see if the key matches the bibcode
if (entry.bibcode === key && subKey !== identifier) {
data.children = data.children || [];
data.children.push(entry.putcode);
}
});
});

return db;
},

/**
* Creates a metadata object based on the work that is passed in that
* helps with understanding the record's relationship with ADS. Figures
Expand Down Expand Up @@ -1102,6 +1231,10 @@ define([
}
out.putcode = rec.putcode;
out.bibcode = rec.bibcode;

if (rec.children) {
out.children = rec.children;
}
}
};

Expand Down
Loading

0 comments on commit 6202d94

Please sign in to comment.