Project

General

Profile

1 43769 argiro.kok
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4 44638 argiro.kok
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 43785 argiro.kok
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10 43941 konstantin
import {SearchResult}     from '../utils/entities/searchResult';
11 44338 konstantin
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
12 43769 argiro.kok
13
@Injectable()
14
export class SearchPublicationsService {
15 43941 konstantin
    private sizeOfDescription: number = 497;
16 43769 argiro.kok
17 44638 argiro.kok
    constructor(private http: Http, public _cache: CacheService) {}
18 44882 konstantin
19 44338 konstantin
    searchPublications (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
20 43769 argiro.kok
21 44726 argiro.kok
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
22 43769 argiro.kok
23 45498 argiro.kok
        let url = link+"?";
24 44338 konstantin
        if(params!= null && params != ''  ) {
25
            url += params;
26
        }
27 44383 argiro.kok
        if(refineParams!= null && refineParams != ''  ) {
28 44338 konstantin
            url += refineParams;
29
        }
30 45498 argiro.kok
        url += "&page="+(page-1)+"&size="+size+"&format=json";
31 43941 konstantin
32 44638 argiro.kok
        let key = url;
33
        if (this._cache.has(key)) {
34 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
35 44638 argiro.kok
        }
36 44035 konstantin
37 43941 konstantin
        return this.http.get(url)
38
                    .map(res => <any> res.json())
39 44976 argiro.kok
                    // .do(res => console.info(res))
40 44638 argiro.kok
                    .do(res => {
41
                      this._cache.set(key, res);
42 44907 argiro.kok
                    })
43
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
44 43769 argiro.kok
    }
45
46 44882 konstantin
    searchAggregators (params: string, refineParams:string, page: number, size: number ):any {
47
48
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
49
50 45484 konstantin
        let url = link+"?"+"&format=json";
51 44882 konstantin
        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 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => this.parseRefineResults(res['refineResults']));
62 44882 konstantin
        }
63
64
        return this.http.get(url)
65
                    .map(res => <any> res.json())
66
                    .do(res => {
67
                      this._cache.set(key, res);
68 44907 argiro.kok
                    })
69
                    .map(res => this.parseRefineResults(res['refineResults']));
70 44882 konstantin
    }
71
72 44797 argiro.kok
    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 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
93 44797 argiro.kok
        }
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 44907 argiro.kok
                    })
101
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
102 44797 argiro.kok
    }
103
104 44726 argiro.kok
    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 43769 argiro.kok
114 44726 argiro.kok
      url += "&page="+(page-1)+"&size="+size;
115
      url += "&format=json";
116
      let key = url;
117
      if (this._cache.has(key)) {
118 44907 argiro.kok
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
119 44726 argiro.kok
      }
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 44907 argiro.kok
      })
126
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
127 44726 argiro.kok
    }
128 44338 konstantin
    searchPublicationsForEntity (params: string, page: number, size: number):any {
129 44766 argiro.kok
        let link = OpenaireProperties.getSearchAPIURLLast();
130
        let url = link+params+"/publications"+ "?format=json";
131 44638 argiro.kok
132
        let key = url;
133
        if (this._cache.has(key)) {
134 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
135 44638 argiro.kok
        }
136
137 44338 konstantin
        return this.http.get(url)
138
                    .map(res => <any> res.json())
139 44638 argiro.kok
                    .do(res => {
140
                      this._cache.set(key, res);
141 44907 argiro.kok
                    })
142
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
143 44338 konstantin
    }
144
145 44382 konstantin
    searchPublicationsForDataproviders(params: string, page: number, size: number):any {
146 44766 argiro.kok
        let link = OpenaireProperties.getSearchAPIURLLast();
147 44796 konstantin
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
148 44638 argiro.kok
        let key = url;
149
        if (this._cache.has(key)) {
150 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
151 44638 argiro.kok
        }
152 44382 konstantin
        return this.http.get(url)
153
                    .map(res => <any> res.json())
154 44638 argiro.kok
                    .do(res => {
155
                      this._cache.set(key, res);
156 44907 argiro.kok
                    })
157
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
158 44382 konstantin
    }
159
160 44732 konstantin
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
161 44382 konstantin
162 44766 argiro.kok
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
163 44732 konstantin
164
        let url = link+"?";
165
        if(params!= null && params != ''  ) {
166
            url += params;
167
        }
168
        if(refineParams!= null && refineParams != ''  ) {
169
            url += refineParams;
170
        }
