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
@Injectable()
12
export class SearchDataprovidersService {
13
    constructor(private http: Http, public _cache: CacheService) {}
14

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

    
17
        let link = OpenaireProperties.getSearchAPIURL()+"datasources";
18

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

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

    
51
      url += "&page="+(page-1)+"&size="+size;
52
      url += "&format=json";
53
      let key = url;
54
      if (this._cache.has(key)) {
55
        return Observable.of(this._cache.get(key));
56
      }
57
      return this.http.get(url)
58
      .map(res => <any> res.json())
59
      //.do(res => console.info(res))
60
      .map(res => [res['meta'].total, this.parseResults(res['results'])])
61
      .do(res => {
62
        this._cache.set(key, res);
63
      });
64
    }
65
    searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[] ):any {
66
      let url = OpenaireProperties.getSearchResourcesAPIURL();
67
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
68
      if(params!= null && params != ''  ) {
69
        url += params;
70
      }
71
      if(refineParams!= null && refineParams != ''  ) {
72
        url += refineParams;
73
      }
74
      url += "&page="+(page-1)+"&size="+size;
75
      url += "&format=json";
76
      let key = url;
77
      if (this._cache.has(key)) {
78
        return Observable.of(this._cache.get(key));
79
      }
80
      return this.http.get(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)])
84
      .do(res => {
85
        this._cache.set(key, res);
86
      });
87
    }
88
    searchEntityRegistries (params: string,refineParams:string, page: number, size: number, refineFields:string[] ):any {
89
      let url = OpenaireProperties.getSearchResourcesAPIURL();
90
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
91
      if(params!= null && params != ''  ) {
92
        url += params;
93
      }
94
      if(refineParams!= null && refineParams != ''  ) {
95
        url += refineParams;
96
      }
97
      url += "&page="+(page-1)+"&size="+size;
98
      url += "&format=json";
99
      let key = url;
100
      if (this._cache.has(key)) {
101
        return Observable.of(this._cache.get(key));
102
      }
103
      return this.http.get(url)
104
      .map(res => <any> res.json())
105
      //.do(res => console.info(res))
106
      .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)])
107
      .do(res => {
108
        this._cache.set(key, res);
109
      });
110
    }
111

    
112
    searchDataprovidersForDeposit (id: string,type:string, page: number, size: number):any {
113
        let link = OpenaireProperties.getSearchResourcesAPIURL();
114
        var compatibilities = "";
115
        if(type == "Datasets"){
116
          compatibilities = " and (datasourcecompatibilityid = openaire2.0_data)"
117
        }else if(type == "Publications"){
118
          compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
119
        }
120
        let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+compatibilities+" ":"")+") and (relorganizationid exact "+id+")";
121
        url += "&page="+(page-1)+"&size="+size;
122
        url += "&format=json";
123

    
124
        let key = url;
125
        if (this._cache.has(key)) {
126
          return Observable.of(this._cache.get(key));
127
        }
128
        return this.http.get(url)
129
                    .map(res => <any> res.json())
130
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
131
                    .do(res => {
132
                      this._cache.set(key, res);
133
                    });
134
    }
135
    getDataProvidersforEntityRegistry(datasourceId: string, page: number, size: number ):any {
136
      let url = OpenaireProperties.getSearchResourcesAPIURL();
137
      var basicQuery = "(oaftype exact datasource) "
138
      url += "?query=";
139
      if(datasourceId!= null && datasourceId != ''  ) {
140
        url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
141
      }else{
142
        url +=" ( "+basicQuery+ " ) ";
143
      }
144

    
145
      url += "&page="+(page-1)+"&size="+size;
146
      url += "&format=json";
147
      let key = url;
148
      if (this._cache.has(key)) {
149
        return Observable.of(this._cache.get(key));
150
      }
151
      return this.http.get(url)
152
      .map(res => <any> res.json())
153
      .map(res => [res['meta'].total, this.parseResults(res['results'])])
154
      .do(res => {
155
        this._cache.set(key, res);
156
      });
157
    }
158
    searchDataprovidersForEntity (params: string, page: number, size: number):any {
159
        let link = OpenaireProperties.getSearchAPIURL();
160
        let url = link+params+"/datasources";
161
        let key = url;
162
        if (this._cache.has(key)) {
163
          return Observable.of(this._cache.get(key));
164
        }
165
        return this.http.get(url)
166
                    .map(res => <any> res.json())
167
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
168
                    .do(res => {
169
                      this._cache.set(key, res);
170
                    });
171
    }
172

    
173
    searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
174

    
175
        let link = OpenaireProperties.getSearchAPIURL()+"datasources";
176

    
177
        let url = link+"?";
178
        if(params!= null && params != ''  ) {
179
            url += params;
180
        }
181
        if(refineParams!= null && refineParams != ''  ) {
182
            url += refineParams;
183
        }
184
        url += "&page="+page+"&size="+size;
185

    
186
        let key = url;
187
        if (this._cache.has(key)) {
188
          return Observable.of(this._cache.get(key));
189
        }
190
        return this.http.get(url)
191
                    .map(res => <any> res.json())
192
                    //.do(res => console.info(res))
193
                    .map(res => this.parseResultsCSV(res['results']))
194
                    .do(res => {
195
                      this._cache.set(key, res);
196
                    });
197
    }
198

    
199
    searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
200
      let url = OpenaireProperties.getSearchResourcesAPIURL();
