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 {StringUtils} from '../utils/string-utils.class';
11
import{EnvProperties} from '../utils/properties/env-properties';
12
import {map} from "rxjs/operators";
13

    
14
@Injectable()
15
export class SearchDataprovidersService {
16
    constructor(private http: HttpClient ) {}
17

    
18
    searchDataproviders (params: string, refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
19

    
20
        let link = properties.searchAPIURLLAst+"datasources";
21

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

    
31

    
32
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
33
                    //.map(res => <any> res.json())
34
                    //.do(res => console.info(res))
35
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]));
36
    }
37
    //((oaftype exact datasource) and(collectedfromdatasourceid exact "openaire____::47ce9e9f4fad46e732cff06419ecaabb"))
38
    advancedSearchDataproviders (params: string, page: number, size: number, properties: EnvProperties ):any {
39
      let url =  properties.searchResourcesAPIURL;
40
      var basicQuery = "(oaftype exact datasource) " +
41
      'and (datasourcecompatibilityid <> "UNKNOWN")';
42
      url += "?query=";
43
      if(params!= null && params != ''  ) {
44
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
45
      }else{
46
        url +=" ( "+basicQuery+ " ) ";
47
      }
48

    
49
      url += "&page="+(page-1)+"&size="+size+"&format=json";
50

    
51
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
52
      //.map(res => <any> res.json())
53
      //.do(res => console.info(res))
54
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]))
55
    }
56

    
57
    searchCompatibleDataprovidersTable ( properties:EnvProperties):any {
58
      let size: number = 0;
59
      let url: string= properties.searchResourcesAPIURL;
60
      url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
61
      url += "&page=0&size=0&format=json";
62

    
63

    
64

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

    
70
    searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
71
      let url: string = properties.searchResourcesAPIURL;
72
      url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
73
      if(params!= null && params != ''  ) {
74
        url += params;
75
      }
76
      if(refineParams!= null && refineParams != ''  ) {
77
        url += refineParams;
78
      }
79
      url += "&page="+(page-1)+"&size="+size+"&format=json";
80

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

    
87
    searchEntityRegistriesTable (properties:EnvProperties):any {
88
      let size: number = 0;
89
      let url: string= properties.searchResourcesAPIURL;
90
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
91
      url += "&page=0&size=0&format=json";
92

    
93

    
94

    
95
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
96
                      //.map(res => <any> res.json())
97
                      .pipe(map(res => res['meta'].total));
98
    }
99

    
100
    searchEntityRegistries (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
101
      let url = properties.searchResourcesAPIURL;
102
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
103
      if(params!= null && params != ''  ) {
104
        url += params;
105
      }
106
      if(refineParams!= null && refineParams != ''  ) {
107
        url += refineParams;
108
      }
109
      url += "&page="+(page-1)+"&size="+size+"&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']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]));
115
    }
116

    
117
    searchJournalsTable ( properties:EnvProperties):any {
118
      let size: number = 0;
119
      let url: string= properties.searchResourcesAPIURL;
120
      url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
121
      url += "&page=0&size=0&format=json";
122

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

    
127
    searchJournals (params: string,refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
128
      let url: string = properties.searchResourcesAPIURL;
129
      //url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
130
      url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
131

    
132
      if(params!= null && params != ''  ) {
133
        url += params;
134
      }
135
      if(refineParams!= null && refineParams != ''  ) {
136
        url += refineParams;
137
      }
138
      url += "&page="+(page-1)+"&size="+size+"&format=json";
139

    
140
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
141
      //.map(res => <any> res.json())
142
      //.do(res => console.info(res))
143
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]));
144
    }
145

    
146
    searchDataprovidersForDeposit (id: string,type:string, page: number, size: number, properties:EnvProperties):any {
147
        let link = properties.searchResourcesAPIURL;
148
        var compatibilities = "";
149
        if(type == "Research Data"){
150
          //compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
151
          compatibilities = " and (datasourcecompatibilityid = openaire2.0_data)";
152
        }else if(type == "Publications"){
153
          //compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
154
          compatibilities = " and (datasourcecompatibilityid <> openaire2.0_data)";
155
        }
156
        let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+compatibilities+" ":"")+") and (relorganizationid exact \""+id+"\")";
157
        url += "&page="+(page-1)+"&size="+size+"&format=json";
158

    
159

    
160
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
161
                    //.map(res => <any> res.json())
162
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
163
    }
164
    searchDataProvidersBySubjects(keyword:string, type:string, page: number, size: number, properties:EnvProperties):any {
165
      let link = properties.searchResourcesAPIURL;
166
      var compatibilities = "";
167
      if(type == "Research Data"){
168
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
169
      }else if(type == "Publications"){
170
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
171
      }
172
      let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+
173
      compatibilities+" ":"")+") "+
