Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
import {SearchResult} from '../utils/entities/searchResult';
4
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
5
import {DOI, StringUtils} from '../utils/string-utils.class';
6
import {ParsingFunctions} from '../landingPages/landing-utils/parsingFunctions.class';
7
import {EnvProperties} from '../utils/properties/env-properties';
8
import {map} from "rxjs/operators";
9

    
10

    
11
@Injectable()
12
export class SearchPublicationsService {
13
    private sizeOfDescription: number = 270;
14
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
15

    
16
    constructor(private http: HttpClient ) {}
17

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

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

    
33

    
34
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
35
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
36
    }
37
    searchPublicationById (id: string, properties:EnvProperties ):any {
38

    
39
        let url = properties.searchAPIURLLAst+"publications/"+id+"?format=json";
40

    
41
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
42
                    .pipe(map(res =>  this.parseResults(res, properties)));
43
    }
44

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

    
47
        let link = properties.searchAPIURLLAst+"publications";
48

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

    
58

    
59

    
60
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
61
                    //.map(res => <any> res.json())
62
                    .pipe(map(res => this.parseRefineResults(id, res['refineResults'])));
63
    }
64

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

    
67
        let link = properties.searchAPIURLLAst+"publications";
68

    
69
        let url = link+"?"+"&format=json&";
70
        var doisParams = "";
71

    
72
        for(var i =0 ;i < DOIs.length; i++){
73
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
74
        }
75
        if(doisParams.length > 0){
76
          url +="&"+doisParams;
77

    
78
        }
79
        if(refineParams!= null && refineParams != ''  ) {
80
            url += refineParams;
81
        }
82
        url += "&page="+(page-1)+"&size="+size;
83

    
84
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
85
                    //.map(res => <any> res.json())
86
                    //.do(res => console.info(res))
87
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
88
    }
89

    
90
    advancedSearchPublications (params: string, page: number, size: number, sortBy: string, properties:EnvProperties ):any {
91
      let url = properties.searchResourcesAPIURL;
92
      var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
93
      url += "?query=";
94
      if(params!= null && params != ''  ) {
95
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
96
      }else{
97
        url +=" ( "+basicQuery+ " ) ";
98
      }
99

    
100
      if(sortBy) {
101
        let sortOptions = sortBy.split(",");
102
        url += "sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+" ";
103
      }
104

    
105
      url += "&page="+(page-1)+"&size="+size;
106
      url += "&format=json";
107

    
108
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
109
      //.map(res => <any> res.json())
110
      //.do(res => console.info(res))
111
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
112
    }
113
    searchPublicationsForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
114
        let link = properties.searchAPIURLLAst;
115
        let url = link+params+"/publications"+ "?format=json";
116
        url += "&page="+(page-1)+"&size="+size;
117

    
118

    
119

    
120
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
121
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
122
    }
123

    
124
    searchPublicationsForDataproviders(params: string, page: number, size: number, properties:EnvProperties):any {
125
        let link = properties.searchAPIURLLAst;
126
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
127
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
128
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
129
    }
130
/*
131
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
132

    
133
        let link = properties.searchAPIURLLAst+"publications";
134

    
135
        let url = link+"?";
136
        if(params!= null && params != ''  ) {
137
            url += params;
138
        }
139
        if(refineParams!= null && refineParams != ''  ) {
140
            url += refineParams;
141
        }
142
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
143

    
144

    
145

    
146
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
147
                    .map(res => <any> res.json())
148
                    //.do(res => console.info(res))
149

    
150
                    .map(res => this.parseResultsCSV(res['results']));
151
    }
152
*/
153

    
154
    parseResults(data: any, properties: EnvProperties): SearchResult[] {
155
        let results: SearchResult[] = [];
156

    
157
        let length = Array.isArray(data) ? data.length : 1;
158

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

    
162
            var result: SearchResult = new SearchResult();
163
            result.entityType = "publication";
164

    
165
            result.types = new Array<string>();
166
            let types = new Set<string>();
167

    
168
            let instance;
169
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
170

    
171
            for(let i=0; i<length; i++) {
172
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
173
              this.parsingFunctions.parseTypes(result.types, types, instance);
174
            }
175

    
176
            if(resData['language'] && resData['language'] != null) {
177
              result.languages = new Array<string>();
178

    
179
              if(!Array.isArray(resData['language'])) {
180
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
181
                  result.languages.push(resData['language'].classname);
182
                }
183
              } else {
184
                for(let i=0; i<resData['language'].length; i++) {
185
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
186
                    result.languages.push(resData['language'][i].classname);
187
                  }
188
                }
189
              }
