openaire-library/utils/staticAutoComplete/ISVocabularies.service.ts

245 lines
9.9 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {BehaviorSubject, from, Observable, of, Subscriber, throwError, zip} from 'rxjs';
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
import {EnvProperties} from '../properties/env-properties';
import {catchError, map} from 'rxjs/operators';
@Injectable({ providedIn: 'root' })
export class ISVocabulariesService {
private vocabularies: Map<string, BehaviorSubject<AutoCompleteValue[]>> = new Map<string, BehaviorSubject<AutoCompleteValue[]>>();
private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null);
private subscriptions = [];
constructor(private http: HttpClient) {}
ngOnDestroy() {
this.clearSubscriptions();
}
clearSubscriptions() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable<any> {
//console.log("getVocabulary field: "+ field + " for entity: "+ entity);
var file = "";
var vocabulary = "";
if (field == "lang") {
// file="languages.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:languages.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "type" && (entity == "publication")) {
// file = "publicationTypes.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:publication_resource.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "type" && (entity == "dataset")) {
// file = "dnet:dataCite_resource.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:dataCite_resource.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "type" && (entity == "software" || entity == "other")) {
return of([]);
} else if (field == "type" && entity == "result" ) {
return zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties));
} else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) {
// file= "accessMode.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:access_modes.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if ((field == "type") && (entity == "dataprovider")) {
// file = "dataProviderType.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasource_typologies.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "compatibility" && (entity == "dataprovider")) {
// file = "dataProviderCompatibility.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasourceCompatibilityLevel.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "country") {
// file = "countries.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:countries.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "fos") {
vocabulary = "fos";
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
} else if (field == "sdg") {
vocabulary = "sdg";
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
}
return null;
}
async getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties): Promise<AutoCompleteValue[]> {
if(!this.vocabularies.has(vocabularyName)) {
await new Promise<void>(resolve => {
this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
vocabularyRes => {
this.vocabularies.get(vocabularyName).next(vocabularyRes);
resolve();
}, error => {
this.vocabularies.get(vocabularyName).next(null);
resolve();
}
));
});
this.clearSubscriptions();
}
return this.vocabularies.get(vocabularyName).getValue();
}
getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
let url = properties.vocabulariesAPI + vocabularyName;
if(vocabularyName == 'fos' || vocabularyName == 'sdg'){
return this.getLocalVocabularyFromService(vocabularyName, properties);
}
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => <any> res.json())
.pipe(map(res => res['terms']))
.pipe(map(res => this.parse(res, vocabularyName)))
.pipe(catchError(this.handleError));
}
getLocalVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
if(vocabularyName == "sdg"){
let url = properties.domain+"/assets/vocabulary/sdg.json";
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => <any> res.json())
.pipe(map(res => res['sdg']))
.pipe(map(res => this.parseSDGs(res)))
.pipe(catchError(this.handleError));
}else if( vocabularyName == "fos"){
let url = properties.domain+"/assets/vocabulary/fos.json";
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => <any> res.json())
.pipe(map(res => res['fos']))
.pipe(map(res => this.parseFOS(res)))
.pipe(catchError(this.handleError));
}
}
parseSDGs(data: any): AutoCompleteValue[] {
var array: AutoCompleteValue[] = []
for (var i = 0; i < data.length; i++) {
var value: AutoCompleteValue = new AutoCompleteValue();
value.id = data[i].id;//data[i].code;
value.label = data[i].id;
array.push(value);
}
return array;
}
parseFOS(data: any): AutoCompleteValue[] {
let array: AutoCompleteValue[] = []
for (let fos of data) {
let value: AutoCompleteValue = new AutoCompleteValue();
value.id = fos.id;//data[i].code;
value.label = fos.label;
array.push(value);
if(fos.children) {
for (let fos2 of fos.children) {
let value: AutoCompleteValue = new AutoCompleteValue();
value.id = fos2.id;//data[i].code;
value.label = fos2.label;
array.push(value);
if(fos2.children) {
for (let fos3 of fos2.children) {
let value: AutoCompleteValue = new AutoCompleteValue();
value.id = fos3.id;//data[i].code;
value.label = fos3.label;
array.push(value);
}
}
}
}
}
return array;
}
parse(data: any, vocabularyName: string): AutoCompleteValue[] {
var array: AutoCompleteValue[] = []
for (var i = 0; i < data.length; i++) {
var value: AutoCompleteValue = new AutoCompleteValue();
value.id = data[i].englishName;//data[i].code;
if (vocabularyName == 'dnet:countries.json') { //use Country code instead of country name
value.id = data[i].code;
}
value.label = data[i].englishName;
array.push(value);
}
return array;
}
getProvenanceActionVocabulary(properties: EnvProperties): Observable<any> {
let vocabulary = "dnet:provenanceActions.json";
return from(this.getProvenanceActionVocabularyFromServiceAsync(vocabulary, properties));
}
async getProvenanceActionVocabularyFromServiceAsync (vocabularyName: string, properties: EnvProperties): Promise<{}> {
if(!this.provenanceActionVocabulary || !this.provenanceActionVocabulary.getValue()) {
await new Promise<void>(resolve => {
this.subscriptions.push(this.getProvenanceActionVocabularyFromService(vocabularyName, properties).subscribe(
vocabularyRes => {
this.provenanceActionVocabulary.next(vocabularyRes);
resolve();
},
error => {
this.provenanceActionVocabulary.next(null);
resolve();
}
));
});
this.clearSubscriptions();
}
return this.provenanceActionVocabulary.getValue();
}
getProvenanceActionVocabularyFromService (vocabularyName: string, properties: EnvProperties): any {
let url = properties.vocabulariesAPI+vocabularyName;
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.pipe(map(res => res['terms']))
.pipe(map(res => this.parseProvenanceActionVocabulary(res)));
}
parseProvenanceActionVocabulary(terms: any) {
var provenanceActionVocabulary: {} = {};
for(let term of terms) {
provenanceActionVocabulary[term.code] = term.englishName;
}
return provenanceActionVocabulary;
}
private handleError(error: HttpErrorResponse) {
// in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.log(error);
return throwError(error || 'Server error');
}
}