1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient} from "@angular/common/http";
|
3
|
|
4
|
|
5
|
|
6
|
import {SearchResult} from '../utils/entities/searchResult';
|
7
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
8
|
import{EnvProperties} from '../utils/properties/env-properties';
|
9
|
import {StringUtils} from '../utils/string-utils.class';
|
10
|
import {map} from "rxjs/operators";
|
11
|
@Injectable()
|
12
|
export class SearchProjectsService {
|
13
|
private sizeOfDescription: number = 270;
|
14
|
|
15
|
constructor(private http: HttpClient ) {}
|
16
|
|
17
|
searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
|
18
|
|
19
|
let link = properties.searchAPIURLLAst+"projects";
|
20
|
|
21
|
let url = link+"?";
|
22
|
if(params!= null && params != '' ) {
|
23
|
url += params;
|
24
|
}
|
25
|
if(refineParams!= null && refineParams != '' ) {
|
26
|
url += refineParams;
|
27
|
}
|
28
|
url += "&page="+(page-1)+"&size="+size + "&format=json";
|
29
|
|
30
|
console.log(url)
|
31
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
32
|
//.map(res => <any> res.json())
|
33
|
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
|
34
|
}
|
35
|
getProjectsforDataProvider (datasourceId: string, page: number, size: number, properties:EnvProperties ):any {
|
36
|
let url = properties.searchResourcesAPIURL;
|
37
|
var basicQuery = "(oaftype exact project) "
|
38
|
url += "?query=";
|
39
|
if(datasourceId!= null && datasourceId != '' ) {
|
40
|
url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\" or resulthostingdatasourceid exact \""+ datasourceId + "\")";
|
41
|
}else{
|
42
|
url +=" ( "+basicQuery+ " ) ";
|
43
|
}
|
44
|
|
45
|
url += "&page="+(page-1)+"&size="+size;
|
46
|
url += "&format=json";
|
47
|
|
48
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
49
|
//.map(res => <any> res.json())
|
50
|
.pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
|
51
|
}
|
52
|
advancedSearchProjects (params: string, page: number, size: number, properties:EnvProperties, refineParams:string=null, refineFields:string[] =null, refineQuery:string = null ):any {
|
53
|
// &type=projects
|
54
|
let url = properties.searchAPIURLLAst+"resources2/?format=json";
|
55
|
// var basicQuery = "(oaftype exact project) "
|
56
|
// url += "?query=";
|
57
|
if(params!= null && params != '' ) {
|
58
|
url +="&query=(" + params + ")";
|
59
|
}
|
60
|
// else{
|
61
|
// url +=" ( "+basicQuery+ " ) ";
|
62
|
// }
|
63
|
if(refineParams!= null && refineParams != '' ) {
|
64
|
url += refineParams;
|
65
|
}
|
66
|
if(refineQuery) {
|
67
|
url += "&" + refineQuery;
|
68
|
}
|
69
|
url += "&page="+(page-1)+"&size="+size;
|
70
|
// url += "&format=json";
|
71
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
72
|
//.map(res => <any> res.json())
|
73
|
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']), RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
|
74
|
}
|
75
|
|
76
|
getProjectsForOrganizations (organizationId: string, filterquery: string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
|
77
|
let url = properties.searchResourcesAPIURL;
|
78
|
var basicQuery = "(oaftype exact project) "
|
79
|
url += "?query=";
|
80
|
if(filterquery!= null && filterquery != '' ) {
|
81
|
url +="( ( "+basicQuery+ " ) and (relorganizationid exact \"" + organizationId + "\")"+" " + filterquery + ")";
|
82
|
}else{
|
83
|
url +=" (( "+basicQuery+ " ) " +" and (relorganizationid exact \"" + organizationId + "\"))";
|
84
|
}
|
85
|
if(refineFields!= null && refineFields.length > 0 ) {
|
86
|
url +="&refine=true";
|
87
|
for(let i=0; i< refineFields.length ; i++ ){
|
88
|
url +="&fields="+refineFields[i];
|
89
|
}
|
90
|
}
|
91
|
url += "&page="+(page-1)+"&size="+size;
|
92
|
url += "&format=json";
|
93
|
|
94
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
95
|
//.map(res => <any> res.json())
|
96
|
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
|
97
|
}
|
98
|
getFunders(properties:EnvProperties ):any {
|
99
|
let url = properties.searchAPIURLLAst+"projects?refine=true&fields=funder&size=0"+ "&format=json";
|
100
|
|
101
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
102
|
//.map(res => <any> res.json())
|
103
|
.pipe(map(res => [res['meta'].total, res['refineResults']['funder']]));
|
104
|
|
105
|
|
106
|
}
|
107
|
|
108
|
searchForProjectsObs(keyword:string, funderId:string, properties:EnvProperties ):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+%28funder+exact+'+funderId+'%29&size=10&locale=en_GB&format=json';
|
111
|
|
112
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).toPromise()
|
113
|
.then(request =>{
|
114
|
//return (request.json().response.results)?request.json().response.results.result:request.json().response.result;
|
115
|
return (request['response'].results)?request['response'].results.result:request['response'].result;
|
116
|
|
117
|
}) ;
|
118
|
}
|
119
|
parseResults(data: any): SearchResult[] {
|
120
|
let results: SearchResult[] = [];
|
121
|
|
122
|
let length = Array.isArray(data) ? data.length : 1;
|
123
|
|
124
|
for(let i=0; i<length; i++) {
|
125
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:project'] : data['result']['metadata']['oaf:entity']['oaf:project'];
|
126
|
|
127
|
var result: SearchResult = new SearchResult();
|
128
|
result.openAccessMandatePublications = (resData['oamandatepublications'])?true:false;
|
129
|
result.openAccessMandateDatasets = (resData['ecarticle29_3'])?true:false;
|
130
|
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
|
131
|
// if(resData['acronym'] != undefined && resData['acronym'] != "") {
|
132
|
// result['title'].name = resData['acronym'] + " - ";
|
133
|
// }
|
134
|
if(Array.isArray(resData['title'])) {
|
135
|
result['title'].name += resData['title'][0];
|
136
|
} else {
|
137
|
result['title'].name += resData['title'];
|
138
|
}
|
139
|
|
140
|
// if(result['title'].name != '') {
|
141
|
// result['title'].name += " ("+resData['code']+")"
|
142
|
// }
|
143
|
|
144
|
if(resData['ecsc39'] == true) {
|
145
|
result['title'].sc39 = "sc39";
|
146
|
}
|
147
|
|
148
|
result.acronym = resData['acronym'];
|
149
|
result.code = resData['code'];
|
150
|
|
151
|
result.budget = resData.totalcost;//"10000";
|
152
|
result.contribution = resData.fundedamount;//"200100";
|
153
|
result.currency = resData.currency;//"EUR";
|
154
|
|
155
|
result.id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
156
|
|
157
|
if(!Array.isArray(resData['summary'])) {
|
158
|
result.description = (resData['summary']) ? String(resData['summary']) : "";
|
159
|
} else {
|
160
|
result.description = (resData['summary'][0]) ? String(resData['summary'][0]) : "";
|
161
|
}
|
162
|
|
163
|
if (result.description && result.description.length > this.sizeOfDescription) {
|
164
|
result.description = result.description.substring(0, this.sizeOfDescription) + "...";
|
165
|
}
|
166
|
|
167
|
if(resData['rels'].hasOwnProperty("rel")) {
|
168
|
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
|
169
|
|
170
|
for(let j=0; j<relLength; j++) {
|
171
|
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
|
172
|
|
173
|
if(relation.hasOwnProperty("to")) {
|
174
|
if(relation['to'].class == "hasParticipant") {
|
175
|
if(result['organizations'] == undefined) {
|
176
|
result['organizations'] = new Array<
|
177
|
{ "name": string, "id": string}>();
|
178
|
}
|
179
|
|
180
|
let countOrganizations = result['organizations'].length;
|
181
|
|
182
|
result['organizations'][countOrganizations] = { "name": "", "id": "" }
|
183
|
|
184
|
result['organizations'][countOrganizations]['id'] =
|
185
|
/*OpenaireProperties.getsearchLinkToOrganization() + */relation['to'].content;
|
186
|
if(relation.legalshortname) {
|
187
|
result['organizations'][countOrganizations]['name'] = relation.legalshortname;
|
188
|
} else {
|
189
|
result['organizations'][countOrganizations]['name'] = relation.legalname;
|
190
|
}
|
191
|
if(!result['organizations'][countOrganizations]['name']) {
|
192
|
result['organizations'][countOrganizations]['name'] = "[no title available]";
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
}
|
198
|
if(resData.hasOwnProperty("fundingtree")) {
|
199
|
/*let funderSet: Set<string>;
|
200
|
if(result['funders'] == undefined) {
|
201
|
result['funders'] = new Array<
|
202
|
{"funderShortname": string, "funderName": string}>();
|
203
|
funderSet = new Set<string>();
|
204
|
}
|
205
|
|
206
|
let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
|
207
|
|
208
|
for(let z=0; z<fundingLength; z++) {
|
209
|
let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
|
210
|
if(fundingData.hasOwnProperty("funder")) {
|
211
|
if(!funderSet.has(fundingData['funder'].shortname)) {
|
212
|
let countFunders = result['funders'].length;
|
213
|
|
214
|
result['funders'][countFunders] = {"funderShortname": "", "funderName": ""};
|
215
|
result['funders'][countFunders]['funderShortname'] = fundingData['funder'].shortname;
|
216
|
result['funders'][countFunders]['funderName'] = fundingData['funder'].name;
|
217
|
funderSet.add(fundingData['funder'].shortname);
|
218
|
}
|
219
|
}
|
220
|
}*/
|
221
|
let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
|
222
|
|
223
|
for(let z=0; z<fundingLength; z++) {
|
224
|
let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
|
225
|
if(fundingData.hasOwnProperty("funder")) {
|
226
|
result['funderShortname'] = fundingData['funder'].shortname;
|
227
|
result['funderId'] = fundingData['funder'].id;
|
228
|
result['jurisdiction'] = (fundingData['funder']['id']['jurisdiction'] )?fundingData['funder']['id']['jurisdiction']:"";
|
229
|
|
230
|
if(fundingData['funding_level_2']){
|
231
|
result['fundingLevel0'] = (fundingData['funding_level_2'] && fundingData['funding_level_2']['parent'] &&
|
232
|
fundingData['funding_level_2']['parent']['funding_level_1'] && fundingData['funding_level_2']['parent']['funding_level_1']['parent']
|
233
|
&& fundingData['funding_level_2']['parent']['funding_level_1']['parent']['funding_level_0'])?
|
234
|
fundingData['funding_level_2']['parent']['funding_level_1']['parent']['funding_level_0']['name']:"";
|
235
|
}else if(fundingData['funding_level_1']){
|
236
|
result['fundingLevel0'] = (fundingData['funding_level_1'] && fundingData['funding_level_1']['parent'] && fundingData['funding_level_1']['parent']['funding_level_0'])?
|
237
|
fundingData['funding_level_1']['parent']['funding_level_0']['name']:"";
|
238
|
}else if(fundingData['funding_level_0']){
|
239
|
result['fundingLevel0'] = (fundingData['funding_level_0'] )?fundingData['funding_level_0']['name']:"";
|
240
|
}else {
|
241
|
result['fundingLevel0']="";
|
242
|
}
|
243
|
break;
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
if(resData.hasOwnProperty("startdate")) {
|
248
|
result.startYear = resData.startdate.split('-')[0];
|
249
|
}
|
250
|
if(resData.hasOwnProperty("enddate")) {
|
251
|
result.endYear = resData.enddate.split('-')[0];
|
252
|
}
|
253
|
|
254
|
results.push(result);
|
255
|
}
|
256
|
|
257
|
return results;
|
258
|
}
|
259
|
|
260
|
numOfProjects(url: string, properties:EnvProperties ):any {
|
261
|
|
262
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
263
|
//.map(res => <any> res.json())
|
264
|
.pipe(map(res => res['total']));
|
265
|
}
|
266
|
|
267
|
numOfEntityProjects(id: string, entity: string, properties:EnvProperties ):any {
|
268
|
//let url = properties.searchAPIURLLAst+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
|
269
|
var parameters: string = "";
|
270
|
if(entity == "organization") {
|
271
|
parameters = "organizations/"+id+"/projects/count";
|
272
|
}
|
273
|
|
274
|
let url = properties.searchAPIURLLAst+parameters+ "?format=json";
|
275
|
|
276
|
return this.numOfProjects(url, properties);
|
277
|
}
|
278
|
|
279
|
numOfSearchProjects(params: string, properties:EnvProperties, refineParams:string=null ):any {
|
280
|
let url = properties.searchAPIURLLAst+"projects/count?format=json";
|
281
|
if(params != "") {
|
282
|
url += "&q=" + StringUtils.URIEncode(params);
|
283
|
}
|
284
|
if(refineParams!= null && refineParams != '' ) {
|
285
|
url += refineParams;
|
286
|
}
|
287
|
return this.numOfProjects(url, properties);
|
288
|
}
|
289
|
numOfSearchProjects2(params: string, properties:EnvProperties, refineParams:string=null ):any {
|
290
|
let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=projects";
|
291
|
if(params != "") {
|
292
|
url += "&query=" + params;
|
293
|
}
|
294
|
if(refineParams!= null && refineParams != '' ) {
|
295
|
url += refineParams;
|
296
|
}
|
297
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
298
|
.pipe(map(res => res['meta']['total']));
|
299
|
}
|
300
|
}
|