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
  searchOrcidAuthor (term: string, authorIds: string[],
16
      authorGivenNames: string[], authorFamilyNames: string[]):any {
17
    console.info("In searchOrcidAuthor: "+term);
18

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

    
22
    let url = OpenaireProperties.getSearchOrcidURL()+term+'/orcid-bio';
23
    let key = url;
24
    if (this._cache.has(key)) {
25
      return Observable.of(this._cache.get(key)).map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
26
    }
27
    return this.http.get(url, { headers: headers })
28
                .map(res => res.json()['orcid-profile'])
29
                .map(res => [res['orcid-bio']['personal-details']['given-names'],
30
                             res['orcid-bio']['personal-details']['family-name'],
31
                             res['orcid-identifier']])
32
                .do(res => {
33
                               this._cache.set(key, res);
34
                             })
35
                .map(res => this.parseOrcidAuthor(res, authorIds, authorGivenNames, authorFamilyNames));
36
    }
37

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

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

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

    
57
    }
58

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

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

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

    
78

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

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

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

    
96
                  return true;
97
              }
98

    
99
              return false;
100
      }
101

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

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

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

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

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

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