openaire-library/utils/configuration/configuration.service.ts

161 lines
5.1 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response, Headers} from '@angular/http';
import {HttpClient} from "@angular/common/http";
import {BehaviorSubject, from, Observable, Subscription} from 'rxjs';
// import 'rxjs/add/operator/do';
// import 'rxjs/add/operator/share';
import {map, mapTo} from 'rxjs/operators';
import {EnvProperties} from "../properties/env-properties";
import {Portal} from "../entities/adminTool/portal";
import {Page} from "../entities/adminTool/page";
@Injectable({ providedIn: 'root' })
export class ConfigurationService {
private communityInformation: BehaviorSubject<Portal> = new BehaviorSubject(null);
private sub: Subscription = null;
private source: Observable<Portal> = null;
private promise: Promise<boolean> = null;
constructor(private http: HttpClient ) {}
ngOnDestroy() {
if(this.sub) {
this.sub.unsubscribe();
}
}
/**
* @deprecated
*/
getCommunityInformation(properties:EnvProperties, community:string){
let url = properties.adminToolsAPIURL +"/"+properties.adminToolsPortalType+"/" + community + '/full';
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
//.map(res => res.json());
}
public initCommunityInformation(properties:EnvProperties, community:string) {
if(community == null) return;
let url = properties.adminToolsAPIURL +"/" + properties.adminToolsPortalType + "/" + community + "/full";
this.promise = new Promise<any>((resolve => {
this.source = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
this.sub = this.source
.subscribe(
(communityInformation: Portal) => {
this.communityInformation.next(communityInformation);
resolve();
},
error => {
this.communityInformation.error(error);
resolve();
});
}));
}
public get communityInformationState(): Observable<Portal> {
return this.communityInformation.asObservable();
}
/**
* @deprecated
*/
isEntityEnabled(APIUrl:string, community:string,entity: string){
//console.log("isEntityEnabled: "+entity);
let url = "isEntityEnabled-"+entity;
// if(entity == "publication" || entity == "dataset" || entity == "datasource"){
// return Observable.of(new Object()).mapTo(false);
// }
// return Observable.of(new Object()).mapTo(true);
return this.http.get(APIUrl + "/page")
.pipe(map(res => true));
}
/**
* @deprecated
*/
isPageEnabled(properties:EnvProperties, community:string,router: string){
let page_route: string = router.split('?')[0].substring(1);
let url = properties.adminToolsAPIURL + "/"+properties.adminToolsPortalType+"/" + community+"/pages?page_route="+page_route;
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => res.json())
.pipe(map(res => {
let result = false;
if(res['length'] >0 && res[0].route == page_route){
result = res[0].isEnabled;
}
return result;
}));//.do(res => {console.log("Route "+page_route +" is "+res)});
}
async isPageEnabledByStateAsync(properties: EnvProperties, community:string, page_route: string) {
if(!this.promise) {
this.initCommunityInformation(properties, community);
}
await this.promise;
return this.filtering(page_route);
}
isPageEnabledByState(properties: EnvProperties, community:string, router: string): Observable<boolean> {
let page_route: string = router.split('?')[0].substring(1);
return from(this.isPageEnabledByStateAsync(properties, community, page_route));
}
filtering(page_route: string) {
let community: Portal = this.communityInformation.getValue();
let pages: Page[] = <Page[]>community.pages;
pages = pages.filter(function (page: Page) {
return page.route == page_route;
});
if (pages) {
let result = false;
if (pages['length'] > 0 && pages[0].route == page_route) {
result = pages[0].isEnabled;
}
return result;
}
}
/**
* @deprecated
*/
getMainPageContent(APIUrl:string, community:string,){
return this.http.get(APIUrl + "/page")
.pipe(map(res => true));
}
/**
* @deprecated
*/
getSpecialAnouncementContent(APIUrl:string, community:string,){
return this.http.get(APIUrl + "/page")
.pipe(map(res => ""));
}
/**
* @deprecated
*/
getHelpPageContent(APIUrl:string, community:string, router:string){
return this.http.get(APIUrl + "/page")
.pipe(map(res => true));
}
// 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 this.http.get(this.APIUrl + "/page")
// .map(res => true);
// }
}