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, usedBy: string="search" ):any {
19
        let link = properties.searchAPIURLLAst+"datasources";
20

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

    
30

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

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

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

    
56
    searchCompatibleDataprovidersTable ( properties:EnvProperties):any {
57
      let size: number = 0;
58
      let url: string= properties.searchResourcesAPIURL;
59
      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"))';
60
      url += "&page=0&size=0&format=json";
61

    
62

    
63

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

    
69
    searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
70
      let url: string = properties.searchResourcesAPIURL;
71
      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"))';
72
      if(params!= null && params != ''  ) {
73
        url += params;
74
      }
75
      if(refineParams!= null && refineParams != ''  ) {
76
        url += refineParams;
77
      }
78
      url += "&page="+(page-1)+"&size="+size+"&format=json";
79

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

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

    
92

    
93

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

    
99
    searchEntityRegistries (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
100
      let url = properties.searchResourcesAPIURL;
101
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
102
      if(params!= null && params != ''  ) {
103
        url += params;
104
      }
105
      if(refineParams!= null && refineParams != ''  ) {
106
        url += refineParams;
107
      }
108
      url += "&page="+(page-1)+"&size="+size+"&format=json";
109

    
110
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
111
      //.map(res => <any> res.json())
112
      //.do(res => console.info(res))
113
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]));
114
    }
115

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

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

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

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

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

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

    
158

    
159
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
160
                    //.map(res => <any> res.json())
161
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
162
    }
163

    
164
    searchDataprovidersForDepositSearch(params: string, refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties, usedBy: string="search" ):any {
165
    let link = properties.searchResourcesAPIURL;
166
      let url = link+"?query=((deletedbyinference = false) AND (oaftype exact datasource)) "+params;
167
      if(refineParams!= null && refineParams != ''  ) {
168
        url += refineParams;
169
      }
170
      url += "&page="+(page-1)+"&size="+size+"&format=json";
171

    
172
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
173
                      .pipe(map(res => [res['meta'].total, this.parseResults(res['results']), RefineResultsUtils.parse(res['refineResults'],
174
                                              refineFields, "datasource", usedBy)]));
175
  }
176

    
177
    searchDataProvidersBySubjects(keyword:string, type:string, page: number, size: number, properties:EnvProperties):any {
178
      let link = properties.searchResourcesAPIURL;
179
      var compatibilities = "";
180
      if(type == "Research Data"){
181
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
182
      }else if(type == "Publications"){
183
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
184
      }
185
      let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+
186
      compatibilities+" ":"")+") "+
187
      " and (datasourcesubject all "+'"'+keyword+'"'+") " ;
188
      url += "&page="+(page-1)+"&size="+size+"&format=json";
189

    
190

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

    
195
    }
196

    
197
    getDataProvidersforEntityRegistry(datasourceId: string, page: number, size: number , properties:EnvProperties):any {
198
      let url = properties.searchResourcesAPIURL;
199
      var basicQuery = "(oaftype exact datasource) "
200
      url += "?query=";
201
      if(datasourceId!= null && datasourceId != ''  ) {
202
        url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
203
      }else{
204
        url +=" ( "+basicQuery+ " ) ";
205
      }
206

    
207
      url += "&page="+(page-1)+"&size="+size+"&format=json";
208

    
209
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
210
      //.map(res => <any> res.json())
211
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
212
    }
213
    searchDataprovidersForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
214
        let link = properties.searchAPIURLLAst;
215
        let url = link+params+"/datasources?format=json";
216

    
217
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
218
                    //.map(res => <any> res.json())
219
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
220
    }
221
/*
222
    searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
223

    
224
        let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
225

    
226
        let url = link+"?";
227
        if(params!= null && params != ''  ) {
228
            url += params;
229
        }
230
        if(refineParams!= null && refineParams != ''  ) {
231
            url += refineParams;
232
        }
233
        url += "&page="+(page-1)+"&size="+size+"&format=json";
234

    
235
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
236
                    .map(res => <any> res.json())
237
                    //.do(res => console.info(res))
238

    
239
                    .map(res => this.parseResultsCSV(res['results']));
240
    }
241
*/
242
/*
243
    searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
244
      let url = properties.searchAPIURLLAst;
245
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
246
      if(params!= null && params != ''  ) {
247
        url += params;
248
      }
249
      if(refineParams!= null && refineParams != ''  ) {
250
        url += refineParams;
251
      }
252
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
253

    
254
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
255
      .map(res => <any> res.json())
256
      //.do(res => console.info(res))
257

    
258
      .map(res => this.parseResultsCSV(res['results']));
259
    }
260
*/
261
/*
262
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
263
      let url = properties.searchAPIURLLAst;
264
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
265
      if(params!= null && params != ''  ) {
266
        url += params;
267
      }
268
      if(refineParams!= null && refineParams != ''  ) {
269
        url += refineParams;
270
      }
271
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
272

    
273
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
274
      .map(res => <any> res.json())
275
      //.do(res => console.info(res))
276

    
277
      .map(res => this.parseResultsCSV(res['results']));
278
    }
279
*/
280

    
281

    
282
    parseResults(data: any): SearchResult[] {
283
        let results: SearchResult[] = [];
284

    
285
        let length = Array.isArray(data) ? data.length : 1;
286

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

    
290
            var result: SearchResult = new SearchResult();
291

    
292
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
293

    
294
            result['title'].name = resData.officialname;
295
            result['englishname'] = resData.englishname;
296

    
297
            //result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
298
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
299
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
300

    
301
            result['type'] = this.getDataproviderType(resData);
302

    
303
            let typeid: string = resData['datasourcetype'].classid;
304
            //console.info(typeid);
305
            if(typeid != "entityregistry" && typeid != "entityregistry::projects" && typeid != "entityregistry::repositories") {
306

    
307
                if(resData.hasOwnProperty('accessinfopackage')) {
308
                    let OAIPMHURL: string;
309
                    if(Array.isArray(resData['accessinfopackage'])) {
310
                        OAIPMHURL = resData['accessinfopackage'][0];
311
                    } else {
312
                        OAIPMHURL = resData['accessinfopackage'];
313
                    }
314

    
315
                    if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
316
                        result['OAIPMHURL'] = OAIPMHURL;
317
                    }
318
                }
319
                let compatibility = this.getDataproviderCompatibility(resData);
320
                result['compatibility'] = compatibility.classname;
321
                if(compatibility.classid == "UNKNOWN") {
322
                  result['compatibilityUNKNOWN'] = true;
323
                }
324
            } else {
325
              result['compatibility'] = "";
326
            }
