2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
2020-11-11 15:43:13 +01:00
|
|
|
import {Observable, throwError, of, BehaviorSubject, from, Subscriber} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
|
2020-05-21 15:17:28 +02:00
|
|
|
import 'rxjs/add/observable/zip';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
import {EnvProperties} from '../properties/env-properties';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {catchError, map} from "rxjs/operators";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-07-13 00:10:43 +02:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2017-12-19 13:53:46 +01:00
|
|
|
export class ISVocabulariesService {
|
2020-07-13 00:10:43 +02:00
|
|
|
private vocabularies: Map<string, BehaviorSubject<AutoCompleteValue[]>> = new Map<string, BehaviorSubject<AutoCompleteValue[]>>();
|
|
|
|
private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null);
|
2020-11-11 15:43:13 +01:00
|
|
|
private subscriptions = [];
|
2020-07-13 00:10:43 +02:00
|
|
|
|
|
|
|
constructor(private http: HttpClient) {}
|
2020-11-11 15:43:13 +01:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSubscriptions() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-13 00:10:43 +02:00
|
|
|
getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable<any> {
|
2020-02-07 14:05:07 +01:00
|
|
|
//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";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "type" && (entity == "publication")) {
|
|
|
|
// file = "publicationTypes.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:publication_resource.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "type" && (entity == "dataset")) {
|
|
|
|
// file = "dnet:dataCite_resource.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:dataCite_resource.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "type" && (entity == "software" || entity == "other")) {
|
|
|
|
return of([]);
|
2020-05-21 15:17:28 +02:00
|
|
|
} else if (field == "type" && entity == "result" ) {
|
2020-07-13 00:10:43 +02:00
|
|
|
//return Observable.zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties));
|
|
|
|
return Observable.zip(from(this.getVocabularyFromServiceAsync("dnet:publication_resource.json", properties)),from(this.getVocabularyFromServiceAsync("dnet:dataCite_resource.json", properties)));
|
2020-04-15 20:54:56 +02:00
|
|
|
} else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) {
|
2020-02-07 14:05:07 +01:00
|
|
|
// file= "accessMode.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:access_modes.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if ((field == "type") && (entity == "dataprovider")) {
|
|
|
|
// file = "dataProviderType.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:datasource_typologies.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
|
|
|
|
} else if (field == "compatibility" && (entity == "dataprovider")) {
|
|
|
|
// file = "dataProviderCompatibility.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:datasourceCompatibilityLevel.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
|
|
|
|
} else if (field == "country") {
|
|
|
|
// file = "countries.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:countries.json";
|
2020-07-13 00:10:43 +02:00
|
|
|
//return this.getVocabularyFromService(vocabulary, properties);
|
|
|
|
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
|
2020-02-07 14:05:07 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-02-07 14:05:07 +01:00
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
2020-07-13 00:10:43 +02:00
|
|
|
|
|
|
|
async getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties): Promise<AutoCompleteValue[]> {
|
|
|
|
if(!this.vocabularies.has(vocabularyName)) {
|
|
|
|
await new Promise<any>(resolve => {
|
|
|
|
this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
|
|
|
|
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
|
2020-07-13 00:10:43 +02:00
|
|
|
vocabularyRes => {
|
|
|
|
this.vocabularies.get(vocabularyName).next(vocabularyRes);
|
|
|
|
resolve();
|
2020-08-11 14:16:50 +02:00
|
|
|
}, error => {
|
|
|
|
this.vocabularies.get(vocabularyName).next(null);
|
|
|
|
resolve();
|
2020-07-13 00:10:43 +02:00
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
));
|
2020-07-13 00:10:43 +02:00
|
|
|
});
|
2020-11-11 15:43:13 +01:00
|
|
|
this.clearSubscriptions();
|
2020-07-13 00:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.vocabularies.get(vocabularyName).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
|
2020-02-07 14:05:07 +01:00
|
|
|
let url = properties.vocabulariesAPI + vocabularyName;
|
2020-07-13 00:10:43 +02:00
|
|
|
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2020-02-07 14:05:07 +01:00
|
|
|
//.map(res => <any> res.json())
|
|
|
|
.pipe(map(res => res['terms']))
|
|
|
|
.pipe(map(res => this.parse(res, vocabularyName)))
|
|
|
|
.pipe(catchError(this.handleError));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-02-07 14:05:07 +01:00
|
|
|
|
|
|
|
return array;
|
|
|
|
|
|
|
|
}
|
2020-07-13 00:10:43 +02:00
|
|
|
|
|
|
|
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<any>(resolve => {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.push(this.getProvenanceActionVocabularyFromService(vocabularyName, properties).subscribe(
|
2020-07-13 00:10:43 +02:00
|
|
|
vocabularyRes => {
|
|
|
|
this.provenanceActionVocabulary.next(vocabularyRes);
|
|
|
|
resolve();
|
2020-07-13 12:13:42 +02:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.provenanceActionVocabulary.next(null);
|
|
|
|
resolve();
|
2020-07-13 00:10:43 +02:00
|
|
|
}
|
2020-11-11 15:43:13 +01:00
|
|
|
));
|
2020-07-13 00:10:43 +02:00
|
|
|
});
|
2020-11-11 15:43:13 +01:00
|
|
|
this.clearSubscriptions();
|
2020-07-13 00:10:43 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
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');
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|