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
@Injectable()
12
export class SearchSoftwareService {
13
    private sizeOfDescription: number = 270;
14

    
15
    constructor(private http: Http ) {}
16

    
17
    searchSoftware (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
18

    
19
        let link = OpenaireProperties.getSearchAPIURLLast()+"software";
20

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

    
30

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

    
38
        let url = OpenaireProperties.getSearchAPIURLLast()+"software/"+id+"?format=json";
39

    
40

    
41
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
42
                    .map(res => <any> res.json())
43
                    .map(res => this.parseResults(res));
44
    }
45

    
46
    searchAggregators (id: string, params: string, refineParams:string, page: number, size: number ):any {
47

    
48
        let link = OpenaireProperties.getSearchAPIURLLast()+"software";
49

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

    
59

    
60
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
61
                    .map(res => <any> res.json())
62
                    .map(res => this.parseRefineResults(id, res['refineResults']))
63
    }
64

    
65
    searchSoftwareByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[] ):any {
66
        let link = OpenaireProperties.getSearchAPIURLLast()+"software";
67
        let url = link+"?";
68
        var doisParams = "";
69

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

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

    
82

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

    
98
      url += "&page="+(page-1)+"&size="+size;
99
      url += "&format=json";
100

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

    
110
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
111
                    .map(res => <any> res.json())
112
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
113
    }
114

    
115
    searchSoftwareForDataproviders(params: string, page: number, size: number):any {
116
        let link = OpenaireProperties.getSearchAPIURLLast();
117
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
118

    
119
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
120
                    .map(res => <any> res.json())
121
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
122
    }
123

    
124
    parseResults(data: any): SearchResult[] {
125
        let results: SearchResult[] = [];
126

    
127
        let length = Array.isArray(data) ? data.length : 1;
128

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

    
132
            var result: SearchResult = new SearchResult();
133

    
134
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
135

    
136
            if(Array.isArray(resData['title'])) {
137
                result['title'].name = resData['title'][0].content;
138
            } else {
139
                result['title'].name = resData['title'].content;
140
            }
141

    
142
            //result['title'].url = OpenaireProperties.getsearchLinkToSoftware();
143
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
144
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
145
            if(resData['bestaccessright'].hasOwnProperty("classid")) {
146
                result['title'].accessMode = resData['bestaccessright'].classid;
147
            }
148

    
149
            if(resData['rels'].hasOwnProperty("rel")) {
150
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
151

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

    
155
                    if(relation.hasOwnProperty("to")) {
156
                        /*if(relation['to'].class == "hasAuthor") {
157
                          if(result['authors'] == undefined) {
158
                            result['authors'] = new Array<{"name": string, "id": string}>();
159
                          }
160

    
161
                          result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
162
                        } else */if(relation['to'].class == "isProducedBy") {
163
                          result['projects'] = this.parseProjects(result['projects'], relation);
164
                        }
165
                    }
166
                }
167
            }
168

    
169
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
170
              if(result['authors'] == undefined) {
171
                result['authors'] = new Array<string>();
172
              }
173

    
174
              let authors = resData['creator'];
175
              let length = Array.isArray(authors) ? authors.length : 1;
176

    
177
              for(let i=0; i<length; i++) {
178
                let author = Array.isArray(authors) ? authors[i] : authors;
179
                result.authors[author.rank-1] = author.content;
180
              }
181
              result.authors = result.authors.filter(function (item) {
182
                return (item != undefined);
183
              });
184
            }
185

    
186
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
187
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
188
            if(!Array.isArray(resData.description)) {
189
                result.description = resData.description;
190
            } else {
191
                result.description = resData.description[0];
192
            }
193
            if(result.description.length > this.sizeOfDescription) {
194
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
195
            }
196

    
197
            result.embargoEndDate = resData.embargoenddate;
198

    
199
            if(!Array.isArray(resData.publisher)) {
200
                result.publisher = resData.publisher;
201
            } else {
202
                for(let i=0; i<resData.publisher.length; i++) {
203
                    if(result.publisher != undefined){
204
                        result.publisher += ', '+resData['publisher'][i];
205
                    } else {
206
                        result.publisher = resData['publisher'][i];
207
                    }
208
                }
209
            }
210

    
211
            results.push(result);
212
        }
213

    
214
        return results;
215
    }
216

    
217
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
218
                              "funderShortname": string, "funderName": string,
219
                              "code": string }[], relation: any ) :  {
220
                              "id": string, "acronym": string, "title": string,
221
                              "funderShortname": string, "funderName": string,
222
                              "code": string }[] {
223
      if(projects == undefined) {
224
          projects = new Array<
225
              { "id": string, "acronym": string, "title": string,
226
                "funderShortname": string, "funderName": string,
227
                "code": string
228
              }>();
229
      }
230

    
231
      let countProjects = projects.length;
232

    
233
      projects[countProjects] = {
234
          "id": "", "acronym": "", "title": "",
235
          "funderShortname": "", "funderName": "",
236
          "code": ""
237
      }
238

    
239
      if(relation.title != 'unidentified') {
240
          projects[countProjects]['id'] =
241
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
242
          projects[countProjects]['acronym'] = relation.acronym;
243
          projects[countProjects]['title'] = relation.title;
244
          projects[countProjects]['code'] = relation.code;
245
      } else {
246
          projects[countProjects]['id'] = "";
247
          projects[countProjects]['acronym'] = "";
248
          projects[countProjects]['title'] = "";
249
          projects[countProjects]['code'] = "";
250
      }
251

    
252
      if(relation.hasOwnProperty("funding")) {
253
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
254

    
255
          for(let z=0; z<fundingLength; z++) {
256
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
257

    
258
              if(fundingData.hasOwnProperty("funder")) {
259
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
260
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
261
              }
262
          }
263
      }
264

    
265
      return projects;
266
    }
267

    
268
    parseRefineResults(id: string, data: any): any {
269
        var results:any = [];
270
        if(data.hasOwnProperty("resulthostingdatasource")) {
271
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
272

    
273
            for(let i=0; i<length; i++) {
274
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
275

    
276
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
277
                result['name'] = datasource.name;
278
                result['id'] = datasource.id.split("||")[0];
279
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
280
                result['count'] = datasource.count;
281

    
282
                if(result['id'] != id && result['name'] != "Unknown Repository") {
283
                    results.push(result);
284
                }
285
            }
286
        }
287
        return results;
288
    }
289

    
290
    numOfSoftware(url: string):any {
291

    
292
      return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
293
                  .map(res => <any> res.json())
294
                  .map(res => res.total);
295
    }
296

    
297
    numOfEntitySoftware(id: string, entity: string):any {
298
        var parameters = "";
299

    
300
        if(entity == "project") {
301
          parameters = "projects/"+id+"/software/count";
302
        }
303

    
304
        let url = OpenaireProperties.getSearchAPIURLLast()+parameters+"?format=json";
305
        return this.numOfSoftware(url);
306
    }
307

    
308
    numOfSearchSoftware(params: string):any {
309
        let url = OpenaireProperties.getSearchAPIURLLast()+"software/count?format=json";
310
        if(params != "") {
311
          url += "&q=" + params;
312
        }
313
        return this.numOfSoftware(url);
314
    }
315
}
(18-18/19)