blob: e494eb3452b7371f1ce13c182714f96e420c9b9e [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Localization} from './localization';
import {Node} from '../navigator/node';
import {NodeService} from '../navigator/node.service';
import {PropertyService} from '../core/property.service';
import {HttpErrorHandler} from '../core/http-error-handler';
import {MDMNotificationService} from '../core/mdm-notification.service';
import {forkJoin as observableForkJoin, Observable} from 'rxjs';
import {catchError, mergeMap, publishReplay, refCount, map} from 'rxjs/operators';
@Injectable()
export class LocalizationService {
private _nodeUrl: string;
private cache: Observable<Localization[]>;
constructor(private http: Http,
private httpErrorHandler: HttpErrorHandler,
private _prop: PropertyService,
private _node: NodeService,
private notificationService: MDMNotificationService) {
this._nodeUrl = _prop.getUrl('mdm/environments');
}
// Caches valueLists if cache is empty. Then returns observable containing cached valueLists.
getLocalizations() {
if (!this.cache) {
this.cache = this._node.getNodes(undefined).pipe(
mergeMap(envs => this.initLocalizations(envs)),
publishReplay(1),
refCount());
}
return this.cache;
}
private initLocalizations(envs: Node[]) {
return observableForkJoin(envs.map(env => this.getLocalization(env))).pipe(
map(locs => locs.reduce((a, b) => a.concat(b), [])));
}
private getLocalization(node: Node): Observable<Localization[]> {
let url = this._nodeUrl + '/' + node.sourceName;
if (node.sourceType === 'Environment') {
url = url + '/localizations?all=true';
} else {
url = url + '/' + node.type.toLowerCase() + 's/localizations';
}
return this.get(url);
}
private get(url: string) {
return this.http.get(url).pipe(
map(res => <Localization[]> res.json().data),
catchError(this.httpErrorHandler.handleError));
}
}