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 { ParsingFunctions } from '../landingPages/landing-utils/parsingFunctions.class';
11
import{EnvProperties} from '../utils/properties/env-properties';
12

    
13
@Injectable()
14
export class SearchDatasetsService {
15
    private sizeOfDescription: number = 270;
16
    public parsingFunctions: ParsingFunctions = new ParsingFunctions();
17

    
18
    constructor(private http: Http ) {}
19

    
20
    searchDatasets (params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[], properties:EnvProperties ):any {
21

    
22
        let link = properties.searchAPIURLLAst+"datasets";
23

    
24
        let url = link+"?";
25
        if(params!= null && params != ''  ) {
26
            url += params;
27
        }
28
        if(refineParams!= null && refineParams != ''  ) {
29
            url += refineParams;
30
        }
31
        if(sortBy) {
32
          url += "&sortBy=" + sortBy;
33
        }
34
        url += "&page="+ (page-1) +"&size="+size+"&format=json";
35

    
36

    
37
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
38
                    .map(res => <any> res.json())
39
                    //.do(res => console.info(res))
40
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "dataset")]);
41
    }
42
    searchDatasetById (id: string , properties:EnvProperties):any {
43

    
44
        let url = properties.searchAPIURLLAst+"datasets/"+id+"?format=json";
45
         return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
46
                    .map(res => <any> res.json())
47
                    .map(res => this.parseResults(res));
48
    }
49

    
50
    searchAggregators (id: string, params: string, refineParams:string, page: number, size: number, properties:EnvProperties ):any {
51

    
52
        let link = properties.searchAPIURLLAst+"datasets";
53

    
54
        let url = link+"?"+"&format=json";
55
        if(params!= null && params != ''  ) {
56
            url += params;
57
        }
58
        if(refineParams!= null && refineParams != ''  ) {
59
            url += refineParams;
60
        }
61
        url += "&page="+(page-1)+"&size="+size;
62

    
63

    
64

    
65
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
66
                    .map(res => <any> res.json())
67
                    .map(res => this.parseRefineResults(id, res['refineResults']))
68
    }
69

    
70
    searchDatasetsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
71
        let link = properties.searchAPIURLLAst+"datasets";
72
        let url = link+"?";
73
        var doisParams = "";
74

    
75
        for(var i =0 ;i < DOIs.length; i++){
76
          doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
77
        }
78
        if(doisParams.length > 0){
79
          url += "&"+doisParams;
80

    
81
        }
82
        if(refineParams!= null && refineParams != ''  ) {
83
            url += refineParams;
84
        }
85
        url += "&page="+ (page-1) +"&size="+size+"&format=json";
86

    
87

    
88
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
89
                    .map(res => <any> res.json())
90
                    //.do(res => console.info(res))
91

    
92
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "dataset")]);
93
    }
94
    advancedSearchDatasets (params: string, page: number, size: number, sortBy: string, properties:EnvProperties ):any {
95
      let url = properties.searchResourcesAPIURL;
96
      var basicQuery = "(oaftype exact result) and (resulttypeid exact dataset)  "
97
      url += "?query=";
98
      if(params!= null && params != ''  ) {
99
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
100
      }else{
101
        url +=" ( "+basicQuery+ " ) ";
102
      }
103

    
104
      if(sortBy) {
105
        let sortOptions = sortBy.split(",");
106
        url += "sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+" ";
107
      }
108

    
109
      url += "&page="+(page-1)+"&size="+size;
110
      url += "&format=json";
111

    
112
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
113
      .map(res => <any> res.json())
114
      //.do(res => console.info(res))
115
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
116
    }
117
    searchDatasetsForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
118
        let link = properties.searchAPIURLLAst;
119
        let url = link+params+"/datasets"+"?format=json";
120

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

    
126
    searchDatasetsForDataproviders(params: string, page: number, size: number, properties:EnvProperties):any {
127
        let link = properties.searchAPIURLLAst;
128
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
129
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
130
                    .map(res => <any> res.json())
131
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
132
    }
133

    
134
    parseResults(data: any): SearchResult[] {
135
        let results: SearchResult[] = [];
136

    
137
        let length = Array.isArray(data) ? data.length : 1;
138

    
139
        for(let i=0; i<length; i++) {
140
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
141

    
142
            var result: SearchResult = new SearchResult();
143
            result.entityType = "dataset";
144

    
145
            result.types = new Array<string>();
146
            let types = new Set<string>();
147
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
148

    
149
            let instance;
150
            for(let i=0; i<length; i++) {
151
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
152
              this.parsingFunctions.parseTypes(result.types, types, instance);
153
            }
154

    
155
            if(resData['language'] && resData['language'] != null) {
156
              result.languages = new Array<string>();
157

    
158
              if(!Array.isArray(resData['language'])) {
159
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
160
                  result.languages.push(resData['language'].classname);
161
                }
162
              } else {
163
                for(let i=0; i<resData['language'].length; i++) {
164
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
165
                    result.languages.push(resData['language'][i].classname);
166
                  }
167
                }
168
              }
169
            }
170
            if(resData['country'] && resData['country'] != null) {
171
              result.countriesForResults = new Array<string>();
172

    
173
              if(!Array.isArray(resData['country'])) {
174
                if(resData['country'].classname != "Undetermined" && resData['country'].classname) {
175
                  result.countriesForResults.push(resData['country'].classname);
176
                }
177
              } else {
178
                for(let i=0; i<resData['country'].length; i++) {
179
                  if(resData['country'][i].classname != "Undetermined" && resData['country'][i].classname) {
180
                    result.countriesForResults.push(resData['country'][i].classname);
181
                  }
182
                }
183
              }
184
            }
