openaire-library/services/refineFieldResults.service.ts

67 lines
2.7 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
import {OpenaireProperties} from '../utils/properties/openaireProperties';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import { } from '../shared/cache.service';
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
@Injectable()
export class RefineFieldResultsService {
// scoobydoo.di.uoa.gr:8181/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/projects?refine=true&fields=funderid&page=1&size=0
constructor(private http: Http ) {}
getRefineFieldsResultsByEntityName(fields:string[], entityName:string):any{
let url = OpenaireProperties.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0";
for(var i=0; i < fields.length; i++){
url += "&fields="+fields[i];
}
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]);
}
getRefineFieldResultsByFieldName(fieldName:string, entityName:string):any{
let link = OpenaireProperties.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName + "&format=json";
return this.getField(link,fieldName)
}
getField (link:string,fieldName:string):any{
let url = link+"&refine=true&page=1&size=0";
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res['refineResults'])
.map(res => this.parse(res,fieldName));
}
parse(data: any,fieldName:string):any {
var values:AutoCompleteValue[] = [];
if(data){
let field = data[fieldName];
for(let i=0; i<field.length; i++) {
var value:AutoCompleteValue = new AutoCompleteValue();
value.label = field[i].name;
value.label = RefineResultsUtils.inParenthesisThePartAfterCharacters(field[i],"||");
value.id = field[i].id;
values.push(value);
}
}
return values;
}
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');
}
}