Project

General

Profile

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

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

    
10
@Injectable({  providedIn: 'root' })
11
export class ISVocabulariesService {
12
  private vocabularies: Map<string, BehaviorSubject<AutoCompleteValue[]>> = new Map<string, BehaviorSubject<AutoCompleteValue[]>>();
13
  private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null);
14
  private subscriptions = [];
15

    
16
  constructor(private http: HttpClient) {}
17

    
18
  ngOnDestroy() {
19
    this.clearSubscriptions();
20
  }
21

    
22
  clearSubscriptions() {
23
    this.subscriptions.forEach(subscription => {
24
      if (subscription instanceof Subscriber) {
25
        subscription.unsubscribe();
26
      }
27
    });
28
  }
29
  getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable<any> {
30
    //console.log("getVocabulary field: "+ field + " for entity: "+ entity);
31
    var file = "";
32
    var vocabulary = "";
33
    if (field == "lang") {
34
      // file="languages.json";
35
      // return this.getVocabularyFromFile(file);
36
      vocabulary = "dnet:languages.json";
37
      //return this.getVocabularyFromService(vocabulary, properties);
38
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
39
    } else if (field == "type" && (entity == "publication")) {
40
      // file = "publicationTypes.json";
41
      // return this.getVocabularyFromFile(file);
42
      vocabulary = "dnet:publication_resource.json";
43
      //return this.getVocabularyFromService(vocabulary, properties);
44
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
45
    } else if (field == "type" && (entity == "dataset")) {
46
      // file = "dnet:dataCite_resource.json";
47
      // return this.getVocabularyFromFile(file);
48
      vocabulary = "dnet:dataCite_resource.json";
49
      //return this.getVocabularyFromService(vocabulary, properties);
50
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
51
    } else if (field == "type" && (entity == "software" || entity == "other")) {
52
      return of([]);
53
    } else if (field == "type" && entity == "result" ) {
54
      //return Observable.zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties));
55
      return Observable.zip(from(this.getVocabularyFromServiceAsync("dnet:publication_resource.json", properties)),from(this.getVocabularyFromServiceAsync("dnet:dataCite_resource.json", properties)));
56
    } else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) {
57
      // file= "accessMode.json";
58
      // return this.getVocabularyFromFile(file);
59
      vocabulary = "dnet:access_modes.json";
60
      //return this.getVocabularyFromService(vocabulary, properties);
61
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
62
    } else if ((field == "type") && (entity == "dataprovider")) {
63
      // file = "dataProviderType.json";
64
      //   return this.getVocabularyFromFile(file);
65
      vocabulary = "dnet:datasource_typologies.json";
66
      //return this.getVocabularyFromService(vocabulary, properties);
67
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
68
      
69
    } else if (field == "compatibility" && (entity == "dataprovider")) {
70
      // file = "dataProviderCompatibility.json";
71
      //   return this.getVocabularyFromFile(file);
72
      vocabulary = "dnet:datasourceCompatibilityLevel.json";
73
      //return this.getVocabularyFromService(vocabulary, properties);
74
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
75
      
76
    } else if (field == "country") {
77
      // file = "countries.json";
78
      //   return this.getVocabularyFromFile(file);
79
      vocabulary = "dnet:countries.json";
80
      //return this.getVocabularyFromService(vocabulary, properties);
81
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
82
      
83
    }
84
    return null;
85
    
86
  }
87

    
88
  async getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties): Promise<AutoCompleteValue[]> {
89
    if(!this.vocabularies.has(vocabularyName)) {
90
      await  new Promise<any>(resolve => {
91
        this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
92

    
93
        this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
94
          vocabularyRes => {
95
            this.vocabularies.get(vocabularyName).next(vocabularyRes);
96
            resolve();
97
          }, error => {
98
            this.vocabularies.get(vocabularyName).next(null);
99
            resolve();
100
          }
101
        ));
102
      });
103
      this.clearSubscriptions();
104
    }
105

    
106
    return this.vocabularies.get(vocabularyName).getValue();
107
  }
108

    
109
  getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
110
    let url = properties.vocabulariesAPI + vocabularyName;
111
    return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
112
      //.map(res => <any> res.json())
113
      .pipe(map(res => res['terms']))
114
      .pipe(map(res => this.parse(res, vocabularyName)))
115
      .pipe(catchError(this.handleError));
116
    
117
  }
118
  
119
  parse(data: any, vocabularyName: string): AutoCompleteValue[] {
120
    var array: AutoCompleteValue[] = []
121
    for (var i = 0; i < data.length; i++) {
122
      var value: AutoCompleteValue = new AutoCompleteValue();
123
      value.id = data[i].englishName;//data[i].code;
124
      if (vocabularyName == 'dnet:countries.json') { //use Country code instead of country name
125
        value.id = data[i].code;
126
      }
127
      value.label = data[i].englishName;
128
      array.push(value);
129
    }
130
    
131
    return array;
132
    
133
  }
134

    
135
  getProvenanceActionVocabulary(properties: EnvProperties): Observable<any> {
136
      let vocabulary = "dnet:provenanceActions.json";
137
      return from(this.getProvenanceActionVocabularyFromServiceAsync(vocabulary, properties));
138
    }
139

    
140
  async getProvenanceActionVocabularyFromServiceAsync (vocabularyName: string, properties: EnvProperties): Promise<{}> {
141
    if(!this.provenanceActionVocabulary || !this.provenanceActionVocabulary.getValue()) {
142
      await  new Promise<any>(resolve => {
143
        this.subscriptions.push(this.getProvenanceActionVocabularyFromService(vocabularyName, properties).subscribe(
144
          vocabularyRes => {
145
            this.provenanceActionVocabulary.next(vocabularyRes);
146
            resolve();
147
          },
148
          error => {
149
            this.provenanceActionVocabulary.next(null);
150
            resolve();
151
          }
152
        ));
153
      });
154
      this.clearSubscriptions();
155
    }
156
    return this.provenanceActionVocabulary.getValue();
157
  }
158

    
159
  getProvenanceActionVocabularyFromService (vocabularyName: string, properties: EnvProperties): any {
160
    let url = properties.vocabulariesAPI+vocabularyName;
161

    
162
    return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
163
      .pipe(map(res => res['terms']))
164
      .pipe(map(res => this.parseProvenanceActionVocabulary(res)));
165
  }
166

    
167
  parseProvenanceActionVocabulary(terms: any) {
168
    var provenanceActionVocabulary: {} = {};
169
    for(let term of terms) {
170
      provenanceActionVocabulary[term.code] = term.englishName;
171
    }
172
    return provenanceActionVocabulary;
173
  }
174

    
175

    
176
  private handleError(error: HttpErrorResponse) {
177
    // in a real world app, we may send the error to some remote logging infrastructure
178
    // instead of just logging it to the console
179
    console.log(error);
180
    return throwError(error || 'Server error');
181
  }
182
}
(1-1/3)