Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient} from "@angular/common/http";
4
import {Observable}     from 'rxjs';
5

    
6

    
7

    
8

    
9
import {SearchResult}     from '../utils/entities/searchResult';
10
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
11
import {DOI, StringUtils} from '../utils/string-utils.class';
12
import { ParsingFunctions } from '../landingPages/landing-utils/parsingFunctions.class';
13
import{EnvProperties} from '../utils/properties/env-properties';
14
import {map} from "rxjs/operators";
15

    
16

    
17
@Injectable()
18
export class SearchPublicationsService {
19
    private sizeOfDescription: number = 270;
20
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
21

    
22
    constructor(private http: HttpClient ) {}
23

    
24
    searchPublications (params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[] , properties:EnvProperties):any {
25
        let link = properties.searchAPIURLLAst+"publications";
26

    
27
        let url = link+"?";
28
        if(params!= null && params != ''  ) {
29
            url += params;
30
        }
31
        if(refineParams!= null && refineParams != ''  ) {
32
            url += refineParams;
33
        }
34
        if(sortBy) {
35
          url += "&sortBy=" + sortBy;
36
        }
37
        url += "&page="+(page-1)+"&size="+size+"&format=json";
38

    
39

    
40
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
41
                    //.map(res => <any> res.json())
42
                    // .do(res => console.info(res))
43
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
44
    }
45
    searchPublicationById (id: string, properties:EnvProperties ):any {
46

    
47
        let url = properties.searchAPIURLLAst+"publications/"+id+"?format=json";
48

    
49
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
50
                    //.map(res => <any> res.json())
51
                    .pipe(map(res =>  this.parseResults(res, properties)));
52
    }
53

    
54
    searchAggregators (id: string, params: string, refineParams:string, page: number, size: number, properties:EnvProperties ):any {
55

    
56
        let link = properties.searchAPIURLLAst+"publications";
57

    
58
        let url = link+"?"+"&format=json";
59
        if(params!= null && params != ''  ) {
60
            url += params;
61
        }
62
        if(refineParams!= null && refineParams != ''  ) {
63
            url += refineParams;
64
        }
65
        url += "&page="+(page-1)+"&size="+size;
66

    
67

    
68

    
69
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
70
                    //.map(res => <any> res.json())
71
                    .pipe(map(res => this.parseRefineResults(id, res['refineResults'])));
72
    }
73

    
74
    searchPublicationsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
75

    
76
        let link = properties.searchAPIURLLAst+"publications";
77

    
78
        let url = link+"?"+"&format=json&";
79
        var doisParams = "";
80

    
81
        for(var i =0 ;i < DOIs.length; i++){
82
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
83
        }
84
        if(doisParams.length > 0){
85
          url +="&"+doisParams;
86

    
87
        }
88
        if(refineParams!= null && refineParams != ''  ) {
89
            url += refineParams;
90
        }
91
        url += "&page="+(page-1)+"&size="+size;
92

    
93
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
94
                    //.map(res => <any> res.json())
95
                    //.do(res => console.info(res))
96
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
97

    
98
    }
99

    
100
    advancedSearchPublications (params: string, page: number, size: number, sortBy: string, properties:EnvProperties ):any {
101
      let url = properties.searchResourcesAPIURL;
102
      var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
103
      url += "?query=";
104
      if(params!= null && params != ''  ) {
105
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
106
      }else{
107
        url +=" ( "+basicQuery+ " ) ";
108
      }
109

    
110
      if(sortBy) {
111
        let sortOptions = sortBy.split(",");
112
        url += "sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+" ";
113
      }
114

    
115
      url += "&page="+(page-1)+"&size="+size;
116
      url += "&format=json";
117

    
118
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
119
      //.map(res => <any> res.json())
120
      //.do(res => console.info(res))
121

    
122
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
123

    
124
    }
125
    searchPublicationsForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
126
        let link = properties.searchAPIURLLAst;
127
        let url = link+params+"/publications"+ "?format=json";
128
        url += "&page="+(page-1)+"&size="+size;
129

    
130

    
131

    
132
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
133
                    //.map(res => <any> res.json())
134
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
135
    }
136

    
137
    searchPublicationsForDataproviders(params: string, page: number, size: number, properties:EnvProperties):any {
138
        let link = properties.searchAPIURLLAst;
139
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
140
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
141
                    //.map(res => <any> res.json())
142
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
143
    }