327

    
328
            result['websiteURL'] = resData.websiteurl;
329

    
330
            let res:[string[], {"name":string, "id":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
331
            result['organizations'] = res[1];
332
            result['countries'] = res[0];
333
            result['subjects'] = this.getDataproviderSubjects(resData);
334
            //console.log(result['subjects']);
335
            results.push(result);
336
        }
337

    
338
        return results;
339
    }
340
    getDataproviderSubjects(resData: any): string [] {
341
      var subjects:string [] = [];
342

    
343
      let length = Array.isArray(resData['subjects']) ? resData['subjects'].length : 1;
344
      for(let i=0; i<length; i++) {
345
          let subject = Array.isArray(resData['subjects']) ? resData['subjects'][i] :resData['subjects'];
346
          subjects.push(subject.content);
347
      }
348
      return subjects;
349
    }
350
    getDataproviderType(resData: any): string {
351
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
352
            return resData['datasourcetype'].classname;
353
        } else {
354
            return '';
355
        }
356
    }
357

    
358
    getDataproviderCompatibility(resData: any): {"classid": string, "classname": string} {
359
        if(resData.hasOwnProperty('openairecompatibility')) {
360
            return {"classid": resData['openairecompatibility'].classid, "classname": resData['openairecompatibility'].classname};
361
        } else {
362
            return {"classid": "", "classname": ""};
363
        }
364
    }
365

    
366
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "id": string}[]] {
367
        let countries: string[] = [];
368
        let organizations: {"name": string, "id": string}[] = [];
369

    
370
        if(resData['rels'].hasOwnProperty("rel")) {
371
            let countriesSet: Set<string> = new Set<string>();
372

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

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

    
378
                if(relation.hasOwnProperty("to")) {
379
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
380
                        if(getOrganizations) {
381
                            let item: {"name":string, "id":string} = {"name": "", "id": ""};
382
                            //item['name'] = relation.legalname;
383
                            if(relation.legalshortname) {
384
                              item['name'] = relation.legalshortname;
385
                            } else {
386
                              item['name'] = relation.legalname;
387
                            }
388
                            item['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/relation['to'].content;
389
                            organizations.push(item);
390
                        }
391

    
392
                        if(getCountries) {
393
                            if(relation.hasOwnProperty('country') &&
394
                               relation.country.hasOwnProperty('classname')) {
395
                                if(!countriesSet.has(relation.country.classname)) {
396
                                    countriesSet.add(relation.country.classname);
397
                                    countries.push(relation.country.classname);
398
                                }
399
                            }
400
                        }
401
                    }
402
                }
403
            }
404
        }
405
        return [countries, organizations];
406
    }
407
/*
408
    parseResultsCSV(data: any): any {
409
        let results: any = [];
410
        let length = Array.isArray(data) ? data.length : 1;
411

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

    
415
            var result: any = [];
416

    
417
            result.push(this.quote(resData.officialname));
418
            result.push(this.quote(this.getDataproviderType(resData)));
419
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
420
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
421
            results.push(result);
422
        }
423
        return results;
424
    }
425
*/
426
    numOfDataproviders(url: string, properties:EnvProperties):any {
427
        //let url = OpenaireProperties. getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
428

    
429
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
430
                    //.map(res => <any> res.json())
431
                    .pipe(map(res => res['total']));
432
    }
433

    
434
    numOfEntityDataproviders(id: string, entity: string, properties:EnvProperties):any {
435
        var parameters = "";
436
        if(entity == "organization") {
437
          parameters = "organizations/"+id+"/datasources/count";
438
        }
439

    
440
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
441
        return this.numOfDataproviders(url, properties);
442
    }
443

    
444
    numOfSearchDataproviders(params: string, properties:EnvProperties, refineQuery:string=null):any {
445
        let url: string = properties.searchAPIURLLAst+"datasources/count?format=json";
446
        if(params != "") {
447
          url += "&q=" + StringUtils.URIEncode(params);
448
        }
449
      if(refineQuery!= null && refineQuery != ''  ) {
450
        url += refineQuery;
451
      }
452

    
453
        return this.numOfDataproviders(url, properties);
454
    }
455
/*
456
    private quote(word: any): string {
457
        return '"'+word+'"';
458
    }
459
*/
460
getDataprovidersTableResults (queryType:string, properties:EnvProperties):any {
461
  let size: number = 0;
462
  let url: string= properties.searchAPIURLLAst;
463
  if(queryType == "compatible"){
464
      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"))';
465
  }else if(queryType=="registries"){
466
    url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
467
  }else if(queryType=="journals"){
468
    url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
469

    
470
  }
471

    
472
  url += "&page=0&size=0&format=json";
473

    
474

    
475

    
476
  return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
477
                  //.map(res => <any> res.json())
478
                  .pipe(map(res => res['meta'].total));
479
}
480
}
(16-16/26)