2021-07-14 13:19:57 +02:00
|
|
|
import {of, 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 {HttpClient} from '@angular/common/http';
|
|
|
|
import {EnvProperties} from '../../../utils/properties/env-properties';
|
|
|
|
import {ClaimEntity, ClaimResult} from '../claimHelper.class';
|
2022-12-16 08:59:48 +01:00
|
|
|
import {catchError, map, timeout} from 'rxjs/operators';
|
2021-11-09 22:04:01 +01:00
|
|
|
import {properties} from "../../../../../environments/environment";
|
2021-07-14 13:19:57 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-16 08:59:48 +01:00
|
|
|
getDataciteResultByDOI(doi: string, properties: EnvProperties, parse: boolean = false, file: boolean = false): any {
|
|
|
|
let timeoutTime: number = properties.environment == "production" ? 6000 : 12000;
|
|
|
|
let timeoutTimeForFile: number = 20000;
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
let url = properties.searchDataciteAPIURL + '/' + doi;
|
|
|
|
let key = url;
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2022-12-16 08:59:48 +01:00
|
|
|
.pipe(timeout(file ? timeoutTimeForFile : (timeoutTime)))
|
2021-07-14 13:19:57 +02:00
|
|
|
.pipe(map(request => (parse ? SearchDataciteService.parse([request["data"]])[0] : request)), catchError(err => 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
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
private extractData(res: any) {
|
2017-12-19 13:53:46 +01:00
|
|
|
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[] = [];
|
|
|
|
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;
|
2021-11-09 22:04:01 +01:00
|
|
|
entity.result.url = properties.doiURL + item.attributes.doi;
|
2019-07-23 14:23:12 +02:00
|
|
|
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];
|
2021-11-11 11:13:25 +01:00
|
|
|
if(author.family || author.literal) {
|
|
|
|
entity.result.authors.push((author.family) ? author.family + (author.given ? ', ' + 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
|
|
|
}
|