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

155 lines
5.0 KiB
TypeScript
Raw Normal View History

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<void> = 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<void>((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<void>((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) {
[Explore | Library | new-theme]: Added more refine filters in Repositories, Journals, Registries pages | Search & Datasource landing: Show not compatible datasources | Result Landing: get relations names by relationsVocabulary (dnet:relation_relClass.json). 1. env-properties.ts & environments/: Removed old properties searchLinkToEntityRegistriesDataProvidersTable and searchLinkToJournalsTable. 2. fetchDataproviders.class.ts & searchDataproviders.service.ts: Removed old unused methods related to subjects/ tables/ csv. 3. searchFields.ts: Added more refine filters in Repositories, Journals, Registries pages (COMPATIBLE_DATAPROVIDER_FIELDS, ENTITY_REGISTRIES_FIELDS, JOURNAL_FIELDS). 4. result-preview.component.ts: Added field @Input() deposit: boolean = false; 5. result-preview.component.html: a. Added link to landing page even for not compatible datasources. b. Added class "uk-label-danger" when compatibility = "not available" only when deposit=true. 6. searchResultsInDeposit.component.html: In <result-preview> added parameter deposit="true". 7. dataProvider.component.html: a. Added class "uk-label-danger" when compatibility = "not available". b. Show custom "Not yet registered" compatibility label when compatibility = "not available". 8. ISVocabularies.service.ts: Added methods for getting relationsVocabulary (dnet:relation_relClass.json). 9. resultLanding.service.ts & parsingFunctions.class.ts: When parsing relations, get relationName from relationsVocabulary. 10. resultLanding.component.ts: Get relationsVocabulary and pass it to "getResultLandingInfo()". 11. orcid-work.component.ts: When calling "getResultLandingInfo()", added null parameter for relationsVocabulary. 12. configuration.service.ts: [Bug fix] Added more checks in method "isPageEnabledByStateAsync()".
2022-08-03 17:21:14 +02:00
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);
// }
}