1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import 'rxjs/add/observable/of';
|
5
|
import 'rxjs/add/operator/do';
|
6
|
import 'rxjs/add/operator/share';
|
7
|
import { CacheService } from '../shared/cache.service';
|
8
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
9
|
import {SearchResult} from '../utils/entities/searchResult';
|
10
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
11
|
|
12
|
@Injectable()
|
13
|
export class SearchProjectsService {
|
14
|
private sizeOfDescription: number = 497;
|
15
|
|
16
|
constructor(private http: Http, public _cache: CacheService) {}
|
17
|
|
18
|
searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
|
19
|
|
20
|
console.info("In searchProjects");
|
21
|
|
22
|
let link = OpenaireProperties.getSearchAPIURLLast()+"projects";
|
23
|
|
24
|
let url = link+"?";
|
25
|
if(params!= null && params != '' ) {
|
26
|
url += params;
|
27
|
}
|
28
|
if(refineParams!= null && refineParams != '' ) {
|
29
|
url += refineParams;
|
30
|
}
|
31
|
url += "&page="+(page-1)+"&size="+size + "&format=json";
|
32
|
let key = url;
|
33
|
if (this._cache.has(key)) {
|
34
|
return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]);
|
35
|
}
|
36
|
|
37
|
return this.http.get(url)
|
38
|
.map(res => <any> res.json())
|
39
|
//.do(res => console.info(res))
|
40
|
.do(res => {
|
41
|
this._cache.set(key, res);
|
42
|
})
|
43
|
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]);
|
44
|
}
|
45
|
getProjectsforDataProvider (datasourceId: string, page: number, size: number ):any {
|
46
|
let url = OpenaireProperties.getSearchResourcesAPIURL();
|
47
|
var basicQuery = "(oaftype exact project) "
|
48
|
url += "?query=";
|
49
|
if(datasourceId!= null && datasourceId != '' ) {
|
50
|
url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
|
51
|
}else{
|
52
|
url +=" ( "+basicQuery+ " ) ";
|
53
|
}
|
54
|
|
55
|
url += "&page="+(page-1)+"&size="+size;
|
56
|
url += "&format=json";
|
57
|
let key = url;
|
58
|
if (this._cache.has(key)) {
|
59
|
return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
60
|
}
|
61
|
return this.http.get(url)
|
62
|
.map(res => <any> res.json())
|
63
|
.do(res => {
|
64
|
this._cache.set(key, res);
|
65
|
})
|
66
|
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
67
|
}
|
68
|
advancedSearchProjects (params: string, page: number, size: number ):any {
|
69
|
let url = OpenaireProperties.getSearchResourcesAPIURL();
|
70
|
var basicQuery = "(oaftype exact project) "
|
71
|
url += "?query=";
|
72
|
if(params!= null && params != '' ) {
|
73
|
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
|
74
|
}else{
|
75
|
url +=" ( "+basicQuery+ " ) ";
|
76
|
}
|
77
|
|
78
|
url += "&page="+(page-1)+"&size="+size;
|
79
|
url += "&format=json";
|
80
|
let key = url;
|
81
|
if (this._cache.has(key)) {
|
82
|
return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
83
|
}
|
84
|
return this.http.get(url)
|
85
|
.map(res => <any> res.json())
|
86
|
//.do(res => console.info(res))
|
87
|
.do(res => {
|
88
|
this._cache.set(key, res);
|
89
|
})
|
90
|
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
91
|
}
|
92
|
getFunders():any {
|
93
|
let url = OpenaireProperties.getSearchAPIURLLast()+"projects?refine=true&fields=funderid&size=0"+ "&format=json";;
|
94
|
let key = url;
|
95
|
if (this._cache.has(key)) {
|
96
|
return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, res['refineResults']['funderid']]);
|
97
|
}
|
98
|
return this.http.get(url)
|
99
|
.map(res => <any> res.json())
|
100
|
.do(res => {
|
101
|
this._cache.set(key, res);
|
102
|
})
|
103
|
.map(res => [res['meta'].total, res['refineResults']['funderid']]);
|
104
|
|
105
|
|
106
|
}
|
107
|
|
108
|
searchForProjectsObs(keyword:string, funderId:string):any {
|
109
|
let url = 'search?action=search&sTransformer=projects_openaire&query='+
|
110
|
'%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';
|
111
|
let key = url;
|
112
|
if (this._cache.has(key)) {
|
113
|
return Observable.of(this._cache.get(key));
|
114
|
}
|
115
|
return this.http.get(url).toPromise()
|
116
|
.then(request =>{
|
117
|
return (request.json().response.results)?request.json().response.results.result:request.json().response.result;
|
118
|
|
119
|
}) ;
|
120
|
}
|
121
|
parseResults(data: any): SearchResult[] {
|
122
|
let results: SearchResult[] = [];
|
123
|
|
124
|
let length = Array.isArray(data) ? data.length : 1;
|
125
|
|
126
|
for(let i=0; i<length; i++) {
|
127
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:project'] : data['result']['metadata']['oaf:entity']['oaf:project'];
|
128
|
|
129
|
var result: SearchResult = new SearchResult();
|
130
|
|
131
|
result['title'] = {"name": '', "url": '', "accessMode": ''};
|
132
|
if(Array.isArray(resData['title'])) {
|
133
|
// resData['title'][0].hasOwnProperty("content") {
|
134
|
result['title'].name = resData['title'][0];
|
135
|
// }
|
136
|
} else {
|
137
|
// resData['title'].hasOwnProperty("content") {
|
138
|
result['title'].name = resData['title'];
|
139
|
// }
|
140
|
}
|
141
|
|
142
|
// if(resData['title'].hasOwnProperty("content")) {
|
143
|
// result['title'].name = resData['title'].content;
|
144
|
// }
|
145
|
result['title'].url = OpenaireProperties.getsearchLinkToProject();
|
146
|
result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
147
|
|
148
|
if(resData['rels'].hasOwnProperty("rel")) {
|
149
|
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
|
150
|
|
151
|
for(let j=0; j<relLength; j++) {
|
152
|
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
|
153
|
|
154
|
if(relation.hasOwnProperty("to")) {
|
155
|
if(relation['to'].class == "hasParticipant") {
|
156
|
if(result['organizations'] == undefined) {
|
157
|
result['organizations'] = new Array<
|
158
|
{ "name": string, "url": string}>();
|
159
|
}
|
160
|
|
161
|
let countOrganizations = result['organizations'].length;
|
162
|
|
163
|
result['organizations'][countOrganizations] = { "name": "", "url": "" }
|
164
|
|
165
|
result['organizations'][countOrganizations]['url'] =
|
166
|
OpenaireProperties.getsearchLinkToOrganization() + relation['to'].content;
|
167
|
result['organizations'][countOrganizations]['name'] = relation.legalname;
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
if(resData.hasOwnProperty("fundingtree")) {
|
177
|
if(result['funders'] == undefined) {
|
178
|
result['funders'] = new Array<
|
179
|
{"funderShortname": string, "funderName": string}>();
|
180
|
}
|
181
|
|
182
|
let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
|
183
|
|
184
|
for(let z=0; z<fundingLength; z++) {
|
185
|
let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
|
186
|
let countFunders = result['funders'].length;
|
187
|
if(fundingData.hasOwnProperty("funder")) {
|
188
|
result['funders'][countFunders] = {"funderShortname": "", "funderName": ""};
|
189
|
result['funders'][countFunders]['funderShortname'] = fundingData['funder'].shortname;
|
190
|
result['funders'][countFunders]['funderName'] = fundingData['funder'].name;
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
if(resData.hasOwnProperty("startdate")) {
|
195
|
result.startYear = resData.startdate.split('-')[0];
|
196
|
}
|
197
|
if(resData.hasOwnProperty("enddate")) {
|
198
|
result.endYear = resData.enddate.split('-')[0];
|
199
|
}
|
200
|
|
201
|
results.push(result);
|
202
|
}
|
203
|
|
204
|
return results;
|
205
|
}
|
206
|
|
207
|
numOfEntityProjects(id: string, entity: string):any {
|
208
|
|
209
|
//OpenaireProperties.getSearchAPIURLLast()
|
210
|
//"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
|
211
|
let url = OpenaireProperties.getSearchAPIURLLast()+entity+id+"/projects/count"+ "?format=json";
|
212
|
let key = url;
|
213
|
if (this._cache.has(key)) {
|
214
|
return Observable.of(this._cache.get(key));
|
215
|
}
|
216
|
return this.http.get(url)
|
217
|
.map(res => <any> res.json())
|
218
|
.map(res => res.total)
|
219
|
.do(res => {
|
220
|
this._cache.set(key, res);
|
221
|
});
|
222
|
}
|
223
|
|
224
|
numOfSearchProjects(params: string):any {
|
225
|
|
226
|
//OpenaireProperties.getSearchAPIURLLast()
|
227
|
//"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
|
228
|
let url = OpenaireProperties.getSearchAPIURLLast()+"projects/count?q="+'"' + params + '"'+ "&format=json";
|
229
|
let key = url;
|
230
|
if (this._cache.has(key)) {
|
231
|
return Observable.of(this._cache.get(key));
|
232
|
}
|
233
|
return this.http.get(url)
|
234
|
.map(res => <any> res.json())
|
235
|
.map(res => res.total)
|
236
|
.do(res => {
|
237
|
this._cache.set(key, res);
|
238
|
});
|
239
|
}
|
240
|
}
|