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
@Injectable()
11
export class SearchOrpsService {
12
    private sizeOfDescription: number = 270;
13
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
14

    
15
    constructor(private http: HttpClient ) {}
16

    
17
    searchOrps (params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[], properties:EnvProperties ):any {
18

    
19
        let link = properties.searchAPIURLLAst+"other";
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
                    //.map(res => <any> res.json())
36
                    //.do(res => console.info(res))
37
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "other")]));
38
    }
39
    searchOrpById (id: string , properties:EnvProperties):any {
40

    
41
        let url = properties.searchAPIURLLAst+"other/"+id+"?format=json";
42

    
43
         return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
44
                    //.map(res => <any> res.json())
45
                    .pipe(map(res => this.parseResults(res, properties)));
46
    }
47

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

    
50
        let link = properties.searchAPIURLLAst+"other";
51

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

    
61

    
62

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

    
68
    searchOrpsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
69
        let link = properties.searchAPIURLLAst+"other";
70

    
71
        let url = link+"?";
72
        var doisParams = "";
73

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

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

    
86

    
87
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
88
                    //.map(res => <any> res.json())
89
                    //.do(res => console.info(res))
90
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "other")]));
91
    }
92
    advancedSearchOrps (params: string, page: number, size: number, sortBy: string, properties:EnvProperties ):any {
93
      let url = properties.searchResourcesAPIURL;
94

    
95
      var basicQuery = "(oaftype exact result) and (resulttypeid exact other)  "
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
      //.map(res => <any> res.json())
113
      //.do(res => console.info(res))
114
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
115
    }
116
    searchOrpsForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
117
        let link = properties.searchAPIURLLAst;
118

    
119
        let url = link+params+"/other"+"?format=json";
120

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

    
126
    searchOrpsForDataproviders(params: string, page: number, size: number, properties:EnvProperties):any {
127
        let link = properties.searchAPIURLLAst;
128

    
129
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
130
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
131
                    //.map(res => <any> res.json())
132
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'], properties)]));
133
    }
134

    
135
    parseResults(data: any, properties: EnvProperties): SearchResult[] {
136
        let results: SearchResult[] = [];
137

    
138
        let length = Array.isArray(data) ? data.length : 1;
139

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

    
143
            var result: SearchResult = new SearchResult();
144
            result.entityType = "other";
145

    
146
            result.types = new Array<string>();
147
            let types = new Set<string>();
148
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
149

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

    
156
            if(resData['language'] && resData['language'] != null) {
157
              result.languages = new Array<string>();
158

    
159
              if(!Array.isArray(resData['language'])) {
160
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
161
                  result.languages.push(resData['language'].classname);
162
                }
163
              } else {
164
                for(let i=0; i<resData['language'].length; i++) {
165
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
166
                    result.languages.push(resData['language'][i].classname);
167
                  }
168
                }
169
              }
170
            }
171
            if(resData['country'] && resData['country'] != null) {
172
              result.countriesForResults = new Array<string>();
173

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

    
187
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
188

    
189
            if(Array.isArray(resData['title'])) {
190
                result['title'].name = String(resData['title'][0].content);
191
            } else {
192
                result['title'].name = String(resData['title'].content);
193
            }
194

    
195
            //result['title'].url = OpenaireProperties.getsearchLinkToOrp();
196
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
197
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
198
            if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
199
                result['title'].accessMode = resData['bestaccessright'].classid;
200
            }
201

    
202
            if(resData['rels'].hasOwnProperty("rel")) {
203
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
204

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

    
208
                    if(relation.hasOwnProperty("to")) {
209
                        /*if(relation['to'].class == "hasAuthor") {
210
                          if(result['authors'] == undefined) {
211
                            result['authors'] = new Array<{"name": string, "id": string}>();
212
                          }
213

    
214
                          result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
215
                        } else */if(relation['to'].class == "isProducedBy") {
216
                          result['projects'] = this.parseProjects(result['projects'], relation);
217
                        }
218
                    }
219
                }
220
            }
221

    
222
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
223
              if(result['authors'] == undefined) {
224
                result['authors'] = new Array<{"fullName": string, "orcid": string}>();
225
              }
226

    
227
              let authors = resData['creator'];
228
              let length = Array.isArray(authors) ? authors.length : 1;
229

    
230
              for(let i=0; i<length; i++) {
231
                let author = Array.isArray(authors) ? authors[i] : authors;
232
                if(author) {
233
                  /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
234
                    author.ORCID = author.ORCID.substr(properties.orcidURL.length);
235
                  }*/
236
                  result['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
237
                }              }
