openaire-library/claims/claim-utils/service/searchOrcid.service.ts

182 lines
6.6 KiB
TypeScript

import {Injectable} from '@angular/core';
import {URLSearchParams} from '@angular/http';
import {Http, Response} from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import{EnvProperties} from '../../../utils/properties/env-properties';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import { ClaimResult} from '../claimEntities.class';
@Injectable()
export class SearchOrcidService {
constructor( private http: Http ) {}
searchOrcidAuthor (term: string, authorIds: string[], authors, properties:EnvProperties, addId):any {
console.info("In searchOrcidAuthor: "+term);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = properties.searchOrcidURL + term+'/record';
let key = url;
return this.http.get(url, { headers: headers })
.map(res => res.json()['person'])
.do(res => console.log(res))
.map(res => [res['name']['given-names'],
res['name']['family-name'],
res['name']])
.do(res => console.log(res))
.map(res => this.parseOrcidAuthor(res, authorIds, authors, addId));
}
searchOrcidAuthors (term: string, authorIds: string[],
properties:EnvProperties):any {
console.info("In search Orcid authors for keyword: "+term);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = properties.searchOrcidURL+'search?defType=edismax&q='+term+'&qf=given-name^1.0+family-name^2.0+other-names^1.0+credit-name^1.0&start=0&rows=10';
let key = url;
return this.http.get(url, { headers: headers })
.map(res => res.json()['result'])
.map(res => this.parseOrcidAuthors(res, authorIds));
}
searchOrcidPublications (id: string, properties:EnvProperties, parse:boolean = false):any {
console.info("In search Orcid publications for author: "+id);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url =properties.searchOrcidURL+id+'/works';
let key = url;
return this.http.get(url, { headers: headers })
.map(res => res.json()['group'])
.do(res => console.log(res))
.map(request => (parse?this.parse(request):request));
//.map(res => res['orcid-work']);
}
parseOrcidAuthor (data: any, authorIds: string[],authors, addId):any {
if(data[2] != null) {
if(addId){
authorIds.push(data[2].path);
}
var author ={};
author['id']=data[2].path;
if(data[0] != null) {
author['authorGivenName']=data[0].value;
} else {
author['authorGivenName']="";
}
if(data[1] != null) {
author['authorFamilyName']=data[1].value;
} else {
author['authorFamilyName']="";
}
authors.push(author);
return true;
}
return false;
}
parseOrcidAuthors (data: any, authorIds: string[]):any {
let ret: boolean = false;
let mydata: any;
let length: number;
if(data != null) {
length = data.length!=undefined ? data.length : 1;
for(let i=0; i<length; i++) {
mydata = data.length!=undefined ? data[i] : data;
if(mydata.hasOwnProperty("orcid-identifier")) {
authorIds.push(mydata['orcid-identifier'].path);
// if(mydata.hasOwnProperty("orcid-bio")) {
// if(mydata['orcid-bio'].hasOwnProperty("personal-details")) {
// if(mydata['orcid-bio']['personal-details'].hasOwnProperty("given-names")) {
// authorGivenNames.push(mydata['orcid-bio']['personal-details']['given-names'].value);
// } else {
// authorGivenNames.push("");
// }
//
// if(mydata['orcid-bio']['personal-details'].hasOwnProperty("family-name")) {
// authorFamilyNames.push(mydata['orcid-bio']['personal-details']['family-name'].value);
// } else {
// authorFamilyNames.push("");
// }
// }
// }
ret = true;
}
}
}
return ret;
}
parse(response):ClaimResult[]{
var results:ClaimResult[] = [];
for(var i=0; i<response.length; i++){
var item=response[i];
var result:ClaimResult = new ClaimResult();
result.DOI = null;
result.url = null;
result.publisher = null;
result.journal = null;
if(item['work-summary'] && item['work-summary'].length > 0){
item = item['work-summary'][0];
}else{
item['work-summary'];
}
if(item['external-ids'] && item['external-ids']['external-id']){
for(var j =0; j< item['external-ids']['external-id'].length; j++){
var id = item['external-ids']['external-id'][j];
if(id['external-id-type'] == "doi"){
result.DOI = id['external-id-value'];
result.url = "http://dx.doi.org/" + result.DOI;
break;
}
}
}
result.id = item['put-code'];
if(item.title && item.title.title){
result.title = item['title']['title'].value;
}
result.journal = item['journal-title'];
result.source = 'orcid';
result.type = 'publication';
if(item['publication-date'] && item['publication-date']['year']){
result.date = item['publication-date']['year'].value
}
result.accessRights = "OPEN";
if(item.publisher){
result.publisher = item.publisher;
}
result.result = item;
results.push(result);
}
return results;
}
}