connect/src/app/community/community.service.ts

98 lines
3.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { ResultInfo } from './results/resultInfo';
@Injectable()
export class CommunityService {
private sizeOfDescription: number = 135;
constructor(private http:Http) {
}
getNumberOfPublications() {
return this.http.get('http://beta.services.openaire.eu:8480/search/rest/v2/api/publications/count?format=json&fq=communityid=egi')
.map(res => <any> res.json()).map(res => res.total).do(res => {console.log(res)});
}
getNumberOfResearchData() {
return this.http.get('http://beta.services.openaire.eu:8480/search/rest/v2/api/datasets/count?format=json&fq=communityid=egi')
.map(res => <any> res.json()).map(res => res.total).do(res => {console.log(res)});
}
getNumberOfSoftware() {
return this.http.get('http://beta.services.openaire.eu:8480/search/rest/v2/api/software/count?format=json&fq=communityid=egi')
.map(res => <any> res.json()).map(res => res.total).do(res => {console.log(res)});
}
getResults(url: string) {
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseResults(res['results']));
}
parseResults(data: any): ResultInfo[] {
let results: ResultInfo[] = [];
let length = Array.isArray(data) ? data.length :1;
for (let i=0; i<length; i++) {
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
var result: ResultInfo = new ResultInfo();
if(Array.isArray(resData['title'])) {
result['title'] = resData['title'][0].content;
} else {
result['title'] = resData['title'].content;
}
result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
if(resData['bestaccessright'].hasOwnProperty("classid")) {
result['accessRights'] = resData['bestaccessright'].classid;
}
if(resData['resulttype'].hasOwnProperty("classid")) {
result['type'] = resData['resulttype'].classid;
}
if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
if(result['authors'] == undefined) {
result['authors'] = new Array<string>();
}
let authors = resData['creator'];
let length = Array.isArray(authors) ? authors.length : 1;
for(let i=0; i<length; i++) {
let author = Array.isArray(authors) ? authors[i] : authors;
result.authors[author.rank-1] = author.content;
}
result.authors = result.authors.filter(function (item) {
return (item != undefined);
});
}
var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
if(!Array.isArray(resData.description)) {
result.description = resData.description;
} else {
result.description = resData.description[0];
}
if(result.description.length > this.sizeOfDescription) {
result.description = result.description.substring(0, this.sizeOfDescription) + "...";
}
results.push(result);
}
return results;
}
}