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

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

    
17
    constructor(private http: Http ) {}
18

    
19
    searchSoftware (params: string, refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
20

    
21
        let link = properties.searchAPIURLLAst+"software";
22

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

    
32

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

    
40
        let url = properties.searchAPIURLLAst+"software/"+id+"?format=json";
41

    
42

    
43
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
44
                    .map(res => <any> res.json())
45
                    .map(res => this.parseResults(res));
46
    }
47

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

    
50
        let link = properties.searchAPIURLLAst+"software";
51

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

    
61

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

    
67
    searchSoftwareByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
68
        let link = properties.searchAPIURLLAst+"software";
69
        let url = link+"?";
70
        var doisParams = "";
71

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

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

    
84

    
85
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
86
                    .map(res => <any> res.json())
87
                    //.do(res => console.info(res))
88
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "software")]);
89
    }
90
    advancedSearchSoftware (params: string, page: number, size: number, properties:EnvProperties ):any {
91
      let url = properties.searchResourcesAPIURL;
92
      var basicQuery = "(oaftype exact result) and (resulttypeid exact software)  "
93
      url += "?query=";
94
      if(params!= null && params != ''  ) {
95
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
96
      }else{
97
        url +=" ( "+basicQuery+ " ) ";
98
      }
99

    
100
      url += "&page="+(page-1)+"&size="+size;
101
      url += "&format=json";
102

    
103
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
104
      .map(res => <any> res.json())
105
      //.do(res => console.info(res))
106
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
107
    }
108
    searchSoftwareForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
109
        let link = properties.searchAPIURLLAst;
110
        let url = link+params+"/software"+"?format=json";
111

    
112
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
113
                    .map(res => <any> res.json())
114
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
115
    }
116

    
117
    searchSoftwareForDataproviders(params: string, page: number, size: number, properties:EnvProperties):any {
118
        let link = properties.searchAPIURLLAst;
119
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&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
    parseResults(data: any): SearchResult[] {
127
        let results: SearchResult[] = [];
128

    
129
        let length = Array.isArray(data) ? data.length : 1;
130

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

    
134
            var result: SearchResult = new SearchResult();
135
            result.entityType = "software";
136

    
137
            result.types = new Array<string>();
138
            let types = new Set<string>();
139

    
140
            let instance;
141
            let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
142

    
143
            for(let i=0; i<length; i++) {
144
              instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
145
              this.parsingFunctions.parseTypes(result.types, types, instance);
146
            }
147

    
148
            if(resData['programmingLanguage'] && resData['programmingLanguage'] != null) {
149
              result.programmingLanguages = new Array<string>();
150

    
151
              if(!Array.isArray(resData['programmingLanguage'])) {
152
                if(resData['programmingLanguage'].classname != "Undetermined" && resData['programmingLanguage'].classname) {
153
                  result.programmingLanguages.push(resData['programmingLanguage'].classname);
154
                }
155
              } else {
156
                for(let i=0; i<resData['programmingLanguage'].length; i++) {
157
                  if(resData['programmingLanguage'][i].classname != "Undetermined" && resData['programmingLanguage'][i].classname) {
158
                    result.programmingLanguages.push(resData['programmingLanguage'][i].classname);
159
                  }
160
                }
161
              }
162
            }
163

    
164
            if(resData['language'] && resData['language'] != null) {
165
              result.languages = new Array<string>();
166

    
167
              if(!Array.isArray(resData['language'])) {
168
                if(resData['language'].classname != "Undetermined" && resData['language'].classname) {
169
                  result.languages.push(resData['language'].classname);
170
                }
171
              } else {
172
                for(let i=0; i<resData['language'].length; i++) {
173
                  if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
174
                    result.languages.push(resData['language'][i].classname);
175
                  }
176
                }
177
              }
178
            }
179

    
180
            if(resData['country'] && resData['country'] != null) {
181
              result.countriesForResults = new Array<string>();
182

    
183
              if(!Array.isArray(resData['country'])) {
184
                if(resData['country'].classname != "Undetermined" && resData['country'].classname) {
185
                  result.countriesForResults.push(resData['country'].classname);
186
                }
187
              } else {
188
                for(let i=0; i<resData['country'].length; i++) {
189
                  if(resData['country'][i].classname != "Undetermined" && resData['country'][i].classname) {
190
                    result.countriesForResults.push(resData['country'][i].classname);
191
                  }
192
                }
193
              }
194
            }
195
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
196

    
197
            if(Array.isArray(resData['title'])) {
198
                result['title'].name = resData['title'][0].content;
199
            } else {
200
                result['title'].name = resData['title'].content;
201
            }
202

    
203
            //result['title'].url = OpenaireProperties.getsearchLinkToSoftware();
204
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
205
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
206
            if(resData['bestaccessright'] && resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
207
                result['title'].accessMode = resData['bestaccessright'].classid;
208
            }
209

    
210
            if(resData['rels'].hasOwnProperty("rel")) {
211
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
212

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

    
216
                    if(relation.hasOwnProperty("to")) {
217
                        /*if(relation['to'].class == "hasAuthor") {
218
                          if(result['authors'] == undefined) {
219
                            result['authors'] = new Array<{"name": string, "id": string}>();
220
                          }
221

    
222
                          result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
223
                        } else */if(relation['to'].class == "isProducedBy") {
224
                          result['projects'] = this.parseProjects(result['projects'], relation);
225
                        }
226
                    }
227
                }
228
            }
