Project

General

Profile

1 50169 argiro.kok
import {Injectable} from '@angular/core';
2 55964 argiro.kok
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3 59816 argiro.kok
import {Observable, throwError, of, BehaviorSubject, from, Subscriber} from 'rxjs';
4 50169 argiro.kok
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
5 58739 argiro.kok
import 'rxjs/add/observable/zip';
6 50169 argiro.kok
7 58064 k.triantaf
import {EnvProperties} from '../properties/env-properties';
8 55964 argiro.kok
import {catchError, map} from "rxjs/operators";
9 50169 argiro.kok
10 59072 konstantin
@Injectable({  providedIn: 'root' })
11 50169 argiro.kok
export class ISVocabulariesService {
12 59072 konstantin
  private vocabularies: Map<string, BehaviorSubject<AutoCompleteValue[]>> = new Map<string, BehaviorSubject<AutoCompleteValue[]>>();
13
  private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null);
14 59816 argiro.kok
  private subscriptions = [];
15 59072 konstantin
16
  constructor(private http: HttpClient) {}
17 59816 argiro.kok
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 59072 konstantin
  getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable<any> {
30 58064 k.triantaf
    //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 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
38
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
39 58064 k.triantaf
    } else if (field == "type" && (entity == "publication")) {
40
      // file = "publicationTypes.json";
41
      // return this.getVocabularyFromFile(file);
42
      vocabulary = "dnet:publication_resource.json";
43 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
44
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
45 58064 k.triantaf
    } else if (field == "type" && (entity == "dataset")) {
46
      // file = "dnet:dataCite_resource.json";
47
      // return this.getVocabularyFromFile(file);
48
      vocabulary = "dnet:dataCite_resource.json";
49 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
50
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
51 58064 k.triantaf
    } else if (field == "type" && (entity == "software" || entity == "other")) {
52
      return of([]);
53 58739 argiro.kok
    } else if (field == "type" && entity == "result" ) {
54 59072 konstantin
      //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 58497 konstantin
    } else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) {
57 58064 k.triantaf
      // file= "accessMode.json";
58
      // return this.getVocabularyFromFile(file);
59
      vocabulary = "dnet:access_modes.json";
60 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
61
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
62 58064 k.triantaf
    } else if ((field == "type") && (entity == "dataprovider")) {
63
      // file = "dataProviderType.json";
64
      //   return this.getVocabularyFromFile(file);
65
      vocabulary = "dnet:datasource_typologies.json";
66 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
67
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
68 58064 k.triantaf
69
    } else if (field == "compatibility" && (entity == "dataprovider")) {
70
      // file = "dataProviderCompatibility.json";
71
      //   return this.getVocabularyFromFile(file);
72
      vocabulary = "dnet:datasourceCompatibilityLevel.json";
73 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
74
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
75 58064 k.triantaf
76
    } else if (field == "country") {
77
      // file = "countries.json";
78
      //   return this.getVocabularyFromFile(file);
79
      vocabulary = "dnet:countries.json";
80 59072 konstantin
      //return this.getVocabularyFromService(vocabulary, properties);
81
      return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
82 58064 k.triantaf
83
    }
84
    return null;
85
86
  }
87 59072 konstantin
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 59816 argiro.kok
        this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
94 59072 konstantin
          vocabularyRes => {
95
            this.vocabularies.get(vocabularyName).next(vocabularyRes);
96
            resolve();
97 59245 k.triantaf
          }, error => {
98
            this.vocabularies.get(vocabularyName).next(null);
99
            resolve();
100 59072 konstantin
          }
101 59816 argiro.kok
        ));
102 59072 konstantin
      });
103 59816 argiro.kok
      this.clearSubscriptions();
104 59072 konstantin
    }
105
106
    return this.vocabularies.get(vocabularyName).getValue();
107
  }
108
109
  getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
110 58064 k.triantaf
    let url = properties.vocabulariesAPI + vocabularyName;
111 59072 konstantin
    return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
112 58064 k.triantaf
      //.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 50169 argiro.kok
      }
127 58064 k.triantaf
      value.label = data[i].englishName;
128
      array.push(value);
129 50169 argiro.kok
    }
130 58064 k.triantaf
131
    return array;
132
133
  }
134 59072 konstantin
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 59816 argiro.kok
        this.subscriptions.push(this.getProvenanceActionVocabularyFromService(vocabularyName, properties).subscribe(
144 59072 konstantin
          vocabularyRes => {
145
            this.provenanceActionVocabulary.next(vocabularyRes);
146
            resolve();
147 59085 konstantin
          },
148
          error => {
149
            this.provenanceActionVocabulary.next(null);
150
            resolve();
151 59072 konstantin
          }
152 59816 argiro.kok
        ));
153 59072 konstantin
      });
154 59816 argiro.kok
      this.clearSubscriptions();
155 59072 konstantin
    }
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 58064 k.triantaf
  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 50169 argiro.kok
}