Project

General

Profile

1
import {Injectable, OnDestroy} 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 {Dates, 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 SearchResearchResultsService {
13
    private sizeOfDescription: number = 270;
14
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
15

    
16
    constructor(private http: HttpClient ) {
17
    }
18

    
19

    
20
    search (resultType:string, params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[] , properties:EnvProperties):any {
21
        let link = properties.searchAPIURLLAst+this.getEntityName(resultType,true);
22

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

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

    
39
    searchById (resultType:string, id: string, properties:EnvProperties ):any {
40
        let url = properties.searchAPIURLLAst+this.getEntityName(resultType,true) +"/"+id+"?format=json";
41

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

    
46
    searchAggregators (resultType:string, id: string, params: string, refineParams:string, page: number, size: number, properties:EnvProperties ):any {
47
        let link = properties.searchAPIURLLAst+this.getEntityName(resultType,true);
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
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
59
                    .pipe(map(res => this.parseRefineResults(id, res['refineResults'])));
60
    }
61

    
62
    searchByListOfDOI (resultType:string, DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
63
        let link = properties.searchAPIURLLAst+ this.getEntityName(resultType,true);
64

    
65
        let url = link+"?"+"&format=json&";
66
        var doisParams = "";
67

    
68
        for(var i =0 ;i < DOIs.length; i++){
69
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
70
        }
71
        if(doisParams.length > 0){
72
          url +="&"+doisParams;
73

    
74
        }
75
        if(refineParams!= null && refineParams != ''  ) {
76
            url += refineParams;
77
        }
78
        url += "&page="+(page-1)+"&size="+size;
79

    
80
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
81
                    .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
82
    }
83

    
84
    advancedSearch (resultType:string, params: string, page: number, size: number, sortBy: string, properties:EnvProperties, refineParams:string=null,  refineFields:string[] =null, refineQuery:string = null ):any {
85
      let url = properties.searchResourcesAPIURL;
86
      var basicQuery = "(oaftype exact result) and (resulttypeid exact "+this.getEntityName(resultType,false) + ") ";
87
      url += "?query=";
88
      if(params!= null && params != ''  ) {
89
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
90
      }else{
91
        url +=" ( "+basicQuery+ " ) ";
92
      }
93
      if(refineParams!= null && refineParams != ''  ) {
94
        url += refineParams;
95
      }
96
      if(sortBy) {
97
        let sortOptions = sortBy.split(",");
98
        url += "sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+" ";
99
      }
100
      if(refineQuery) {
101
        url += "&" + refineQuery;
102
      }
103

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

    
107
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
108
      .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
109
    }
110
  advancedSearchResults (resultType:string, params: string, page: number, size: number, sortBy: string, properties:EnvProperties, refineParams:string=null,  refineFields:string[] =null, refineQuery:string = null ):any {
111
    let url = properties.searchAPIURLLAst+"resources2/?format=json";
112
    if(params!= null && params != ''  ) {
113
      url +="&query=(" + params + ")";
114
    }
115
    if(sortBy) {
116
      let sortOptions = sortBy.split(",");
117
      url += (params ? " " : "&query=(*) ")+"sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+(params ? " " : " ");
118
    }
119
    if(refineParams!= null && refineParams != ''  ) {
120
      url += refineParams;
121
    }
122
    if(refineQuery) {
123
      url += "&" + refineQuery;
124
    }
125

    
126
    url += "&page="+(page-1)+"&size="+size;
127
    // url += "&format=json";
128

    
129
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
130
      .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]));
131
  }
132

    
133
    searchResultForEntity (resultType:string, params: string, page: number, size: number, properties:EnvProperties):any {
134
        let link = properties.searchAPIURLLAst;
135
        //let url = link+params+"/"+this.getEntityName(resultType,true)+ "?format=json";
136
        //url += "&page="+(page-1)+"&size="+size;
137
        //url += "&sortBy=resultdateofacceptance,descending";
138

    
139
        //let url = link+"/resources2?format=json&query="+params+" sortBy resultdateofacceptance/sort.descending&type="+this.getEntityName(resultType,true);
140

    
141
        let url = link+"/"+this.getEntityName(resultType,true);
142
        url += "?format=json";
143
        url += "&fq="+params;
144
        url += "&sortBy=resultdateofacceptance,descending";
145
        url += "&page="+(page-1)+"&size="+size;
146

    
147
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
148
                    .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties)]));
