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

184 lines
6.7 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {URLSearchParams} from '@angular/http';
import {Http, Response} from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import{EnvProperties} from '../../../utils/properties/env-properties';
import { ClaimResult} from '../claimEntities.class';
import {map} from "rxjs/operators";
@Injectable()
export class SearchOrcidService {
constructor( private http: HttpClient ) {}
searchOrcidAuthor (term: string, authorIds: string[], authors, properties:EnvProperties, addId):any {
//var headers = new Headers();
//headers.append('Accept', 'application/orcid+json');
let headers = new HttpHeaders({'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'])
.pipe(map(res => res['person']))
.pipe(map(res => [res['name']['given-names'],
res['name']['family-name'],
res['name']]))
.pipe(map(res => this.parseOrcidAuthor(res, authorIds, authors, addId)));
}
searchOrcidAuthors (term: string, authorIds: string[],
properties:EnvProperties):any {
//var headers = new Headers();
//headers.append('Accept', 'application/orcid+json');
let headers = new HttpHeaders({'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'])
.pipe(map(res => res['result']))
.pipe(map(res => this.parseOrcidAuthors(res, authorIds)));
}
searchOrcidPublications (id: string, properties:EnvProperties, parse:boolean = false):any {
//var headers = new Headers();
//headers.append('Accept', 'application/orcid+json');
let headers = new HttpHeaders({'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'])
.pipe(map(res => res['group']))
.pipe(map(request => (parse?this.parse(id, 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(authorId, 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 = authorId+"-"+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;
}
}