1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
|
5
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
6
|
import 'rxjs/add/observable/of';
|
7
|
import 'rxjs/add/operator/do';
|
8
|
import 'rxjs/add/operator/share';
|
9
|
import { } from '../shared/cache.service';
|
10
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
11
|
|
12
|
@Injectable()
|
13
|
export class RefineFieldResultsService {
|
14
|
// scoobydoo.di.uoa.gr:8181/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/projects?refine=true&fields=funderid&page=1&size=0
|
15
|
constructor(private http: Http ) {}
|
16
|
getRefineFieldsResultsByEntityName(fields:string[], entityName:string):any{
|
17
|
let url = OpenaireProperties.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0";
|
18
|
for(var i=0; i < fields.length; i++){
|
19
|
url += "&fields="+fields[i];
|
20
|
}
|
21
|
|
22
|
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
|
23
|
.map(res => <any> res.json())
|
24
|
|
25
|
.map(res => [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]);
|
26
|
|
27
|
}
|
28
|
getRefineFieldResultsByFieldName(fieldName:string, entityName:string):any{
|
29
|
let link = OpenaireProperties.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName + "&format=json";
|
30
|
return this.getField(link,fieldName)
|
31
|
|
32
|
}
|
33
|
|
34
|
getField (link:string,fieldName:string):any{
|
35
|
let url = link+"&refine=true&page=1&size=0";
|
36
|
|
37
|
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
|
38
|
.map(res => <any> res.json())
|
39
|
.map(res => res['refineResults'])
|
40
|
.map(res => this.parse(res,fieldName));
|
41
|
|
42
|
}
|
43
|
parse(data: any,fieldName:string):any {
|
44
|
var values:AutoCompleteValue[] = [];
|
45
|
if(data){
|
46
|
let field = data[fieldName];
|
47
|
for(let i=0; i<field.length; i++) {
|
48
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
49
|
value.label = field[i].name;
|
50
|
value.label = RefineResultsUtils.inParenthesisThePartAfterCharacters(field[i],"||");
|
51
|
value.id = field[i].id;
|
52
|
values.push(value);
|
53
|
|
54
|
}
|
55
|
}
|
56
|
|
57
|
return values;
|
58
|
}
|
59
|
|
60
|
private handleError (error: Response) {
|
61
|
// in a real world app, we may send the error to some remote logging infrastructure
|
62
|
// instead of just logging it to the console
|
63
|
console.log(error);
|
64
|
return Observable.throw(error || 'Server error');
|
65
|
}
|
66
|
}
|