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 {SearchResult} from '../utils/entities/searchResult';
|
6
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
7
|
|
8
|
@Injectable()
|
9
|
export class SearchProjectsService {
|
10
|
private sizeOfDescription: number = 497;
|
11
|
|
12
|
constructor(private http: Http) {}
|
13
|
|
14
|
searchProjects (params: string, page: number, size: number, refineFields:string[] ):any {
|
15
|
|
16
|
console.info("In searchProjects");
|
17
|
|
18
|
let link = OpenaireProperties.getSearchAPIURL()+"projects";
|
19
|
|
20
|
let url = link+"?";
|
21
|
if(params != '') {
|
22
|
url += params+"&page="+page+"&size="+size;
|
23
|
} else {
|
24
|
url += "page="+page+"&size="+size;
|
25
|
}
|
26
|
|
27
|
return this.http.get(url)
|
28
|
.map(res => <any> res.json())
|
29
|
//.do(res => console.info(res))
|
30
|
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)]);
|
31
|
}
|
32
|
|
33
|
|
34
|
parseResults(data: any): SearchResult[] {
|
35
|
let results: SearchResult[] = [];
|
36
|
|
37
|
let length = Array.isArray(data) ? data.length : 1;
|
38
|
|
39
|
for(let i=0; i<length; i++) {
|
40
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:project'] : data['result']['metadata']['oaf:entity']['oaf:project'];
|
41
|
|
42
|
var result: SearchResult = new SearchResult();
|
43
|
|
44
|
result['title'] = {"name": '', "url": '', "accessMode": ''};
|
45
|
if(Array.isArray(resData['title'])) {
|
46
|
// resData['title'][0].hasOwnProperty("content") {
|
47
|
result['title'].name = resData['title'][0];
|
48
|
// }
|
49
|
} else {
|
50
|
// resData['title'].hasOwnProperty("content") {
|
51
|
result['title'].name = resData['title'];
|
52
|
// }
|
53
|
}
|
54
|
|
55
|
// if(resData['title'].hasOwnProperty("content")) {
|
56
|
// result['title'].name = resData['title'].content;
|
57
|
// }
|
58
|
result['title'].url = OpenaireProperties.getsearchLinkToProject();
|
59
|
result['title'].url += length > 1 ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
60
|
|
61
|
if(resData['rels'].hasOwnProperty("rel")) {
|
62
|
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
|
63
|
|
64
|
for(let j=0; j<relLength; j++) {
|
65
|
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
|
66
|
|
67
|
if(relation.hasOwnProperty("to")) {
|
68
|
if(relation['to'].class == "hasParticipant") {
|
69
|
if(result['organizations'] == undefined) {
|
70
|
result['organizations'] = new Array<
|
71
|
{ "name": string, "url": string}>();
|
72
|
}
|
73
|
|
74
|
let countOrganizations = result['organizations'].length;
|
75
|
|
76
|
result['organizations'][countOrganizations] = { "name": "", "url": "" }
|
77
|
|
78
|
result['organizations'][countOrganizations]['url'] =
|
79
|
OpenaireProperties.getsearchLinkToOrganization() + relation['to'].content;
|
80
|
result['organizations'][countOrganizations]['name'] = relation.legalname;
|
81
|
|
82
|
|
83
|
|
84
|
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
if(resData.hasOwnProperty("fundingtree")) {
|
90
|
if(result['funders'] == undefined) {
|
91
|
result['funders'] = new Array<
|
92
|
{"funderShortname": string, "funderName": string}>();
|
93
|
}
|
94
|
|
95
|
let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
|
96
|
|
97
|
for(let z=0; z<fundingLength; z++) {
|
98
|
let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
|
99
|
let countFunders = result['funders'].length;
|
100
|
if(fundingData.hasOwnProperty("funder")) {
|
101
|
result['funders'][countFunders] = {"funderShortname": "", "funderName": ""};
|
102
|
result['funders'][countFunders]['funderShortname'] = fundingData['funder'].shortname;
|
103
|
result['funders'][countFunders]['funderName'] = fundingData['funder'].name;
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
if(resData.hasOwnProperty("startdate")) {
|
108
|
result.startYear = resData.startdate.split('-')[0];
|
109
|
}
|
110
|
if(resData.hasOwnProperty("enddate")) {
|
111
|
result.endYear = resData.enddate.split('-')[0];
|
112
|
}
|
113
|
|
114
|
results.push(result);
|
115
|
}
|
116
|
|
117
|
return results;
|
118
|
}
|
119
|
}
|