Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {SearchResult}     from '../utils/entities/searchResult';
5
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
6
import 'rxjs/add/observable/of';
7
import 'rxjs/add/operator/do';
8
import 'rxjs/add/operator/share';
9
import {StringUtils} from '../utils/string-utils.class';
10
import{EnvProperties} from '../utils/properties/env-properties';
11

    
12
@Injectable()
13
export class SearchDataprovidersService {
14
    constructor(private http: Http ) {}
15

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

    
18
        let link = properties.searchAPIURLLAst+"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

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

    
46
      url += "&page="+(page-1)+"&size="+size+"&format=json";
47

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

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

    
60

    
61

    
62
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
63

    
64
                        .map(res => res['meta'].total);
65
    }
66

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

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

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

    
91

    
92

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

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

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

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

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

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

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

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

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

    
155

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

    
173

    
174
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
175
                  .map(res => <any> res.json())
176
                  .map(res => [res['meta'].total, this.parseResults(res['results'])]);
177

    
178
    }
179

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

    
190
      url += "&page="+(page-1)+"&size="+size+"&format=json";
191

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

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

    
207
        let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
208

    
209
        let url = link+"?";
210
        if(params!= null && params != ''  ) {
211
            url += params;
212
        }
213
        if(refineParams!= null && refineParams != ''  ) {
214
            url += refineParams;
215
        }
216
        url += "&page="+(page-1)+"&size="+size+"&format=json";
217

    
218
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
219
                    .map(res => <any> res.json())
220
                    //.do(res => console.info(res))
221

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

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

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

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

    
260
      .map(res => this.parseResultsCSV(res['results']));
261
    }
262
*/
263

    
264

    
265
    parseResults(data: any): SearchResult[] {
266
        let results: SearchResult[] = [];
267

    
268
        let length = Array.isArray(data) ? data.length : 1;
269

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

    
273
            var result: SearchResult = new SearchResult();
274

    
275
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
276

    
277
            result['title'].name = resData.officialname;
278
            result['englishname'] = resData.englishname;
279

    
280
            //result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
281
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
282
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
283

    
284
            result['type'] = this.getDataproviderType(resData);
285

    
286
            let typeid: string = resData['datasourcetype'].classid;
287
            //console.info(typeid);
288
            if(typeid != "entityregistry" && typeid != "entityregistry::projects" && typeid != "entityregistry::repositories") {
289

    
290
                if(resData.hasOwnProperty('accessinfopackage')) {
291
                    let OAIPMHURL: string;
292
                    if(Array.isArray(resData['accessinfopackage'])) {
293
                        OAIPMHURL = resData['accessinfopackage'][0];
294
                    } else {
295
                        OAIPMHURL = resData['accessinfopackage'];
296
                    }
297

    
298
                    if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
299
                        result['OAIPMHURL'] = OAIPMHURL;
300
                    }
301
                }
302
                result['compatibility'] = this.getDataproviderCompatibility(resData);
303
            } else {
304
              result['compatibility'] = '';
305
            }
306

    
307
            result['websiteURL'] = resData.websiteurl;
308

    
309
            let res:[string[], {"name":string, "id":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
310
            result['organizations'] = res[1];
311
            result['countries'] = res[0];
312
            result['subjects'] = this.getDataproviderSubjects(resData);
313
            //console.log(result['subjects']);
314
            results.push(result);
315
        }
316

    
317
        return results;
318
    }
319
    getDataproviderSubjects(resData: any): string [] {
320
      var subjects:string [] = [];
321

    
322
      let length = Array.isArray(resData['subjects']) ? resData['subjects'].length : 1;
323
      for(let i=0; i<length; i++) {
324
          let subject = Array.isArray(resData['subjects']) ? resData['subjects'][i] :resData['subjects'];
325
          subjects.push(subject.content);
326
      }
327
      return subjects;
328
    }
329
    getDataproviderType(resData: any): string {
330
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
331
            return resData['datasourcetype'].classname;
332
        } else {
333
            return '';
334
        }
335
    }
336

    
337
    getDataproviderCompatibility(resData: any): string {
338
        if(resData.hasOwnProperty('openairecompatibility')) {
339
            return resData['openairecompatibility'].classname;
340
        } else {
341
            return '';
342
        }
343
    }
344

    
345
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "id": string}[]] {
346
        let countries: string[] = [];
347
        let organizations: {"name": string, "id": string}[] = [];
348

    
349
        if(resData['rels'].hasOwnProperty("rel")) {
350
            let countriesSet: Set<string> = new Set<string>();
351

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

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

    
357
                if(relation.hasOwnProperty("to")) {
358
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
359
                        if(getOrganizations) {
360
                            let item: {"name":string, "id":string} = {"name": "", "id": ""};
361
                            //item['name'] = relation.legalname;
362
                            if(relation.legalshortname) {
363
                              item['name'] = relation.legalshortname;
364
                            } else {
365
                              item['name'] = relation.legalname;
366
                            }
367
                            item['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/relation['to'].content;
368
                            organizations.push(item);
369
                        }
370

    
371
                        if(getCountries) {
372
                            if(relation.hasOwnProperty('country') &&
373
                               relation.country.hasOwnProperty('classname')) {
374
                                if(!countriesSet.has(relation.country.classname)) {
375
                                    countriesSet.add(relation.country.classname);
376
                                    countries.push(relation.country.classname);
377
                                }
378
                            }
379
                        }
380
                    }
381
                }
382
            }
383
        }
384
        return [countries, organizations];
385
    }
386
/*
387
    parseResultsCSV(data: any): any {
388
        let results: any = [];
389
        let length = Array.isArray(data) ? data.length : 1;
390

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

    
394
            var result: any = [];
395

    
396
            result.push(this.quote(resData.officialname));
397
            result.push(this.quote(this.getDataproviderType(resData)));
398
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
399
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
400
            results.push(result);
401
        }
402
        return results;
403
    }
404
*/
405
    numOfDataproviders(url: string, properties:EnvProperties):any {
406
        //let url = OpenaireProperties. getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
407

    
408
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
409
                    .map(res => <any> res.json())
410
                    .map(res => res.total);
411
    }
412

    
413
    numOfEntityDataproviders(id: string, entity: string, properties:EnvProperties):any {
414
        var parameters = "";
415
        if(entity == "organization") {
416
          parameters = "organizations/"+id+"/datasources/count";
417
        }
418

    
419
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
420
        return this.numOfDataproviders(url, properties);
421
    }
422

    
423
    numOfSearchDataproviders(params: string, properties:EnvProperties):any {
424
        let url: string = properties.searchAPIURLLAst+"datasources/count?format=json";
425
        if(params != "") {
426
          url += "&q=" + params;
427
        }
428

    
429
        return this.numOfDataproviders(url, properties);
430
    }
431
/*
432
    private quote(word: any): string {
433
        return '"'+word+'"';
434
    }
435
*/
436
getDataprovidersTableResults (queryType:string, properties:EnvProperties):any {
437
  let size: number = 0;
438
  let url: string= properties.searchAPIURLLAst;
439
  if(queryType == "compatible"){
440
      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"))';
441
  }else if(queryType=="registries"){
442
    url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
443
  }else if(queryType=="journals"){
444
    url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
445

    
446
  }
447

    
448
  url += "&page=0&size=0&format=json";
449

    
450

    
451

    
452
  return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
453

    
454
                    .map(res => res['meta'].total);
455
}
456
}
(13-13/19)