149
    }
150
//???? why different from above?
151
    searchForDataproviders(resultType:string, params: string, page: number, size: number, properties:EnvProperties):any {
152
        let link = properties.searchAPIURLLAst;
153
        let url = link+params;
154
        url += "&sortBy=resultdateofacceptance,descending";
155
        url += "&page="+(page-1)+"&size="+size + "&format=json";
156
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
157
                    .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties)]));
158
    }
159

    
160
    parseResults(resultType:string, data: any, properties: EnvProperties): SearchResult[] {
161
        let results: SearchResult[] = [];
162

    
163
        let length = Array.isArray(data) ? data.length : 1;
164

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

    
168
            var result: SearchResult = new SearchResult();
169
            if(resData['resulttype']) {
170
              result.entityType = resData['resulttype']['classname'];
171
            } else {
172
              result.entityType = resultType;
173
            }
174
            result.types = new Array<string>();
175
            let types = new Set<string>();
176

    
177
            let instance;
178
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
179

    
180
            for(let i=0; i<length; i++) {
181
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
182
              this.parsingFunctions.parseTypes(result.types, types, instance);
183
            }
184
            /////////////////////////// Athena Code ///////////////////////////
185
            if(resData['pid']) {
186
              for(let i=0; i<resData['pid'].length; i++){
187
                if(resData['pid'][i].classid == 'doi'){
188
                  if(resData['pid'][i].content != '' && resData['pid'][i].content != null){
189
                    result.DOIs.push(resData['pid'][i].content.replace("https://doi.org/",""));
190
                  }
191
                }
192
              }
193
            }
194
            /////////////////////////// Athena Code ///////////////////////////
195
            if(resData['programmingLanguage'] && resData['programmingLanguage'] != null) {
196
              result.programmingLanguages = new Array<string>();
197

    
198
              if(!Array.isArray(resData['programmingLanguage'])) {
199
                if(resData['programmingLanguage'].classname != "Undetermined" && resData['programmingLanguage'].classname) {
200
                  result.programmingLanguages.push(resData['programmingLanguage'].classname);
201
                }
202
              } else {
203
                for(let i=0; i<resData['programmingLanguage'].length; i++) {
204
                  if(resData['programmingLanguage'][i].classname != "Undetermined" && resData['programmingLanguage'][i].classname) {
205
                    result.programmingLanguages.push(resData['programmingLanguage'][i].classname);
206
                  }
207
                }
208
              }
209
            }
210

    
211
            if(resData['language'] && resData['language'] != null) {
212
              result.languages = new Array<string>();
213

    
214
              if(!Array.isArray(resData['language'])) {
215
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
216
                  result.languages.push(resData['language'].classname);
217
                }
218
              } else {
219
                for(let i=0; i<resData['language'].length; i++) {
220
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
221
                    result.languages.push(resData['language'][i].classname);
222
                  }
223
                }
224
              }
225
            }
226

    
227
            if(resData['country'] && resData['country'] != null) {
228
              result.countriesForResults = new Array<string>();
229

    
230
              if(!Array.isArray(resData['country'])) {
231
                if(resData['country'].classname != "Undetermined" && resData['country'].classname) {
232
                  result.countriesForResults.push(resData['country'].classname);
233
                }
234
              } else {
235
                for(let i=0; i<resData['country'].length; i++) {
236
                  if(resData['country'][i].classname != "Undetermined" && resData['country'][i].classname) {
237
                    result.countriesForResults.push(resData['country'][i].classname);
238
                  }
239
                }
240
              }
241
            }
