Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {OpenaireProperties} from '../utils/properties/openaireProperties';
5
import {SearchResult}     from '../utils/entities/searchResult';
6
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
7
import 'rxjs/add/observable/of';
8
import 'rxjs/add/operator/do';
9
import 'rxjs/add/operator/share';
10
import { CacheService  } from '../shared/cache.service';
11
import {StringUtils} from '../utils/string-utils.class';
12
@Injectable()
13
export class SearchDataprovidersService {
14
    constructor(private http: Http, public _cache: CacheService) {}
15

    
16
    searchDataproviders (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
17

    
18
        let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
19

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

    
29
        let key = url;
30
        if (this._cache.has(key)) {
31
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
32
        }
33
        return this.http.get(url)
34
                    .map(res => <any> res.json())
35
                    //.do(res => console.info(res))
36
                    .do(res => {
37
                      this._cache.set(key, res);
38
                    })
39
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
40
    }
41
    //((oaftype exact datasource) and(collectedfromdatasourceid exact "openaire____::47ce9e9f4fad46e732cff06419ecaabb"))
42
    advancedSearchDataproviders (params: string, page: number, size: number ):any {
43
      let url = OpenaireProperties.getSearchResourcesAPIURL();
44
      var basicQuery = "(oaftype exact datasource) "
45
      url += "?query=";
46
      if(params!= null && params != ''  ) {
47
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
48
      }else{
49
        url +=" ( "+basicQuery+ " ) ";
50
      }
51

    
52
      url += "&page="+(page-1)+"&size="+size+"&format=json";
53
      let key = url;
54
      if (this._cache.has(key)) {
55
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])])
56
      }
57
      return this.http.get(url)
58
      .map(res => <any> res.json())
59
      //.do(res => console.info(res))
60
      .do(res => {
61
        this._cache.set(key, res);
62
      })
63
      .map(res => [res['meta'].total, this.parseResults(res['results'])])
64
    }
65

    
66
    searchCompatibleDataprovidersTable (params: string,refineParams:string):any {
67
      let size: number = 0;
68
      let url: string= OpenaireProperties.getSearchResourcesAPIURL();
69
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
70
      if(params!= null && params != ''  ) {
71
        url += params;
72
      }
73
      if(refineParams!= null && refineParams != ''  ) {
74
        url += refineParams;
75
      }
76
      url += "&page=0&size=1&format=json";
77

    
78
      let key = url;
79
      if (this._cache.has(key)) {
80
        return Observable.of(this._cache.get(key)).map(res => res['meta'].total);
81
      }
82

    
83
      return this.http.get(url).map(res => <any> res.json())
84
                        .do(res => {
85
                          this._cache.set(key, res);
86
                        })
87
                        .map(res => res['meta'].total);
88
    }
89

    
90
    searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[] ):any {
91
      console.info("in search Compatible Dataproviders service function");
92
      let url: string = OpenaireProperties.getSearchResourcesAPIURL();
93
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
94
      if(params!= null && params != ''  ) {
95
        url += params;
96
      }
97
      if(refineParams!= null && refineParams != ''  ) {
98
        url += refineParams;
99
      }
100
      url += "&page="+(page-1)+"&size="+size+"&format=json";
101
      let key = url;
102
      if (this._cache.has(key)) {
103
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
104
      }
105
      return this.http.get(url)
106
      .map(res => <any> res.json())
107
      //.do(res => console.info(res))
108
      .do(res => {
109
        this._cache.set(key, res);
110
      })
111
      .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
112
    }
113

    
114
    searchEntityRegistriesTable (params: string,refineParams:string):any {
115
      let size: number = 0;
116
      let url: string= OpenaireProperties.getSearchResourcesAPIURL();
117
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
118
      if(params!= null && params != ''  ) {
119
        url += params;
120
      }
121
      if(refineParams!= null && refineParams != ''  ) {
122
        url += refineParams;
123
      }
124
      url += "&page=0&size=1&format=json";
125

    
126
      let key = url;
127
      if (this._cache.has(key)) {
128
        return Observable.of(this._cache.get(key)).map(res => res['meta'].total);
129
      }
130

    
131
      return this.http.get(url).map(res => <any> res.json())
132
                        .do(res => {
133
                          this._cache.set(key, res);
134
                        })
135
                        .map(res => res['meta'].total);
136
    }
