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
import { CacheService  } from '../../../shared/cache.service';
11
@Injectable()
12
export class SearchOrcidService {
13
  constructor( private http: Http, public _cache: CacheService) {}
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
    if (this._cache.has(key)) {
26
      return Observable.of(this._cache.get(key)).map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
27
    }
28
    return this.http.get(url, { headers: headers })
29
                .map(res => res.json()['orcid-profile'])
30
                .map(res => [res['orcid-bio']['personal-details']['given-names'],
31
                             res['orcid-bio']['personal-details']['family-name'],
32
                             res['orcid-identifier']])
33
                .do(res => {
34
                               this._cache.set(key, res);
35
                             })
36
                .map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
37
    }
38

    
39
  searchOrcidAuthors (term: string, authorIds: string[],
40
      authorGivenNames: string[], authorFamilyNames: string[]):any {
41
    console.info("In search Orcid authors for keyword: "+term);
42

    
43
    var headers = new Headers();
44
    headers.append('Accept', 'application/orcid+json');
45

    
46
    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';
47
    let key = url;
48
    if (this._cache.has(key)) {
49
      return Observable.of(this._cache.get(key)).map(res => this.parseOrcidAuthors(res, authorIds, authorGivenNames, authorFamilyNames));
50
    }
51
    return this.http.get(url, { headers: headers })
52
      .map(res => res.json()['orcid-search-results']['orcid-search-result'])
53
      .do(res => {
54
        this._cache.set(key, res);
55
      })
56
      .map(res => this.parseOrcidAuthors(res, authorIds, authorGivenNames, authorFamilyNames));
57

    
58
    }
59

    
60
    searchOrcidPublications (id: string):any {
61
      console.info("In search Orcid publications for author: "+id);
62

    
63
      var headers = new Headers();
64
      headers.append('Accept', 'application/orcid+json');
65

    
66
      let url = OpenaireProperties.getSearchOrcidURL()+id+'/orcid-works';
67
      let key = url;
68
      if (this._cache.has(key)) {
69
        return Observable.of(this._cache.get(key));
70
      }
71
      return this.http.get(url, { headers: headers })
72
        .map(res => res.json()['orcid-profile']['orcid-activities']['orcid-works'])
73
        .do(res => {
74
          this._cache.set(key, res);
75
        });
76
        //.map(res => res['orcid-work']);
77
      }
78

    
79

    
80
      parseOrcidAuthor (data: any, authorIds: string[],
81
          authorGivenNames: string[], authorFamilyNames: string[]):any {
82

    
83
              if(data[2] != null) {
84
                  authorIds.push(data[2].path);
85

    
86
                  if(data[0] != null) {
87
                    authorGivenNames.push(data[0].value);
88
                  } else {
89
                    authorGivenNames.push("");
90
                  }
91
                  if(data[1] != null) {
92
                    authorFamilyNames.push(data[1].value);
93
                  } else {
94
                    authorFamilyNames.push("");
95
                  }
96

    
97
                  return true;
98
              }
99

    
100
              return false;
101
      }
102

    
103
      parseOrcidAuthors (data: any, authorIds: string[],
104
          authorGivenNames: string[], authorFamilyNames: string[]):any {
105
              let ret: boolean = false;
106
              let mydata: any;
107
              let length: number;
108

    
109
              if(data != null) {
110
                  length = data.length!=undefined ? data.length : 1;
111

    
112
                  for(let i=0; i<length; i++) {
113
                      mydata = data.length!=undefined  ? data[i] : data;
114

    
115
                      if(mydata.hasOwnProperty("orcid-profile")) {
116
                           if(mydata['orcid-profile'].hasOwnProperty("orcid-identifier")) {
117
                               authorIds.push(mydata['orcid-profile']['orcid-identifier'].path);
118

    
119
                               if(mydata['orcid-profile'].hasOwnProperty("orcid-bio")) {
120
                                   if(mydata['orcid-profile']['orcid-bio'].hasOwnProperty("personal-details")) {
121
                                       if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("given-names")) {
122
                                           authorGivenNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['given-names'].value);
123
                                       } else {
124
                                           authorGivenNames.push("");
125
                                       }
126

    
127
                                       if(mydata['orcid-profile']['orcid-bio']['personal-details'].hasOwnProperty("family-name")) {
128
                                           authorFamilyNames.push(mydata['orcid-profile']['orcid-bio']['personal-details']['family-name'].value);
129
                                       } else {
130
                                           authorFamilyNames.push("");
131
                                       }
132
                                   }
133
                               }
134
                               ret = true;
135
                           }
136
                      }
137
                  }
138
              }
139
              return ret;
140
      }
141
}
(10-10/10)