201
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
202
      if(params!= null && params != ''  ) {
203
        url += params;
204
      }
205
      if(refineParams!= null && refineParams != ''  ) {
206
        url += refineParams;
207
      }
208
      url += "&page="+page+"&size="+size;
209
      let key = url;
210
      if (this._cache.has(key)) {
211
        return Observable.of(this._cache.get(key));
212
      }
213
      return this.http.get(url)
214
      .map(res => <any> res.json())
215
      //.do(res => console.info(res))
216
      .map(res => this.parseResultsCSV(res['results']))
217
      .do(res => {
218
        this._cache.set(key, res);
219
      });
220
    }
221

    
222
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
223
      let url = OpenaireProperties.getSearchResourcesAPIURL();
224
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
225
      if(params!= null && params != ''  ) {
226
        url += params;
227
      }
228
      if(refineParams!= null && refineParams != ''  ) {
229
        url += refineParams;
230
      }
231
      url += "&page="+page+"&size="+size;
232
      let key = url;
233
      if (this._cache.has(key)) {
234
        return Observable.of(this._cache.get(key));
235
      }
236
      return this.http.get(url)
237
      .map(res => <any> res.json())
238
      //.do(res => console.info(res))
239
      .map(res => this.parseResultsCSV(res['results']))
240
      .do(res => {
241
        this._cache.set(key, res);
242
      });
243
    }
244

    
245
    parseResults(data: any): SearchResult[] {
246
        let results: SearchResult[] = [];
247

    
248
        let length = Array.isArray(data) ? data.length : 1;
249

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

    
253
            var result: SearchResult = new SearchResult();
254

    
255
            result['title'] = {"name": '', "url": '', "accessMode": ''};
256

    
257
            result['title'].name = resData.officialname;
258

    
259
            result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
260
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
261

    
262
            result['type'] = this.getDataproviderType(resData);
263

    
264
            if(resData.hasOwnProperty('accessinfopackage')) {
265
                let OAIPMHURL: string;
266
                if(Array.isArray(resData['accessinfopackage'])) {
267
                    OAIPMHURL = resData['accessinfopackage'][0];
268
                } else {
269
                    OAIPMHURL = resData['accessinfopackage'];
270
                }
271

    
272
                if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
273
                    result['OAIPMHURL'] = OAIPMHURL;
274
                }
275
            }
276

    
277
            result['websiteURL'] = resData.websiteurl;
278

    
279
            let res:[string[], {"name":string, "url":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
280
            result['organizations'] = res[1];
281
            result['countries'] = res[0];
282

    
283
            result['compatibility'] = this.getDataproviderCompatibility(resData)
284

    
285
            results.push(result);
286

    
287
        }
288

    
289
        return results;
290
    }
291

    
292
    getDataproviderType(resData: any): string {
293
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
294
            return resData['datasourcetype'].classname;
295
        } else {
296
            return '';
297
        }
298
    }
299

    
300
    getDataproviderCompatibility(resData: any): string {
301
        if(resData.hasOwnProperty('openairecompatibility')) {
302
            return resData['openairecompatibility'].classname;
303
        } else {
304
            return '';
305
        }
306
    }
307

    
308
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "url": string}[]] {
309
        let countries: string[] = [];
310
        let organizations: {"name": string, "url": string}[] = [];
311

    
312
        if(resData['rels'].hasOwnProperty("rel")) {
313
            let countriesSet: Set<string> = new Set<string>();
314

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

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

    
320
                if(relation.hasOwnProperty("to")) {
321
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
322
                        if(getOrganizations) {
323
                            let item: {"name":string, "url":string} = {"name": "", "url": ""};
324
                            item['name'] = relation.legalname;
325
                            item['url'] = OpenaireProperties.getsearchLinkToOrganization()+relation['to'].content;
326
                            organizations.push(item);
327
                        }
328

    
329
                        if(getCountries) {
330
                            if(relation.hasOwnProperty('country') &&
331
                               relation.country.hasOwnProperty('classname')) {
332
                                if(!countriesSet.has(relation.country.classname)) {
333
                                    countriesSet.add(relation.country.classname);
334
                                    countries.push(relation.country.classname);
335
                                }
336
                            }
337
                        }
338
                    }
339
                }
340
            }
341
        }
342
        return [countries, organizations];
343
    }
344

    
345
    parseResultsCSV(data: any): any {
346
        let results: any = [];
347
        let length = Array.isArray(data) ? data.length : 1;
348

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

    
352
            var result: any = [];
353

    
354
            result.push(this.quote(resData.officialname));
355
            result.push(this.quote(this.getDataproviderType(resData)));
356
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
357
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
358
            results.push(result);
359
        }
360
        return results;
361
    }
362

    
363
    numOfDataproviders(params: string):any {
364
        console.info("getOfDataproviders : Dataproviders Service + params="+params);
365

    
366
        let url = OpenaireProperties.getSearchAPIURL()+params;
367
        let key = url;
368
        if (this._cache.has(key)) {
369
          return Observable.of(this._cache.get(key));
370
        }
371
        return this.http.get(url)
372
                    .map(res => <any> res.json())
373
                    .map(res => res.total)
374
                    .do(res => {
375
                      this._cache.set(key, res);
376
                    });
377
    }
378

    
379
    private quote(word: any): string {
380
        return '"'+word+'"';
381
    }
382
}
(15-15/22)