137

    
138
    searchEntityRegistries (params: string,refineParams:string, page: number, size: number, refineFields:string[] ):any {
139
      let url = OpenaireProperties.getSearchResourcesAPIURL();
140
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
141
      if(params!= null && params != ''  ) {
142
        url += params;
143
      }
144
      if(refineParams!= null && refineParams != ''  ) {
145
        url += refineParams;
146
      }
147
      url += "&page="+(page-1)+"&size="+size+"&format=json";
148
      let key = url;
149
      if (this._cache.has(key)) {
150
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
151
      }
152
      return this.http.get(url)
153
      .map(res => <any> res.json())
154
      //.do(res => console.info(res))
155
      .do(res => {
156
        this._cache.set(key, res);
157
      })
158
      .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
159
    }
160

    
161
    searchDataprovidersForDeposit (id: string,type:string, page: number, size: number):any {
162
        let link = OpenaireProperties.getSearchResourcesAPIURL();
163
        var compatibilities = "";
164
        if(type == "Research Data"){
165
          compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
166
        }else if(type == "Publications"){
167
          compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
168
        }
169
        let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+compatibilities+" ":"")+") and (relorganizationid exact "+id+")";
170
        url += "&page="+(page-1)+"&size="+size+"&format=json";
171

    
172
        let key = url;
173
        if (this._cache.has(key)) {
174
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
175
        }
176
        return this.http.get(url)
177
                    .map(res => <any> res.json())
178
                    .do(res => {
179
                      this._cache.set(key, res);
180
                    })
181
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
182
    }
183
    searchDataProvidersBySubjects(keyword:string, type:string, page: number, size: number):any {
184
      let link = OpenaireProperties.getSearchResourcesAPIURL();
185
      var compatibilities = "";
186
      if(type == "Research Data"){
187
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
188
      }else if(type == "Publications"){
189
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
190
      }
191
      let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+
192
      compatibilities+" ":"")+") "+
193
      " and (datasourcesubject all "+'"'+keyword+'"'+") " ;
194
      url += "&page="+(page-1)+"&size="+size+"&format=json";
195

    
196
      let key = url;
197
      if (this._cache.has(key)) {
198
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
199
      }
200
      return this.http.get(url)
201
                  .map(res => <any> res.json())
202
                  .do(res => {
203
                    this._cache.set(key, res);
204
                  })
205
                  .map(res => [res['meta'].total, this.parseResults(res['results'])]);
206

    
207
    }
208

    
209
    getDataProvidersforEntityRegistry(datasourceId: string, page: number, size: number ):any {
210
      let url = OpenaireProperties.getSearchResourcesAPIURL();
211
      var basicQuery = "(oaftype exact datasource) "
212
      url += "?query=";
213
      if(datasourceId!= null && datasourceId != ''  ) {
214
        url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
215
      }else{
216
        url +=" ( "+basicQuery+ " ) ";
217
      }
218

    
219
      url += "&page="+(page-1)+"&size="+size+"&format=json";
220
      let key = url;
221
      if (this._cache.has(key)) {
222
        return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
223
      }
224
      return this.http.get(url)
225
      .map(res => <any> res.json())
226
      .do(res => {
227
        this._cache.set(key, res);
228
      })
229
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
230
    }
231
    searchDataprovidersForEntity (params: string, page: number, size: number):any {
232
        let link = OpenaireProperties. getSearchAPIURLLast();
233
        let url = link+params+"/datasources?format=json";
234
        let key = url;
235
        if (this._cache.has(key)) {
236
          return Observable.of(this._cache.get(key)).map(res => [res['meta'].total, this.parseResults(res['results'])]);
237
        }
238
        return this.http.get(url)
239
                    .map(res => <any> res.json())
240
                    .do(res => {
241
                      this._cache.set(key, res);
242
                    })
243
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
244
    }
