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
|
import 'rxjs/add/observable/of';
|
8
|
import 'rxjs/add/operator/do';
|
9
|
import 'rxjs/add/operator/share';
|
10
|
import { CacheService } from '../shared/cache.service';
|
11
|
@Injectable()
|
12
|
export class SearchDatasetsService {
|
13
|
private sizeOfDescription: number = 497;
|
14
|
|
15
|
constructor(private http: Http, public _cache: CacheService) {}
|
16
|
|
17
|
searchDatasets (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
|
18
|
|
19
|
let link = OpenaireProperties.getSearchAPIURLLast()+"datasets";
|
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
|
let key = url;
|
31
|
if (this._cache.has(key)) {
|
32
|
return Observable.of(this._cache.get(key));
|
33
|
}
|
34
|
return this.http.get(url)
|
35
|
.map(res => <any> res.json())
|
36
|
//.do(res => console.info(res))
|
37
|
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)])
|
38
|
.do(res => {
|
39
|
this._cache.set(key, res);
|
40
|
});
|
41
|
}
|
42
|
|
43
|
advancedSearchDatasets (params: string, page: number, size: number ):any {
|
44
|
let url = OpenaireProperties.getSearchResourcesAPIURL();
|
45
|
var basicQuery = "(oaftype exact result) and (resulttypeid exact dataset) "
|
46
|
url += "?query=";
|
47
|
if(params!= null && params != '' ) {
|
48
|
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
|
49
|
}else{
|
50
|
url +=" ( "+basicQuery+ " ) ";
|
51
|
}
|
52
|
|
53
|
url += "&page="+(page-1)+"&size="+size;
|
54
|
url += "&format=json";
|
55
|
let key = url;
|
56
|
if (this._cache.has(key)) {
|
57
|
return Observable.of(this._cache.get(key));
|
58
|
}
|
59
|
return this.http.get(url)
|
60
|
.map(res => <any> res.json())
|
61
|
//.do(res => console.info(res))
|
62
|
.map(res => [res['meta'].total, this.parseResults(res['results'])])
|
63
|
.do(res => {
|
64
|
this._cache.set(key, res);
|
65
|
});
|
66
|
}
|
67
|
searchDatasetsForEntity (params: string, page: number, size: number):any {
|
68
|
let link = OpenaireProperties.getSearchAPIURLLast();
|
69
|
let url = link+params+"/datasets"+"?format=json";
|
70
|
let key = url;
|
71
|
if (this._cache.has(key)) {
|
72
|
return Observable.of(this._cache.get(key));
|
73
|
}
|
74
|
return this.http.get(url)
|
75
|
.map(res => <any> res.json())
|
76
|
.map(res => [res['meta'].total, this.parseResults(res['results'])])
|
77
|
.do(res => {
|
78
|
this._cache.set(key, res);
|
79
|
});
|
80
|
}
|
81
|
|
82
|
searchDatasetsForDataproviders(params: string, page: number, size: number):any {
|
83
|
let link = OpenaireProperties.getSearchAPIURLLast();
|
84
|
let url = link+params+"&format=json";
|
85
|
let key = url;
|
86
|
if (this._cache.has(key)) {
|
87
|
return Observable.of(this._cache.get(key));
|
88
|
}
|
89
|
return this.http.get(url)
|
90
|
.map(res => <any> res.json())
|
91
|
.map(res => [res['meta'].total, this.parseResults(res['results'])])
|
92
|
.do(res => {
|
93
|
this._cache.set(key, res);
|
94
|
});
|
95
|
}
|
96
|
|
97
|
parseResults(data: any): SearchResult[] {
|
98
|
let results: SearchResult[] = [];
|
99
|
|
100
|
let length = Array.isArray(data) ? data.length : 1;
|
101
|
|
102
|
for(let i=0; i<length; i++) {
|
103
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
|
104
|
|
105
|
var result: SearchResult = new SearchResult();
|
106
|
|
107
|
result['title'] = {"name": '', "url": '', "accessMode": ''};
|
108
|
|
109
|
if(Array.isArray(resData['title'])) {
|
110
|
result['title'].name = resData['title'][0].content;
|
111
|
} else {
|
112
|
result['title'].name = resData['title'].content;
|
113
|
}
|
114
|
|
115
|
result['title'].url = OpenaireProperties.getsearchLinkToDataset();
|
116
|
result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
117
|
result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
118
|
if(resData['bestlicense'].hasOwnProperty("classid")) {
|
119
|
result['title'].accessMode = resData['bestlicense'].classid;
|
120
|
}
|
121
|
|
122
|
if(resData['rels'].hasOwnProperty("rel")) {
|
123
|
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
|
124
|
|
125
|
for(let j=0; j<relLength; j++) {
|
126
|
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
|
127
|
|
128
|
if(relation.hasOwnProperty("to")) {
|
129
|
if(relation['to'].class == "hasAuthor") {
|
130
|
if(result['authors'] == undefined) {
|
131
|
result['authors'] = new Array<{"name": string, "url": string}>();
|
132
|
}
|
133
|
|
134
|
result['authors'].push({"name": relation.fullname, "url": OpenaireProperties.getsearchLinkToPerson()+relation['to'].content});
|
135
|
} else if(relation['to'].class == "isProducedBy") {
|
136
|
if(result['projects'] == undefined) {
|
137
|
result['projects'] = new Array<
|
138
|
{ "url": string, "acronym": string, "title": string,
|
139
|
"funderShortname": string, "funderName": string,
|
140
|
"code": string
|
141
|
}>();
|
142
|
}
|
143
|
|
144
|
let countProjects = result['projects'].length;
|
145
|
|
146
|
result['projects'][countProjects] = {
|
147
|
"url": "", "acronym": "", "title": "",
|
148
|
"funderShortname": "", "funderName": "",
|
149
|
"code": ""
|
150
|
}
|
151
|
|
152
|
result['projects'][countProjects]['url'] =
|
153
|
OpenaireProperties.getsearchLinkToProject() + relation['to'].content;
|
154
|
result['projects'][countProjects]['acronym'] = relation.acronym;
|
155
|
result['projects'][countProjects]['title'] = relation.title;
|
156
|
result['projects'][countProjects]['code'] = relation.code;
|
157
|
|
158
|
if(relation.hasOwnProperty("funding")) {
|
159
|
let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
|
160
|
|
161
|
for(let z=0; z<fundingLength; z++) {
|
162
|
let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
|
163
|
|
164
|
if(fundingData.hasOwnProperty("funder")) {
|
165
|
result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
|
166
|
result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
|
176
|
result.year = resData.dateofacceptance.split('-')[0];
|
177
|
|
178
|
if(!Array.isArray(resData.description)) {
|
179
|
result.description = resData.description;
|
180
|
} else {
|
181
|
result.description = resData.description[0];
|
182
|
}
|
183
|
if(result.description.length > this.sizeOfDescription) {
|
184
|
result.description = result.description.substring(0, this.sizeOfDescription)+"...";
|
185
|
}
|
186
|
|
187
|
result.embargoEndDate = resData.embargoenddate;
|
188
|
|
189
|
if(!Array.isArray(resData.publisher)) {
|
190
|
result.publisher = resData.publisher;
|
191
|
} else {
|
192
|
for(let i=0; i<resData.publisher.length; i++) {
|
193
|
if(result.publisher != undefined){
|
194
|
result.publisher += ', '+resData['publisher'][i];
|
195
|
} else {
|
196
|
result.publisher = resData['publisher'][i];
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
|
201
|
results.push(result);
|
202
|
}
|
203
|
|
204
|
return results;
|
205
|
}
|
206
|
|
207
|
numOfEntityDatasets(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+"/datasets/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
|
numOfSearchDatasets(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()+"datasets/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
|
}
|