Project

General

Profile

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

    
9
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10
import {SearchResult}     from '../utils/entities/searchResult';
11
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
12

    
13
@Injectable()
14
export class SearchPublicationsService {
15
    private sizeOfDescription: number = 497;
16

    
17
    constructor(private http: Http, public _cache: CacheService) {}
18

    
19
    searchPublications (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
20

    
21
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
22

    
23
        let url = link+"?";
24
        if(params!= null && params != ''  ) {
25
            url += params;
26
        }
27
        if(refineParams!= null && refineParams != ''  ) {
28
            url += refineParams;
29
        }
30
        url += "&page="+(page-1)+"&size="+size+"&format=json";
31

    
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, "publication")]);
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, "publication")]);
44
    }
45

    
46
    searchAggregators (params: string, refineParams:string, page: number, size: number ):any {
47

    
48
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
49

    
50
        let url = link+"?"+"&format=json";
51
        if(params!= null && params != ''  ) {
52
            url += params;
53
        }
54
        if(refineParams!= null && refineParams != ''  ) {
55
            url += refineParams;
56
        }
57
        url += "&page="+(page-1)+"&size="+size;
58

    
59
        let key = url;
60
        if (this._cache.has(key)) {
61
          return Observable.of(this._cache.get(key)).map(res => this.parseRefineResults(res['refineResults']));
62
        }
63

    
64
        return this.http.get(url)
65
                    .map(res => <any> res.json())
66
                    .do(res => {
67
                      this._cache.set(key, res);
68
                    })
69
                    .map(res => this.parseRefineResults(res['refineResults']));
70
    }
71

    
72
    searchPublicationsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[] ):any {
73

    
74
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
75

    
76
        let url = link+"?"+"&format=json&";
77
        var doisParams = "";
78

    
79
        for(var i =0 ;i < DOIs.length; i++){
80
          doisParams+=(doisParams.length > 0?" or ":"")+'((pidclassid exact doi) and (pid exact "'+ DOIs[i]+'"))';
81
        }
82
        if(doisParams.length > 0){
83
          url += "q=("+doisParams+")"
84
        }
85
        if(refineParams!= null && refineParams != ''  ) {
86
            url += refineParams;
87
        }
88
        url += "&page="+(page-1)+"&size="+size;
89

    
90
        let key = url;
91
        if (this._cache.has(key)) {
92
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
93
        }
94

    
95
        return this.http.get(url)
96
                    .map(res => <any> res.json())
97
                    //.do(res => console.info(res))
98
                    .do(res => {
99
                      this._cache.set(key, res);
100
                    })
101
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
102
    }
103

    
104
    advancedSearchPublications (params: string, page: number, size: number ):any {
105
      let url = OpenaireProperties.getSearchResourcesAPIURL();
106
      var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
107
      url += "?query=";
108
      if(params!= null && params != ''  ) {
109
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
110
      }else{
111
        url +=" ( "+basicQuery+ " ) ";
112
      }
113

    
114
      url += "&page="+(page-1)+"&size="+size;
115
      url += "&format=json";
116
      let key = url;
117
      if (this._cache.has(key)) {
118
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
119
      }
120
      return this.http.get(url)
121
      .map(res => <any> res.json())
122
      //.do(res => console.info(res))
123
      .do(res => {
124
        this._cache.set(key, res);
125
      })
126
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
127
    }
128
    searchPublicationsForEntity (params: string, page: number, size: number):any {
129
        let link = OpenaireProperties.getSearchAPIURLLast();
130
        let url = link+params+"/publications"+ "?format=json";
131

    
132
        let key = url;
133
        if (this._cache.has(key)) {
134
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
135
        }
136

    
137
        return this.http.get(url)
138
                    .map(res => <any> res.json())
139
                    .do(res => {
140
                      this._cache.set(key, res);
141
                    })
142
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
143
    }
144

    
145
    searchPublicationsForDataproviders(params: string, page: number, size: number):any {
146
        let link = OpenaireProperties.getSearchAPIURLLast();
147
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
148
        let key = url;
149
        if (this._cache.has(key)) {
150
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
151
        }
152
        return this.http.get(url)
153
                    .map(res => <any> res.json())
154
                    .do(res => {
155
                      this._cache.set(key, res);
156
                    })
157
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
158
    }
159

    
160
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
161

    
162
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
163

    
164
        let url = link+"?";
165
        if(params!= null && params != ''  ) {
166
            url += params;
167
        }