245

    
246
    searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
247

    
248
        let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
249

    
250
        let url = link+"?";
251
        if(params!= null && params != ''  ) {
252
            url += params;
253
        }
254
        if(refineParams!= null && refineParams != ''  ) {
255
            url += refineParams;
256
        }
257
        url += "&page="+(page-1)+"&size="+size+"&format=json";
258

    
259
        let key = url;
260
        if (this._cache.has(key)) {
261
          return Observable.of(this._cache.get(key)).map(res => this.parseResultsCSV(res['results']));
262
        }
263
        return this.http.get(url)
264
                    .map(res => <any> res.json())
265
                    //.do(res => console.info(res))
266
                    .do(res => {
267
                      this._cache.set(key, res);
268
                    })
269
                    .map(res => this.parseResultsCSV(res['results']));
270
    }
271

    
272
    searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
273
      let url = OpenaireProperties.getSearchResourcesAPIURL();
274
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
275
      if(params!= null && params != ''  ) {
276
        url += params;
277
      }
278
      if(refineParams!= null && refineParams != ''  ) {
279
        url += refineParams;
280
      }
281
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
282
      let key = url;
283
      if (this._cache.has(key)) {
284
        return Observable.of(this._cache.get(key)).map(res => this.parseResultsCSV(res['results']));
285
      }
286
      return this.http.get(url)
287
      .map(res => <any> res.json())
288
      //.do(res => console.info(res))
289
      .do(res => {
290
        this._cache.set(key, res)
291
      })
292
      .map(res => this.parseResultsCSV(res['results']));
293
    }
294

    
295
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
296
      let url = OpenaireProperties.getSearchResourcesAPIURL();
297
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
298
      if(params!= null && params != ''  ) {
299
        url += params;
300
      }
301
      if(refineParams!= null && refineParams != ''  ) {
302
        url += refineParams;
303
      }
304
      url += "&page="+(page - 1)+"&size="+size+"&format=json";
305
      let key = url;
306
      if (this._cache.has(key)) {
307
        return Observable.of(this._cache.get(key)).map(res => this.parseResultsCSV(res['results']));
308
      }
309
      return this.http.get(url)
310
      .map(res => <any> res.json())
311
      //.do(res => console.info(res))
312
      .do(res => {
313
        this._cache.set(key, res);
314
      })
315
      .map(res => this.parseResultsCSV(res['results']));
316
    }
317

    
318

    
319

    
320
    parseResults(data: any): SearchResult[] {
321
        let results: SearchResult[] = [];
322

    
323
        let length = Array.isArray(data) ? data.length : 1;
324

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

    
328
            var result: SearchResult = new SearchResult();
329

    
330
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
331

    
332
            result['title'].name = resData.officialname;
333

    
334
            //result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
335
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
336
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
337

    
338
            result['type'] = this.getDataproviderType(resData);
339

    
340
            let typeid: string = resData['datasourcetype'].classid;
341
            //console.info(typeid);
342
            if(typeid != "entityregistry" && typeid != "entityregistry::projects" && typeid != "entityregistry::repositories") {
343

    
344
                if(resData.hasOwnProperty('accessinfopackage')) {
345
                    let OAIPMHURL: string;
346
                    if(Array.isArray(resData['accessinfopackage'])) {
347
                        OAIPMHURL = resData['accessinfopackage'][0];
348
                    } else {
349
                        OAIPMHURL = resData['accessinfopackage'];
350
                    }
351

    
352
                    if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
353
                        result['OAIPMHURL'] = OAIPMHURL;
354
                    }
355
                }
356
                result['compatibility'] = this.getDataproviderCompatibility(resData);
357
            }
358

    
359
            result['websiteURL'] = resData.websiteurl;
