2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Http, Response} from '@angular/http';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class';
|
|
|
|
import 'rxjs/add/observable/of';
|
|
|
|
import 'rxjs/add/operator/do';
|
|
|
|
import 'rxjs/add/operator/share';
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../properties/env-properties';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ISVocabulariesService {
|
|
|
|
constructor(private http: Http ) {}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
getVocabularyByType(field:string,entity:string, properties:EnvProperties):any{
|
2017-12-19 13:53:46 +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";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
}else if ( field == "type" && (entity == "publication")){
|
|
|
|
// file = "publicationTypes.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:publication_resource.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
}else if ( field == "type" && (entity == "dataset")){
|
|
|
|
// file = "dnet:dataCite_resource.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:dataCite_resource.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2018-07-25 15:27:22 +02:00
|
|
|
}else if ( field == "type" && (entity == "software" || entity == "other")){
|
2018-03-22 13:13:48 +01:00
|
|
|
return Observable.of([]);
|
|
|
|
|
2018-07-25 15:27:22 +02:00
|
|
|
}else if( field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other")){
|
2017-12-19 13:53:46 +01:00
|
|
|
// file= "accessMode.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:access_modes.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
} else if( (field == "type") && (entity == "dataprovider")){
|
|
|
|
// file = "dataProviderType.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:datasource_typologies.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
} else if( field == "compatibility" && (entity == "dataprovider")){
|
|
|
|
// file = "dataProviderCompatibility.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:datasourceCompatibilityLevel.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
} else if( field == "country" ){
|
|
|
|
// file = "countries.json";
|
|
|
|
// return this.getVocabularyFromFile(file);
|
|
|
|
vocabulary = "dnet:countries.json";
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.getVocabularyFromService(vocabulary, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
// getVocabularyFromFile (file:string):AutoCompleteValue[] {
|
|
|
|
// var lang = JSON.parse(JSON.stringify(require('../utils/vocabularies/'+file)));
|
|
|
|
// return this.parse(lang["terms"]);
|
|
|
|
// }
|
2018-02-05 14:14:59 +01:00
|
|
|
getVocabularyFromService (vocabularyName:string, properties:EnvProperties):any {
|
|
|
|
let url = properties.vocabulariesAPI + vocabularyName;
|
2017-12-19 13:53:46 +01:00
|
|
|
console.log(url);
|
|
|
|
|
|
|
|
// return this.http.get(url).toPromise()
|
|
|
|
// .then(request =>
|
|
|
|
// {
|
|
|
|
// request = request.json()['terms'];
|
|
|
|
// var results:AutoCompleteValue[] = this.parse(request);
|
|
|
|
// console.log("Get vocabulary : "+ vocabularyName+ " - get " +results.length+ "results");
|
|
|
|
// return results;
|
|
|
|
// });
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2017-12-19 13:53:46 +01:00
|
|
|
.do(res => console.log(res))
|
|
|
|
.map(res => <any> res.json())
|
|
|
|
.map(res => res['terms'])
|
|
|
|
.do(res => console.log(res))
|
|
|
|
.map(res => this.parse(res))
|
|
|
|
.do(res => console.log(res))
|
|
|
|
.catch(this.handleError);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
parse (data: any):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;
|
|
|
|
value.label = data[i].englishName;
|
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
|
|
|
}
|
|
|
|
private handleError (error: Response) {
|
|
|
|
// 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 Observable.throw(error || 'Server error');
|
|
|
|
}
|
|
|
|
}
|