Project

General

Profile

1 44035 konstantin
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
import { CacheService  } from '../shared/cache.service';
8 44035 konstantin
import {OpenaireProperties} from '../utils/properties/openaireProperties';
9
import {SearchResult}     from '../utils/entities/searchResult';
10 44338 konstantin
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
11 44035 konstantin
12
@Injectable()
13
export class SearchPeopleService {
14
15 44638 argiro.kok
    constructor(private http: Http, public _cache: CacheService) {}
16 44035 konstantin
17 44338 konstantin
    searchPeople (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
18 44035 konstantin
19 44338 konstantin
        console.info("In searchProjects");
20 44035 konstantin
21 44766 argiro.kok
        let link = OpenaireProperties.getSearchAPIURLLast()+"people";
22 44035 konstantin
23
        let url = link+"?";
24 44338 konstantin
        if(params!= null && params != ''  ) {
25
            url += params;
26 44035 konstantin
        }
27 44338 konstantin
        if(refineParams!= null && params != ''  ) {
28
            url += refineParams;
29
        }
30 44766 argiro.kok
        url += "&page="+(page-1)+"&size="+size + "&format=json";
31 44638 argiro.kok
        let key = url;
32
        if (this._cache.has(key)) {
33 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "person")]);
34 44638 argiro.kok
        }
35 44035 konstantin
36
        return this.http.get(url)
37
                    .map(res => <any> res.json())
38 44338 konstantin
                    //.do(res => console.info(res))
39 44638 argiro.kok
                    .do(res => {
40
                      this._cache.set(key, res);
41 44907 argiro.kok
                    })
42
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "person")]);
43 44035 konstantin
    }
44 44726 argiro.kok
    advancedSearchPeople (params: string, page: number, size: number ):any {
45
      let url = OpenaireProperties.getSearchResourcesAPIURL();
46
      var basicQuery = "(oaftype exact person) "
47
      url += "?query=";
48
      if(params!= null && params != ''  ) {
49
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
50
      }else{
51
        url +=" ( "+basicQuery+ " ) ";
52
      }
53 44035 konstantin
54 44726 argiro.kok
      url += "&page="+(page-1)+"&size="+size;
55
      url += "&format=json";
56
      let key = url;
57
      if (this._cache.has(key)) {
58 44907 argiro.kok
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
59 44726 argiro.kok
      }
60
      return this.http.get(url)
61
      .map(res => <any> res.json())
62
      //.do(res => console.info(res))
63
      .do(res => {
64
        this._cache.set(key, res);
65 44907 argiro.kok
      })
66
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
67 44726 argiro.kok
    }
68 44035 konstantin
    parseResults(data: any): SearchResult[] {
69
        let results: SearchResult[] = [];
70
71
        let length = Array.isArray(data) ? data.length : 1;
72
73
        for(let i=0; i<length; i++) {
74 44084 konstantin
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:person'] : data['result']['metadata']['oaf:entity']['oaf:person'];
75 44035 konstantin
76
            var result: SearchResult = new SearchResult();
77
78
            result['title'] = {"name": '', "url": '', "accessMode": ''};
79
80
            result['title'].name = resData.fullname;
81
82
            result['title'].url = OpenaireProperties.getsearchLinkToPerson();
83 44907 argiro.kok
            result['title'].url +=  Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
84 44035 konstantin
85
            results.push(result);
86
        }
87
88
        return results;
89
    }
90 44300 konstantin
91
    numOfEntityPeople(id: string, entity: string):any {
92
93 44766 argiro.kok
        //OpenaireProperties.getSearchAPIURLLast()
94 44300 konstantin
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
95 44766 argiro.kok
        let url = OpenaireProperties.getSearchAPIURLLast()+entity+id+"/people/count"+ "?format=json";
96 44638 argiro.kok
        let key = url;
97
        if (this._cache.has(key)) {
98
          return Observable.of(this._cache.get(key));
99
        }
100 44300 konstantin
        return this.http.get(url)
101
                    .map(res => <any> res.json())
102 44638 argiro.kok
                    .map(res => res.total)
103
                    .do(res => {
104
                      this._cache.set(key, res);
105
                    });
106 44300 konstantin
    }
107
108
    numOfSearchPeople(params: string):any {
109
110 44766 argiro.kok
        //OpenaireProperties.getSearchAPIURLLast()
111 44300 konstantin
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
112 44797 argiro.kok
        let url = OpenaireProperties.getSearchAPIURLLast()+"people/count?q="+'"' + params + '"'+ "&format=json";
113 44638 argiro.kok
        let key = url;
114
        if (this._cache.has(key)) {
115
          return Observable.of(this._cache.get(key));
116
        }
117 44300 konstantin
        return this.http.get(url)
118
                    .map(res => <any> res.json())
119 44638 argiro.kok
                    .map(res => res.total)
120
                    .do(res => {
121
                      this._cache.set(key, res);
122
                    });
123 44300 konstantin
    }
124 44035 konstantin
}