blob: b7dea7de3775af27aba310243085c8cdeb5f9bd9 [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, Response, Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {MDMItem} from '../core/mdm-item';
import {Node} from './node';
import {PropertyService} from '../core/property.service';
import {PreferenceService, Preference} from '../core/preference.service';
import {QueryService, Query} from '../tableview/query.service';
import {HttpErrorHandler} from '../core/http-error-handler';
import {plainToClass} from 'class-transformer';
@Injectable()
export class NodeService {
private _nodeUrl: string;
static mapSourceNameToName(environments: Node[], sourceName: string) {
let env = environments.find(n => n.sourceName === sourceName);
return env ? env.name : sourceName;
}
constructor(private http: Http,
private httpErrorHandler: HttpErrorHandler,
private _prop: PropertyService,
private queryService: QueryService,
private preferenceService: PreferenceService) {
this._nodeUrl = _prop.getUrl('mdm/environments');
}
searchNodes(query, env, type) {
return this.http.get(this._nodeUrl + '/' + env + '/' + this.typeToUrl(type) + '?' + query).pipe(
map(res => plainToClass(Node, res.json().data)),
catchError(this.httpErrorHandler.handleError));
}
searchFT(query, env) {
return this.http.get(this._nodeUrl + '/' + env + '/search?q=' + query).pipe(
map(res => plainToClass(Node, res.json().data)),
catchError(this.httpErrorHandler.handleError));
}
getNodes(node?: Node) {
if (node == undefined) {
return this.getRootNodes();
}
return this.getNode(this.getUrl(node));
}
addNode (name: string): Observable<Node> {
let body = JSON.stringify({ name });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this._nodeUrl, body, options).pipe(
map(res => plainToClass(Node, res.json().data)[0]),
catchError(this.httpErrorHandler.handleError));
}
deleteNode(node: Node) {
return this.http.delete(this.getUrl(node)).pipe(
map(res => plainToClass(Node, res.json().data)),
catchError(this.httpErrorHandler.handleError));
}
compareNode(node1, node2) {
if (node1 == undefined || node2 == undefined) { return; }
let n1 = node1.name + node1.id + node1.type + node1.sourceName;
let n2 = node2.name + node2.id + node2.type + node2.sourceName;
if (n1 === n2) { return true; }
return false;
}
getRootNodes() {
return this.http.get(this._nodeUrl).pipe(
map(res => plainToClass(Node, res.json().data)),
catchError(this.httpErrorHandler.handleError));
}
getNodeFromItem(mdmItem: MDMItem) {
if (mdmItem.type === 'Environment') {
return this.getNode(this._nodeUrl + '/' + mdmItem.source).pipe(
map(nodes => (nodes && nodes.length > 0) ? nodes[0] : undefined));
} else {
return this.getNode(this._nodeUrl + '/' + mdmItem.source + '/' + this.typeToUrl(mdmItem.type) + '/' + mdmItem.id).pipe(
map(nodes => (nodes && nodes.length > 0) ? nodes[0] : undefined));
}
}
getNodesFromItem(mdmItem: MDMItem) {
return this.getNode(this._nodeUrl + '/' + mdmItem.source + '/' + this.typeToUrl(mdmItem.type) + '/' + mdmItem.id).pipe(
map(nodes => (nodes && nodes.length > 0) ? nodes[0] : undefined));
}
typeToUrl(type: string) {
switch (type) {
case 'StructureLevel':
return 'pools';
case 'MeaResult':
return 'measurements';
case 'SubMatrix':
return 'channelgroups';
case 'MeaQuantity':
return 'channels';
case 'Quantity':
return 'quantities';
default:
return type.toLowerCase() + 's';
}
}
getNode(url: string) {
return this.http.get(url).pipe(
map(res => plainToClass(Node, res.json().data)),
catchError(this.httpErrorHandler.handleError));
}
getNodesByUrl(url: string) {
// return this.http.get(this._nodeUrl + url).pipe(
// map(res => plainToClass(Node, res.json().data)),
// catchError(this.httpErrorHandler.handleError));
return this.getNode(this._nodeUrl + url);
}
getNodesByAbsoluteUrl(url: string) {
return this.getNode(url);
}
getNodeByRelation(sourceName: string, type: string, id: string) {
return this.getNode(this._nodeUrl + '/' + sourceName + '/' + this.typeToUrl(type) + '/' + id);
}
private getUrl(node: Node) {
return this._nodeUrl + '/' + node.sourceName + '/' + node.type + '/' + node.id;
}
}