360

    
361
            let res:[string[], {"name":string, "id":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
362
            result['organizations'] = res[1];
363
            result['countries'] = res[0];
364
            result['subjects'] = this.getDataproviderSubjects(resData);
365
            console.log(result['subjects']);
366
            results.push(result);
367
        }
368

    
369
        return results;
370
    }
371
    getDataproviderSubjects(resData: any): string [] {
372
      var subjects:string [] = [];
373

    
374
      let length = Array.isArray(resData['subjects']) ? resData['subjects'].length : 1;
375
      for(let i=0; i<length; i++) {
376
          let subject = Array.isArray(resData['subjects']) ? resData['subjects'][i] :resData['subjects'];
377
          subjects.push(subject.content);
378
      }
379
      return subjects;
380
    }
381
    getDataproviderType(resData: any): string {
382
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
383
            return resData['datasourcetype'].classname;
384
        } else {
385
            return '';
386
        }
387
    }
388

    
389
    getDataproviderCompatibility(resData: any): string {
390
        if(resData.hasOwnProperty('openairecompatibility')) {
391
            return resData['openairecompatibility'].classname;
392
        } else {
393
            return '';
394
        }
395
    }
396

    
397
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "id": string}[]] {
398
        let countries: string[] = [];
399
        let organizations: {"name": string, "id": string}[] = [];
400

    
401
        if(resData['rels'].hasOwnProperty("rel")) {
402
            let countriesSet: Set<string> = new Set<string>();
403

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

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

    
409
                if(relation.hasOwnProperty("to")) {
410
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
411
                        if(getOrganizations) {
412
                            let item: {"name":string, "id":string} = {"name": "", "id": ""};
413
                            item['name'] = relation.legalname;
414
                            item['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/relation['to'].content;
415
                            organizations.push(item);
416
                        }
417

    
418
                        if(getCountries) {
419
                            if(relation.hasOwnProperty('country') &&
420
                               relation.country.hasOwnProperty('classname')) {
421
                                if(!countriesSet.has(relation.country.classname)) {
422
                                    countriesSet.add(relation.country.classname);
423
                                    countries.push(relation.country.classname);
424
                                }
425
                            }
426
                        }
427
                    }
428
                }
429
            }
430
        }
431
        return [countries, organizations];
432
    }
433

    
434
    parseResultsCSV(data: any): any {
435
        let results: any = [];
436
        let length = Array.isArray(data) ? data.length : 1;
437

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

    
441
            var result: any = [];
442

    
443
            result.push(this.quote(resData.officialname));
444
            result.push(this.quote(this.getDataproviderType(resData)));
445
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
446
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
447
            results.push(result);
448
        }
449
        return results;
450
    }
451

    
452
    numOfDataproviders(url: string):any {
453
        //let url = OpenaireProperties. getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
454
        let key = url;
455
        if (this._cache.has(key)) {
456
          return Observable.of(this._cache.get(key));
457
        }
458
        return this.http.get(url)
459
                    .map(res => <any> res.json())
460
                    .map(res => res.total)
461
                    .do(res => {
462
                      this._cache.set(key, res);
463
                    });
464
    }
465

    
466
    numOfEntityDataproviders(id: string, entity: string):any {
467
        var parameters = "";
468
        if(entity == "organization") {
469
          parameters = "organizations/"+id+"/datasources/count";
470
        }
471

    
472
        let url = OpenaireProperties.getSearchAPIURLLast()+parameters+"?format=json";
473
        return this.numOfDataproviders(url);
474
    }
475

    
476
    numOfSearchDataproviders(params: string):any {
477
        let url: string = OpenaireProperties.getSearchAPIURLLast()+"datasources/count?format=json";
478
        if(params != "") {
479
          url += "&q=" + params;
480
        }
481

    
482
        return this.numOfDataproviders(url);
483
    }
484

    
485
    private quote(word: any): string {
486
        return '"'+word+'"';
487
    }
488
}
(15-15/20)