174
      " and (datasourcesubject all "+'"'+keyword+'"'+") " ;
175
      url += "&page="+(page-1)+"&size="+size+"&format=json";
176

    
177

    
178
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
179
                  //.map(res => <any> res.json())
180
                  .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
181

    
182
    }
183

    
184
    getDataProvidersforEntityRegistry(datasourceId: string, page: number, size: number , properties:EnvProperties):any {
185
      let url = properties.searchResourcesAPIURL;
186
      var basicQuery = "(oaftype exact datasource) "
187
      url += "?query=";
188
      if(datasourceId!= null && datasourceId != ''  ) {
189
        url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
190
      }else{
191
        url +=" ( "+basicQuery+ " ) ";
192
      }
193

    
194
      url += "&page="+(page-1)+"&size="+size+"&format=json";
195

    
196
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
197
      //.map(res => <any> res.json())
198
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
199
    }
200
    searchDataprovidersForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
201
        let link = properties.searchAPIURLLAst;
202
        let url = link+params+"/datasources?format=json";
203

    
204
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
205
                    //.map(res => <any> res.json())
206
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
207
    }
208
/*
209
    searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
210

    
211
        let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
212

    
213
        let url = link+"?";
214
        if(params!= null && params != ''  ) {
215
            url += params;
216
        }
217
        if(refineParams!= null && refineParams != ''  ) {
218
            url += refineParams;
219
        }
220
        url += "&page="+(page-1)+"&size="+size+"&format=json";
221

    
222
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
223
                    .map(res => <any> res.json())
224
                    //.do(res => console.info(res))
225

    
226
                    .map(res => this.parseResultsCSV(res['results']));
227
    }
228
*/
229
/*
230
    searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
231
      let url = properties.searchAPIURLLAst;
232
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
233
      if(params!= null && params != ''  ) {
234
        url += params;
235
      }
236
      if(refineParams!= null && refineParams != ''  ) {
237
        url += refineParams;
238
      }
239
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
240

    
241
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
242
      .map(res => <any> res.json())
243
      //.do(res => console.info(res))
244

    
245
      .map(res => this.parseResultsCSV(res['results']));
246
    }
247
*/
248
/*
249
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
250
      let url = properties.searchAPIURLLAst;
251
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
252
      if(params!= null && params != ''  ) {
253
        url += params;
254
      }
255
      if(refineParams!= null && refineParams != ''  ) {
256
        url += refineParams;
257
      }
258
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
259

    
260
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
261
      .map(res => <any> res.json())
262
      //.do(res => console.info(res))
263

    
264
      .map(res => this.parseResultsCSV(res['results']));
265
    }
266
*/
267

    
268

    
269
    parseResults(data: any): SearchResult[] {
270
        let results: SearchResult[] = [];
271

    
272
        let length = Array.isArray(data) ? data.length : 1;
273

    
274
        for(let i=0; i<length; i++) {
275
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:datasource'] : data['result']['metadata']['oaf:entity']['oaf:datasource'];
276

    
277
            var result: SearchResult = new SearchResult();
278

    
279
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
280

    
281
            result['title'].name = resData.officialname;
282
            result['englishname'] = resData.englishname;
283

    
284
            //result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
285
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
286
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
287

    
288
            result['type'] = this.getDataproviderType(resData);
289

    
290
            let typeid: string = resData['datasourcetype'].classid;
291
            //console.info(typeid);
292
            if(typeid != "entityregistry" && typeid != "entityregistry::projects" && typeid != "entityregistry::repositories") {
293

    
294
                if(resData.hasOwnProperty('accessinfopackage')) {
295
                    let OAIPMHURL: string;
296
                    if(Array.isArray(resData['accessinfopackage'])) {
297
                        OAIPMHURL = resData['accessinfopackage'][0];
298
                    } else {
299
                        OAIPMHURL = resData['accessinfopackage'];
300
                    }
301

    
302
                    if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
303
                        result['OAIPMHURL'] = OAIPMHURL;
304
                    }
305
                }
306
                let compatibility = this.getDataproviderCompatibility(resData);
307
                result['compatibility'] = compatibility.classname;
308
                if(compatibility.classid == "UNKNOWN") {
309
                  result['compatibilityUNKNOWN'] = true;
310
                }
311
            } else {
312
              result['compatibility'] = "";
313
            }
314

    
315
            result['websiteURL'] = resData.websiteurl;
316

    
317
            let res:[string[], {"name":string, "id":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
318
            result['organizations'] = res[1];
319
            result['countries'] = res[0];
320
            result['subjects'] = this.getDataproviderSubjects(resData);
321
            //console.log(result['subjects']);
322
            results.push(result);
323
        }
324

    
325
        return results;
326
    }
327
    getDataproviderSubjects(resData: any): string [] {
328
      var subjects:string [] = [];
329

    
330
      let length = Array.isArray(resData['subjects']) ? resData['subjects'].length : 1;
331
      for(let i=0; i<length; i++) {
332
          let subject = Array.isArray(resData['subjects']) ? resData['subjects'][i] :resData['subjects'];
333
          subjects.push(subject.content);
334
      }
335
      return subjects;
336
    }
337
    getDataproviderType(resData: any): string {
338
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
339
            return resData['datasourcetype'].classname;
340
        } else {
341
            return '';
342
        }
343
    }
