Project

General

Profile

1 43769 argiro.kok
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4 44638 argiro.kok
import 'rxjs/add/observable/of';
5
import 'rxjs/add/operator/do';
6
import 'rxjs/add/operator/share';
7
8 45673 argiro.kok
import { CacheService  } from '../../shared/cache.service';
9
import {PersonInfo} from '../../utils/entities/personInfo';
10
import {OpenaireProperties} from '../../utils/properties/openaireProperties'
11 43769 argiro.kok
12 44638 argiro.kok
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 43769 argiro.kok
@Injectable()
26
export class PersonService {
27
28 44638 argiro.kok
    constructor(private http: Http, public _cache: CacheService) {}
29 43769 argiro.kok
30
    personInfo: PersonInfo;
31
32
    getPersonInfo (id: string):any {
33 44638 argiro.kok
34 43769 argiro.kok
        console.info("getPersonInfo in service");
35
36 44766 argiro.kok
        let url = OpenaireProperties. getSearchAPIURLLast()+'people/'+id+"?format=json";
37 44638 argiro.kok
        let key = url;
38
        if (this._cache.has(key)) {
39 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => this.parsePersonInfo(res));
40 44638 argiro.kok
        }
41 43769 argiro.kok
        return this.http.get(url)
42
                    .map(res => <any> res.json())
43
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:person'])
44 44638 argiro.kok
                    .do(res => {
45
                      this._cache.set(key, res);
46 44907 argiro.kok
                    })
47
                    .map(res => this.parsePersonInfo(res));
48 43769 argiro.kok
49 44638 argiro.kok
50
51 43769 argiro.kok
    }
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 44907 argiro.kok
        console.log(error);
57 43769 argiro.kok
        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
}