242

    
243
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
244

    
245
            if(Array.isArray(resData['title'])) {
246
                result['title'].name = (resData['title'][0] && resData['title'][0].content) ? String(resData['title'][0].content) : "";
247
            } else {
248
                result['title'].name = (resData['title'] && resData['title'].content) ? String(resData['title'].content) : "";
249
            }
250

    
251
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
252
            if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classname")) {
253
                result['title'].accessMode = resData['bestaccessright'].classname;
254
            }
255
            if(resData['rels'].hasOwnProperty("rel")) {
256
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
257

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

    
261
                    if(relation.hasOwnProperty("to")) {
262
                        if(relation['to'].class == "isProducedBy") {
263
                            result['projects'] = this.parseProjects(result['projects'], relation);
264
                        }
265
                    }
266
                }
267
            }
268

    
269
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
270
              if(result['authors'] == undefined) {
271
                result['authors'] = new Array<{"fullName": string, "orcid": string}>();
272
              }
273

    
274
              let authors = resData['creator'];
275
              let length = Array.isArray(authors) ? authors.length : 1;
276

    
277
              for(let i=0; i<length; i++) {
278
                let author = Array.isArray(authors) ? authors[i] : authors;
279
                if(author) {
280
                  result['authors'][author.rank] = {"fullName": author.content, "orcid": author.orcid};
281
                }
282
              }
283
              result.authors = result.authors.filter(function (item) {
284
                return (item != undefined && item.fullName != undefined);
285
              });
286
            }
287

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

    
291
            if(!Array.isArray(resData.description)) {
292
                result.description = (resData.description) ? String(resData.description) : "";
293
            } else {
294
                result.description = (resData.description[0]) ? String(resData.description[0]) : "";
295
            }
296

    
297
            if(result.description && result.description.length > this.sizeOfDescription) {
298
                result.description = result.description.substring(0, this.sizeOfDescription) + "...";
299
            }
300

    
301
            if(resData.embargoenddate && resData.embargoenddate != '') {
302
              result.embargoEndDate = Dates.getDate(resData.embargoenddate);
303
            }
304

    
305
            if(!Array.isArray(resData.publisher)) {
306
              result.publisher = resData.publisher;
307
            } else {
308
              for(let i=0; i<resData.publisher.length; i++) {
309
                if(result.publisher != undefined){
310
                  result.publisher += ', '+resData['publisher'][i];
311
                } else {
312
                  result.publisher = resData['publisher'][i];
313
                }
314
              }
315
            }
316

    
317
            results.push(result);
318
        }
319

    
320
        return results;
321
    }
322

    
323
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
324
                              "funderShortname": string, "funderName": string,
325
                              "code": string }[], relation: any ) :  {
326
                              "id": string, "acronym": string, "title": string,
327
                              "funderShortname": string, "funderName": string,
328
                              "code": string }[] {
329
      if(projects == undefined) {
330
          projects = new Array<
331
              { "id": string, "acronym": string, "title": string,
332
                "funderShortname": string, "funderName": string,
333
                "code": string
334
              }>();
335
      }
336

    
337
      let countProjects = projects.length;
338

    
339
      projects[countProjects] = {
340
          "id": "", "acronym": "", "title": "",
341
          "funderShortname": "", "funderName": "",
342
          "code": ""
343
      };
344

    
345
      if(relation.title != 'unidentified') {
346
          projects[countProjects]['id'] = relation['to'].content;
347
          projects[countProjects]['acronym'] = relation.acronym;
348
          projects[countProjects]['title'] = relation.title;
349
          projects[countProjects]['code'] = relation.code;
350
      } else {
351
          projects[countProjects]['id'] = "";
352
          projects[countProjects]['acronym'] = "";
353
          projects[countProjects]['title'] = "";
354
          projects[countProjects]['code'] = "";
355
      }
356

    
357
      if(relation.hasOwnProperty("funding")) {
358
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
359

    
360
          for(let z=0; z<fundingLength; z++) {
361
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
362

    
363
              if(fundingData.hasOwnProperty("funder")) {
364
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
365
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
366
              }
367
          }
368
      }
369

    
370
      return projects;
371
    }
