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

127 lines
5.1 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 {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';
@Injectable()
export class SearchOrcidService {
constructor( private http: Http ) {}
searchOrcidAuthor (term: string, authorIds: string[],
authorGivenNames: string[], authorFamilyNames: string[], properties:EnvProperties):any {
console.info("In searchOrcidAuthor: "+term);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = properties.searchOrcidURL + term+'/orcid-bio';
let key = url;
return this.http.get(url, { headers: headers })
.map(res => res.json()['orcid-profile'])
.map(res => [res['orcid-bio']['personal-details']['given-names'],
res['orcid-bio']['personal-details']['family-name'],
res['orcid-identifier']])
.map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
}
searchOrcidAuthors (term: string, authorIds: string[],
authorGivenNames: string[], authorFamilyNames: 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/orcid-bio?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()['orcid-search-results']['orcid-search-result'])
.map(res => this.parseOrcidAuthors(res, authorIds, authorGivenNames, authorFamilyNames));
}
searchOrcidPublications (id: string, properties:EnvProperties):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+'/orcid-works';
let key = url;
return this.http.get(url, { headers: headers })
.map(res => res.json()['orcid-profile']['orcid-activities']['orcid-works']);
//.map(res => res['orcid-work']);
}
parseOrcidAuthor (data: any, authorIds: string[],
authorGivenNames: string[], authorFamilyNames: string[]):any {
if(data[2] != null) {
authorIds.push(data[2].path);
if(data[0] != null) {
authorGivenNames.push(data[0].value);
} else {
authorGivenNames.push("");
}
if(data[1] != null) {
authorFamilyNames.push(data[1].value);
} else {
authorFamilyNames.push("");
}
return true;
}
return false;
}
parseOrcidAuthors (data: any, authorIds: string[],
authorGivenNames: string[], authorFamilyNames: 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-profile")) {
if(mydata['orcid-profile'].hasOwnProperty("orcid-identifier")) {
authorIds.push(mydata['orcid-profile']['orcid-identifier'].path);
if(mydata['orcid-profile'].hasOwnProperty("orcid-bio")) {
if(mydata['orcid-profile']['orcid-bio'].hasOwnProperty("personal-details")) {
if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("given-names")) {
authorGivenNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['given-names'].value);
} else {
authorGivenNames.push("");
}
if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("family-name")) {
authorFamilyNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['family-name'].value);
} else {
authorFamilyNames.push("");
}
}
}
ret = true;
}
}
}
}
return ret;
}
}