2017-12-19 13:53:46 +01:00
import { Injectable } from '@angular/core' ;
2019-06-03 15:20:36 +02:00
import { HttpClient , HttpHeaders } from '@angular/common/http' ;
2019-07-23 14:23:12 +02:00
import { EnvProperties } from '../../../utils/properties/env-properties' ;
import { ClaimEntity , ClaimResult } from '../claimHelper.class' ;
2019-06-03 15:20:36 +02:00
import { map } from "rxjs/operators" ;
2019-07-01 12:14:31 +02:00
import { StringUtils } from "../../../utils/string-utils.class" ;
2021-11-09 22:04:01 +01:00
import { properties } from "../../../../../environments/environment" ;
2017-12-19 13:53:46 +01:00
2019-07-23 14:23:12 +02:00
2017-12-19 13:53:46 +01:00
@Injectable ( )
export class SearchOrcidService {
2019-06-03 15:20:36 +02:00
constructor ( private http : HttpClient ) { }
2017-12-19 13:53:46 +01:00
2019-07-23 14:23:12 +02:00
searchOrcidAuthor ( term : string , authorIds : string [ ] , authors , properties : EnvProperties , addId ) : any {
2017-12-19 13:53:46 +01:00
2019-06-03 15:20:36 +02:00
//var headers = new Headers();
//headers.append('Accept', 'application/orcid+json');
let headers = new HttpHeaders ( { 'Accept' : 'application/orcid+json' } ) ;
2017-12-19 13:53:46 +01:00
2019-07-23 14:23:12 +02:00
let url = properties . searchOrcidURL + term + '/record' ;
2017-12-19 13:53:46 +01:00
return this . http . get ( url , { headers : headers } )
2019-06-03 15:20:36 +02:00
//.map(res => res.json()['person'])
. pipe ( map ( res = > res [ 'person' ] ) )
. pipe ( map ( res = > [ res [ 'name' ] [ 'given-names' ] ,
2018-05-02 14:26:54 +02:00
res [ 'name' ] [ 'family-name' ] ,
2019-06-03 15:20:36 +02:00
res [ 'name' ] ] ) )
2019-07-23 14:23:12 +02:00
. pipe ( map ( res = > SearchOrcidService . parseOrcidAuthor ( res , authorIds , authors , addId ) ) ) ;
2017-12-19 13:53:46 +01:00
}
2019-07-23 14:23:12 +02:00
searchOrcidAuthors ( term : string ,
properties : EnvProperties ) : any {
2017-12-19 13:53:46 +01:00
2019-06-03 15:20:36 +02:00
let headers = new HttpHeaders ( { 'Accept' : 'application/orcid+json' } ) ;
2017-12-19 13:53:46 +01:00
2019-07-01 12:14:31 +02:00
// 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 url = properties . searchOrcidURL + 'search?q=' + StringUtils . URIEncode ( '{!edismax qf="given-and-family-names^50.0 family-name^10.0 given-names^5.0 credit-name^10.0 other-names^5.0 text^1.0" pf="given-and-family-names^50.0" mm=1}' ) + term + '&start=0&rows=10' ;
//q={!edismax qf="given-and-family-names^50.0 family-name^10.0 given-names^5.0 credit-name^10.0 other-names^5.0 text^1.0" pf="given-and-family-names^50.0" mm=1}alessia bardi&start=0&rows=10
2017-12-19 13:53:46 +01:00
let key = url ;
2019-07-23 14:23:12 +02:00
return this . http . get ( url , { headers : headers } )
2019-06-03 15:20:36 +02:00
. pipe ( map ( res = > res [ 'result' ] ) )
2019-07-23 14:23:12 +02:00
. pipe ( map ( res = > SearchOrcidService . parseOrcidAuthors ( res ) ) ) ;
2017-12-19 13:53:46 +01:00
2019-07-23 14:23:12 +02:00
}
2017-12-19 13:53:46 +01:00
2019-07-23 14:23:12 +02:00
searchOrcidPublications ( id : string , properties : EnvProperties , parse : boolean = false ) : any {
let headers = new HttpHeaders ( { 'Accept' : 'application/orcid+json' } ) ;
let url = properties . searchOrcidURL + id + '/works' ;
2017-12-19 13:53:46 +01:00
return this . http . get ( url , { headers : headers } )
2019-06-03 15:20:36 +02:00
. pipe ( map ( res = > res [ 'group' ] ) )
2019-07-23 14:23:12 +02:00
. pipe ( map ( request = > ( parse ? SearchOrcidService . parse ( id , request ) : request ) ) ) ;
2017-12-19 13:53:46 +01:00
}
2019-07-23 14:23:12 +02:00
static parseOrcidAuthor ( data : any , authorIds : string [ ] , authors , addId ) : any {
if ( data [ 2 ] != null ) {
if ( addId ) {
authorIds . push ( data [ 2 ] . path ) ;
2017-12-19 13:53:46 +01:00
}
2019-07-23 14:23:12 +02:00
const author = { } ;
author [ 'id' ] = data [ 2 ] . path ;
if ( data [ 0 ] != null ) {
author [ 'authorGivenName' ] = data [ 0 ] . value ;
} else {
author [ 'authorGivenName' ] = "" ;
2017-12-19 13:53:46 +01:00
}
2019-07-23 14:23:12 +02:00
if ( data [ 1 ] != null ) {
author [ 'authorFamilyName' ] = data [ 1 ] . value ;
} else {
author [ 'authorFamilyName' ] = "" ;
}
authors . push ( author ) ;
return true ;
}
return false ;
}
static parseOrcidAuthors ( data : any ) : any {
let mydata : any ;
let length : number ;
let authorIds : string [ ] = [ ] ;
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 ) ;
}
}
}
return authorIds ;
}
static parse ( authorId , response ) : ClaimEntity [ ] {
const results : ClaimEntity [ ] = [ ] ;
for ( let i = 0 ; i < response . length ; i ++ ) {
let item = response [ i ] ;
const entity : ClaimEntity = new ClaimEntity ( ) ;
entity . result = new ClaimResult ( ) ;
entity . result . DOI = null ;
entity . result . url = null ;
entity . result . publisher = null ;
entity . result . journal = null ;
if ( item [ 'work-summary' ] && item [ 'work-summary' ] . length > 0 ) {
item = item [ 'work-summary' ] [ 0 ] ;
} else {
item = item [ 'work-summary' ] ;
}
let handleId = null ;
if ( item [ 'external-ids' ] && item [ 'external-ids' ] [ 'external-id' ] ) {
for ( let j = 0 ; j < item [ 'external-ids' ] [ 'external-id' ] . length ; j ++ ) {
const id = item [ 'external-ids' ] [ 'external-id' ] [ j ] ;
if ( id [ 'external-id-type' ] == "doi" ) {
entity . result . DOI = id [ 'external-id-value' ] ;
2021-11-09 22:04:01 +01:00
entity . result . url = properties . doiURL + entity . result . DOI ;
2019-07-23 14:23:12 +02:00
break ;
} else if ( id [ 'external-id-type' ] == "handle" && ! handleId ) {
handleId = id [ 'external-id-value' ] ;
break ;
2018-05-03 11:58:30 +02:00
}
}
}
2019-07-23 14:23:12 +02:00
if ( ! entity . result . url && handleId ) {
entity . result . url = handleId ;
2018-05-03 11:58:30 +02:00
2019-07-23 14:23:12 +02:00
}
entity . id = authorId + "-" + item [ 'put-code' ] ;
if ( item . title && item . title . title ) {
entity . title = item [ 'title' ] [ 'title' ] . value ;
}
entity . result . journal = item [ 'journal-title' ] ;
entity . result . source = 'orcid' ;
entity . type = 'publication' ;
if ( item [ 'publication-date' ] && item [ 'publication-date' ] [ 'year' ] ) {
entity . result . date = item [ 'publication-date' ] [ 'year' ] . value
}
entity . result . accessRights = "OPEN" ;
if ( item . publisher ) {
entity . result . publisher = item . publisher ;
}
entity . result . authors = [ ] ;
entity . result . record = item ;
results . push ( entity ) ;
}
return results ;
}
2017-12-19 13:53:46 +01:00
}