openaire-library/landingPages/project/project.service.ts

228 lines
8.7 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {ProjectInfo} from '../../utils/entities/projectInfo';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import{EnvProperties} from '../../utils/properties/env-properties';
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
@Injectable()
export class ProjectService {
constructor(private http: Http ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
projectInfo: ProjectInfo;
getProjectInfo (id: string, properties:EnvProperties):any {
console.info("getProjectInfo in service");
let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
let key = url;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
.map(res => [res,
res['fundingtree'],
res['rels']['rel']])
.map(res => this.parseProjectInfo(res, properties));
}
getProjectInfoByGrantId (grant: string, funder:string, properties:EnvProperties):any {
let url =properties.searchAPIURLLAst+ 'resources?query=(oaftype exact project)'+
'and (projectcode_nt exact "'+grant+'" ) and (fundershortname exact '+'"'+funder+ '"'+')'+"&format=json&size=1";
let key = url;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res['results'][0])
.map(res => [res['result']['metadata']['oaf:entity']['oaf:project'],res['result']['header']['dri:objIdentifier']])
.map(res => [res[0],
res[0]['fundingtree'],
res[0]['rels']['rel'],res[1]])
.map(res => this.parseProjectInfo(res, properties));
}
/*
get project strtDate and endDate
*/
getProjectDates (id: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst+'projects/'+id+"?format=json";
let key = url+'_projectDates';
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
.map(res => [res,
res['fundingtree'],
res['rels']['rel']])
.map(res => this.parseProjectDates(id,res))
}
getHTMLInfo(id: string, properties:EnvProperties): any {
let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
let key = url;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
.map(res => this.parseHTMLInfo(res));
}
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');
}
parseHTMLInfo (data: any):any {
let htmlInfo: {"title": string, "acronym": string, "callIdentifier": string};
if(data != null) {
htmlInfo = {"title": data.title, "acronym": data.acronym, "callIdentifier": data.callidentifier};
}
return htmlInfo;
}
parseProjectInfo (data: any, properties:EnvProperties):any {
this.projectInfo = new ProjectInfo();
if(data[3] != null) {
this.projectInfo.id =data[3];
}
if(data[0] != null) {
this.projectInfo.acronym = data[0].acronym;
if(Array.isArray(data[0]['title'])) {
this.projectInfo.title = data[0].title[0];
} else {
this.projectInfo.title = data[0].title;
}
this.projectInfo.callIdentifier = data[0].callidentifier;
this.projectInfo.contractNum = data[0].code;
this.projectInfo.startDate = data[0].startdate;
this.projectInfo.endDate = data[0].enddate;
this.projectInfo.openAccessMandatePublications = data[0].oamandatepublications;
this.projectInfo.specialClause39 = data[0].ecsc39;
this.projectInfo.openAccessMandateDatasets = data[0].ecarticle29_3;
}
if(data[1] != null) {
/*
if(data[1]['funder'] != null) {
this.projectInfo.funder = data[1]['funder'].shortname;
}
let funding;
this.projectInfo.funding = "";
if(data[1]['funding_level_2'] != null) {
funding = data[1]['funding_level_2'].id;
} else if(data[1]['funding_level_1'] != null) {
funding = data[1]['funding_level_1'].id;
} else if(data[1]['funding_level_0'] != null) {
funding = data[1]['funding_level_0'].id;
}
if(funding != undefined) {
funding = funding.split("::");
for(let i=1; i<funding.length; i++) {
if(this.projectInfo.funding != "") {
this.projectInfo.funding += " | ";
}
this.projectInfo.funding += funding[i];
}
}
*/
let funding: {"funderName": string, "funderShortname": string, "stream": string};
funding = this.parsingFunctions.parseFundingTrees(data[1]);
// if(funding.funderName) {
// this.publicationInfo.fundedByProjects[counter]['funderName'] = funding.funderName;
// }
if(funding.funderShortname) {
this.projectInfo.funder = funding.funderShortname;
}
if(funding.stream) {
this.projectInfo['funding'] = funding.stream;
}
}
if(data[2] != null) {
this.projectInfo.organizations = [];//new Map<string, string>();
let acronym: string = "";
let name: string = "";
let id: string = "";
if(!Array.isArray(data[2])) {
if(data[2].hasOwnProperty("legalshortname")) {
acronym = data[2].legalshortname;
}
if(data[2].hasOwnProperty("legalname")) {
name = data[2].legalname;
}
if(!acronym && !name){
// acronym is displayed with link and name only in tooltip
acronym = "[no title available]";
}
if(data[2].hasOwnProperty("to")) {
id = data[2]['to'].content;
}
this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
} else {
for(let i=0; i<data[2].length; i++) {
if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
if(data[2][i].hasOwnProperty("legalshortname")) {
acronym = data[2][i].legalshortname;
}
if(data[2][i].hasOwnProperty("legalname")) {
name = data[2][i].legalname;
}
if(!acronym && !name){
acronym = "[no title available]";
}
if(data[2][i].hasOwnProperty("to")) {
id = data[2][i]['to'].content;
}
this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
}
}
}
}
if(this.projectInfo.funder == "EC") {
this.projectInfo.url = properties.cordisURL+this.projectInfo.contractNum;
this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
}
return this.projectInfo;
}
parseProjectDates (id: string, data: any):any {
var project = { id: id, startDate: "", endDate: ""};
if(data[0] != null) {
project.startDate = data[0].startdate;
project.endDate = data[0].enddate;
}
return project;
}
}