2019-08-27 15:19:58 +02:00
|
|
|
import {Observable, throwError as observableThrowError} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-07-23 14:23:12 +02:00
|
|
|
import {Response} from '@angular/http';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
|
|
|
import {EnvProperties} from '../../../utils/properties/env-properties';
|
|
|
|
import {ClaimEntity, ClaimResult} from '../claimHelper.class';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2019-08-27 15:19:58 +02:00
|
|
|
import 'rxjs/add/operator/catch';
|
|
|
|
import 'rxjs/add/observable/of';
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
|
|
|
export class SearchDataciteService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
searchDataciteResults(term: string, size: number, page: number, properties: EnvProperties, parse: boolean = false): any {
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.info("In search datacite results "+term+ " "+properties.searchDataciteAPIURL);
|
2019-07-23 14:23:12 +02:00
|
|
|
let url = properties.searchDataciteAPIURL + '?query=' + term + '&page[size]=' + size + '&page[number]=' + page;
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url;
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.pipe(map(request => [request["meta"]["total"], (parse ? SearchDataciteService.parse(request["data"]) : request)]));
|
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
2018-05-03 11:58:30 +02:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
getDataciteResultByDOI(doi: string, properties: EnvProperties, parse: boolean = false): any {
|
|
|
|
let url = properties.searchDataciteAPIURL + '/' + doi;
|
|
|
|
let key = url;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2019-08-27 15:19:58 +02:00
|
|
|
.pipe(map(request => (parse ? SearchDataciteService.parse([request["data"]])[0] : request))).catch(e => Observable.of(null));
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
private handleError(error: Response) {
|
2017-12-19 13:53:46 +01:00
|
|
|
// in a real world app, we may send the error to some remote logging infrastructure
|
|
|
|
// instead of just logging it to the console
|
|
|
|
console.log(error);
|
2019-06-03 15:20:36 +02:00
|
|
|
return observableThrowError(error || 'Server error');
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
private extractData(res: Response) {
|
|
|
|
if (res.status < 200 || res.status >= 300) {
|
|
|
|
throw new Error('Bad response status: ' + res.status);
|
|
|
|
}
|
|
|
|
let body = res.json();
|
2019-07-23 14:23:12 +02:00
|
|
|
return body.data || {};
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
|
|
|
|
static parse(response): ClaimEntity[] {
|
|
|
|
const results: ClaimEntity[] = [];
|
|
|
|
console.log(response);
|
|
|
|
for (let i = 0; i < response.length; i++) {
|
|
|
|
const item = response[i];
|
|
|
|
const entity: ClaimEntity = new ClaimEntity();
|
|
|
|
entity.result = new ClaimResult();
|
|
|
|
entity.result.publisher = null;
|
|
|
|
entity.result.journal = null;
|
|
|
|
entity.result.DOI = item.attributes.doi;
|
|
|
|
entity.id = item.attributes.doi;
|
|
|
|
entity.title = item.attributes.title;
|
|
|
|
entity.result.url = 'http://dx.doi.org/' + item.attributes.doi;
|
|
|
|
entity.result.source = 'datacite';
|
|
|
|
entity.type = 'dataset';
|
|
|
|
entity.result.date = item.attributes.published;
|
|
|
|
entity.result.accessRights = "OPEN";
|
|
|
|
entity.result.publisher = item.attributes['container-title'];
|
|
|
|
entity.result.journal = null;
|
|
|
|
entity.result.record = item;
|
|
|
|
if (item.attributes.author) {
|
|
|
|
entity.result.authors = [];
|
|
|
|
for (let j = 0; j < item.attributes.author.length; j++) {
|
|
|
|
const author = item.attributes.author[j];
|
|
|
|
entity.result.authors.push((author.family) ? author.family + ', ' + author.given : author.literal);
|
2018-05-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
results.push(entity);
|
2018-05-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|