82 lines
3.2 KiB
TypeScript
82 lines
3.2 KiB
TypeScript
import {Observable, throwError as observableThrowError} from 'rxjs';
|
|
import {Injectable} from '@angular/core';
|
|
import {Response} from '@angular/http';
|
|
import {HttpClient} from '@angular/common/http';
|
|
import {EnvProperties} from '../../../utils/properties/env-properties';
|
|
import {ClaimEntity, ClaimResult} from '../claimHelper.class';
|
|
import {map} from "rxjs/operators";
|
|
import 'rxjs/add/operator/catch';
|
|
import 'rxjs/add/observable/of';
|
|
@Injectable()
|
|
export class SearchDataciteService {
|
|
constructor(private http: HttpClient ) {}
|
|
|
|
searchDataciteResults(term: string, size: number, page: number, properties: EnvProperties, parse: boolean = false): any {
|
|
//console.info("In search datacite results "+term+ " "+properties.searchDataciteAPIURL);
|
|
let url = properties.searchDataciteAPIURL + '?query=' + term + '&page[size]=' + size + '&page[number]=' + page;
|
|
let key = url;
|
|
|
|
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);
|
|
}
|
|
|
|
getDataciteResultByDOI(doi: string, properties: EnvProperties, parse: boolean = false): any {
|
|
let url = properties.searchDataciteAPIURL + '/' + doi;
|
|
let key = url;
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
.pipe(map(request => (parse ? SearchDataciteService.parse([request["data"]])[0] : request))).catch(e => Observable.of(null));
|
|
}
|
|
|
|
|
|
private handleError(error: Response) {
|
|
// 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);
|
|
return observableThrowError(error || 'Server error');
|
|
}
|
|
|
|
private extractData(res: Response) {
|
|
if (res.status < 200 || res.status >= 300) {
|
|
throw new Error('Bad response status: ' + res.status);
|
|
}
|
|
let body = res.json();
|
|
return body.data || {};
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
results.push(entity);
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
}
|