2019-07-23 14:23:12 +02:00
|
|
|
import {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 {ClaimEntity, ClaimResult} from '../claimHelper.class';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2020-04-15 20:54:56 +02:00
|
|
|
import {EnvProperties} from "../../../utils/properties/env-properties";
|
2019-07-23 14:23:12 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
|
|
|
export class SearchCrossrefService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor( private http: HttpClient ) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
|
2020-04-15 20:54:56 +02:00
|
|
|
searchCrossrefResults(term: string, size: number, page: number, properties: EnvProperties, parse: boolean = false): any {
|
|
|
|
let url = properties.searchCrossrefAPIURL + '?query=' + term + '&rows=' + size + '&offset=' + (size * (page - 1));
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-04-15 20:54:56 +02:00
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(request => request['message']))
|
2019-07-23 14:23:12 +02:00
|
|
|
.pipe(map(request => [request["total-results"], parse ? this.parse(request.items) : request]))
|
2017-12-19 13:53:46 +01:00
|
|
|
//.catch(this.handleError);
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-04-15 20:54:56 +02:00
|
|
|
searchCrossrefByDOIs(DOIs: string[], properties: EnvProperties, parse: boolean = false): any {
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
var doisParams = "";
|
|
|
|
for (var i = 0; i < DOIs.length; i++) {
|
|
|
|
doisParams += (doisParams.length > 0 ? "," : "") + 'doi:' + DOIs[i];
|
|
|
|
}
|
2020-04-15 20:54:56 +02:00
|
|
|
let url = properties.searchCrossrefAPIURL + '?filter=' + doisParams;
|
2019-07-23 14:23:12 +02:00
|
|
|
return this.http.get(url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(request => <any> request.json().message)
|
|
|
|
.pipe(map(request => request['message']))
|
2019-07-23 14:23:12 +02:00
|
|
|
.pipe(map(request => [request["total-results"], parse ? this.parse(request['items']) : request]))
|
2017-12-19 13:53:46 +01:00
|
|
|
//.catch(this.handleError);
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
2020-04-15 20:54:56 +02:00
|
|
|
searchCrossrefByMultipleDOIs(dois: string[], properties: EnvProperties, parse: boolean = false): any {
|
|
|
|
let url = properties.searchCrossrefAPIURL + '?filter=doi:';
|
2019-07-23 14:23:12 +02:00
|
|
|
for (var i = 0; i < dois.length; i++) {
|
|
|
|
url = url + (url.length == 0 ? '' : ',') + 'doi:' + dois[i];
|
|
|
|
}
|
2020-04-15 20:54:56 +02:00
|
|
|
url = properties.searchCrossrefAPIURL + '?filter=' + url;
|
2017-12-19 13:53:46 +01:00
|
|
|
return this.http.get(url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(request => <any> request.json().message)
|
|
|
|
.pipe(map(request => request['message']))
|
2019-07-23 14:23:12 +02:00
|
|
|
.pipe(map( request => [request["total-results"], parse ? this.parse(request.items) : request]))
|
2017-12-19 13:53:46 +01:00
|
|
|
//.catch(this.handleError);
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
private handleError(error: any) {
|
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
|
|
|
|
|
|
|
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.DOI;
|
|
|
|
entity.id = item.DOI;
|
|
|
|
entity.title = item.title;
|
|
|
|
entity.result.url = item.URL;
|
|
|
|
entity.result.source = 'crossref';
|
|
|
|
entity.type = 'publication';
|
|
|
|
if (item['published-print']) {
|
|
|
|
entity.result.date = item['published-print']['date-parts'][0][0];
|
2018-05-03 11:58:30 +02:00
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
entity.result.accessRights = "OPEN";
|
|
|
|
entity.result.publisher = item.publisher;
|
|
|
|
entity.result.journal = null;
|
|
|
|
entity.result.record = item;
|
|
|
|
if (item.author) {
|
|
|
|
entity.result.authors = [];
|
|
|
|
for (let j = 0; j < item.author.length; j++) {
|
|
|
|
entity.result.authors.push(item.author[j].family + ", " + item.author[j].given);
|
2018-05-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-23 14:23:12 +02:00
|
|
|
if (item.editor) {
|
|
|
|
for (let j = 0; j < item.editor.length; j++) {
|
|
|
|
entity.result.editors.push(item.editor[j].family + ", " + item.editor[j].given);
|
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
|
|
|
|
|
|
|
}
|