Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import 'rxjs/add/observable/of';
5
import 'rxjs/add/operator/do';
6
import 'rxjs/add/operator/share';
7
import {   } from '../shared/cache.service';
8

    
9
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10
import {SearchResult}     from '../utils/entities/searchResult';
11
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
12

    
13
@Injectable()
14
export class SearchPublicationsService {
15
    private sizeOfDescription: number = 270;
16

    
17
    constructor(private http: Http ) {}
18

    
19
    searchPublications (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
20
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
21

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

    
31

    
32
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): 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, "publication")]);
36
    }
37
    searchPublicationById (id: string ):any {
38

    
39
        let url = OpenaireProperties.getSearchAPIURLLast()+"publications/"+id+"?format=json";
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()+"publications";
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

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

    
66
    searchPublicationsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[] ):any {
67

    
68
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
69

    
70
        let url = link+"?"+"&format=json&";
71
        var doisParams = "";
72

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

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

    
85
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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, "publication")]);
89
    }
90

    
91
    advancedSearchPublications (params: string, page: number, size: number ):any {
92
      let url = OpenaireProperties.getSearchResourcesAPIURL();
93
      var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
94
      url += "?query=";
95
      if(params!= null && params != ''  ) {
96
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
97
      }else{
98
        url +=" ( "+basicQuery+ " ) ";
99
      }
100

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

    
104
      return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
105
      .map(res => <any> res.json())
106
      //.do(res => console.info(res))
107

    
108
      .map(res => [res['meta'].total, this.parseResults(res['results'])]);
109
    }
110
    searchPublicationsForEntity (params: string, page: number, size: number):any {
111
        let link = OpenaireProperties.getSearchAPIURLLast();
112
        let url = link+params+"/publications"+ "?format=json";
113
        url += "&page="+(page-1)+"&size="+size;
114

    
115

    
116

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

    
122
    searchPublicationsForDataproviders(params: string, page: number, size: number):any {
123
        let link = OpenaireProperties.getSearchAPIURLLast();
124
        let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json";
125
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
126
                    .map(res => <any> res.json())
127
                    .map(res => [res['meta'].total, this.parseResults(res['results'])]);
128
    }
129
/*
130
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
131

    
132
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
133

    
134
        let url = link+"?";
135
        if(params!= null && params != ''  ) {
136
            url += params;
137
        }
138
        if(refineParams!= null && refineParams != ''  ) {
139
            url += refineParams;
140
        }
141
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
142

    
143

    
144

    
145
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
146
                    .map(res => <any> res.json())
147
                    //.do(res => console.info(res))
148

    
149
                    .map(res => this.parseResultsCSV(res['results']));
150
    }
151
*/
152

    
153
    parseResults(data: any): SearchResult[] {
154
        let results: SearchResult[] = [];
155

    
156
        let length = Array.isArray(data) ? data.length : 1;
157

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

    
161
            var result: SearchResult = new SearchResult();
162

    
163
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
164

    
165
            if(Array.isArray(resData['title'])) {
166
                // resData['title'][0].hasOwnProperty("content") {
167
                    result['title'].name = resData['title'][0].content;
168
                // }
169
            } else {
170
                // resData['title'].hasOwnProperty("content") {
171
                    result['title'].name = resData['title'].content;
172
                // }
173
            }
174

    
175
            //result['title'].url = OpenaireProperties.getsearchLinkToPublication();
176
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
177
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
178
            if(resData['bestaccessright'].hasOwnProperty("classid")) {
179
                result['title'].accessMode = resData['bestaccessright'].classid;
180
            }
181

    
182
            if(resData['rels'].hasOwnProperty("rel")) {
183
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
184

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

    
188
                    if(relation.hasOwnProperty("to")) {
189
                        /*if(relation['to'].class == "hasAuthor") {
190
                            if(result['authors'] == undefined) {
191
                                result['authors'] = new Array<{"name": string, "id": string}>();
192
                            }
193

    
194
                            result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
195
                        } else */if(relation['to'].class == "isProducedBy") {
196
                            result['projects'] = this.parseProjects(result['projects'], relation);
197
                        }
198
                    }
199
                }
200
            }
201

    
202
            if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
203
              if(result['authors'] == undefined) {
204
                result['authors'] = new Array<string>();
205
              }
206

    
207
              let authors = resData['creator'];
208
              let length = Array.isArray(authors) ? authors.length : 1;
209

    
210
              for(let i=0; i<length; i++) {
211
                let author = Array.isArray(authors) ? authors[i] : authors;
212
                result.authors[author.rank-1] = author.content;
213
              }
214
              result.authors = result.authors.filter(function (item) {
215
                return (item != undefined);
216
              });
217
            }
218

    
219
            var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
220
            result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
221

    
222
            if(!Array.isArray(resData.description)) {
223
                result.description = resData.description;
224
            } else {
225
                result.description = resData.description[0];
226
            }
227

    
228
            if(result.description.length > this.sizeOfDescription) {
229
                result.description = result.description.substring(0, this.sizeOfDescription) + "...";
230
            }
231

    
232

    
233
            result.embargoEndDate = resData.embargoenddate;
234

    
235
            results.push(result);
236
        }
237

    
238
        return results;
239
    }