144
/*
145
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
146

    
147
        let link = properties.searchAPIURLLAst+"publications";
148

    
149
        let url = link+"?";
150
        if(params!= null && params != ''  ) {
151
            url += params;
152
        }
153
        if(refineParams!= null && refineParams != ''  ) {
154
            url += refineParams;
155
        }
156
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
157

    
158

    
159

    
160
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
161
                    .map(res => <any> res.json())
162
                    //.do(res => console.info(res))
163

    
164
                    .map(res => this.parseResultsCSV(res['results']));
165
    }
166
*/
167

    
168
    parseResults(data: any, properties: EnvProperties): SearchResult[] {
169
        let results: SearchResult[] = [];
170

    
171
        let length = Array.isArray(data) ? data.length : 1;
172

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

    
176
            var result: SearchResult = new SearchResult();
177
            result.entityType = "publication";
178

    
179
            result.types = new Array<string>();
180
            let types = new Set<string>();
181

    
182
            let instance;
183
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
184

    
185
            for(let i=0; i<length; i++) {
186
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
187
              this.parsingFunctions.parseTypes(result.types, types, instance);
188
            }
189

    
190
            if(resData['language'] && resData['language'] != null) {
191
              result.languages = new Array<string>();
192

    
193
              if(!Array.isArray(resData['language'])) {
194
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
195
                  result.languages.push(resData['language'].classname);
196
                }
197
              } else {
198
                for(let i=0; i<resData['language'].length; i++) {
199
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
200
                    result.languages.push(resData['language'][i].classname);
201
                  }
202
                }
203
              }
204
            }
205

    
206
            if(resData['country'] && resData['country'] != null) {
207
              result.countriesForResults = new Array<string>();
208

    
209
              if(!Array.isArray(resData['country'])) {
210
                if(resData['country'].classname != "Undetermined" && resData['country'].classname) {
211
                  result.countriesForResults.push(resData['country'].classname);
212
                }
213
              } else {
214
                for(let i=0; i<resData['country'].length; i++) {
215
                  if(resData['country'][i].classname != "Undetermined" && resData['country'][i].classname) {
216
                    result.countriesForResults.push(resData['country'][i].classname);
217
                  }
218
                }
219
              }
220
            }
221

    
222
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
223

    
224
            if(Array.isArray(resData['title'])) {
225
                // resData['title'][0].hasOwnProperty("content") {
226
                    result['title'].name = String(resData['title'][0].content);
227
                // }
228
            } else {
229
                // resData['title'].hasOwnProperty("content") {
230
                    result['title'].name = String(resData['title'].content);
231
                // }
232
            }
233

    
234
            //result['title'].url = OpenaireProperties.getsearchLinkToPublication();
235
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
236
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
237
            if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
238
                result['title'].accessMode = resData['bestaccessright'].classid;
239
            }
240

    
241
            if(resData['rels'].hasOwnProperty("rel")) {
242
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
243

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

    
247
                    if(relation.hasOwnProperty("to")) {
248
                        /*if(relation['to'].class == "hasAuthor") {
249
                            if(result['authors'] == undefined) {
250
                                result['authors'] = new Array<{"name": string, "id": string}>();
251
                            }
252

    
253
                            result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
254
                        } else */if(relation['to'].class == "isProducedBy") {
255
                            result['projects'] = this.parseProjects(result['projects'], relation);
256
                        }
257
                    }
258
                }
259
            }
260

    
261
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
262
              if(result['authors'] == undefined) {
263
                result['authors'] = new Array<{"fullName": string, "orcid": string}>();
264
              }
265

    
266
              let authors = resData['creator'];
267
              let length = Array.isArray(authors) ? authors.length : 1;
268

    
269
              for(let i=0; i<length; i++) {
270
                let author = Array.isArray(authors) ? authors[i] : authors;
271
                if(author) {
272
                  /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
273
                    author.ORCID = author.ORCID.substr(properties.orcidURL.length);
274
                  }*/
275
                  result['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
276
                }
277
              }
278
              result.authors = result.authors.filter(function (item) {
279
                return (item != undefined && item.fullName != undefined);
280
              });
281
            }
282

    
283
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
284
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
285

    
286
            if(!Array.isArray(resData.description)) {
287
                result.description = String(resData.description);
288
            } else {
289
                result.description = String(resData.description[0]);
290
            }
291

    
292
            if(result.description && result.description.length > this.sizeOfDescription) {
293
                result.description = result.description.substring(0, this.sizeOfDescription) + "...";
294
            }
295

    
296

    
297
            result.embargoEndDate = resData.embargoenddate;
298

    
299
            results.push(result);
300
        }
301

    
302
        return results;
303
    }
304

    
305
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
306
                              "funderShortname": string, "funderName": string,
307
                              "code": string }[], relation: any ) :  {
308
                              "id": string, "acronym": string, "title": string,
309
                              "funderShortname": string, "funderName": string,
310
                              "code": string }[] {
311
      if(projects == undefined) {
312
          projects = new Array<
313
              { "id": string, "acronym": string, "title": string,
314
                "funderShortname": string, "funderName": string,
315
                "code": string
316
              }>();
317
      }
318

    
319
      let countProjects = projects.length;
320

    
321
      projects[countProjects] = {
322
          "id": "", "acronym": "", "title": "",
323
          "funderShortname": "", "funderName": "",
324
          "code": ""
325
      }
326

    
327
      if(relation.title != 'unidentified') {
328
          projects[countProjects]['id'] =
329
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
330
          projects[countProjects]['acronym'] = relation.acronym;
331
          projects[countProjects]['title'] = relation.title;
332
          projects[countProjects]['code'] = relation.code;
333
      } else {
334
          projects[countProjects]['id'] = "";
335
          projects[countProjects]['acronym'] = "";
336
          projects[countProjects]['title'] = "";
337
          projects[countProjects]['code'] = "";
338
      }
339

    
340
      if(relation.hasOwnProperty("funding")) {
341
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
342

    
343
          for(let z=0; z<fundingLength; z++) {
344
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
345

    
346
              if(fundingData.hasOwnProperty("funder")) {
347
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
348
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
349
              }
350
          }
351
      }
352

    
353
      return projects;
354
    }
