Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
import { Observable } from 'rxjs/Rx';
4

    
5
import { ResultInfo } from '../openaireLibrary/connect/results/resultInfo';
6
import { CommunityInfo } from '../openaireLibrary/connect/community/communityInfo';
7
import {map} from "rxjs/operators";
8

    
9
@Injectable()
10
export class SearchEntriesService {
11

    
12
  private sizeOfDescription: number = 135;
13

    
14
  constructor(private http:HttpClient) {
15
    }
16

    
17
  getTotal(url: string) {
18
      return  this.http.get(url)
19
                  //.map(res => <any> res.json())
20
                  .pipe(map(res => res['total']));
21
      //.do(res => {console.log(res)});
22
  }
23

    
24
  countTotal(url:string) {
25
      return  this.http.get(url)
26
                  //.map(res => <any> res.json())
27
                  .pipe(map(res => res['length']));
28
  }
29

    
30
  getResults(url: string) {
31
      return  this.http.get(url)
32
                  //.map(res => <any> res.json())
33
                  .pipe(map(res => this.parseResults(res['results'])));
34
  }
35

    
36
  parseResults(data: any): ResultInfo[] {
37
      let results: ResultInfo[] = [];
38

    
39
      let length = Array.isArray(data) ? data.length :1;
40

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

    
44
          var result: ResultInfo = new ResultInfo();
45

    
46

    
47
          if(Array.isArray(resData['title'])) {
48
              result['title'] = resData['title'][0].content;
49
          } else {
50
              result['title'] = resData['title'].content;
51
          }
52

    
53
          result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
54

    
55
          if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
56
              result['accessRights'] = resData['bestaccessright'].classid;
57
          }
58

    
59
          if(resData['resulttype'].hasOwnProperty("classid")) {
60
              result['type'] = resData['resulttype'].classid;
61
          }
62

    
63
          if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
64
              if(result['authors'] == undefined) {
65
                result['authors'] = new Array<string>();
66
              }
67

    
68
              let authors = resData['creator'];
69
              let length = Array.isArray(authors) ? authors.length : 1;
70

    
71
              for(let i=0; i<length; i++) {
72
                let author = Array.isArray(authors) ? authors[i] : authors;
73
                result.authors[author.rank-1] = author.content;
74
              }
75

    
76
              result.authors = result.authors.filter(function (item) {
77
                return (item != undefined);
78
              });
79
          }
80

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

    
84
          if(!Array.isArray(resData.description)) {
85
              result.description = resData.description;
86
          } else {
87
              result.description = resData.description[0];
88
          }
89

    
90
          if(result.description.length > this.sizeOfDescription) {
91
            result.description = result.description.substring(0, this.sizeOfDescription) + "...";
92
          }
93

    
94
          results.push(result);
95
      }
96
      return results;
97
  }
98
}
    (1-1/1)