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 {AutoCompleteValue} from '../../../searchPages/searchUtils/searchHelperClasses.class';
7
import 'rxjs/add/observable/of';
8
import 'rxjs/add/operator/do';
9
import 'rxjs/add/operator/share';
10
import { COOKIE  } from '../../../login/utils/helper.class';
11
@Injectable()
12
export class ContextsService {
13
  constructor(private http: Http ) {
14
  }
15

    
16
  public getCommunities( apiUrl:string):any {
17
  let  url   = apiUrl + 's/';
18

    
19
  let key = url;
20
    console.info('ContextsService: request communities  '+url);
21
      return this.http.get(url)
22
                    .map(res => <any> res.json()).map(res => this.parseCommunities(res) )
23
                    // .do(request => console.info("Get claims: offset = "))
24
                    .catch(this.handleError);
25
  }
26
  parseCommunities(data){
27
    var communities = [];
28

    
29
    for(var i = 0; i< data.length; i++){
30
      if(data[i].type && (data[i].type == "ri" || data[i].type == "community")){
31
        communities.push(data[i]);
32
      }
33
    }
34
    return communities;
35
  }
36
  public getCategories(communityId :string, apiUrl:string):any {
37
    console.info('ContextsService:  request categories for community  with id '+communityId);
38
    let url= apiUrl  + '/' + communityId ;
39
    let key = url;
40

    
41

    
42
      return this.http.get(url)
43
                    .map(request => <any> request.json())
44
                    // .do(request => console.info("Get claims: offset = " ))
45
                    .catch(this.handleError);;
46
  }
47
  public getConcepts(categoryId :string, keyword: string, parsing:boolean, apiUrl:string):any {
48
    console.info('ContextsService:  request concept for category  with id '+categoryId + ' and keyword '+ keyword);
49
    let url= apiUrl  + '/category/' + categoryId;
50
    let key = url+"_parsing="+parsing;
51

    
52

    
53
      return this.http.get(url )
54
                    .map(request => <any> request.json())
55
                    .catch(this.handleError)
56
                    .map(res => (parsing)?this.parse(res):res);
57
                    // .do(res => console.info("Result is "+ res.length ));
58
  }
59
  public getSubConcepts(subConceptID :string, keyword: string, parsing:boolean, apiUrl:string):any {
60
    console.info('ContextsService:  request sub concept for concept with id '+subConceptID + ' and keyword '+ keyword);
61
    let url= apiUrl  + '/category/concept/' + subConceptID;
62
    let key = url+"_parsing="+parsing;
63

    
64

    
65
      return this.http.get(url )
66
                    .map(request => <any> request.json())
67
                    .catch(this.handleError)
68
                    .map(res => (parsing)?this.parseSubConcepts(res):res);
69
                    // .do(res => console.info("Result is "+ res.length ));
70
  }
71
  parse (data: any):any {
72
      var array =[]
73
      if(!Array.isArray(data) && data.id && data.label){
74
        var value ={id:"",label:"",hasSubConcept:""};
75
        value.id = data.id;
76
        value.label = data.label;
77
        value.hasSubConcept = data.hasSubConcept;
78
        array.push(value);
79
      }
80
      for(var i = 0; i < data.length; i++){
81
        var value={id:"",label:"",hasSubConcept:""};
82
        value.id = data[i].id;
83
        value.label = data[i].label;
84
        value.hasSubConcept = data[i].hasSubConcept;
85
        array.push(value);
86
      }
87

    
88
      return array;
89

    
90
  }
91
  parseSubConcepts (data: any):any {
92
      var array = []
93
      if(data.length >0 && data[0].concepts){
94
        var concepts = data[0].concepts;
95
        for(var i = 0; i < concepts.length; i++){
96
          var value ={id:"",label:"",hasSubConcept:""};
97
          value.id = concepts[i].id;
98
          value.label = concepts[i].label;
99
          value.hasSubConcept = concepts[i].hasSubConcept;
100
          if(concepts[i].concepts){
101
            var subconcepts = concepts[i].concepts;
102
            for(var x = 0; x < subconcepts.length; x++){
103
              var value ={id:"",label:"",hasSubConcept:""};
104
              value.id = subconcepts[x].id;
105
              value.label = subconcepts[x].label;
106
              value.hasSubConcept = subconcepts[x].hasSubConcept;
107
              array.push(value);
108
            }
109
          }
110
          array.push(value);
111
        }
112
    }
113
      return array;
114

    
115
  }
116

    
117
  private handleError (error: Response) {
118
    // in a real world app, we may send the error to some remote logging infrastructure
119
    // instead of just logging it to the console
120
    console.log(error);
121
    return Observable.throw(error  || 'Server error');
122
  }
123
  private getAuthOptions():RequestOptions{
124
  let headers = new Headers();
125
  headers.append('X-XSRF-TOKEN',  COOKIE.getCookie(COOKIE.cookieName_id));
126
  let options = new RequestOptions({ headers: headers, withCredentials:true });
127
    return options;
128
  }
129
}
(3-3/10)