Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
5
import 'rxjs/add/observable/of';
6
import 'rxjs/add/operator/do';
7
import 'rxjs/add/operator/share';
8

    
9
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
10

    
11
@Injectable()
12
export class ISVocabulariesService {
13
    private api =OpenaireProperties.getVocabulariesAPI();
14
    constructor(private http: Http ) {}
15

    
16
    getVocabularyByType(field:string,entity:string):any{
17
      console.log("getVocabulary field: "+ field + " for entity: "+ entity);
18
      var file = "";
19
      var vocabulary = "";
20
      if( field == "lang"){
21
          // file="languages.json";
22
          // return this.getVocabularyFromFile(file);
23
          vocabulary = "dnet:languages.json";
24
          return this.getVocabularyFromService(vocabulary);
25
      }else if ( field == "type" && (entity == "publication")){
26
        // file = "publicationTypes.json";
27
        // return this.getVocabularyFromFile(file);
28
        vocabulary = "dnet:publication_resource.json";
29
        return this.getVocabularyFromService(vocabulary);
30

    
31
      }else if ( field == "type" && (entity == "dataset")){
32
        // file = "dnet:dataCite_resource.json";
33
        // return this.getVocabularyFromFile(file);
34
        vocabulary = "dnet:dataCite_resource.json";
35
        return this.getVocabularyFromService(vocabulary);
36

    
37
      }else if( field == "access" && (entity == "publication" || entity == "dataset")){
38
        // file= "accessMode.json";
39
        // return this.getVocabularyFromFile(file);
40
        vocabulary = "dnet:access_modes.json";
41
        return this.getVocabularyFromService(vocabulary);
42

    
43
      } else if( (field == "type") && (entity == "dataprovider")){
44
        // file = "dataProviderType.json";
45
        //   return this.getVocabularyFromFile(file);
46
          vocabulary = "dnet:datasource_typologies.json";
47
          return this.getVocabularyFromService(vocabulary);
48

    
49
      } else if( field == "compatibility" && (entity == "dataprovider")){
50
        // file = "dataProviderCompatibility.json";
51
        //   return this.getVocabularyFromFile(file);
52
          vocabulary = "dnet:datasourceCompatibilityLevel.json";
53
          return this.getVocabularyFromService(vocabulary);
54

    
55
      } else if( field == "country" ){
56
        // file = "countries.json";
57
        //   return this.getVocabularyFromFile(file);
58
          vocabulary = "dnet:countries.json";
59
          return this.getVocabularyFromService(vocabulary);
60

    
61
      }
62
      return null;
63

    
64
    }
65
    // getVocabularyFromFile (file:string):AutoCompleteValue[] {
66
    //        var lang = JSON.parse(JSON.stringify(require('../utils/vocabularies/'+file)));
67
    //       return  this.parse(lang["terms"]);
68
    // }
69
    getVocabularyFromService (vocabularyName:string):any {
70
        let url = this.api + vocabularyName;
71
        console.log(url);
72

    
73
        // return this.http.get(url).toPromise()
74
        // .then(request =>
75
        //   {
76
        //     request = request.json()['terms'];
77
        //     var results:AutoCompleteValue[] = this.parse(request);
78
        //     console.log("Get vocabulary : "+ vocabularyName+ " - get " +results.length+ "results");
79
        //     return results;
80
        //   });
81
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
82
                    .do(res => console.log(res))
83
                    .map(res => <any> res.json())
84
                    .map(res => res['terms'])
85
                    .do(res => console.log(res))
86
                    .map(res => this.parse(res))
87
                    .do(res => console.log(res))
88
                    .catch(this.handleError);
89

    
90
    }
91

    
92
    parse (data: any):AutoCompleteValue[] {
93
        var array:AutoCompleteValue[] =[]
94
        for(var i = 0; i < data.length; i++){
95
          var value:AutoCompleteValue =  new AutoCompleteValue();
96
          value.id =  data[i].englishName;//data[i].code;
97
          value.label = data[i].englishName;
98
          array.push(value);
99
        }
100

    
101
        return array;
102

    
103
    }
104
private handleError (error: Response) {
105
      // in a real world app, we may send the error to some remote logging infrastructure
106
      // instead of just logging it to the console
107
      console.log(error);
108
      return Observable.throw(error  || 'Server error');
109
    }
110
}
(1-1/3)