185

    
186
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
187

    
188
            if(Array.isArray(resData['title'])) {
189
                result['title'].name = resData['title'][0].content;
190
            } else {
191
                result['title'].name = resData['title'].content;
192
            }
193

    
194
            //result['title'].url = OpenaireProperties.getsearchLinkToDataset();
195
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
196
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
197
            if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
198
                result['title'].accessMode = resData['bestaccessright'].classid;
199
            }
200

    
201
            if(resData['rels'].hasOwnProperty("rel")) {
202
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
203

    
204
                for(let j=0; j<relLength; j++) {
205
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
206

    
207
                    if(relation.hasOwnProperty("to")) {
208
                        /*if(relation['to'].class == "hasAuthor") {
209
                          if(result['authors'] == undefined) {
210
                            result['authors'] = new Array<{"name": string, "id": string}>();
211
                          }
212

    
213
                          result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
214
                        } else */if(relation['to'].class == "isProducedBy") {
215
                          result['projects'] = this.parseProjects(result['projects'], relation);
216
                        }
217
                    }
218
                }
219
            }
220

    
221
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
222
              if(result['authors'] == undefined) {
223
                result['authors'] = new Array<string>();
224
              }
225

    
226
              let authors = resData['creator'];
227
              let length = Array.isArray(authors) ? authors.length : 1;
228

    
229
              for(let i=0; i<length; i++) {
230
                let author = Array.isArray(authors) ? authors[i] : authors;
231
                result.authors[author.rank-1] = author.content;
232
              }
233
              result.authors = result.authors.filter(function (item) {
234
                return (item != undefined);
235
              });
236
            }
237

    
238
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
239
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
240
            if(!Array.isArray(resData.description)) {
241
                result.description = resData.description;
242
            } else {
243
                result.description = resData.description[0];
244
            }
245
            if(result.description && result.description.length > this.sizeOfDescription) {
246
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
247
            }
248

    
249
            result.embargoEndDate = resData.embargoenddate;
250

    
251
            if(!Array.isArray(resData.publisher)) {
252
                result.publisher = resData.publisher;
253
            } else {
254
                for(let i=0; i<resData.publisher.length; i++) {
255
                    if(result.publisher != undefined){
256
                        result.publisher += ', '+resData['publisher'][i];
257
                    } else {
258
                        result.publisher = resData['publisher'][i];
259
                    }
260
                }
261
            }
262

    
263
            results.push(result);
264
        }
265

    
266
        return results;
267
    }
268

    
269
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
270
                              "funderShortname": string, "funderName": string,
271
                              "code": string }[], relation: any ) :  {
272
                              "id": string, "acronym": string, "title": string,
273
                              "funderShortname": string, "funderName": string,
274
                              "code": string }[] {
275
      if(projects == undefined) {
276
          projects = new Array<
277
              { "id": string, "acronym": string, "title": string,
278
                "funderShortname": string, "funderName": string,
279
                "code": string
280
              }>();
281
      }
282

    
283
      let countProjects = projects.length;
284

    
285
      projects[countProjects] = {
286
          "id": "", "acronym": "", "title": "",
287
          "funderShortname": "", "funderName": "",
288
          "code": ""
289
      }
290

    
291
      if(relation.title != 'unidentified') {
292
          projects[countProjects]['id'] =
293
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
294
          projects[countProjects]['acronym'] = relation.acronym;
295
          projects[countProjects]['title'] = relation.title;
296
          projects[countProjects]['code'] = relation.code;
297
      } else {
298
          projects[countProjects]['id'] = "";
299
          projects[countProjects]['acronym'] = "";
300
          projects[countProjects]['title'] = "";
301
          projects[countProjects]['code'] = "";
302
      }
303

    
304
      if(relation.hasOwnProperty("funding")) {
305
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
306

    
307
          for(let z=0; z<fundingLength; z++) {
308
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
309

    
310
              if(fundingData.hasOwnProperty("funder")) {
311
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
312
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
313
              }
314
          }
315
      }
316

    
317
      return projects;
318
    }
319

    
320
    parseRefineResults(id: string, data: any): any {
321
        var results:any = [];
322
        if(data.hasOwnProperty("resulthostingdatasource")) {
323
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
324

    
325
            for(let i=0; i<length; i++) {
326
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
327

    
328
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
329
                result['name'] = datasource.name;
330
                result['id'] = datasource.id.split("||")[0];
331
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
332
                result['count'] = datasource.count;
333

    
334
                if(result['id'] != id && result['name'] != "Unknown Repository") {
335
                    results.push(result);
336
                }
337
            }
338
        }
339
        return results;
340
    }
341

    
342
    numOfDatasets(url: string, properties:EnvProperties):any {
343

    
344
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
345
                  .map(res => <any> res.json())
346
                  .map(res => res.total);
347
    }
348

    
349
    numOfEntityDatasets(id: string, entity: string, properties:EnvProperties):any {
350
        var parameters = "";
351

    
352
        if(entity == "project") {
353
          parameters = "projects/"+id+"/datasets/count";
354
        } else if(entity == "organization") {
355
          parameters = "organizations/"+id+"/datasets/count";
356
        }
357

    
358
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
359
        return this.numOfDatasets(url, properties);
360
    }
361

    
362
    numOfSearchDatasets(params: string, properties:EnvProperties, refineParams:string=null):any {
363
        let url = properties.searchAPIURLLAst+"datasets/count?format=json";
364
        if(params != "") {
365
          url += "&q=" + StringUtils.URIEncode(params);
366
        }
367
        if(refineParams!= null && refineParams != ''  ) {
368
            url += refineParams;
369
        }
370
        return this.numOfDatasets(url, properties);
371
    }
372
}
(15-15/21)