171 44766 argiro.kok
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
172 44732 konstantin
173
        let key = url;
174
        if (this._cache.has(key)) {
175 44907 argiro.kok
          return Observable.of(this._cache.get(key)).map(res => this.parseResultsCSV(res['results']));
176 44732 konstantin
        }
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 44907 argiro.kok
                    })
184
                    .map(res => this.parseResultsCSV(res['results']));
185 44732 konstantin
    }
186
187
188 43941 konstantin
    parseResults(data: any): SearchResult[] {
189
        let results: SearchResult[] = [];
190 43769 argiro.kok
191 43941 konstantin
        let length = Array.isArray(data) ? data.length : 1;
192 44732 konstantin
193 43941 konstantin
        for(let i=0; i<length; i++) {
194 44084 konstantin
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
195 43769 argiro.kok
196 43941 konstantin
            var result: SearchResult = new SearchResult();
197 43769 argiro.kok
198 43941 konstantin
            result['title'] = {"name": '', "url": '', "accessMode": ''};
199 43956 konstantin
200
            if(Array.isArray(resData['title'])) {
201 43957 argiro.kok
                // resData['title'][0].hasOwnProperty("content") {
202 43956 konstantin
                    result['title'].name = resData['title'][0].content;
203 43957 argiro.kok
                // }
204 43956 konstantin
            } else {
205 43957 argiro.kok
                // resData['title'].hasOwnProperty("content") {
206 43956 konstantin
                    result['title'].name = resData['title'].content;
207 43957 argiro.kok
                // }
208 43941 konstantin
            }
209 43956 konstantin
210 43941 konstantin
            result['title'].url = OpenaireProperties.getsearchLinkToPublication();
211 44084 konstantin
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
212 44641 konstantin
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
213 43941 konstantin
            if(resData['bestlicense'].hasOwnProperty("classid")) {
214
                result['title'].accessMode = resData['bestlicense'].classid;
215
            }
216 43769 argiro.kok
217 43941 konstantin
            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 44084 konstantin
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
222 43941 konstantin
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 44084 konstantin
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
258 43941 konstantin
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 43990 konstantin
            if(!Array.isArray(resData.description)) {
274
                result.description = resData.description;
275
            } else {
276
                result.description = resData.description[0];
277
            }
278 44796 konstantin
279 43941 konstantin
            if(result.description.length > this.sizeOfDescription) {
280 44796 konstantin
                result.description = result.description.substring(0, this.sizeOfDescription) + "...";
281 43941 konstantin
            }
282
283 44796 konstantin
284 43941 konstantin
            result.embargoEndDate = resData.embargoenddate;
285
286
            results.push(result);
287
        }
288
289
        return results;
290
    }
291 44300 konstantin
292 44732 konstantin
    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 44882 konstantin
    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 44300 konstantin
    numOfEntityPublications(id: string, entity: string):any {
402
403 44766 argiro.kok
        //OpenaireProperties.getSearchAPIURLLast()
404 44300 konstantin
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
405 44766 argiro.kok
        let url = OpenaireProperties.getSearchAPIURLLast()+entity+id+"/publications/count"+ "?format=json";
406 44638 argiro.kok
        let key = url;
407
        if (this._cache.has(key)) {
408
          return Observable.of(this._cache.get(key));
409
        }
410 44300 konstantin
        return this.http.get(url)
411
                    .map(res => <any> res.json())
412 44638 argiro.kok
                    .map(res => res.total).do(res => {
413
                      this._cache.set(key, res);
414
                    });
415 44300 konstantin
    }
416
417
    numOfSearchPublications(params: string):any {
418
419 44766 argiro.kok
        //OpenaireProperties.getSearchAPIURLLast()
420 44300 konstantin
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
421 44797 argiro.kok
        let url = OpenaireProperties.getSearchAPIURLLast()+"publications/count?q="+ '"' + params + '"' +"&format=json";
422 44638 argiro.kok
        let key = url;
423
        if (this._cache.has(key)) {
424
          return Observable.of(this._cache.get(key));
425
        }
426 44300 konstantin
        return this.http.get(url)
427
                    .map(res => <any> res.json())
428 44638 argiro.kok
                    .map(res => res.total).do(res => {
429
                      this._cache.set(key, res);
430
                    });
431 44300 konstantin
    }
432 44732 konstantin
433
    private quote(word: any): string {
434
        return '"'+word+'"';
435
    }
436 43769 argiro.kok
}