1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import 'rxjs/add/observable/of';
|
5
|
import 'rxjs/add/operator/do';
|
6
|
import 'rxjs/add/operator/share';
|
7
|
|
8
|
import { CacheService } from '../../shared/cache.service';
|
9
|
import {PersonInfo} from '../../utils/entities/personInfo';
|
10
|
import {OpenaireProperties} from '../../utils/properties/openaireProperties'
|
11
|
|
12
|
export function hashCodeString(str: string): string {
|
13
|
let hash = 0;
|
14
|
if (str.length === 0) {
|
15
|
return hash + '';
|
16
|
}
|
17
|
for (let i = 0; i < str.length; i++) {
|
18
|
let char = str.charCodeAt(i);
|
19
|
hash = ((hash << 5) - hash) + char;
|
20
|
hash = hash & hash; // Convert to 32bit integer
|
21
|
}
|
22
|
return hash + '';
|
23
|
}
|
24
|
|
25
|
@Injectable()
|
26
|
export class PersonService {
|
27
|
|
28
|
constructor(private http: Http, public _cache: CacheService) {}
|
29
|
|
30
|
personInfo: PersonInfo;
|
31
|
|
32
|
getPersonInfo (id: string):any {
|
33
|
|
34
|
console.info("getPersonInfo in service");
|
35
|
|
36
|
let url = OpenaireProperties. getSearchAPIURLLast()+'people/'+id+"?format=json";
|
37
|
let key = url;
|
38
|
if (this._cache.has(key)) {
|
39
|
return Observable.of(this._cache.get(key)).map(res => this.parsePersonInfo(res));
|
40
|
}
|
41
|
return this.http.get(url)
|
42
|
.map(res => <any> res.json())
|
43
|
.map(res => res['result']['metadata']['oaf:entity']['oaf:person'])
|
44
|
.do(res => {
|
45
|
this._cache.set(key, res);
|
46
|
})
|
47
|
.map(res => this.parsePersonInfo(res));
|
48
|
|
49
|
|
50
|
|
51
|
}
|
52
|
|
53
|
private handleError (error: Response) {
|
54
|
// in a real world app, we may send the error to some remote logging infrastructure
|
55
|
// instead of just logging it to the console
|
56
|
console.log(error);
|
57
|
return Observable.throw(error || 'Server error');
|
58
|
}
|
59
|
|
60
|
parsePersonInfo (data: any):any {
|
61
|
console.info("parsePersonInfo");
|
62
|
this.personInfo = new PersonInfo();
|
63
|
|
64
|
if(data != null) {
|
65
|
if(data.hasOwnProperty('firstname')) {
|
66
|
this.personInfo.firstname = data.firstname;
|
67
|
}
|
68
|
if(data.hasOwnProperty('secondnames')) {
|
69
|
this.personInfo.secondnames = data.secondnames;
|
70
|
}
|
71
|
if(data.hasOwnProperty('fullname')) {
|
72
|
this.personInfo.fullname = data.fullname;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
return this.personInfo;
|
77
|
|
78
|
}
|
79
|
|
80
|
}
|