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
import {SearchResult}     from '../utils/entities/searchResult';
6
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
7

    
8

    
9

    
10
import { ParsingFunctions } from '../landingPages/landing-utils/parsingFunctions.class';
11
import{EnvProperties} from '../utils/properties/env-properties';
12
import {DOI, StringUtils} from '../utils/string-utils.class';
13
import {map} from "rxjs/operators";
14
@Injectable()
15
export class SearchSoftwareService {
16
    private sizeOfDescription: number = 270;
17
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
18

    
19
    constructor(private http: HttpClient ) {}
20

    
21
    searchSoftware (params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[], properties:EnvProperties ):any {
22

    
23
        let link = properties.searchAPIURLLAst+"software";
24

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

    
37

    
38
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
39
                    //.do(res => console.info(res))
40
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "software")]));
41

    
42
    }
43
    searchSoftwareById (id: string, properties:EnvProperties ):any {
44

    
45
        let url = properties.searchAPIURLLAst+"software/"+id+"?format=json";
46

    
47

    
48
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
49
                    .pipe(map(res => this.parseResults(res, properties)));
50
    }
51

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

    
54
        let link = properties.searchAPIURLLAst+"software";
55

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

    
65

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

    
71
    searchSoftwareByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
72
        let link = properties.searchAPIURLLAst+"software";
73
        let url = link+"?";
74
        var doisParams = "";
75

    
76
        for(var i =0 ;i < DOIs.length; i++){
77
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
78
        }
79
        if(doisParams.length > 0){
80
          url += "&"+doisParams;
81

    
82
        }
83
        if(refineParams!= null && refineParams != ''  ) {
84
            url += refineParams;
85
        }
86
        url += "&page="+ (page-1) +"&size="+size+"&format=json";
87

    
88

    
89
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
90
                    //.do(res => console.info(res))
91
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "software")]));
92
    }
93
    advancedSearchSoftware (params: string, page: number, size: number, sortBy: string, properties:EnvProperties ):any {
94
      let url = properties.searchResourcesAPIURL;
95
      var basicQuery = "(oaftype exact result) and (resulttypeid exact software)  "
96
      url += "?query=";
97
      if(params!= null && params != ''  ) {
98
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
99
      }else{
100
        url +=" ( "+basicQuery+ " ) ";
101
      }
102

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

    
108
      url += "&page="+(page-1)+"&size="+size;
109
      url += "&format=json";
110

    
111
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
112
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
113

    
114
    }
115
    searchSoftwareForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
116
        let link = properties.searchAPIURLLAst;
117
        let url = link+params+"/software"+"?format=json";
118

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

    
122
    }
123

    
124
    searchSoftwareForDataproviders(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

    
128
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
129
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
130
    }
131

    
132
    parseResults(data: any, properties: EnvProperties): SearchResult[] {
133
        let results: SearchResult[] = [];
134

    
135
        let length = Array.isArray(data) ? data.length : 1;
136

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

    
140
            var result: SearchResult = new SearchResult();
141
            result.entityType = "software";
142

    
143
            result.types = new Array<string>();
144
            let types = new Set<string>();
145

    
146
            let instance;
147
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
148

    
149
            for(let i=0; i<length; i++) {
150
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
151
              this.parsingFunctions.parseTypes(result.types, types, instance);
152
            }
153

    
154
            if(resData['programmingLanguage'] && resData['programmingLanguage'] != null) {
155
              result.programmingLanguages = new Array<string>();
156

    
157
              if(!Array.isArray(resData['programmingLanguage'])) {
158
                if(resData['programmingLanguage'].classname != "Undetermined" && resData['programmingLanguage'].classname) {
159
                  result.programmingLanguages.push(resData['programmingLanguage'].classname);
160
                }
161
              } else {
162
                for(let i=0; i<resData['programmingLanguage'].length; i++) {
163
                  if(resData['programmingLanguage'][i].classname != "Undetermined" && resData['programmingLanguage'][i].classname) {
164
                    result.programmingLanguages.push(resData['programmingLanguage'][i].classname);
165
                  }
166
                }
167
              }
168
            }
169

    
170
            if(resData['language'] && resData['language'] != null) {
171
              result.languages = new Array<string>();
172

    
173
              if(!Array.isArray(resData['language'])) {
174
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
175
                  result.languages.push(resData['language'].classname);
176
                }
177
              } else {
178
                for(let i=0; i<resData['language'].length; i++) {
179
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
180
                    result.languages.push(resData['language'][i].classname);
181
                  }
182
                }
183
              }
184
            }
185

    
186
            if(resData['country'] && resData['country'] != null) {
187
              result.countriesForResults = new Array<string>();
188

    
189
              if(!Array.isArray(resData['country'])) {
190
                if(resData['country'].classname != "Undetermined" && resData['country'].classname) {
191
                  result.countriesForResults.push(resData['country'].classname);
192
                }
193
              } else {
194
                for(let i=0; i<resData['country'].length; i++) {
195
                  if(resData['country'][i].classname != "Undetermined" && resData['country'][i].classname) {
196
                    result.countriesForResults.push(resData['country'][i].classname);
197
                  }
198
                }
199
              }
200
            }
201
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
202

    
203
            if(Array.isArray(resData['title'])) {
204
                result['title'].name = String(resData['title'][0].content);
205
            } else {
206
                result['title'].name = String(resData['title'].content);
207
            }
