2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-07-14 13:19:57 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
|
|
|
import {BehaviorSubject, from, Observable, of, Subscriber, throwError, zip} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
|
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
import {EnvProperties} from '../properties/env-properties';
|
2021-07-14 13:19:57 +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);
|
2022-06-09 15:45:39 +02:00
|
|
|
private subjectsVocabulary: BehaviorSubject<any> = new BehaviorSubject<any>(null);
|
2022-08-03 17:21:14 +02:00
|
|
|
private relationsVocabulary: BehaviorSubject<any> = new BehaviorSubject<any>(null);
|
2020-11-11 15:43:13 +01:00
|
|
|
private subscriptions = [];
|
2022-09-23 11:24:15 +02:00
|
|
|
private vocabulariesPromises: Map<string, Promise<void>> = new Map<string, Promise<void>>();
|
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> {
|
2022-06-09 15:52:57 +02:00
|
|
|
let file = "";
|
|
|
|
let vocabulary = "";
|
2020-02-07 14:05:07 +01:00
|
|
|
if (field == "lang") {
|
|
|
|
vocabulary = "dnet:languages.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "type" && (entity == "publication")) {
|
|
|
|
vocabulary = "dnet:publication_resource.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "type" && (entity == "dataset")) {
|
|
|
|
vocabulary = "dnet:dataCite_resource.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return 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" ) {
|
2022-01-13 15:47:33 +01:00
|
|
|
return zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("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
|
|
|
vocabulary = "dnet:access_modes.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2022-10-04 10:39:44 +02:00
|
|
|
} else if ((field == "type") && (entity == "dataprovider" || entity == "service")) {
|
2020-02-07 14:05:07 +01:00
|
|
|
vocabulary = "dnet:datasource_typologies.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2022-10-04 10:39:44 +02:00
|
|
|
} else if (field == "compatibility" && (entity == "dataprovider" || entity == "service")) {
|
2020-02-07 14:05:07 +01:00
|
|
|
vocabulary = "dnet:datasourceCompatibilityLevel.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2020-02-07 14:05:07 +01:00
|
|
|
} else if (field == "country") {
|
|
|
|
vocabulary = "dnet:countries.json";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2022-03-18 12:14:07 +01:00
|
|
|
} else if (field == "fos") {
|
|
|
|
vocabulary = "fos";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2022-03-18 12:14:07 +01:00
|
|
|
} else if (field == "sdg") {
|
|
|
|
vocabulary = "sdg";
|
2022-06-09 15:52:57 +02:00
|
|
|
return this.getVocabularyFromServiceAsync(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-02-07 14:05:07 +01:00
|
|
|
return null;
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
}
|
2020-07-13 00:10:43 +02:00
|
|
|
|
2022-06-09 15:52:57 +02:00
|
|
|
getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties) {
|
2020-07-13 00:10:43 +02:00
|
|
|
if(!this.vocabularies.has(vocabularyName)) {
|
2022-06-09 15:52:57 +02:00
|
|
|
this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
|
|
|
|
if(!this.vocabulariesPromises.has(vocabularyName)) {
|
|
|
|
this.vocabulariesPromises.set(vocabularyName,
|
2022-09-23 11:24:15 +02:00
|
|
|
new Promise<void>(resolve => {
|
2022-06-09 15:52:57 +02:00
|
|
|
this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
|
|
|
|
vocabularyRes => {
|
|
|
|
this.vocabularies.get(vocabularyName).next(vocabularyRes);
|
|
|
|
resolve();
|
|
|
|
}, error => {
|
|
|
|
this.vocabularies.get(vocabularyName).next(null);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}));
|
|
|
|
}
|
2020-07-13 00:10:43 +02:00
|
|
|
}
|
2022-06-09 15:52:57 +02:00
|
|
|
return from(this.getAsync(vocabularyName));
|
|
|
|
}
|
|
|
|
async getAsync(vocabularyName: string): Promise<AutoCompleteValue[]> {
|
|
|
|
await this.vocabulariesPromises.get(vocabularyName);
|
2020-07-13 00:10:43 +02:00
|
|
|
return this.vocabularies.get(vocabularyName).getValue();
|
|
|
|
}
|
2022-06-09 15:52:57 +02:00
|
|
|
|
2020-07-13 00:10:43 +02:00
|
|
|
getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
|
2020-02-07 14:05:07 +01:00
|
|
|
let url = properties.vocabulariesAPI + vocabularyName;
|
2022-03-18 12:14:07 +01:00
|
|
|
if(vocabularyName == 'fos' || vocabularyName == 'sdg'){
|
|
|
|
return this.getLocalVocabularyFromService(vocabularyName, properties);
|
|
|
|
}
|
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));
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
}
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2022-03-18 12:14:07 +01:00
|
|
|
getLocalVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
|
|
|
|
if(vocabularyName == "sdg"){
|
2023-04-03 11:53:40 +02:00
|
|
|
return this.getSDGs(properties)
|
2022-03-18 12:14:07 +01:00
|
|
|
.pipe(map(res => res['sdg']))
|
|
|
|
.pipe(map(res => this.parseSDGs(res)))
|
|
|
|
.pipe(catchError(this.handleError));
|
|
|
|
}else if( vocabularyName == "fos"){
|
2023-04-03 11:53:40 +02:00
|
|
|
return this.getFos(properties)
|
2022-03-18 12:14:07 +01:00
|
|
|
.pipe(map(res => res['fos']))
|
|
|
|
.pipe(map(res => this.parseFOS(res)))
|
|
|
|
.pipe(catchError(this.handleError));
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 11:53:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
getFos(properties: EnvProperties): Observable<any> {
|
|
|
|
let url = "/assets/common-assets/vocabulary/fos.json";
|
2023-07-05 10:40:43 +02:00
|
|
|
return this.http.get(url).pipe(map(res => (res && res['fos']) ? res : {fos: []}));
|
2023-04-03 11:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getSDGs(properties: EnvProperties): Observable<any> {
|
|
|
|
let url = "/assets/common-assets/vocabulary/sdg.json";
|
2023-05-27 21:48:13 +02:00
|
|
|
return this.http.get(url).pipe(map(res => (res && res['sdg'])?res:{sdg: []}));
|
2023-04-03 11:53:40 +02:00
|
|
|
}
|
2022-03-18 12:14:07 +01:00
|
|
|
|
|
|
|
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;
|
2022-03-24 11:57:33 +01:00
|
|
|
value.label = data[i].id;
|
2022-03-18 12:14:07 +01:00
|
|
|
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);
|
2022-03-24 11:57:33 +01:00
|
|
|
if(fos.children) {
|
|
|
|
for (let fos2 of fos.children) {
|
2022-03-18 12:14:07 +01:00
|
|
|
let value: AutoCompleteValue = new AutoCompleteValue();
|
2022-03-24 11:57:33 +01:00
|
|
|
value.id = fos2.id;//data[i].code;
|
|
|
|
value.label = fos2.label;
|
2022-03-18 12:14:07 +01:00
|
|
|
array.push(value);
|
2022-03-24 11:57:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 12:14:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2023-04-03 11:53:40 +02:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
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
|
|
|
}
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
return array;
|
2022-01-13 15:47:33 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
}
|
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()) {
|
2022-09-23 11:24:15 +02:00
|
|
|
await new Promise<void>(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
|
|
|
});
|
2022-06-09 15:45:39 +02: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;
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:45:39 +02:00
|
|
|
getSubjectsVocabulary(properties: EnvProperties): Observable<any> {
|
|
|
|
let vocabulary = "dnet:subject_classification_typologies.json";
|
|
|
|
return from(this.getSubjectsVocabularyFromServiceAsync(vocabulary, properties));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getSubjectsVocabularyFromServiceAsync (vocabularyName: string, properties: EnvProperties): Promise<{}> {
|
|
|
|
if(!this.subjectsVocabulary || !this.subjectsVocabulary.getValue()) {
|
2022-09-23 11:24:15 +02:00
|
|
|
await new Promise<void>(resolve => {
|
2022-06-09 15:45:39 +02:00
|
|
|
this.subscriptions.push(this.getSubjectsVocabularyFromService(vocabularyName, properties).subscribe(
|
|
|
|
vocabularyRes => {
|
|
|
|
// console.log(vocabularyRes);
|
|
|
|
this.subjectsVocabulary.next(vocabularyRes);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.subjectsVocabulary.next(null);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
});
|
|
|
|
// this.clearSubscriptions();
|
|
|
|
}
|
|
|
|
return this.subjectsVocabulary.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
getSubjectsVocabularyFromService (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 => res.code))
|
|
|
|
.pipe(map(res => this.parseSubjectsVocabulary(res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
parseSubjectsVocabulary(terms: any) {
|
|
|
|
var subjectsVocabulary: {} = {};
|
|
|
|
for(let term of terms) {
|
|
|
|
if(term.code != "keyword") {
|
|
|
|
subjectsVocabulary[term.code] = term.englishName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return subjectsVocabulary;
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:21:14 +02:00
|
|
|
getRelationsVocabulary(properties: EnvProperties): Observable<any> {
|
|
|
|
let vocabulary = "dnet:relation_relClass.json";
|
|
|
|
return from(this.getRelationsVocabularyFromServiceAsync(vocabulary, properties));
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRelationsVocabularyFromServiceAsync (vocabularyName: string, properties: EnvProperties): Promise<{}> {
|
|
|
|
if(!this.relationsVocabulary || !this.relationsVocabulary.getValue()) {
|
2022-09-23 11:24:15 +02:00
|
|
|
await new Promise<void>(resolve => {
|
2022-08-03 17:21:14 +02:00
|
|
|
this.subscriptions.push(this.getRelationsVocabularyFromService(vocabularyName, properties).subscribe(
|
|
|
|
vocabularyRes => {
|
|
|
|
this.relationsVocabulary.next(vocabularyRes);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.relationsVocabulary.next(null);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this.relationsVocabulary.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
getRelationsVocabularyFromService (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.parseRelationsVocabulary(res)));
|
|
|
|
}
|
|
|
|
|
|
|
|
parseRelationsVocabulary(terms: any) {
|
|
|
|
var relationsVocabulary: {} = {};
|
|
|
|
for(let term of terms) {
|
|
|
|
if(term.code != "keyword") {
|
|
|
|
relationsVocabulary[term.code] = term.englishName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relationsVocabulary;
|
|
|
|
}
|
|
|
|
|
2020-07-13 00:10:43 +02:00
|
|
|
|
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
|
|
|
}
|