1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
5
|
import 'rxjs/add/observable/of';
|
6
|
import 'rxjs/add/operator/do';
|
7
|
import 'rxjs/add/operator/share';
|
8
|
import { CacheService } from '../shared/cache.service';
|
9
|
|
10
|
@Injectable()
|
11
|
export class OpenaireProjectsService {
|
12
|
private searchUrl;
|
13
|
constructor( private http: Http, public _cache: CacheService) {
|
14
|
this.searchUrl = OpenaireProperties.getSearchServiceURL();
|
15
|
|
16
|
}
|
17
|
getFunders():any {
|
18
|
let url = this.searchUrl+'search?action=refine&rTransformer=results_openaire_browse&fields=funderid&query=%28oaftype+exact+project%29&locale=en_GB&format=json';
|
19
|
let key = url;
|
20
|
if (this._cache.has(key)) {
|
21
|
return Observable.of(this._cache.get(key));
|
22
|
}
|
23
|
|
24
|
return this.http.get( url)
|
25
|
.map(request => <any> request.json().response.browseResults.result)
|
26
|
.do(funders => console.log("getFunders : "+funders))
|
27
|
.catch(this.handleError)
|
28
|
.do(res => {
|
29
|
this._cache.set(key, res);
|
30
|
});
|
31
|
|
32
|
}
|
33
|
|
34
|
searchForProjects(keyword:string, funderId:string):any {
|
35
|
let url = this.searchUrl+'search?action=search&sTransformer=projects_openaire&query='+
|
36
|
'%28oaftype+exact+project%29+and+%28%28projecttitle+%3D+%22'+keyword+'%22%29+or+%28projectacronym+%3D+%22'+keyword+'%22%29+or+%28projectcode+%3D+%22'+keyword+'%22%29%29+and+%28funderid+exact+'+funderId+'%29&size=10&locale=en_GB&format=json';
|
37
|
let key = url;
|
38
|
if (this._cache.has(key)) {
|
39
|
return Observable.of(this._cache.get(key));
|
40
|
}
|
41
|
|
42
|
return this.http.get( url)
|
43
|
.map(request => <any> (request.json().response.results)?request.json().response.results.result:request.json().response.results)
|
44
|
.do(funders => console.log("getFunders : "+funders))
|
45
|
.catch(this.handleError)
|
46
|
.do(res => {
|
47
|
this._cache.set(key, res);
|
48
|
});
|
49
|
|
50
|
}
|
51
|
searchForProjectsObs(keyword:string, funderId:string):any {
|
52
|
let url = this.searchUrl+'search?action=search&sTransformer=projects_openaire&query='+
|
53
|
'%28oaftype+exact+project%29+and+%28%28projecttitle+%3D+%22'+keyword+'%22%29+or+%28projectacronym+%3D+%22'+keyword+'%22%29+or+%28projectcode+%3D+%22'+keyword+'%22%29%29+and+%28funderid+exact+'+funderId+'%29&size=10&locale=en_GB&format=json';
|
54
|
let key = url;
|
55
|
if (this._cache.has(key)) {
|
56
|
return Observable.of(this._cache.get(key));
|
57
|
}
|
58
|
|
59
|
return this.http.get(url).toPromise()
|
60
|
.then(request =>{
|
61
|
|
62
|
var valid:boolean= this.isJsonString(request);
|
63
|
if(valid==true){
|
64
|
return (request.json().response.results)?request.json().response.results.result:request.json().response.result;
|
65
|
}else{
|
66
|
return [];
|
67
|
}
|
68
|
})
|
69
|
}
|
70
|
|
71
|
private handleError (error: Response) {
|
72
|
// in a real world app, we may send the error to some remote logging infrastructure
|
73
|
// instead of just logging it to the console
|
74
|
console.error(error);
|
75
|
return Observable.throw(error || 'Server error');
|
76
|
}
|
77
|
private isJsonString(str) {
|
78
|
console.info("Check jsooon");
|
79
|
if(str instanceof Array || str instanceof Object) {
|
80
|
try {
|
81
|
str.json();
|
82
|
} catch (e) {
|
83
|
console.info("INValid");
|
84
|
return false;
|
85
|
}
|
86
|
console.info("valid");
|
87
|
return true
|
88
|
}
|
89
|
console.info("INValid");
|
90
|
return false
|
91
|
}
|
92
|
}
|