openaire-library/claims/claim-utils/service/searchDatacite.service.ts

85 lines
3.3 KiB
TypeScript
Raw Normal View History

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';
import{EnvProperties} from '../../../utils/properties/env-properties';
import { ClaimResult} from '../claimEntities.class';
@Injectable()
export class SearchDataciteService {
constructor(private http: Http ) {}
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+'&rows='+size+'&start='+(size*(page-1));
let key = url;
return this.http.get( ( properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(request => <any> request.json())
.map(request => (parse?this.parse(request.data):request))
.do(items => console.info(items))
//.catch(this.handleError);
}
getDataciteResultByDOI (doi: string, properties:EnvProperties, parse:boolean = false):any {
console.info("Fetch datacite resultt by DOI: "+doi);
let url = properties.searchDataciteAPIURL+'/'+doi;
let key = url;
return this.http.get( (properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(request => <any> request.json())
.map(request => (parse?this.parse(request.data):request))
.do(items => console.info(items));
// .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 || { };
}
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;
}
}