Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Jsonp, URLSearchParams, RequestOptions, Headers} 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 {   } from '../../../shared/cache.service';
12
import { COOKIE  } from '../../../login/utils/helper.class';
13
@Injectable()
14
export class ContextsService {
15
  private baseUrl;
16
  constructor(private http: Http ) {
17
    this.baseUrl = OpenaireProperties.getClaimsAPIURL();
18
  }
19

    
20
  public getCommunities():any {
21
  let  url   = this.baseUrl + 'communities';
22
  let key = url;
23

    
24

    
25
    console.info('ContextsService: request communities  '+url);
26
      return this.http.get(url, this.getAuthOptions())
27
                    .map(request => <any> request.json().data)
28
                    // .do(request => console.info("Get claims: offset = "))
29
                    .catch(this.handleError);
30
  }
31
  public getCategories(communityId :string):any {
32
    console.info('ContextsService:  request categories for community  with id '+communityId);
33
    let url= this.baseUrl  + 'communities/' + communityId + '/categories';
34
    let key = url;
35

    
36

    
37
      return this.http.get(url, this.getAuthOptions())
38
                    .map(request => <any> request.json().data)
39
                    // .do(request => console.info("Get claims: offset = " ))
40
                    .catch(this.handleError);;
41
  }
42
  public getConcepts(categoryId :string, keyword: string, parsing:boolean):any {
43
    console.info('ContextsService:  request concept for category  with id '+categoryId + ' and keyword '+ keyword);
44
    let url= this.baseUrl  + 'categories/' + categoryId+ "/concepts";
45
    let key = url+"_parsing="+parsing;
46

    
47

    
48
      return this.http.get(url, this.getAuthOptions())
49
                    .map(request => <any> request.json().data)
50
                    .catch(this.handleError)
51
                    .map(res => (parsing)?this.parse(res.concept):res.concept);
52
                    // .do(res => console.info("Result is "+ res.length ));
53
  }
54
  parse (data: any):AutoCompleteValue[] {
55
      var array:AutoCompleteValue[] =[]
56
      if(!Array.isArray(data) && data.id && data.label){
57
        var value:AutoCompleteValue =  new AutoCompleteValue();
58
        value.id = data.id;
59
        value.label = data.label;
60
        array.push(value);
61
      }
62
      for(var i = 0; i < data.length; i++){
63
        var value:AutoCompleteValue =  new AutoCompleteValue();
64
        value.id = data[i].id;
65
        value.label = data[i].label;
66
        array.push(value);
67
      }
68

    
69
      return array;
70

    
71
  }
72

    
73
  private handleError (error: Response) {
74
    // in a real world app, we may send the error to some remote logging infrastructure
75
    // instead of just logging it to the console
76
    console.log(error);
77
    return Observable.throw(error  || 'Server error');
78
  }
79
  private getAuthOptions():RequestOptions{
80
  let headers = new Headers();
81
  headers.append('X-XSRF-TOKEN',  COOKIE.getCookie(COOKIE.cookieName_id));
82
  let options = new RequestOptions({ headers: headers, withCredentials:true });
83
    return options;
84
  }
85
}
(3-3/10)