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
      'and (datasourcecompatibilityid <> "UNKNOWN")';
40
      url += "?query=";
41
      if(params!= null && params != ''  ) {
42
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
43
      }else{
44
        url +=" ( "+basicQuery+ " ) ";
45
      }
46

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

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

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

    
61

    
62

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

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

    
68
    searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
69
      console.info("in search Compatible Dataproviders service function");
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
      .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).map(res => <any> res.json())
95
                        .map(res => res['meta'].total);
96
    }
97

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

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

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

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

    
125
    searchJournals (params: string,refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
126
      console.info("in search Journals service function");
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
      .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
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
162
    }
163
    searchDataProvidersBySubjects(keyword:string, type:string, page: number, size: number, properties:EnvProperties):any {
164
      let link = properties.searchResourcesAPIURL;
165
      var compatibilities = "";
166
      if(type == "Research Data"){
167
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
168
      }else if(type == "Publications"){
169
        compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
170
      }
171
      let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+
172
      compatibilities+" ":"")+") "+
173
      " and (datasourcesubject all "+'"'+keyword+'"'+") " ;
174
      url += "&page="+(page-1)+"&size="+size+"&format=json";
175

    
176

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

    
181
    }
182

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

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

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

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

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

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

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

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

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

    
244
      .map(res => this.parseResultsCSV(res['results']));
245
    }
246
*/
247
/*
248
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
249
      let url = properties.searchAPIURLLAst;
250
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
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
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
260
      .map(res => <any> res.json())
261
      //.do(res => console.info(res))
262

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

    
267

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
401
            var result: any = [];
402

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

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

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

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

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

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

    
453
  }
454

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

    
457

    
458

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

    
461
                    .map(res => res['meta'].total);
462
}
463
}
(14-14/21)