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

155 lines
5.0 KiB
TypeScript

import {Injectable, OnDestroy} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {BehaviorSubject, from, Observable, of, Subscription} from 'rxjs';
import {map} from 'rxjs/operators';
import {EnvProperties} from "../properties/env-properties";
import {Portal} from "../entities/adminTool/portal";
import {Page} from "../entities/adminTool/page";
import {properties} from "../../../../environments/environment";
@Injectable({providedIn: 'root'})
export class ConfigurationService {
private communityInformation: BehaviorSubject<Portal> = new BehaviorSubject(null);
private sub: Subscription = null;
private promise: Promise<boolean> = null;
constructor(private http: HttpClient) {
}
ngOnDestroy() {
this.clearSubscriptions();
}
clearSubscriptions() {
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 initStaticCommunityInformation(communityInformation: Portal) {
this.promise = new Promise<any>((resolve => {
this.communityInformation.next(communityInformation);
resolve();
}));
}
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.sub = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).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));
}
isPageEnabled(portal: string, route: string, portalType = properties.adminToolsPortalType) {
let url = properties.adminToolsAPIURL + "/" + portalType + "/" + portal + "/pages?page_route=" + route;
return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.pipe(map(res => {
let result = false;
if (res['length'] > 0 && res[0].route == route) {
result = res[0].isEnabled;
}
return result;
}));
}
async isPageEnabledByStateAsync(properties: EnvProperties, portal: string, page_route: string) {
if (!this.promise || (this.communityInformation && this.communityInformation.getValue() && portal !== this.communityInformation.getValue().pid)) {
this.initCommunityInformation(properties, portal);
}
await this.promise;
if (this.sub) {
this.sub.unsubscribe();
}
return this.filtering(page_route);
}
isPageEnabledByState(properties: EnvProperties, portal: string, router: string): Observable<boolean> {
let page_route: string = router.split('?')[0].substring(1);
return from(this.isPageEnabledByStateAsync(properties, portal, page_route));
}
filtering(page_route: string) {
let community: Portal = this.communityInformation.getValue();
let pages: Page[] = <Page[]>community.pages;
if (pages) {
let page = pages.find((page: Page) => page.route == page_route);
return page && page.isEnabled;
} else {
return false;
}
}
/**
* @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);
// }
}