import {Injectable} from "@angular/core"; import {HttpClient} from "@angular/common/http"; import {BehaviorSubject, Observable} from "rxjs"; import {Indicator, Section, Stakeholder, StakeholderInfo} from "../entities/stakeholder"; import {HelperFunctions} from "../../utils/HelperFunctions.class"; import {map} from "rxjs/operators"; let maps: string[] = ['parameters', 'filters']; @Injectable({ providedIn: "root" }) export class StakeholderService { private stakeholderSubject: BehaviorSubject = null; constructor(private http: HttpClient) { this.stakeholderSubject = new BehaviorSubject(null); } getStakeholder(url: string, alias:string): Observable { /*return new BehaviorSubject( StakeholderCreator.createFunderFromDefaultProfile("ec","funder","ec__________::EC", "European Commission","EC", false,"ec",true,true)).asObservable();*/ return this.http.get(url + '/stakeholder/' + encodeURIComponent(alias)).pipe(map(stakeholder => { return this.formalize(stakeholder); })); } getAllStakeholders(url: string, type: string = null): Observable { return this.http.get(url + '/stakeholder/all' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } getStakeholders(url: string, type: string = null): Observable<(Stakeholder & StakeholderInfo)[]> { return this.http.get(url + '/stakeholder' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } getDefaultStakeholders(url: string, type: string = null): Observable { return this.http.get(url + '/stakeholder/default' + ((type)?('?type=' + type):'')).pipe(map(stakeholders => { return this.formalize(stakeholders); })); } buildStakeholder(url: string, stakeholder: Stakeholder): Observable { return this.http.post(url + '/build-stakeholder', stakeholder).pipe(map(stakeholder => { return this.formalize(stakeholder); })); } saveStakeholder(url: string, stakeholder: Stakeholder): Observable { return this.http.post(url + '/stakeholder/save', stakeholder).pipe(map(stakeholder => { return this.formalize(stakeholder); })); } toggleStatus(url: string, path: string[]): Observable { return this.http.post(url + '/' + path.join('/') + '/toggle-status', null); } toggleAccess(url: string, path: string[]): Observable { return this.http.post(url + '/' + path.join('/') + '/toggle-access', null); } saveElement(url: string, element: any, path: string[] = []): Observable { path = HelperFunctions.encodeArray(path); return this.http.post(url + ((path.length > 0)?'/':'') + path.join('/') + '/save', element).pipe(map(element => { return this.formalize(element); })); } saveSection(url: string, element: any, path: string[] = [], index: number = -1): Observable
{ path = HelperFunctions.encodeArray(path); return this.http.post
(url + ((path.length > 0)?'/':'') + path.join('/') + '/save/' + index, element).pipe(map(element => { return this.formalize(element); })); } deleteElement(url: string, path: string[], childrenAction: string = null): Observable { path = HelperFunctions.encodeArray(path); let params: string = ""; if(childrenAction) { params = "?children="+childrenAction; } return this.http.delete(url + '/' + path.join('/') + '/delete'+params); } reorderIndicators(url: string, path: string[], indicatorIds: string[], type: string = 'chart'): Observable { path = HelperFunctions.encodeArray(path); return this.http.post(url + '/' + path.join('/') + '/' + type + '/reorder', indicatorIds).pipe(map(indicators => { return this.formalize(indicators); })); } getStakeholderAsObservable(): Observable { return this.stakeholderSubject.asObservable(); } get stakeholder(): Stakeholder { return this.stakeholderSubject.getValue(); } setStakeholder(stakeholder: Stakeholder) { this.stakeholderSubject.next(stakeholder); } private formalize(element: any) { return HelperFunctions.copy(element); } }