208

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

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

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

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

    
228
                          result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
229
                        } else */if(relation['to'].class == "isProducedBy") {
230
                          result['projects'] = this.parseProjects(result['projects'], relation);
231
                        }
232
                    }
233
                }
234
            }
235

    
236
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
237
              if(result['authors'] == undefined) {
238
                result['authors'] = new Array<{"fullName": string, "orcid": string}>();
239
              }
240

    
241
              let authors = resData['creator'];
242
              let length = Array.isArray(authors) ? authors.length : 1;
243

    
244
              for(let i=0; i<length; i++) {
245
                let author = Array.isArray(authors) ? authors[i] : authors;
246
                if(author) {
247
                  /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
248
                    author.ORCID = author.ORCID.substr(properties.orcidURL.length);
249
                  }*/
250
                  result['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
251
                }
252
              }
253
              result.authors = result.authors.filter(function (item) {
254
                return (item != undefined && item.fullName != undefined);
255
              });
256
            }
257

    
258
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
259
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
260
            result.description = Array.isArray(resData.description) ? String(resData.description[0]) : String(resData.description);
261
            if(result.description && result.description.length > this.sizeOfDescription) {
262
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
263
            }
264

    
265
            result.embargoEndDate = resData.embargoenddate;
266

    
267
            if(!Array.isArray(resData.publisher)) {
268
                result.publisher = resData.publisher;
269
            } else {
270
                for(let i=0; i<resData.publisher.length; i++) {
271
                    if(result.publisher != undefined){
272
                        result.publisher += ', '+resData['publisher'][i];
273
                    } else {
274
                        result.publisher = resData['publisher'][i];
275
                    }
276
                }
277
            }
278

    
279
            results.push(result);
280
        }
281

    
282
        return results;
283
    }
284

    
285
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
286
                              "funderShortname": string, "funderName": string,
287
                              "code": string }[], relation: any ) :  {
288
                              "id": string, "acronym": string, "title": string,
289
                              "funderShortname": string, "funderName": string,
290
                              "code": string }[] {
291
      if(projects == undefined) {
292
          projects = new Array<
293
              { "id": string, "acronym": string, "title": string,
294
                "funderShortname": string, "funderName": string,
295
                "code": string
296
              }>();
297
      }
298

    
299
      let countProjects = projects.length;
300

    
301
      projects[countProjects] = {
302
          "id": "", "acronym": "", "title": "",
303
          "funderShortname": "", "funderName": "",
304
          "code": ""
305
      }
306

    
307
      if(relation.title != 'unidentified') {
308
          projects[countProjects]['id'] =
309
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
310
          projects[countProjects]['acronym'] = relation.acronym;
311
          projects[countProjects]['title'] = relation.title;
312
          projects[countProjects]['code'] = relation.code;
313
      } else {
314
          projects[countProjects]['id'] = "";
315
          projects[countProjects]['acronym'] = "";
316
          projects[countProjects]['title'] = "";
317
          projects[countProjects]['code'] = "";
318
      }
319

    
320
      if(relation.hasOwnProperty("funding")) {
321
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
322

    
323
          for(let z=0; z<fundingLength; z++) {
324
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
325

    
326
              if(fundingData.hasOwnProperty("funder")) {
327
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
328
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
329
              }
330
          }
331
      }
332

    
333
      return projects;
334
    }
335

    
336
    parseRefineResults(id: string, data: any): any {
337
        var results:any = [];
338
        if(data.hasOwnProperty("resulthostingdatasource")) {
339
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
340

    
341
            for(let i=0; i<length; i++) {
342
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
343

    
344
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
345
                result['name'] = datasource.name;
346
                result['id'] = datasource.id.split("||")[0];
347
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
348
                result['count'] = datasource.count;
349

    
350
                if(result['id'] != id && result['name'] != "Unknown Repository") {
351
                    results.push(result);
352
                }
353
            }
354
        }
355
        return results;
356
    }
357

    
358
    numOfSoftware(url: string, properties:EnvProperties):any {
359

    
360
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
361
                  //.map(res => <any> res.json())
362
                  .pipe(map(res => res['total']));
363
    }
364

    
365
    numOfEntitySoftware(id: string, entity: string, properties:EnvProperties):any {
366
        var parameters = "";
367

    
368
        if(entity == "project") {
369
          parameters = "projects/"+id+"/software/count";
370
        }
371

    
372
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
373
        return this.numOfSoftware(url, properties);
374
    }
375

    
376
    numOfSearchSoftware(params: string, properties:EnvProperties, refineParams:string=null):any {
377
        let url = properties.searchAPIURLLAst+"software/count?format=json";
378

    
379
        if(params.length > 0){
380
          var DOIs:string[] = DOI.getDOIsFromString(params);
381
          var doisParams = "";
382

    
383
          for(var i =0 ;i < DOIs.length; i++){
384
            doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
385
          }
386
          if(doisParams.length > 0){
387
            url += "&"+doisParams;
388
          }else{
389
            url += "&q=" + StringUtils.URIEncode(params);
390
          }
391
        }
392
        // if(params != "") {
393
        //   url += "&q=" + StringUtils.URIEncode(params);
394
        // }
395
        if(refineParams!= null && refineParams != ''  ) {
396
            url += refineParams;
397
        }
398
        return this.numOfSoftware(url, properties);
399
    }
400
}
(22-22/23)