Project

General

Profile

1
import {of, throwError as observableThrowError} from 'rxjs';
2
import {Injectable} from '@angular/core';
3
import {HttpClient} from '@angular/common/http';
4
import {EnvProperties} from '../../../utils/properties/env-properties';
5
import {ClaimEntity, ClaimResult} from '../claimHelper.class';
6
import {catchError, map} from 'rxjs/operators';
7

    
8

    
9
@Injectable()
10
export class SearchDataciteService {
11
  constructor(private http: HttpClient ) {}
12

    
13
  searchDataciteResults(term: string, size: number, page: number, properties: EnvProperties, parse: boolean = false): any {
14
    //console.info("In search datacite results "+term+ " "+properties.searchDataciteAPIURL);
15
    let url = properties.searchDataciteAPIURL + '?query=' + term + '&page[size]=' + size + '&page[number]=' + page;
16
    let key = url;
17

    
18
    return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
19
      .pipe(map(request => [request["meta"]["total"], (parse ? SearchDataciteService.parse(request["data"]) : request)]));
20
    //.catch(this.handleError);
21
  }
22

    
23
  getDataciteResultByDOI(doi: string, properties: EnvProperties, parse: boolean = false): any {
24
    let url = properties.searchDataciteAPIURL + '/' + doi;
25
    let key = url;
26
    return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
27
      .pipe(map(request =>  (parse ? SearchDataciteService.parse([request["data"]])[0] : request)), catchError(err => of(null)));
28
  }
29

    
30

    
31
  private handleError(error: Response) {
32
    // in a real world app, we may send the error to some remote logging infrastructure
33
    // instead of just logging it to the console
34
    console.log(error);
35
    return observableThrowError(error  || 'Server error');
36
  }
37

    
38
  private extractData(res: any) {
39
    if (res.status < 200 || res.status >= 300) {
40
      throw new Error('Bad response status: ' + res.status);
41
    }
42
    let body = res.json();
43
    return body.data || {};
44
  }
45

    
46
  static parse(response): ClaimEntity[] {
47
    const results: ClaimEntity[] = [];
48
    console.log(response);
49
    for (let i = 0; i < response.length; i++) {
50
      const item = response[i];
51
      const entity: ClaimEntity = new ClaimEntity();
52
      entity.result = new ClaimResult();
53
      entity.result.publisher = null;
54
      entity.result.journal = null;
55
      entity.result.DOI = item.attributes.doi;
56
      entity.id = item.attributes.doi;
57
      entity.title = item.attributes.title;
58
      entity.result.url = 'http://dx.doi.org/' + item.attributes.doi;
59
      entity.result.source = 'datacite';
60
      entity.type = 'dataset';
61
      entity.result.date = item.attributes.published;
62
      entity.result.accessRights = "OPEN";
63
      entity.result.publisher = item.attributes['container-title'];
64
      entity.result.journal = null;
65
      entity.result.record = item;
66
      if (item.attributes.author) {
67
        entity.result.authors = [];
68
        for (let j = 0; j < item.attributes.author.length; j++) {
69
          const author = item.attributes.author[j];
70
          entity.result.authors.push((author.family) ? author.family + ', ' + author.given : author.literal);
71
        }
72
      }
73
      results.push(entity);
74
    }
75

    
76
    return results;
77

    
78
  }
79
}
(7-7/9)