372
    parseRefineResults(id: string, data: any): any {
373
        var results:any = [];
374
        if(data.hasOwnProperty("resulthostingdatasource")) {
375
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
376

    
377
            for(let i=0; i<length; i++) {
378
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
379

    
380
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
381
                result['name'] = datasource.name;
382
                result['id'] = datasource.id.split("||")[0];
383
                result['count'] = datasource.count;
384

    
385
                if(result['id'] != id && result['name'] != "Unknown Repository") {
386
                    results.push(result);
387
                }
388
            }
389
        }
390
        return results;
391
    }
392

    
393
    private numOfResults(url: string, properties:EnvProperties): any {
394
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
395
                  .pipe(map(res => res['total']));
396
    }
397

    
398
    numOfEntityResults(resultType:string, id: string, entity: string, properties:EnvProperties):any {
399
        var parameters: string = "";
400
        parameters = this.getEntityName(entity, true) + "/"+id+"/"+this.getEntityName(resultType, true)+"/count";
401
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
402
        return this.numOfResults(url , properties);
403
    }
404
  numOfResearchOutcomes( params: string, properties:EnvProperties, refineParams:string=null):any {
405
    let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=results";
406
    if(params.length > 0){
407
      // var DOIs:string[] = DOI.getDOIsFromString(params);
408
      // var doisParams = "";
409
      //
410
      // for(var i =0 ;i < DOIs.length; i++){
411
      //   doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
412
      // }
413
      // if(doisParams.length > 0){
414
      //   url += "&"+doisParams;
415
      // }else{
416
      //   url += "&query=" + StringUtils.URIEncode(params);
417
      // }
418
      url += "&query=" + params;
419
    }
420

    
421
    if(refineParams!= null && refineParams != ''  ) {
422
      url += refineParams;
423
    }
424
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
425
      .pipe(map(res => res['meta']['total']));
426
  }
427
    numOfSearchResults(resultType:string, params: string, properties:EnvProperties, refineParams:string=null):any {
428
        let url = properties.searchAPIURLLAst+this.getEntityName(resultType, true)+"/count?format=json";
429
        if(params.length > 0){
430
          var DOIs:string[] = DOI.getDOIsFromString(params);
431
          var doisParams = "";
432

    
433
          for(var i =0 ;i < DOIs.length; i++){
434
            doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
435
          }
436
          if(doisParams.length > 0){
437
            url += "&"+doisParams;
438
          }else{
439
            url += "&q=" + StringUtils.URIEncode(params);
440
          }
441
        }
442

    
443
        if(refineParams!= null && refineParams != ''  ) {
444
            url += refineParams;
445
        }
446
        return this.numOfResults(url, properties);
447
    }
448
  numOfSearchResultsLinkedToPub(resultType:string, properties:EnvProperties):any {
449
    let url = properties.searchAPIURLLAst+"resources?query="+encodeURIComponent("( (oaftype exact result) and (resulttypeid exact "+resultType+") and (relresulttype=publication)  )")+"&page=0&size=0&format=json";
450
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
451
      .pipe(map(res => res['meta']['total']));
452
  }
453

    
454
    countTotalResults(resultType:string,  properties:EnvProperties, refineParams:string=null):any {
455
      let url = properties.searchAPIURLLAst+this.getEntityName(resultType, true)+"/count?format=json"+refineParams;
456
      return this.numOfResults(url, properties);
457
    }
458
/*
459
    private quote(word: any): string {
460
        return '"'+word+'"';
461
    }
462
*/
463

    
464
    private getEntityName (entityType:string, plural:boolean){
465
      if(entityType == "publication" ||entityType == "dataset" || entityType == "organization" || entityType == "datasource" || entityType == "project" ){
466
        if(plural){
467
          return entityType+ "s";
468
        }else{
469
          return entityType;
470
        }
471
      }else{
472
        return entityType;
473
      }
474
    }
475
}
(19-19/23)