240

    
241
    parseProjects(projects: { "id": string, "acronym": string, "title": string,
242
                              "funderShortname": string, "funderName": string,
243
                              "code": string }[], relation: any ) :  {
244
                              "id": string, "acronym": string, "title": string,
245
                              "funderShortname": string, "funderName": string,
246
                              "code": string }[] {
247
      if(projects == undefined) {
248
          projects = new Array<
249
              { "id": string, "acronym": string, "title": string,
250
                "funderShortname": string, "funderName": string,
251
                "code": string
252
              }>();
253
      }
254

    
255
      let countProjects = projects.length;
256

    
257
      projects[countProjects] = {
258
          "id": "", "acronym": "", "title": "",
259
          "funderShortname": "", "funderName": "",
260
          "code": ""
261
      }
262

    
263
      if(relation.title != 'unidentified') {
264
          projects[countProjects]['id'] =
265
              /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
266
          projects[countProjects]['acronym'] = relation.acronym;
267
          projects[countProjects]['title'] = relation.title;
268
          projects[countProjects]['code'] = relation.code;
269
      } else {
270
          projects[countProjects]['id'] = "";
271
          projects[countProjects]['acronym'] = "";
272
          projects[countProjects]['title'] = "";
273
          projects[countProjects]['code'] = "";
274
      }
275

    
276
      if(relation.hasOwnProperty("funding")) {
277
          let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
278

    
279
          for(let z=0; z<fundingLength; z++) {
280
              let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
281

    
282
              if(fundingData.hasOwnProperty("funder")) {
283
                  projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
284
                  projects[countProjects]['funderName'] = fundingData['funder'].name;
285
              }
286
          }
287
      }
288

    
289
      return projects;
290
    }
291
/*
292
    parseResultsCSV(data: any): any {
293
        let results: any = [];
294

    
295

    
296
        let length = Array.isArray(data) ? data.length : 1;
297

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

    
301
            var result: any = [];
302

    
303
            if(Array.isArray(resData['title'])) {
304
                result.push(this.quote(resData['title'][0].content));
305
            } else {
306
                result.push(this.quote(resData['title'].content));
307
            }
308

    
309
            var authors: string[] = [];
310
            var projects: string[] = [];
311
            var funder: string = "";
312
            if(resData['rels'].hasOwnProperty("rel")) {
313
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
314

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

    
318
                    if(relation.hasOwnProperty("to")) {
319
                        if(relation['to'].class == "hasAuthor") {
320
                            authors.push(relation.fullname);
321
                        } else if(relation['to'].class == "isProducedBy") {
322
                            if(relation.code != "") {
323
                                projects.push(relation.title+" ("+relation.code+")");
324
                            } else {
325
                                projects.push(relation.title);
326
                            }
327

    
328
                            if(relation.hasOwnProperty("funding")) {
329
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
330

    
331
                                for(let z=0; z<fundingLength; z++) {
332
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
333

    
334
                                    if(fundingData.hasOwnProperty("funder")) {
335
                                        funder = fundingData['funder'].shortname;
336
                                    }
337
                                }
338
                            }
339

    
340
                        }
341
                    }
342
                }
343
                result.push(this.quote(authors));
344
                var year: string = "";
345
                if(resData.hasOwnProperty("dateofacceptance")) {
346
                    year = resData.dateofacceptance;
347
                }
348
                result.push(year);
349
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
350
                result.push(this.quote(id));
351
                result.push(funder);
352
                result.push(this.quote(projects));
353
            } else {
354
                result.push(this.quote(''));
355
                var year: string = "";
356
                if(resData.hasOwnProperty("dateofacceptance")) {
357
                    year = resData.dateofacceptance;
358
                }
359
                result.push(year);
360
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
361
                result.push(this.quote(id));
362
                result.push(funder);
363
                result.push("");
364
            }
365

    
366

    
367
            //result.push(resData.embargoenddate);
368

    
369
            if(resData['bestaccessright'].hasOwnProperty("classid")) {
370
                result.push(this.quote(resData['bestaccessright'].classid));
371
            } else {
372
                result.push("");
373
            }
374

    
375
            results.push(result);
376
        }
377

    
378
        return results;
379
    }
380
*/
381
    parseRefineResults(id: string, data: any): any {
382
        var results:any = [];
383
        if(data.hasOwnProperty("resulthostingdatasource")) {
384
            let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
385

    
386
            for(let i=0; i<length; i++) {
387
                let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
388

    
389
                let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
390
                result['name'] = datasource.name;
391
                result['id'] = datasource.id.split("||")[0];
392
                //result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
393
                result['count'] = datasource.count;
394

    
395
                if(result['id'] != id && result['name'] != "Unknown Repository") {
396
                    results.push(result);
397
                }
398
            }
399
        }
400
        return results;
401
    }
402

    
403
    numOfPublications(url: string): any {
404

    
405
      return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
406
                  .map(res => <any> res.json())
407
                  .map(res => res.total);
408
    }
409

    
410
    numOfEntityPublications(id: string, entity: string):any {
411
        var parameters: string = "";
412
        if(entity == "project") {
413
          parameters = "projects/"+id+"/publications/count";
414
        }
415
        let url = OpenaireProperties.getSearchAPIURLLast()+parameters+"?format=json";
416
        return this.numOfPublications(url);
417
    }
418

    
419
    numOfSearchPublications(params: string):any {
420
        let url = OpenaireProperties.getSearchAPIURLLast()+"publications/count?format=json";
421
        if(params != "") {
422
          url += "&q=" + params;
423
        }
424
        return this.numOfPublications(url);
425
    }
426
/*
427
    private quote(word: any): string {
428
        return '"'+word+'"';
429
    }
430
*/
431
}
(17-17/19)