168
        if(refineParams!= null && refineParams != ''  ) {
169
            url += refineParams;
170
        }
171
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
172

    
173
        let key = url;
174
        if (this._cache.has(key)) {
175
          return Observable.of(this._cache.get(key)).map(res => this.parseResultsCSV(res['results']));
176
        }
177

    
178
        return this.http.get(url)
179
                    .map(res => <any> res.json())
180
                    //.do(res => console.info(res))
181
                    .do(res => {
182
                      this._cache.set(key, res);
183
                    })
184
                    .map(res => this.parseResultsCSV(res['results']));
185
    }
186

    
187

    
188
    parseResults(data: any): SearchResult[] {
189
        let results: SearchResult[] = [];
190

    
191
        let length = Array.isArray(data) ? data.length : 1;
192

    
193
        for(let i=0; i<length; i++) {
194
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
195

    
196
            var result: SearchResult = new SearchResult();
197

    
198
            result['title'] = {"name": '', "url": '', "accessMode": ''};
199

    
200
            if(Array.isArray(resData['title'])) {
201
                // resData['title'][0].hasOwnProperty("content") {
202
                    result['title'].name = resData['title'][0].content;
203
                // }
204
            } else {
205
                // resData['title'].hasOwnProperty("content") {
206
                    result['title'].name = resData['title'].content;
207
                // }
208
            }
209

    
210
            result['title'].url = OpenaireProperties.getsearchLinkToPublication();
211
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
212
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
213
            if(resData['bestlicense'].hasOwnProperty("classid")) {
214
                result['title'].accessMode = resData['bestlicense'].classid;
215
            }
216

    
217
            if(resData['rels'].hasOwnProperty("rel")) {
218
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
219

    
220
                for(let j=0; j<relLength; j++) {
221
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
222

    
223
                    if(relation.hasOwnProperty("to")) {
224
                        if(relation['to'].class == "hasAuthor") {
225
                            if(result['authors'] == undefined) {
226
                                result['authors'] = new Array<{"name": string, "url": string}>();
227
                            }
228

    
229
                            result['authors'].push({"name": relation.fullname, "url": OpenaireProperties.getsearchLinkToPerson()+relation['to'].content});
230
                        } else if(relation['to'].class == "isProducedBy") {
231
                            if(result['projects'] == undefined) {
232
                                result['projects'] = new Array<
233
                                    { "url": string, "acronym": string, "title": string,
234
                                      "funderShortname": string, "funderName": string,
235
                                      "code": string
236
                                    }>();
237
                            }
238

    
239
                            let countProjects = result['projects'].length;
240

    
241
                            result['projects'][countProjects] = {
242
                                "url": "", "acronym": "", "title": "",
243
                                "funderShortname": "", "funderName": "",
244
                                "code": ""
245
                            }
246

    
247
                            result['projects'][countProjects]['url'] =
248
                                OpenaireProperties.getsearchLinkToProject() + relation['to'].content;
249
                            result['projects'][countProjects]['acronym'] = relation.acronym;
250
                            result['projects'][countProjects]['title'] = relation.title;
251
                            result['projects'][countProjects]['code'] = relation.code;
252

    
253
                            if(relation.hasOwnProperty("funding")) {
254
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
255

    
256
                                for(let z=0; z<fundingLength; z++) {
257
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
258

    
259
                                    if(fundingData.hasOwnProperty("funder")) {
260
                                        result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
261
                                        result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
262
                                    }
263
                                }
264
                            }
265

    
266
                        }
267
                    }
268
                }
269
            }
270

    
271
            result.year = resData.dateofacceptance.split('-')[0];
272

    
273
            if(!Array.isArray(resData.description)) {
274
                result.description = resData.description;
275
            } else {
276
                result.description = resData.description[0];
277
            }
278

    
279
            if(result.description.length > this.sizeOfDescription) {
280
                result.description = result.description.substring(0, this.sizeOfDescription) + "...";
281
            }
282

    
283

    
284
            result.embargoEndDate = resData.embargoenddate;
285

    
286
            results.push(result);
287
        }
288

    
289
        return results;
290
    }
