blob: 5ba4f4b5a153e31df984a264a73fe33df1a8c694 [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, EventEmitter} from '@angular/core';
import {Http} from '@angular/http';
import {map} from 'rxjs/operators';
import {MDMNotificationService} from '../core/mdm-notification.service';
import {PreferenceService, Scope} from '../core/preference.service';
import {QueryService} from '../tableview/query.service';
import {PropertyService} from '../core/property.service';
import {MDMItem} from '../core/mdm-item';
import { TranslateService } from '@ngx-translate/core';
import { TRANSLATE } from '../core/mdm-core.module';
declare function require(path: string): any;
const defaultNodeProvider = require('./defaultnodeprovider.json');
@Injectable()
export class NodeproviderService {
public nodeProviderChanged: EventEmitter<any> = new EventEmitter<any>();
private nodeproviders = [defaultNodeProvider];
private activeNodeprovider: any = this.nodeproviders[0];
constructor(private http: Http,
private _prop: PropertyService,
private queryService: QueryService,
private preferenceService: PreferenceService,
private notificationService: MDMNotificationService,
private translateService: TranslateService) {
this.loadNodeproviders();
}
setActiveNodeprovider(nodeprovider: any) {
this.activeNodeprovider = nodeprovider;
this.nodeProviderChanged.emit(this.activeNodeprovider);
}
getActiveNodeprovider() {
return this.activeNodeprovider;
}
getNodeproviders() {
return this.nodeproviders;
}
loadNodeproviders() {
this.preferenceService.getPreferenceForScope(Scope.SYSTEM, 'nodeprovider.').pipe(
map(prefs => prefs.map(p => JSON.parse(p.value))))
.subscribe(
nodeproviders => this.setNewNodeproviders(nodeproviders),
error => this.notificationService.notifyError(
this.translateService.instant('navigator.nodeprovider.err-cannot-load-node-provider-from-settings'), error)
);
}
setNewNodeproviders(nodeproviders) {
this.nodeproviders = nodeproviders;
this.nodeproviders.unshift(defaultNodeProvider);
}
getQueryForChildren(item: MDMItem) {
return this.replace(this.getSubNodeprovider(item), item);
}
getSubNodeprovider(item: MDMItem) {
let current = this.activeNodeprovider;
do {
if (current.type === item.type && current.children) {
return current.children.query;
} else {
current = current.children;
}
}
while (current);
return current;
}
replace(query: string, item: MDMItem) {
return '/' + item.source + query.replace(/{(\w+)\.(\w+)}/g, function(match, type, attr) {
if (type !== item.type) {
this.notificationService.notifyWarn(
this.translateService.instant(TRANSLATE('navigator.nodeprovider.err-unsupported-type'),
<any>{'type': type, 'typeToUse': item.type}));
}
if (attr === 'Id') {
return item.id;
}
});
}
getPathTypes(type: string): string[] {
let current = this.activeNodeprovider;
let ancestorList: string[] = [];
while (current) {
ancestorList.push(current.type);
if (current.type === type) {
return ancestorList;
}
current = current.children;
}
return [];
}
}