Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError,  of } from 'rxjs';
5
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
6

    
7
import{EnvProperties} from '../properties/env-properties';
8
import {catchError, map} from "rxjs/operators";
9

    
10
@Injectable()
11
export class ISVocabulariesService {
12
    constructor(private http: HttpClient ) {}
13

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

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

    
35
      }else if ( field == "type" && (entity == "software" || entity == "other")){
36
        return of([]);
37

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

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

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

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

    
62
      }
63
      return null;
64

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

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

    
88
    }
89

    
90
    parse (data: any, vocabularyName: string):AutoCompleteValue[] {
91
        var array:AutoCompleteValue[] =[]
92
        for(var i = 0; i < data.length; i++){
93
          var value:AutoCompleteValue =  new AutoCompleteValue();
94
          value.id = data[i].englishName;//data[i].code;
95
          if(vocabularyName == 'dnet:countries.json'){ //use Country code instead of country name
96
            value.id =  data[i].code;
97
          }
98
          value.label = data[i].englishName;
99
          array.push(value);
100
        }
101

    
102
        return array;
103

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