344

    
345
    getDataproviderCompatibility(resData: any): {"classid": string, "classname": string} {
346
        if(resData.hasOwnProperty('openairecompatibility')) {
347
            return {"classid": resData['openairecompatibility'].classid, "classname": resData['openairecompatibility'].classname};
348
        } else {
349
            return {"classid": "", "classname": ""};
350
        }
351
    }
352

    
353
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "id": string}[]] {
354
        let countries: string[] = [];
355
        let organizations: {"name": string, "id": string}[] = [];
356

    
357
        if(resData['rels'].hasOwnProperty("rel")) {
358
            let countriesSet: Set<string> = new Set<string>();
359

    
360
            let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
361

    
362
            for(let i=0; i<relLength; i++) {
363
                let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][i] : resData['rels']['rel'];
364

    
365
                if(relation.hasOwnProperty("to")) {
366
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
367
                        if(getOrganizations) {
368
                            let item: {"name":string, "id":string} = {"name": "", "id": ""};
369
                            //item['name'] = relation.legalname;
370
                            if(relation.legalshortname) {
371
                              item['name'] = relation.legalshortname;
372
                            } else {
373
                              item['name'] = relation.legalname;
374
                            }
375
                            item['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/relation['to'].content;
376
                            organizations.push(item);
377
                        }
378

    
379
                        if(getCountries) {
380
                            if(relation.hasOwnProperty('country') &&
381
                               relation.country.hasOwnProperty('classname')) {
382
                                if(!countriesSet.has(relation.country.classname)) {
383
                                    countriesSet.add(relation.country.classname);
384
                                    countries.push(relation.country.classname);
385
                                }
386
                            }
387
                        }
388
                    }
389
                }
390
            }
391
        }
392
        return [countries, organizations];
393
    }
394
/*
395
    parseResultsCSV(data: any): any {
396
        let results: any = [];
397
        let length = Array.isArray(data) ? data.length : 1;
398

    
399
        for(let i=0; i<length; i++) {
400
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:datasource'] : data['result']['metadata']['oaf:entity']['oaf:datasource'];
401

    
402
            var result: any = [];
403

    
404
            result.push(this.quote(resData.officialname));
405
            result.push(this.quote(this.getDataproviderType(resData)));
406
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
407
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
408
            results.push(result);
409
        }
410
        return results;
411
    }
412
*/
413
    numOfDataproviders(url: string, properties:EnvProperties):any {
414
        //let url = OpenaireProperties. getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
415

    
416
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
417
                    //.map(res => <any> res.json())
418
                    .pipe(map(res => res['total']));
419
    }
420

    
421
    numOfEntityDataproviders(id: string, entity: string, properties:EnvProperties):any {
422
        var parameters = "";
423
        if(entity == "organization") {
424
          parameters = "organizations/"+id+"/datasources/count";
425
        }
426

    
427
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
428
        return this.numOfDataproviders(url, properties);
429
    }
430

    
431
    numOfSearchDataproviders(params: string, properties:EnvProperties):any {
432
        let url: string = properties.searchAPIURLLAst+"datasources/count?format=json";
433
        if(params != "") {
434
          url += "&q=" + StringUtils.URIEncode(params);
435
        }
436

    
437
        return this.numOfDataproviders(url, properties);
438
    }
439
/*
440
    private quote(word: any): string {
441
        return '"'+word+'"';
442
    }
443
*/
444
getDataprovidersTableResults (queryType:string, properties:EnvProperties):any {
445
  let size: number = 0;
446
  let url: string= properties.searchAPIURLLAst;
447
  if(queryType == "compatible"){
448
      url += '?query=((oaftype exact datasource) not (datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
449
  }else if(queryType=="registries"){
450
    url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
451
  }else if(queryType=="journals"){
452
    url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
453

    
454
  }
455

    
456
  url += "&page=0&size=0&format=json";
457

    
458

    
459

    
460
  return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
461
                  //.map(res => <any> res.json())
462
                  .pipe(map(res => res['meta'].total));
463
}
464
}
(15-15/22)