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

102 lines
4.2 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {Observable, throwError, of} from 'rxjs';
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
import 'rxjs/add/observable/zip';
import {EnvProperties} from '../properties/env-properties';
import {catchError, map} from "rxjs/operators";
@Injectable()
export class ISVocabulariesService {
constructor(private http: HttpClient) {
}
getVocabularyByType(field: string, entity: string, properties: EnvProperties): 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);
} else if (field == "type" && (entity == "publication")) {
// file = "publicationTypes.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:publication_resource.json";
return this.getVocabularyFromService(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);
} else if (field == "type" && (entity == "software" || entity == "other")) {
return of([]);
} else if (field == "type" && entity == "result" ) {
return Observable.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);
} else if ((field == "type") && (entity == "dataprovider")) {
// file = "dataProviderType.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasource_typologies.json";
return this.getVocabularyFromService(vocabulary, properties);
} else if (field == "compatibility" && (entity == "dataprovider")) {
// file = "dataProviderCompatibility.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasourceCompatibilityLevel.json";
return this.getVocabularyFromService(vocabulary, properties);
} else if (field == "country") {
// file = "countries.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:countries.json";
return this.getVocabularyFromService(vocabulary, properties);
}
return null;
}
getVocabularyFromService(vocabularyName: string, properties: EnvProperties): any {
let url = properties.vocabulariesAPI + vocabularyName;
return this.http.get((properties.useCache) ? (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));
}
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;
}
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');
}
}