238
              result.authors = result.authors.filter(function (item) {
239
                return (item != undefined && item.fullName != undefined);
240
              });
241
            }
242

    
243
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
244
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
245
            if(!Array.isArray(resData.description)) {
246
                result.description = String(resData.description);
247
            } else {
248
                result.description = String(resData.description[0]);
249
            }
250
            if(result.description && result.description.length > this.sizeOfDescription) {
251
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
252
            }
253

    
254
          result.embargoEndDate = resData.embargoenddate;
255

    
256
            if(!Array.isArray(resData.publisher)) {
257
                result.publisher = resData.publisher;
258
            } else {
259
                for(let i=0; i<resData.publisher.length; i++) {
260
                    if(result.publisher != undefined){
261
                        result.publisher += ', '+resData['publisher'][i];
262
                    } else {
263
                        result.publisher = resData['publisher'][i];
264
                    }
265
                }
266
            }
267

    
268
            results.push(result);
269
        }
270

    
271
        return results;
272
    }
273

    
274
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
275
                              "funderShortname": string, "funderName": string,
276
                              "code": string }[], relation: any ) :  {
277
                              "id": string, "acronym": string, "title": string,
278
                              "funderShortname": string, "funderName": string,
279
                              "code": string }[] {
280
      if(projects == undefined) {
281
          projects = new Array<
282
              { "id": string, "acronym": string, "title": string,
283
                "funderShortname": string, "funderName": string,
284
                "code": string
285
              }>();
286
      }
287

    
288
      let countProjects = projects.length;
289

    
290
      projects[countProjects] = {
291
          "id": "", "acronym": "", "title": "",
292
          "funderShortname": "", "funderName": "",
293
          "code": ""
294
      }
295

    
296
      if(relation.title != 'unidentified') {
297
          projects[countProjects]['id'] =
298
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
299
          projects[countProjects]['acronym'] = relation.acronym;
300
          projects[countProjects]['title'] = relation.title;
301
          projects[countProjects]['code'] = relation.code;
302
      } else {
303
          projects[countProjects]['id'] = "";
304
          projects[countProjects]['acronym'] = "";
305
          projects[countProjects]['title'] = "";
306
          projects[countProjects]['code'] = "";
307
      }
308

    
309
      if(relation.hasOwnProperty("funding")) {
310
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
311

    
312
          for(let z=0; z<fundingLength; z++) {
313
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
314

    
315
              if(fundingData.hasOwnProperty("funder")) {
316
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
317
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
318
              }
319
          }
320
      }
321

    
322
      return projects;
323
    }
324

    
325
    parseRefineResults(id: string, data: any): any {
326
        var results:any = [];
327
        if(data.hasOwnProperty("resulthostingdatasource")) {
328
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
329

    
330
            for(let i=0; i<length; i++) {
331
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
332

    
333
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
334
                result['name'] = datasource.name;
335
                result['id'] = datasource.id.split("||")[0];
336
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
337
                result['count'] = datasource.count;
338

    
339
                if(result['id'] != id && result['name'] != "Unknown Repository") {
340
                    results.push(result);
341
                }
342
            }
343
        }
344
        return results;
345
    }
346

    
347
    numOfOrps(url: string, properties:EnvProperties):any {
348

    
349
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
350
                  //.map(res => <any> res.json())
351
                  .pipe(map(res => res['total']));
352
    }
353

    
354
    numOfEntityOrps(id: string, entity: string, properties:EnvProperties):any {
355
        var parameters = "";
356

    
357
        if(entity == "project") {
358
          parameters = "projects/"+id+"/other/count";
359
        }
360

    
361
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
362

    
363
        return this.numOfOrps(url, properties);
364
    }
365

    
366
    numOfSearchOrps(params: string, properties:EnvProperties, refineParams:string=null):any {
367
        let url = properties.searchAPIURLLAst+"other/count?format=json";
368

    
369
        if(params.length > 0){
370
          var DOIs:string[] = DOI.getDOIsFromString(params);
371
          var doisParams = "";
372

    
373
          for(var i =0 ;i < DOIs.length; i++){
374
            doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
375
          }
376
          if(doisParams.length > 0){
377
            url += "&"+doisParams;
378
          }else{
379
            url += "&q=" + StringUtils.URIEncode(params);
380
          }
381
        }
382
        // if(params != "") {
383
        //   url += "&q=" + StringUtils.URIEncode(params);
384
        // }
385
        if(refineParams!= null && refineParams != ''  ) {
386
            url += refineParams;
387
        }
388
        return this.numOfOrps(url, properties);
389
    }
390
}
(18-18/25)