291

    
292
    parseResultsCSV(data: any): any {
293
        let results: any = [];
294

    
295

    
296
        let length = Array.isArray(data) ? data.length : 1;
297

    
298
        for(let i=0; i<length; i++) {
299
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
300

    
301
            var result: any = [];
302

    
303
            if(Array.isArray(resData['title'])) {
304
                result.push(this.quote(resData['title'][0].content));
305
            } else {
306
                result.push(this.quote(resData['title'].content));
307
            }
308

    
309
            var authors: string[] = [];
310
            var projects: string[] = [];
311
            var funder: string = "";
312
            if(resData['rels'].hasOwnProperty("rel")) {
313
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
314

    
315
                for(let j=0; j<relLength; j++) {
316
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
317

    
318
                    if(relation.hasOwnProperty("to")) {
319
                        if(relation['to'].class == "hasAuthor") {
320
                            authors.push(relation.fullname);
321
                        } else if(relation['to'].class == "isProducedBy") {
322
                            if(relation.code != "") {
323
                                projects.push(relation.title+" ("+relation.code+")");
324
                            } else {
325
                                projects.push(relation.title);
326
                            }
327

    
328
                            if(relation.hasOwnProperty("funding")) {
329
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
330

    
331
                                for(let z=0; z<fundingLength; z++) {
332
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
333

    
334
                                    if(fundingData.hasOwnProperty("funder")) {
335
                                        funder = fundingData['funder'].shortname;
336
                                    }
337
                                }
338
                            }
339

    
340
                        }
341
                    }
342
                }
343
                result.push(this.quote(authors));
344
                var year: string = "";
345
                if(resData.hasOwnProperty("dateofacceptance")) {
346
                    year = resData.dateofacceptance;
347
                }
348
                result.push(year);
349
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
350
                result.push(this.quote(id));
351
                result.push(funder);
352
                result.push(this.quote(projects));
353
            } else {
354
                result.push(this.quote(''));
355
                var year: string = "";
356
                if(resData.hasOwnProperty("dateofacceptance")) {
357
                    year = resData.dateofacceptance;
358
                }
359
                result.push(year);
360
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
361
                result.push(this.quote(id));
362
                result.push(funder);
363
                result.push("");
364
            }
365

    
366

    
367
            //result.push(resData.embargoenddate);
368

    
369
            if(resData['bestlicense'].hasOwnProperty("classid")) {
370
                result.push(this.quote(resData['bestlicense'].classid));
371
            } else {
372
                result.push("");
373
            }
374

    
375
            results.push(result);
376
        }
377

    
378
        return results;
379
    }
380

    
381
    parseRefineResults(data: any): any {
382
        var results:any = [];
383
        if(data.hasOwnProperty("resulthostingdatasourceid")) {
384
            let length = Array.isArray(data['resulthostingdatasourceid']) ? data['resulthostingdatasourceid'].length : 1;
385

    
386
            for(let i=0; i<length; i++) {
387
                let datasource = Array.isArray(data['resulthostingdatasourceid']) ? data['resulthostingdatasourceid'][i] : data['resulthostingdatasourceid'];
388

    
389
                let result: {"name": string, "id": string, "url":string, "count": number} = {"name": "", "id": "", "url": "", "count": 0};
390
                result['name'] = datasource.name;
391
                result['id'] = datasource.id;
392
                result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
393
                result['count'] = datasource.count;
394

    
395
                results.push(result);
396
            }
397
        }
398
        return results;
399
    }
400

    
401
    numOfEntityPublications(id: string, entity: string):any {
402

    
403
        //OpenaireProperties.getSearchAPIURLLast()
404
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
405
        let url = OpenaireProperties.getSearchAPIURLLast()+entity+id+"/publications/count"+ "?format=json";
406
        let key = url;
407
        if (this._cache.has(key)) {
408
          return Observable.of(this._cache.get(key));
409
        }
410
        return this.http.get(url)
411
                    .map(res => <any> res.json())
412
                    .map(res => res.total).do(res => {
413
                      this._cache.set(key, res);
414
                    });
415
    }
416

    
417
    numOfSearchPublications(params: string):any {
418

    
419
        //OpenaireProperties.getSearchAPIURLLast()
420
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
421
        let url = OpenaireProperties.getSearchAPIURLLast()+"publications/count?q="+ '"' + params + '"' +"&format=json";
422
        let key = url;
423
        if (this._cache.has(key)) {
424
          return Observable.of(this._cache.get(key));
425
        }
426
        return this.http.get(url)
427
                    .map(res => <any> res.json())
428
                    .map(res => res.total).do(res => {
429
                      this._cache.set(key, res);
430
                    });
431
    }
432

    
433
    private quote(word: any): string {
434
        return '"'+word+'"';
435
    }
436
}
(24-24/24)