blob: 9610ac17cb1661092fea23ca0811769cc83b4eaf [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, Headers, RequestOptions } from '@angular/http';
import { PropertyService } from './property.service';
import { plainToClass } from 'class-transformer';
import { HttpErrorHandler } from './http-error-handler';
import { throwError as observableThrowError, Observable } from 'rxjs';
import { mergeMap, map, catchError } from 'rxjs/operators';
export class Scope {
public static readonly SYSTEM = 'SYSTEM';
public static readonly SOURCE = 'SOURCE';
public static readonly USER = 'USER';
static toLabel(scope: string) {
return scope.charAt(0).toUpperCase() + scope.slice(1).toLowerCase();
}
}
export class Preference {
scope: string;
source?: string;
user?: string;
key: string;
value: string;
id: number;
static sortByScope(p1: Preference, p2: Preference) {
let getPriority = (scope: string) => {
switch (scope) {
case Scope.SYSTEM: return 1;
case Scope.SOURCE: return 2;
case Scope.USER: return 3;
default: return 4;
}
};
return getPriority(p1.scope) - getPriority(p2.scope);
}
constructor() {
this.key = '';
}
}
@Injectable()
export class PreferenceService {
private prefEndpoint: string;
constructor(private http: Http,
private httpErrorHandler: HttpErrorHandler,
private _prop: PropertyService) {
this.prefEndpoint = _prop.getUrl('mdm/preferences');
}
getPreferenceForScope(scope: string, key?: string): Observable<Preference[]> {
if (key == null) {
key = '';
}
return this.http.get(this.prefEndpoint + '?scope=' + scope + '&key=' + key).pipe(
map(response => plainToClass(Preference, response.json().preferences)),
catchError(this.handleError));
}
getPreference(key?: string) {
if (key == null) {
key = '';
}
return this.http.get(this.prefEndpoint + '?key=' + key).pipe(
map(response => plainToClass(Preference, response.json().preferences)),
catchError(this.handleError));
}
savePreference(preference: Preference) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.put(this.prefEndpoint, JSON.stringify(preference), options).pipe(
catchError(this.handleError));
}
deletePreference(id: number) {
return this.http.delete(this.prefEndpoint + '/' + id).pipe(
catchError(this.handleError));
}
deletePreferenceByScopeAndKey(scope: string, key: string) {
return this.getPreferenceForScope(scope, key).pipe(mergeMap(p => this.deletePreference(p[0].id)));
}
private handleError(e: Error | any) {
if (e instanceof Response) {
let response = <Response> e;
if (response.status !== 200) {
return observableThrowError('Could not request preferences! '
+ 'Please check if application server is running and preference database is configured correctly.');
}
}
return this.httpErrorHandler.handleError(e);
}
}