355
/*
356
    parseResultsCSV(data: any): any {
357
        let results: any = [];
358

    
359

    
360
        let length = Array.isArray(data) ? data.length : 1;
361

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

    
365
            var result: any = [];
366

    
367
            if(Array.isArray(resData['title'])) {
368
                result.push(this.quote(resData['title'][0].content));
369
            } else {
370
                result.push(this.quote(resData['title'].content));
371
            }
372

    
373
            var authors: string[] = [];
374
            var projects: string[] = [];
375
            var funder: string = "";
376
            if(resData['rels'].hasOwnProperty("rel")) {
377
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
378

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

    
382
                    if(relation.hasOwnProperty("to")) {
383
                        if(relation['to'].class == "hasAuthor") {
384
                            authors.push(relation.fullname);
385
                        } else if(relation['to'].class == "isProducedBy") {
386
                            if(relation.code != "") {
387
                                projects.push(relation.title+" ("+relation.code+")");
388
                            } else {
389
                                projects.push(relation.title);
390
                            }
391

    
392
                            if(relation.hasOwnProperty("funding")) {
393
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
394

    
395
                                for(let z=0; z<fundingLength; z++) {
396
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
397

    
398
                                    if(fundingData.hasOwnProperty("funder")) {
399
                                        funder = fundingData['funder'].shortname;
400
                                    }
401
                                }
402
                            }
403

    
404
                        }
405
                    }
406
                }
407
                result.push(this.quote(authors));
408
                var year: string = "";
409
                if(resData.hasOwnProperty("dateofacceptance")) {
410
                    year = resData.dateofacceptance;
411
                }
412
                result.push(year);
413
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
414
                result.push(this.quote(id));
415
                result.push(funder);
416
                result.push(this.quote(projects));
417
            } else {
418
                result.push(this.quote(''));
419
                var year: string = "";
420
                if(resData.hasOwnProperty("dateofacceptance")) {
421
                    year = resData.dateofacceptance;
422
                }
423
                result.push(year);
424
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
425
                result.push(this.quote(id));
426
                result.push(funder);
427
                result.push("");
428
            }
429

    
430

    
431
            //result.push(resData.embargoenddate);
432

    
433
            if(resData['bestaccessright'].hasOwnProperty("classid")) {
434
                result.push(this.quote(resData['bestaccessright'].classid));
435
            } else {
436
                result.push("");
437
            }
438

    
439
            results.push(result);
440
        }
441

    
442
        return results;
443
    }
444
*/
445
    parseRefineResults(id: string, data: any): any {
446
        var results:any = [];
447
        if(data.hasOwnProperty("resulthostingdatasource")) {
448
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
449

    
450
            for(let i=0; i<length; i++) {
451
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
452

    
453
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
454
                result['name'] = datasource.name;
455
                result['id'] = datasource.id.split("||")[0];
456
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
457
                result['count'] = datasource.count;
458

    
459
                if(result['id'] != id && result['name'] != "Unknown Repository") {
460
                    results.push(result);
461
                }
462
            }
463
        }
464
        return results;
465
    }
466

    
467
    numOfPublications(url: string, properties:EnvProperties): any {
468

    
469
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
470
                  //.map(res => <any> res.json())
471
                  .pipe(map(res => res['total']));
472
    }
473

    
474
    numOfEntityPublications(id: string, entity: string, properties:EnvProperties):any {
475
        var parameters: string = "";
476
        if(entity == "project") {
477
          parameters = "projects/"+id+"/publications/count";
478
        } else if(entity == "organization") {
479
          parameters = "organizations/"+id+"/publications/count";
480
        }
481
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
482
        return this.numOfPublications(url, properties);
483
    }
484

    
485
    numOfSearchPublications(params: string, properties:EnvProperties, refineParams:string=null):any {
486
        let url = properties.searchAPIURLLAst+"publications/count?format=json";
487
        if(params.length > 0){
488
          var DOIs:string[] = DOI.getDOIsFromString(params);
489
          var doisParams = "";
490

    
491
          for(var i =0 ;i < DOIs.length; i++){
492
            doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
493
          }
494
          if(doisParams.length > 0){
495
            url += "&"+doisParams;
496
          }else{
497
            url += "&q=" + StringUtils.URIEncode(params);
498
          }
499
        }
500
        // if(params != "") {
501
        //   url += "&q=" + StringUtils.URIEncode(params);
502
        // }
503
        if(refineParams!= null && refineParams != ''  ) {
504
            url += refineParams;
505
        }
506
        return this.numOfPublications(url, properties);
507
    }
508
/*
509
    private quote(word: any): string {
510
        return '"'+word+'"';
511
    }
512
*/
513
}
(21-21/23)