blob: 6e3a0db0eabfb8ea375670519d6757f51213288f [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2018 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 { Component, OnInit, ViewChild, Input, OnDestroy, Output, EventEmitter } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, FormArray, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { DropdownModule } from 'primeng/dropdown';
import { TreeTableModule } from 'primeng/treetable';
import { TreeNode, MenuItem } from 'primeng/api';
import { ContextMenuModule } from 'primeng/contextmenu';
import { TreeTable, TreeTableToggler, DataTable } from 'primeng/primeng';
import { DialogModule } from 'primeng/dialog';
import { Observable, BehaviorSubject } from 'rxjs';
import { MDMNotificationService } from '../core/mdm-notification.service';
import { ExtSystemService } from './extsystem.service';
import { Node, Attribute } from '../navigator/node';
import { plainToClass } from 'class-transformer';
@Component( {
selector: 'mdm-extsystem-viewer',
templateUrl: './extsystem-viewer.component.html',
styleUrls: ['./extsystem.component.css']
})
export class ExtSystemViewerComponent implements OnInit, OnDestroy {
// passed down from parent
@Input() extSystems: Node[];
@Input() selectedEnvironment: Node;
@Output() editMode = new EventEmitter<boolean>();
@Output() selectedES = new EventEmitter<string>();
// dialog and loading states
dialogExtSystemCreate = false;
dialogExtSystemDelete = false;
loadingExtSystemAttr = false;
// temporary data for dialogs
tmpExtSystemCreate: Node;
tmpExtSystemDelete: Node;
// external system attributes
extSystemAttrs: Node[];
bsExtSystemAttrs: BehaviorSubject<Node[]> = new BehaviorSubject<Node[]>(undefined);
// table selection
selectedExtSystem: Node;
selectedExtSystemAttr: Node;
constructor(private extSystemService: ExtSystemService,
private notificationService: MDMNotificationService,
private translateService: TranslateService) {
this.bsExtSystemAttrs.subscribe(value => {
this.extSystemAttrs = value;
});
}
ngOnInit() {
this.bsExtSystemAttrs.next(undefined);
}
ngOnDestroy() {
}
getExternalSystems() {
let data = new Array();
for (let i in this.extSystems) {
if (this.extSystems[i].type === 'ExtSystem') {
data.push(this.extSystems[i]);
}
}
return data;
}
getExternalSystemAttributes() {
if (!this.extSystemAttrs) {
return new Array();
}
return this.extSystemAttrs.filter(attr => attr.type === 'ExtSystemAttribute');
}
getMDMAttributes() {
let ids = new Array();
// need double forEach as the map function transfers the array into another array
this.selectedExtSystemAttr.relations.filter(rel => rel.entityType === 'MDMAttribute').map(rel => rel.ids)
.forEach(i => i.forEach(id => ids.push(id)));
return this.extSystemAttrs.filter(attr => attr.type === 'MDMAttribute' && ids.find(i => i === attr.id));
}
getAttributeValueFromNode(node: Node, attribute: string) {
for (let attr of node.attributes) {
if (attr.name === attribute) {
return attr.value;
}
}
return '';
}
getAttributeFromNode(node: Node, attribute: string) {
if (node.attributes !== undefined) {
for (let attr of node.attributes) {
if (attr.name === attribute) {
return attr;
}
}
}
return undefined;
}
onExtSystemRowSelect(event: any) {
this.selectedExtSystemAttr = undefined;
this.bsExtSystemAttrs.next(undefined);
this.loadingExtSystemAttr = true;
this.selectedExtSystem = event.data;
this.extSystemService.getExtSystemAttributesForScope(this.selectedEnvironment.sourceName, this.selectedExtSystem.id)
.subscribe(attrs => {
this.bsExtSystemAttrs.next(attrs);
this.loadingExtSystemAttr = false;
});
}
onExtSystemRowUnselect(event: any) {
this.selectedExtSystem = undefined;
this.bsExtSystemAttrs.next(undefined);
}
createAttribute(name: string, value?: string) {
let attr = new Attribute();
attr.dataType = 'STRING';
attr.unit = '';
attr.value = value !== undefined ? value : '';
attr.name = name;
return attr;
}
getIndicesForIds(ids: string[]) {
let indices = new Array();
for (let id of ids) {
for (let extSystem of this.extSystems) {
if (extSystem.id === id) {
indices.push(this.extSystems.indexOf(extSystem));
}
}
}
return indices;
}
addExtSystem() {
this.tmpExtSystemCreate = new Node();
this.tmpExtSystemCreate.type = 'ExtSystem';
this.tmpExtSystemCreate.sourceType = 'ExtSystem';
this.tmpExtSystemCreate.sourceName = this.selectedEnvironment.sourceName;
this.tmpExtSystemCreate.attributes = new Array();
this.tmpExtSystemCreate.attributes.push(this.createAttribute('Description'));
this.tmpExtSystemCreate.attributes.push(this.createAttribute('Name'));
this.tmpExtSystemCreate.attributes.push(this.createAttribute('MimeType', 'application/x-asam.aoany.extsystem'));
this.dialogExtSystemCreate = true;
}
editExtSystem(extSystem?: Node) {
if (extSystem != undefined) {
this.tmpExtSystemCreate = extSystem;
// this.dialogExtSystem = true;
this.selectedES.next(extSystem.id);
this.editMode.next(true);
}
}
removeExtSystem(extSystem?: Node) {
this.tmpExtSystemDelete = extSystem;
this.dialogExtSystemDelete = true;
}
cancelRemoveExtSystem() {
this.tmpExtSystemDelete = undefined;
this.dialogExtSystemDelete = false;
}
confirmRemoveExtSystem() {
if (this.tmpExtSystemDelete != undefined) {
let idxES: number = this.extSystems.indexOf(this.tmpExtSystemDelete);
if (idxES !== -1) {
this.extSystems.splice(idxES, 1);
if (this.tmpExtSystemDelete.relations !== undefined && this.tmpExtSystemDelete.relations.length > 0) {
// remove all children
let indices = new Array<number>();
for (let relation of this.tmpExtSystemDelete.relations) {
// the ext system attributes
let indicesESA = this.getIndicesForIds(relation.ids);
for (let ind of indicesESA) {
for (let r of this.extSystems[ind].relations) {
// the mdm attributes
indices = indices.concat(this.getIndicesForIds(r.ids));
}
}
indices = indices.concat(indicesESA);
}
indices.sort((a, b) => b - a);
for (let i of indices) {
this.extSystems.splice(i, 1);
}
}
}
if (this.tmpExtSystemDelete.id !== undefined && parseInt(this.tmpExtSystemDelete.id, 10) > 0) {
this.extSystemService.deleteExtSystem(this.selectedEnvironment.sourceName, this.tmpExtSystemDelete.id).subscribe();
}
}
this.selectedExtSystem = undefined;
this.tmpExtSystemDelete = undefined;
this.dialogExtSystemDelete = false;
}
// for new external systems only
saveDialogExtSystem() {
// update the name attribute
this.getAttributeFromNode(this.tmpExtSystemCreate, 'Name').value = this.tmpExtSystemCreate.name;
this.extSystemService.saveExtSystem(this.selectedEnvironment.sourceName, this.tmpExtSystemCreate)
.subscribe(
response => this.patchResponse(plainToClass(Node, response.json().data)),
error => this.notificationService.notifyError(
this.translateService.instant('administration.extsystem.err-cannot-save-ext-system'), error)
);
this.dialogExtSystemCreate = false;
}
cancelDialogExtSystem() {
this.dialogExtSystemCreate = false;
this.tmpExtSystemCreate = undefined;
}
patchResponse(nodes: Node[]) {
for (let node of nodes) {
if (node.name === this.tmpExtSystemCreate.name) {
if (this.tmpExtSystemCreate.id === undefined) {
this.extSystems.push(this.tmpExtSystemCreate);
}
this.tmpExtSystemCreate.id = node.id;
}
}
this.tmpExtSystemCreate = undefined;
}
}