Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import { Http, Response, Headers, RequestOptions } from '@angular/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

    
8
@Injectable()
9
export class SearchEntriesService {
10

    
11
  private sizeOfDescription: number = 135;
12

    
13
  constructor(private http:Http) {
14
    }
15

    
16
  getTotal(url: string) {
17
      return this.http.get(url).map(res => <any> res.json()).map(res => res.total);
18
      //.do(res => {console.log(res)});
19
  }
20

    
21
  countTotal(url:string) {
22
      return this.http.get(url).map(res => <any> res.json()).map(res => res.length);
23
  }
24

    
25
  getResults(url: string) {
26
      return this.http.get(url).map(res => <any> res.json()).map(res => this.parseResults(res['results']));
27
  }
28

    
29
  parseResults(data: any): ResultInfo[] {
30
      let results: ResultInfo[] = [];
31

    
32
      let length = Array.isArray(data) ? data.length :1;
33

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

    
37
          var result: ResultInfo = new ResultInfo();
38

    
39

    
40
          if(Array.isArray(resData['title'])) {
41
              result['title'] = resData['title'][0].content;
42
          } else {
43
              result['title'] = resData['title'].content;
44
          }
45

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

    
48
          if(resData['bestaccessright'] && resData['bestaccessright'].hasOwnProperty("classid")) {
49
              result['accessRights'] = resData['bestaccessright'].classid;
50
          }
51

    
52
          if(resData['resulttype'].hasOwnProperty("classid")) {
53
              result['type'] = resData['resulttype'].classid;
54
          }
55

    
56
          if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
57
              if(result['authors'] == undefined) {
58
                result['authors'] = new Array<string>();
59
              }
60

    
61
              let authors = resData['creator'];
62
              let length = Array.isArray(authors) ? authors.length : 1;
63

    
64
              for(let i=0; i<length; i++) {
65
                let author = Array.isArray(authors) ? authors[i] : authors;
66
                result.authors[author.rank-1] = author.content;
67
              }
68

    
69
              result.authors = result.authors.filter(function (item) {
70
                return (item != undefined);
71
              });
72
          }
73

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

    
77
          if(!Array.isArray(resData.description)) {
78
              result.description = resData.description;
79
          } else {
80
              result.description = resData.description[0];
81
          }
82

    
83
          if(result.description.length > this.sizeOfDescription) {
84
            result.description = result.description.substring(0, this.sizeOfDescription) + "...";
85
          }
86

    
87
          results.push(result);
88
      }
89
      return results;
90
  }
91
}
    (1-1/1)