190
            }
191

    
192
            if(resData['country'] && resData['country'] != null) {
193
              result.countriesForResults = new Array<string>();
194

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

    
208
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
209

    
210
            if(Array.isArray(resData['title'])) {
211
                // resData['title'][0].hasOwnProperty("content") {
212
                    result['title'].name = String(resData['title'][0].content);
213
                // }
214
            } else {
215
                // resData['title'].hasOwnProperty("content") {
216
                    result['title'].name = String(resData['title'].content);
217
                // }
218
            }
219

    
220
            //result['title'].url = OpenaireProperties.getsearchLinkToPublication();
221
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
222
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
223
            if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
224
                result['title'].accessMode = resData['bestaccessright'].classid;
225
            }
226

    
227
            if(resData['rels'].hasOwnProperty("rel")) {
228
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
229

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

    
233
                    if(relation.hasOwnProperty("to")) {
234
                        /*if(relation['to'].class == "hasAuthor") {
235
                            if(result['authors'] == undefined) {
236
                                result['authors'] = new Array<{"name": string, "id": string}>();
237
                            }
238

    
239
                            result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
240
                        } else */if(relation['to'].class == "isProducedBy") {
241
                            result['projects'] = this.parseProjects(result['projects'], relation);
242
                        }
243
                    }
244
                }
245
            }
246

    
247
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
248
              if(result['authors'] == undefined) {
249
                result['authors'] = new Array<{"fullName": string, "orcid": string}>();
250
              }
251

    
252
              let authors = resData['creator'];
253
              let length = Array.isArray(authors) ? authors.length : 1;
254

    
255
              for(let i=0; i<length; i++) {
256
                let author = Array.isArray(authors) ? authors[i] : authors;
257
                if(author) {
258
                  /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
259
                    author.ORCID = author.ORCID.substr(properties.orcidURL.length);
260
                  }*/
261
                  result['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
262
                }
263
              }
264
              result.authors = result.authors.filter(function (item) {
265
                return (item != undefined && item.fullName != undefined);
266
              });
267
            }
268

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

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

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

    
282

    
283
            result.embargoEndDate = resData.embargoenddate;
284

    
285
            results.push(result);
286
        }
287

    
288
        return results;
289
    }
290

    
291
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
292
                              "funderShortname": string, "funderName": string,
293
                              "code": string }[], relation: any ) :  {
294
                              "id": string, "acronym": string, "title": string,
295
                              "funderShortname": string, "funderName": string,
296
                              "code": string }[] {
297
      if(projects == undefined) {
298
          projects = new Array<
299
              { "id": string, "acronym": string, "title": string,
300
                "funderShortname": string, "funderName": string,
301
                "code": string
302
              }>();
303
      }
304

    
305
      let countProjects = projects.length;
306

    
307
      projects[countProjects] = {
308
          "id": "", "acronym": "", "title": "",
309
          "funderShortname": "", "funderName": "",
310
          "code": ""
311
      }
312

    
313
      if(relation.title != 'unidentified') {
314
          projects[countProjects]['id'] =
315
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
316
          projects[countProjects]['acronym'] = relation.acronym;
317
          projects[countProjects]['title'] = relation.title;
318
          projects[countProjects]['code'] = relation.code;
319
      } else {
320
          projects[countProjects]['id'] = "";
321
          projects[countProjects]['acronym'] = "";
322
          projects[countProjects]['title'] = "";
323
          projects[countProjects]['code'] = "";
324
      }
325

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

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

    
332
              if(fundingData.hasOwnProperty("funder")) {
333
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
334
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
335
              }
336
          }
337
      }
338

    
339
      return projects;
340
    }
