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

229 lines
7.8 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {throwError} from 'rxjs';
import {ProjectInfo} from '../../utils/entities/projectInfo';
import {EnvProperties} from '../../utils/properties/env-properties';
import {ParsingFunctions} from '../landing-utils/parsingFunctions.class';
import {map} from "rxjs/operators";
import {StringUtils} from "../../utils/string-utils.class";
@Injectable()
export class ProjectService {
constructor(private http: HttpClient) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
projectInfo: ProjectInfo;
getProjectInfo(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)
.pipe(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())
.pipe(map(res => res['results'][0]))
.pipe(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)
.pipe(map(res => this.parseProjectDates(id, res)))
}
/**
* @deprecated
*/
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())
.pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
.pipe(map(res => this.parseHTMLInfo(res)));
}
private handleError(error: HttpErrorResponse) {
console.log(error);
return throwError(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 {
if(data == null) {
return null;
}
this.projectInfo = new ProjectInfo();
if(data["header"]) {
this.projectInfo.id = data["header"]["id"];
}
if(data["project"]) {
let project = data["project"];
this.projectInfo.funding = {
funderName: "", funderShortName: "", funderJurisdiction: "", code: "", callIdentifier: "",
fundingStream: "", budget: "", contribution: "", currency: ""
};
this.projectInfo.acronym = project.acronym;
if (project['title']) {
this.projectInfo.title = Array.isArray(project['title']) ? StringUtils.HTMLToString(String(project.title[0])) : StringUtils.HTMLToString(String(project.title));
} else {
this.projectInfo.title = "";
}
this.projectInfo.funding.code = project.code;
if (project.startdate) {
let date: number = Date.parse(project.startdate);
this.projectInfo.startDate = (date ? date : null);
}
if (project.enddate) {
let date: number = Date.parse(project.enddate);
this.projectInfo.endDate = (date ? date : null);
}
if (this.projectInfo.endDate || this.projectInfo.startDate) {
let todayDate = Date.parse(new Date().toString());
this.projectInfo.currentDate = todayDate;
if (this.projectInfo.startDate) {
let startDate = +(this.projectInfo.startDate);
if (todayDate < startDate) {
this.projectInfo.status = "Not started";
}
}
if (this.projectInfo.endDate && !this.projectInfo.status) {
let endDate = +(this.projectInfo.endDate);
if (todayDate <= endDate) {
this.projectInfo.status = "On going";
} else {
this.projectInfo.status = "Closed";
}
}
}
if (this.projectInfo.funding) {
// if (this.projectInfo.funding.funderShortName == "EC") {
this.projectInfo.openAccessMandatePublications = project.oamandatepublications;
if (project["projectoamandatedata"]) {
this.projectInfo.openAccessMandateDatasets = project.oamandatedata;
} else if (project["ecarticle29_3"]) {
this.projectInfo.openAccessMandateDatasets = project.ecarticle29_3;
}
this.projectInfo.funding.callIdentifier = project.callidentifier;
// }
this.projectInfo.funding.budget = project.totalcost;//"10000";
this.projectInfo.funding.contribution = project.fundedamount;//"200100";
this.projectInfo.funding.currency = project.currency;//"EUR";
}
this.projectInfo.description = this.parsingFunctions.parseDescription(project && project.summary ? project.summary : []);
if (project['funding']) {
let funding: { "funderName": string, "funderShortname": string, "funderJurisdiction": string, "stream": string };
funding = this.parsingFunctions.parseFundingTrees(project.funding);
if (funding.funderName) {
this.projectInfo.funding.funderName = funding.funderName;
}
if (funding.funderShortname) {
this.projectInfo.funding.funderShortName = funding.funderShortname;
}
if (funding.funderJurisdiction) {
this.projectInfo.funding.funderJurisdiction = funding.funderJurisdiction;
}
if (funding.stream) {
this.projectInfo.funding.fundingStream = funding.stream;
}
}
}
if(data["links"]) {
this.projectInfo.organizations = [];
let links = data['links'];
let relLength = Array.isArray(links) ? links.length : 1;
for (let i = 0; i < relLength; i++) {
let relation = Array.isArray(links) ? links[i] : links;
if (relation["header"] && relation['header'].relationClass && relation['header'].relationClass.toLowerCase() == "hasparticipant") {
let country: string = "";
let acronym: string = "";
let name: string = "";
let id: string = "";
id = relation['header'].relatedIdentifier;
if(relation["country"]) {
country = relation.country.label;
}
if (relation["legalshortname"]) {
acronym = relation.legalshortname;
}
if (relation["legalname"]) {
name = relation.legalname;
}
if (!acronym && !name) {
// acronym is displayed with link and name only in tooltip
acronym = "[no title available]";
}
this.projectInfo.organizations.push({"country": country, "acronym": acronym, "name": name, "id": id});
}
}
}
if (data.measures) {
this.projectInfo.measure = this.parsingFunctions.parseMeasures(data.measures);
}
return this.projectInfo;
}
parseProjectDates(id: string, data: any): any {
let project = {id: id, startDate: "", endDate: ""};
if (data['project'] != null) {
project.startDate = data['project'].startdate;
project.endDate = data['project'].enddate;
}
return project;
}
}