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 {   } from '../shared/cache.service';
11
import {StringUtils} from '../utils/string-utils.class';
12
@Injectable()
13
export class SearchDataprovidersService {
14
    constructor(private http: Http ) {}
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

    
30
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 ):any {
37
      let url = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 ():any {
55
      let size: number = 0;
56
      let url: string= OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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[] ):any {
68
      console.info("in search Compatible Dataproviders service function");
69
      let url: string = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 ():any {
86
      let size: number = 0;
87
      let url: string= OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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[] ):any {
98
      let url = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 ():any {
115
      let size: number = 0;
116
      let url: string= OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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[] ):any {
125
      console.info("in search Journals service function");
126
      let url: string = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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):any {
145
        let link = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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):any {
161
      let link = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 ):any {
181
      let url = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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):any {
197
        let link = OpenaireProperties. getSearchAPIURLLast();
198
        let url = link+params+"/datasources?format=json";
199

    
200
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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 = OpenaireProperties.getSearchResourcesAPIURL();
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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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

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

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

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

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

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

    
304
            result['websiteURL'] = resData.websiteurl;
305

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

    
314
        return results;
315
    }
316
    getDataproviderSubjects(resData: any): string [] {
317
      var subjects:string [] = [];
318

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

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

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

    
346
        if(resData['rels'].hasOwnProperty("rel")) {
347
            let countriesSet: Set<string> = new Set<string>();
348

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

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

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

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

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

    
391
            var result: any = [];
392

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

    
405
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
406
                    .map(res => <any> res.json())
407
                    .map(res => res.total);
408
    }
409

    
410
    numOfEntityDataproviders(id: string, entity: string):any {
411
        var parameters = "";
412
        if(entity == "organization") {
413
          parameters = "organizations/"+id+"/datasources/count";
414
        }
415

    
416
        let url = OpenaireProperties.getSearchAPIURLLast()+parameters+"?format=json";
417
        return this.numOfDataproviders(url);
418
    }
419

    
420
    numOfSearchDataproviders(params: string):any {
421
        let url: string = OpenaireProperties.getSearchAPIURLLast()+"datasources/count?format=json";
422
        if(params != "") {
423
          url += "&q=" + params;
424
        }
425

    
426
        return this.numOfDataproviders(url);
427
    }
428
/*
429
    private quote(word: any): string {
430
        return '"'+word+'"';
431
    }
432
*/
433
}
(13-13/19)