229

    
230
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
231
              if(result['authors'] == undefined) {
232
                result['authors'] = new Array<string>();
233
              }
234

    
235
              let authors = resData['creator'];
236
              let length = Array.isArray(authors) ? authors.length : 1;
237

    
238
              for(let i=0; i<length; i++) {
239
                let author = Array.isArray(authors) ? authors[i] : authors;
240
                result.authors[author.rank-1] = author.content;
241
              }
242
              result.authors = result.authors.filter(function (item) {
243
                return (item != undefined);
244
              });
245
            }
246

    
247
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
248
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
249
            if(!Array.isArray(resData.description)) {
250
                result.description = resData.description;
251
            } else {
252
                result.description = resData.description[0];
253
            }
254
            if(result.description && result.description.length > this.sizeOfDescription) {
255
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
256
            }
257

    
258
            result.embargoEndDate = resData.embargoenddate;
259

    
260
            if(!Array.isArray(resData.publisher)) {
261
                result.publisher = resData.publisher;
262
            } else {
263
                for(let i=0; i<resData.publisher.length; i++) {
264
                    if(result.publisher != undefined){
265
                        result.publisher += ', '+resData['publisher'][i];
266
                    } else {
267
                        result.publisher = resData['publisher'][i];
268
                    }
269
                }
270
            }
271

    
272
            results.push(result);
273
        }
274

    
275
        return results;
276
    }
277

    
278
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
279
                              "funderShortname": string, "funderName": string,
280
                              "code": string }[], relation: any ) :  {
281
                              "id": string, "acronym": string, "title": string,
282
                              "funderShortname": string, "funderName": string,
283
                              "code": string }[] {
284
      if(projects == undefined) {
285
          projects = new Array<
286
              { "id": string, "acronym": string, "title": string,
287
                "funderShortname": string, "funderName": string,
288
                "code": string
289
              }>();
290
      }
291

    
292
      let countProjects = projects.length;
293

    
294
      projects[countProjects] = {
295
          "id": "", "acronym": "", "title": "",
296
          "funderShortname": "", "funderName": "",
297
          "code": ""
298
      }
299

    
300
      if(relation.title != 'unidentified') {
301
          projects[countProjects]['id'] =
302
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
303
          projects[countProjects]['acronym'] = relation.acronym;
304
          projects[countProjects]['title'] = relation.title;
305
          projects[countProjects]['code'] = relation.code;
306
      } else {
307
          projects[countProjects]['id'] = "";
308
          projects[countProjects]['acronym'] = "";
309
          projects[countProjects]['title'] = "";
310
          projects[countProjects]['code'] = "";
311
      }
312

    
313
      if(relation.hasOwnProperty("funding")) {
314
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
315

    
316
          for(let z=0; z<fundingLength; z++) {
317
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
318

    
319
              if(fundingData.hasOwnProperty("funder")) {
320
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
321
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
322
              }
323
          }
324
      }
325

    
326
      return projects;
327
    }
328

    
329
    parseRefineResults(id: string, data: any): any {
330
        var results:any = [];
331
        if(data.hasOwnProperty("resulthostingdatasource")) {
332
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
333

    
334
            for(let i=0; i<length; i++) {
335
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
336

    
337
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
338
                result['name'] = datasource.name;
339
                result['id'] = datasource.id.split("||")[0];
340
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
341
                result['count'] = datasource.count;
342

    
343
                if(result['id'] != id && result['name'] != "Unknown Repository") {
344
                    results.push(result);
345
                }
346
            }
347
        }
348
        return results;
349
    }
350

    
351
    numOfSoftware(url: string, properties:EnvProperties):any {
352

    
353
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
354
                  .map(res => <any> res.json())
355
                  .map(res => res.total);
356
    }
357

    
358
    numOfEntitySoftware(id: string, entity: string, properties:EnvProperties):any {
359
        var parameters = "";
360

    
361
        if(entity == "project") {
362
          parameters = "projects/"+id+"/software/count";
363
        }
364

    
365
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
366
        return this.numOfSoftware(url, properties);
367
    }
368

    
369
    numOfSearchSoftware(params: string, properties:EnvProperties):any {
370
        let url = properties.searchAPIURLLAst+"software/count?format=json";
371
        if(params != "") {
372
          url += "&q=" + params;
373
        }
374
        return this.numOfSoftware(url, properties);
375
    }
376
}
(20-20/21)