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
    advancedSearchDataproviders (params: string, page: number, size: number ):any {
41
      let url = OpenaireProperties.getSearchResourcesAPIURL();
42
      var basicQuery = "(oaftype exact datasource) "
43
      url += "?query=";
44
      if(params!= null && params != ''  ) {
45
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
46
      }else{
47
        url +=" ( "+basicQuery+ " ) ";
48
      }
49

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

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

    
123
        let key = url;
124
        if (this._cache.has(key)) {
125
          return Observable.of(this._cache.get(key));
126
        }
127
        return this.http.get(url)
128
                    .map(res => <any> res.json())
129
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
130
                    .do(res => {
131
                      this._cache.set(key, res);
132
                    });
133
    }
134

    
135
    searchDataprovidersForEntity (params: string, page: number, size: number):any {
136
        let link = OpenaireProperties.getSearchAPIURL();
137
        let url = link+params+"/datasources";
138
        let key = url;
139
        if (this._cache.has(key)) {
140
          return Observable.of(this._cache.get(key));
141
        }
142
        return this.http.get(url)
143
                    .map(res => <any> res.json())
144
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
145
                    .do(res => {
146
                      this._cache.set(key, res);
147
                    });
148
    }
149

    
150
    searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
151

    
152
        let link = OpenaireProperties.getSearchAPIURL()+"datasources";
153

    
154
        let url = link+"?";
155
        if(params!= null && params != ''  ) {
156
            url += params;
157
        }
158
        if(refineParams!= null && refineParams != ''  ) {
159
            url += refineParams;
160
        }
161
        url += "&page="+page+"&size="+size;
162

    
163
        let key = url;
164
        if (this._cache.has(key)) {
165
          return Observable.of(this._cache.get(key));
166
        }
167
        return this.http.get(url)
168
                    .map(res => <any> res.json())
169
                    //.do(res => console.info(res))
170
                    .map(res => this.parseResultsCSV(res['results']))
171
                    .do(res => {
172
                      this._cache.set(key, res);
173
                    });
174
    }
175

    
176
    searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
177
      let url = OpenaireProperties.getSearchResourcesAPIURL();
178
      url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
179
      if(params!= null && params != ''  ) {
180
        url += params;
181
      }
182
      if(refineParams!= null && refineParams != ''  ) {
183
        url += refineParams;
184
      }
185
      url += "&page="+page+"&size="+size;
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
    searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
200
      let url = OpenaireProperties.getSearchResourcesAPIURL();
201
      url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(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
    parseResults(data: any): SearchResult[] {
223
        let results: SearchResult[] = [];
224

    
225
        let length = Array.isArray(data) ? data.length : 1;
226

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

    
230
            var result: SearchResult = new SearchResult();
231

    
232
            result['title'] = {"name": '', "url": '', "accessMode": ''};
233

    
234
            result['title'].name = resData.officialname;
235

    
236
            result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
237
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
238

    
239
            result['type'] = this.getDataproviderType(resData);
240

    
241
            if(resData.hasOwnProperty('accessinfopackage')) {
242
                let OAIPMHURL: string;
243
                if(Array.isArray(resData['accessinfopackage'])) {
244
                    OAIPMHURL = resData['accessinfopackage'][0];
245
                } else {
246
                    OAIPMHURL = resData['accessinfopackage'];
247
                }
248

    
249
                if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
250
                    result['OAIPMHURL'] = OAIPMHURL;
251
                }
252
            }
253

    
254
            result['websiteURL'] = resData.websiteurl;
255

    
256
            let res:[string[], {"name":string, "url":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
257
            result['organizations'] = res[1];
258
            result['countries'] = res[0];
259

    
260
            result['compatibility'] = this.getDataproviderCompatibility(resData)
261

    
262
            results.push(result);
263

    
264
        }
265

    
266
        return results;
267
    }
268

    
269
    getDataproviderType(resData: any): string {
270
        if(resData['datasourcetype'].hasOwnProperty("classname")) {
271
            return resData['datasourcetype'].classname;
272
        } else {
273
            return '';
274
        }
275
    }
276

    
277
    getDataproviderCompatibility(resData: any): string {
278
        if(resData.hasOwnProperty('openairecompatibility')) {
279
            return resData['openairecompatibility'].classname;
280
        } else {
281
            return '';
282
        }
283
    }
284

    
285
    getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "url": string}[]] {
286
        let countries: string[] = [];
287
        let organizations: {"name": string, "url": string}[] = [];
288

    
289
        if(resData['rels'].hasOwnProperty("rel")) {
290
            let countriesSet: Set<string> = new Set<string>();
291

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

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

    
297
                if(relation.hasOwnProperty("to")) {
298
                    if(relation['to'].class == "provides" && relation['to'].type == "organization") {
299
                        if(getOrganizations) {
300
                            let item: {"name":string, "url":string} = {"name": "", "url": ""};
301
                            item['name'] = relation.legalname;
302
                            item['url'] = OpenaireProperties.getsearchLinkToOrganization()+relation['to'].content;
303
                            organizations.push(item);
304
                        }
305

    
306
                        if(getCountries) {
307
                            if(relation.hasOwnProperty('country') &&
308
                               relation.country.hasOwnProperty('classname')) {
309
                                if(!countriesSet.has(relation.country.classname)) {
310
                                    countriesSet.add(relation.country.classname);
311
                                    countries.push(relation.country.classname);
312
                                }
313
                            }
314
                        }
315
                    }
316
                }
317
            }
318
        }
319
        return [countries, organizations];
320
    }
321

    
322
    parseResultsCSV(data: any): any {
323
        let results: any = [];
324
        let length = Array.isArray(data) ? data.length : 1;
325

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

    
329
            var result: any = [];
330

    
331
            result.push(this.quote(resData.officialname));
332
            result.push(this.quote(this.getDataproviderType(resData)));
333
            result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
334
            result.push(this.quote(this.getDataproviderCompatibility(resData)));
335
            results.push(result);
336
        }
337
        return results;
338
    }
339

    
340
    numOfDataproviders(params: string):any {
341
        console.info("getOfDataproviders : Dataproviders Service + params="+params);
342

    
343
        let url = OpenaireProperties.getSearchAPIURL()+params;
344
        let key = url;
345
        if (this._cache.has(key)) {
346
          return Observable.of(this._cache.get(key));
347
        }
348
        return this.http.get(url)
349
                    .map(res => <any> res.json())
350
                    .map(res => res.total)
351
                    .do(res => {
352
                      this._cache.set(key, res);
353
                    });
354
    }
355

    
356
    private quote(word: any): string {
357
        return '"'+word+'"';
358
    }
359
}
(15-15/22)