explore-services/portal-2/src/app/services/searchOrcid.service.ts

126 lines
5.2 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Jsonp, URLSearchParams} from '@angular/http';
import {Http, Response} from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {OpenaireProperties} from '../utils/properties/openaireProperties';
@Injectable()
export class SearchOrcidService {
constructor(private jsonp: Jsonp, private http: Http) {}
searchOrcidAuthor (term: string, authorIds: string[],
authorGivenNames: string[], authorFamilyNames: string[]):any {
console.info("In searchOrcidAuthor: "+term);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = OpenaireProperties.getSearchOrcidURL()+term+'/orcid-bio';
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[]):any {
console.info("In search Orcid authors for keyword: "+term);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = OpenaireProperties.getSearchOrcidURL()+'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';
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));
/*
.flatMap(res => res['orcid-search-result'])
.map(res => res['orcid-profile'])
.map(res => [res['orcid-bio']['personal-details']['given-names'],
res['orcid-bio']['personal-details']['family-name'],
res['orcid-identifier']]);
*/
}
searchOrcidPublications (id: string):any {
console.info("In search Orcid publications for author: "+id);
var headers = new Headers();
headers.append('Accept', 'application/orcid+json');
let url = OpenaireProperties.getSearchOrcidURL()+id+'/orcid-works';
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 = length > 1 ? 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;
}
}