Project

General

Profile

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

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

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

    
15
  constructor(private http: HttpClient) {}
16

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

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

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

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

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

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

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

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

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

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

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

    
174

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