2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Http, Response} from '@angular/http';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import 'rxjs/add/observable/of';
|
|
|
|
import 'rxjs/add/operator/do';
|
|
|
|
import 'rxjs/add/operator/share';
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../../../utils/properties/env-properties';
|
2018-05-03 11:58:30 +02:00
|
|
|
import { ClaimResult} from '../claimEntities.class';
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
|
|
|
export class SearchDataciteService {
|
|
|
|
constructor(private http: Http ) {}
|
|
|
|
|
2018-05-03 11:58:30 +02:00
|
|
|
searchDataciteResults (term: string, size : number, page : number, properties:EnvProperties, parse:boolean = false):any {
|
2018-02-05 14:14:59 +01:00
|
|
|
console.info("In search datacite results "+term+ " "+properties.searchDataciteAPIURL);
|
|
|
|
let url = properties.searchDataciteAPIURL+'?query='+term+'&rows='+size+'&start='+(size*(page-1));
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url;
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get( ( properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2017-12-19 13:53:46 +01:00
|
|
|
.map(request => <any> request.json())
|
2018-05-03 11:58:30 +02:00
|
|
|
.map(request => (parse?this.parse(request.data):request))
|
2017-12-19 13:53:46 +01:00
|
|
|
.do(items => console.info(items))
|
2018-05-03 11:58:30 +02:00
|
|
|
//.catch(this.handleError);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-05-03 11:58:30 +02:00
|
|
|
getDataciteResultByDOI (doi: string, properties:EnvProperties, parse:boolean = false):any {
|
2017-12-19 13:53:46 +01:00
|
|
|
console.info("Fetch datacite resultt by DOI: "+doi);
|
2018-02-05 14:14:59 +01:00
|
|
|
let url = properties.searchDataciteAPIURL+'/'+doi;
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url;
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get( (properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2017-12-19 13:53:46 +01:00
|
|
|
.map(request => <any> request.json())
|
2018-05-03 11:58:30 +02:00
|
|
|
.map(request => (parse?this.parse(request.data):request))
|
2017-12-19 13:53:46 +01:00
|
|
|
.do(items => console.info(items));
|
2018-05-03 11:58:30 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(items => console.log("Datacite Results: total results = "+items.meta.total+" doi = "+doi))
|
|
|
|
|
|
|
|
//.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 Observable.throw(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 || { };
|
|
|
|
}
|
2018-05-03 11:58:30 +02:00
|
|
|
parse(response):ClaimResult[]{
|
|
|
|
var results:ClaimResult[] = [];
|
|
|
|
for(var i=0; i<response.length; i++){
|
|
|
|
var item=response[i];
|
|
|
|
var result:ClaimResult = new ClaimResult();
|
|
|
|
result.publisher = null;
|
|
|
|
result.journal = null;
|
|
|
|
result.DOI = item.attributes.doi;
|
|
|
|
result.id = item.attributes.doi;
|
|
|
|
result.title = item.attributes.title;
|
|
|
|
result.url = 'http://dx.doi.org/'+item.attributes.doi;
|
|
|
|
result.source = 'datacite';
|
|
|
|
result.type = 'dataset';
|
|
|
|
result.date = item.attributes.published;
|
|
|
|
result.accessRights = "OPEN";
|
|
|
|
result.publisher =item.attributes['container-title']
|
|
|
|
result.journal = null;
|
|
|
|
result.result = item;
|
|
|
|
if(item.attributes.author){
|
|
|
|
for(var j=0; j<item.attributes.author.length; j++){
|
|
|
|
var author = item.attributes.author[j];
|
|
|
|
result.authors.push((author.family)?author.family+', '+author.given:author.literal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|