Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {URLSearchParams} from '@angular/http';
3
import {Http, Response} from '@angular/http';
4
import { Headers, RequestOptions } from '@angular/http';
5
import {Observable}     from 'rxjs/Observable';
6
import {OpenaireProperties} from '../../../utils/properties/openaireProperties';
7
import 'rxjs/add/observable/of';
8
import 'rxjs/add/operator/do';
9
import 'rxjs/add/operator/share';
10

    
11
@Injectable()
12
export class SearchOrcidService {
13
  constructor( private http: Http ) {}
14

    
15

    
16
  searchOrcidAuthor (term: string, authorIds: string[],
17
      authorGivenNames: string[], authorFamilyNames: string[]):any {
18
    console.info("In searchOrcidAuthor: "+term);
19

    
20
    var headers = new Headers();
21
    headers.append('Accept', 'application/orcid+json');
22

    
23
    let url = OpenaireProperties.getSearchOrcidURL()+term+'/orcid-bio';
24
    let key = url;
25

    
26
    return this.http.get(url, { headers: headers })
27
                .map(res => res.json()['orcid-profile'])
28
                .map(res => [res['orcid-bio']['personal-details']['given-names'],
29
                             res['orcid-bio']['personal-details']['family-name'],
30
                             res['orcid-identifier']])
31
                .map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
32
    }
33

    
34
  searchOrcidAuthors (term: string, authorIds: string[],
35
      authorGivenNames: string[], authorFamilyNames: string[]):any {
36
    console.info("In search Orcid authors for keyword: "+term);
37

    
38
    var headers = new Headers();
39
    headers.append('Accept', 'application/orcid+json');
40

    
41
    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';
42
    let key = url;
43

    
44
    return this.http.get(url, { headers: headers })
45
      .map(res => res.json()['orcid-search-results']['orcid-search-result'])
46
      .map(res => this.parseOrcidAuthors(res, authorIds, authorGivenNames, authorFamilyNames));
47

    
48
    }
49

    
50
    searchOrcidPublications (id: string):any {
51
      console.info("In search Orcid publications for author: "+id);
52

    
53
      var headers = new Headers();
54
      headers.append('Accept', 'application/orcid+json');
55

    
56
      let url = OpenaireProperties.getSearchOrcidURL()+id+'/orcid-works';
57
      let key = url;
58

    
59
      return this.http.get(url, { headers: headers })
60
        .map(res => res.json()['orcid-profile']['orcid-activities']['orcid-works']);
61
        //.map(res => res['orcid-work']);
62
      }
63

    
64

    
65
      parseOrcidAuthor (data: any, authorIds: string[],
66
          authorGivenNames: string[], authorFamilyNames: string[]):any {
67

    
68
              if(data[2] != null) {
69
                  authorIds.push(data[2].path);
70

    
71
                  if(data[0] != null) {
72
                    authorGivenNames.push(data[0].value);
73
                  } else {
74
                    authorGivenNames.push("");
75
                  }
76
                  if(data[1] != null) {
77
                    authorFamilyNames.push(data[1].value);
78
                  } else {
79
                    authorFamilyNames.push("");
80
                  }
81

    
82
                  return true;
83
              }
84

    
85
              return false;
86
      }
87

    
88
      parseOrcidAuthors (data: any, authorIds: string[],
89
          authorGivenNames: string[], authorFamilyNames: string[]):any {
90
              let ret: boolean = false;
91
              let mydata: any;
92
              let length: number;
93

    
94
              if(data != null) {
95
                  length = data.length!=undefined ? data.length : 1;
96

    
97
                  for(let i=0; i<length; i++) {
98
                      mydata = data.length!=undefined  ? data[i] : data;
99

    
100
                      if(mydata.hasOwnProperty("orcid-profile")) {
101
                           if(mydata['orcid-profile'].hasOwnProperty("orcid-identifier")) {
102
                               authorIds.push(mydata['orcid-profile']['orcid-identifier'].path);
103

    
104
                               if(mydata['orcid-profile'].hasOwnProperty("orcid-bio")) {
105
                                   if(mydata['orcid-profile']['orcid-bio'].hasOwnProperty("personal-details")) {
106
                                       if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("given-names")) {
107
                                           authorGivenNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['given-names'].value);
108
                                       } else {
109
                                           authorGivenNames.push("");
110
                                       }
111

    
112
                                       if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("family-name")) {
113
                                           authorFamilyNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['family-name'].value);
114
                                       } else {
115
                                           authorFamilyNames.push("");
116
                                       }
117
                                   }
118
                               }
119
                               ret = true;
120
                           }
121
                      }
122
                  }
123
              }
124
              return ret;
125
      }
126
}
(10-10/10)