Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Jsonp, URLSearchParams,ResponseOptions} from '@angular/http';
3
import {Http, Response} from '@angular/http';
4
import {Observable}     from 'rxjs/Observable';
5
import {Claim}           from '../claim';
6
import {OpenaireProperties} from '../../../utils/properties/openaireProperties';
7
import {AutoCompleteValue} from '../../../searchPages/searchUtils/searchHelperClasses.class';
8
import 'rxjs/add/observable/of';
9
import 'rxjs/add/operator/do';
10
import 'rxjs/add/operator/share';
11
import { CacheService  } from '../../../shared/cache.service';
12

    
13
@Injectable()
14
export class ContextsService {
15
  private baseUrl;
16
  constructor(private http: Http, public _cache: CacheService) {
17
    this.baseUrl = OpenaireProperties.getClaimsAPIURL();
18
  }
19

    
20
  public getCommunities(token:string):any {
21
  let  url   = this.baseUrl + 'communities';
22
  let key = url;
23
  if (this._cache.has(key)) {
24
    return Observable.of(this._cache.get(key));
25
  }
26

    
27
    console.info('ContextsService: request communities  '+url);
28
      return this.http.get( url+"?token="+token)
29
                    .map(request => <any> request.json().data)
30
                    // .do(request => console.info("Get claims: offset = "))
31
                    .catch(this.handleError)
32
                    .do(res => {
33
                      this._cache.set(key, res);
34
                    });
35
  }
36
  public getCategories(communityId :string,token:string):any {
37
    console.info('ContextsService:  request categories for community  with id '+communityId);
38
    let url= this.baseUrl  + 'communities/' + communityId + '/categories';
39
    let key = url;
40
    if (this._cache.has(key)) {
41
      return Observable.of(this._cache.get(key));
42
    }
43

    
44
      return this.http.get( url+"?token="+token)
45
                    .map(request => <any> request.json().data)
46
                    // .do(request => console.info("Get claims: offset = " ))
47
                    .catch(this.handleError)
48
                    .do(res => {
49
                      this._cache.set(key, res);
50
                    });
51
  }
52
  public getConcepts(categoryId :string, keyword: string,token:string,parsing:boolean):any {
53
    console.info('ContextsService:  request concept for category  with id '+categoryId + ' and keyword '+ keyword);
54
    let url= this.baseUrl  + 'categories/' + categoryId+ "/concepts";
55
    let key = url+"_parsing="+parsing;
56
    if (this._cache.has(key)) {
57
      return Observable.of(this._cache.get(key));
58
    }
59

    
60
      return this.http.get( url+"?token="+token)
61
                    .map(request => <any> request.json().data)
62
                    .map(res => (parsing)?this.parse(res.concept):res.concept)
63
                    .do(res => console.info(res ))
64
                    .catch(this.handleError)
65
                    .do(res => {
66
                      this._cache.set(key, res);
67
                    });
68
  }
69
  parse (data: any):AutoCompleteValue[] {
70
      var array:AutoCompleteValue[] =[]
71
      for(var i = 0; i < data.length; i++){
72
        var value:AutoCompleteValue =  new AutoCompleteValue();
73
        value.id = data[i].id;
74
        value.label = data[i].label;
75
        array.push(value);
76
      }
77

    
78
      return array;
79

    
80
  }
81

    
82
  private handleError (error: Response) {
83
    // in a real world app, we may send the error to some remote logging infrastructure
84
    // instead of just logging it to the console
85
    console.log(error);
86
    return Observable.throw(error  || 'Server error');
87
  }
88
}
(3-3/9)