341
/*
342
    parseResultsCSV(data: any): any {
343
        let results: any = [];
344

    
345

    
346
        let length = Array.isArray(data) ? data.length : 1;
347

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

    
351
            var result: any = [];
352

    
353
            if(Array.isArray(resData['title'])) {
354
                result.push(this.quote(resData['title'][0].content));
355
            } else {
356
                result.push(this.quote(resData['title'].content));
357
            }
358

    
359
            var authors: string[] = [];
360
            var projects: string[] = [];
361
            var funder: string = "";
362
            if(resData['rels'].hasOwnProperty("rel")) {
363
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
364

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

    
368
                    if(relation.hasOwnProperty("to")) {
369
                        if(relation['to'].class == "hasAuthor") {
370
                            authors.push(relation.fullname);
371
                        } else if(relation['to'].class == "isProducedBy") {
372
                            if(relation.code != "") {
373
                                projects.push(relation.title+" ("+relation.code+")");
374
                            } else {
375
                                projects.push(relation.title);
376
                            }
377

    
378
                            if(relation.hasOwnProperty("funding")) {
379
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
380

    
381
                                for(let z=0; z<fundingLength; z++) {
382
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
383

    
384
                                    if(fundingData.hasOwnProperty("funder")) {
385
                                        funder = fundingData['funder'].shortname;
386
                                    }
387
                                }
388
                            }
389

    
390
                        }
391
                    }
392
                }
393
                result.push(this.quote(authors));
394
                var year: string = "";
395
                if(resData.hasOwnProperty("dateofacceptance")) {
396
                    year = resData.dateofacceptance;
397
                }
398
                result.push(year);
399
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
400
                result.push(this.quote(id));
401
                result.push(funder);
402
                result.push(this.quote(projects));
403
            } else {
404
                result.push(this.quote(''));
405
                var year: string = "";
406
                if(resData.hasOwnProperty("dateofacceptance")) {
407
                    year = resData.dateofacceptance;
408
                }
409
                result.push(year);
410
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
411
                result.push(this.quote(id));
412
                result.push(funder);
413
                result.push("");
414
            }
415

    
416

    
417
            //result.push(resData.embargoenddate);
418

    
419
            if(resData['bestaccessright'].hasOwnProperty("classid")) {
420
                result.push(this.quote(resData['bestaccessright'].classid));
421
            } else {
422
                result.push("");
423
            }
424

    
425
            results.push(result);
426
        }
427

    
428
        return results;
429
    }
430
*/
431
    parseRefineResults(id: string, data: any): any {
432
        var results:any = [];
433
        if(data.hasOwnProperty("resulthostingdatasource")) {
434
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
435

    
436
            for(let i=0; i<length; i++) {
437
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
438

    
439
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
440
                result['name'] = datasource.name;
441
                result['id'] = datasource.id.split("||")[0];
442
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
443
                result['count'] = datasource.count;
444

    
445
                if(result['id'] != id && result['name'] != "Unknown Repository") {
446
                    results.push(result);
447
                }
448
            }
449
        }
450
        return results;
451
    }
452

    
453
    numOfPublications(url: string, properties:EnvProperties): any {
454

    
455
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
456
                  //.map(res => <any> res.json())
457
                  .pipe(map(res => res['total']));
458
    }
459

    
460
    numOfEntityPublications(id: string, entity: string, properties:EnvProperties):any {
461
        var parameters: string = "";
462
        if(entity == "project") {
463
          parameters = "projects/"+id+"/publications/count";
464
        } else if(entity == "organization") {
465
          parameters = "organizations/"+id+"/publications/count";
466
        }
467
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
468
        return this.numOfPublications(url, properties);
469
    }
470

    
471
    numOfSearchPublications(params: string, properties:EnvProperties, refineParams:string=null):any {
472
        let url = properties.searchAPIURLLAst+"publications/count?format=json";
473
        if(params.length > 0){
474
          var DOIs:string[] = DOI.getDOIsFromString(params);
475
          var doisParams = "";
476

    
477
          for(var i =0 ;i < DOIs.length; i++){
478
            doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
479
          }
480
          if(doisParams.length > 0){
481
            url += "&"+doisParams;
482
          }else{
483
            url += "&q=" + StringUtils.URIEncode(params);
484
          }
485
        }
486
        // if(params != "") {
487
        //   url += "&q=" + StringUtils.URIEncode(params);
488
        // }
489
        if(refineParams!= null && refineParams != ''  ) {
490
            url += refineParams;
491
        }
492
        return this.numOfPublications(url, properties);
493
    }
494
/*
495
    private quote(word: any): string {
496
        return '"'+word+'"';
497
    }
498
*/
499
}
(21-21/23)