dnet-applications/frontends/dnet-is-application/src/app/dsm/dsm.component.ts

252 lines
7.4 KiB
TypeScript

import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { Page, BrowseTerm, Datasource, KeyValue, DsmConf, ProtocolParam, Api, ApiInsert } from '../common/is.model';
import { ISService } from '../common/is.service';
import { ActivatedRoute, Params } from '@angular/router';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MatTableDataSource } from '@angular/material/table';
import { MatSort } from '@angular/material/sort';
import { combineLatest } from 'rxjs';
import { Router } from '@angular/router';
import { PageEvent } from '@angular/material/paginator';
import { FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { MatSelectChange } from '@angular/material/select';
@Component({
selector: 'app-dsm-search',
templateUrl: './dsm-search.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmSearchComponent implements OnInit {
searchText: string = '';
browsableFields: KeyValue[] = [];
constructor(public service: ISService, public route: ActivatedRoute, public router: Router, public dialog: MatDialog) {
}
ngOnInit() {
this.service.dsmBrowsableFields((data: KeyValue[]) => this.browsableFields = data);
}
search() {
this.router.navigate(['/dsm/results/0/50'], {
queryParams: { value: this.searchText }
});
}
browseField(field: string, label: string) {
const dialogRef = this.dialog.open(DsmBrowseDialog, {
data: { field: field, label: label },
width: '80%'
});
}
}
@Component({
selector: 'app-dsm-results',
templateUrl: './dsm-results.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmResultsComponent implements OnInit {
filterText: string = '';
results: Datasource[] = [];
nResults: number = 0;
currPage: number = 0;
nPages: number = 0;
pageSize: number = 0;
field?: string;
value: string = '';
constructor(public service: ISService, public route: ActivatedRoute, public router: Router, public dialog: MatDialog) {
}
ngOnInit(): void {
combineLatest([this.route.params, this.route.queryParams],
(params: Params, queryParams: Params) => ({ params, queryParams })
).subscribe((res: { params: Params; queryParams: Params }) => {
const { params, queryParams } = res;
this.currPage = parseInt(params['page']);
this.pageSize = parseInt(params['size']);
this.field = queryParams['field'];
this.value = queryParams['value'];
this.reload();
});
}
reload() {
if (this.field) {
this.service.dsmSearchByField(this.field, this.value, this.currPage, this.pageSize, (page: Page<Datasource>) => {
this.results = page.content;
this.nResults = page.totalElements;
this.nPages = page.totalPages;
});
} else {
this.service.dsmSearch(this.value, this.currPage, this.pageSize, (page: Page<Datasource>) => {
this.results = page.content;
this.nResults = page.totalElements;
this.nPages = page.totalPages;
});
}
}
changePage(event: PageEvent) {
let path = '/dsm/results/' + event.pageIndex + '/' + event.pageSize;
let qp = this.field ?
{ field: this.field, value: this.value } :
{ value: this.value };
this.router.navigate([path], {
queryParams: qp
});
}
openAddApiDialog(dsId: string, dsName: string) {
const dialogRef = this.dialog.open(DsmAddApiDialog, {
data: { dsId: dsId, dsName: dsName },
width: '80%'
});
dialogRef.afterClosed().subscribe(result => {
if (result) this.reload();
});
}
}
@Component({
selector: 'app-dsm-api',
templateUrl: './dsm-api.component.html',
styleUrls: ['./dsm.component.css']
})
export class DsmApiComponent {
}
@Component({
selector: 'dsm-browse-dialog',
templateUrl: 'dsm-browse-dialog.html',
styleUrls: ['./dsm.component.css']
})
export class DsmBrowseDialog implements OnInit {
datasource: MatTableDataSource<BrowseTerm> = new MatTableDataSource<BrowseTerm>([]);
colums: string[] = ['name', 'total'];
@ViewChild(MatSort) sort: MatSort | undefined
constructor(public dialogRef: MatDialogRef<DsmBrowseDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
}
ngOnInit(): void {
this.service.dsmBrowse(this.data.field, (res: BrowseTerm[]) => this.datasource.data = res);
}
ngAfterViewInit() {
if (this.sort) this.datasource.sort = this.sort;
}
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value.trim().toLowerCase();
this.datasource.filter = filterValue;
}
onNoClick(): void {
this.dialogRef.close();
}
}
@Component({
selector: 'dsm-add-api-dialog',
templateUrl: './dsm-add-api.dialog.html',
styleUrls: ['./dsm.component.css']
})
export class DsmAddApiDialog {
selectedProtocol: string = '';
apiPrefix: string = '';
paramPrefix: string = '__PARAM_';
protocols: string[] = [];
compatibilityLevels: string[] = [];
contentDescTypes: string[] = [];
protocolsMap: any = {};
selProtParams: ProtocolParam[] = [];
addApiForm: FormGroup = new FormGroup({
apiIdSuffix: new FormControl('', [Validators.required]),
compatibility: new FormControl('', [Validators.required]),
contentdescription: new FormControl('', [Validators.required]),
protocol: new FormControl('', [Validators.required]),
baseurl: new FormControl('', [Validators.required, Validators.pattern('^(http|https|ftp|file|sftp|jar|mongodb):\/\/')]),
metadataIdentifierPath: new FormControl('', [Validators.required])
});
constructor(public dialogRef: MatDialogRef<DsmAddApiDialog>, @Inject(MAT_DIALOG_DATA) public data: any, public service: ISService) {
this.apiPrefix = 'api_________::' + data.dsId + '::';
this.service.dsmConf((conf: DsmConf) => {
this.compatibilityLevels = conf.compatibilityLevels;
this.contentDescTypes = conf.contentDescTypes;
conf.protocols.forEach((p) => {
this.protocols.push(p.id);
this.protocolsMap[p.id] = p.params;
});
});
}
onChangeProtocol(e: MatSelectChange): void {
this.selectedProtocol = e.value;
Object.keys(this.addApiForm.controls).forEach(k => {
if (k.startsWith(this.paramPrefix)) {
this.addApiForm.removeControl(k);
}
});
if (this.protocolsMap[e.value]) {
this.selProtParams = this.protocolsMap[e.value];
this.selProtParams.forEach(p => {
let validations: ValidatorFn[] = [];
if (!p.optional) { validations.push(Validators.required); }
if (p.type == 'INT') { validations.push(Validators.pattern('^[0-9]*$')); }
this.addApiForm.addControl(this.paramPrefix + p.name, new FormControl('', validations));
});
} else {
this.selProtParams = [];
}
}
onSubmit(): void {
let api: ApiInsert = {
id: this.apiPrefix + this.addApiForm.get('apiIdSuffix')?.value,
protocol: this.selectedProtocol,
datasource: this.data.dsId,
contentdescription: this.addApiForm.get('contentdescription')?.value,
removable: true,
compatibility: this.addApiForm.get('compatibility')?.value,
metadataIdentifierPath: this.addApiForm.get('metadataIdentifierPath')?.value,
baseurl: this.addApiForm.get('baseurl')?.value,
apiParams: []
};
Object.keys(this.addApiForm.controls).forEach(k => {
if (k.startsWith(this.paramPrefix)) {
let val = this.addApiForm.get(k)?.value;
if (val) {
api.apiParams.push({
param: k.substring(this.paramPrefix.length),
value: (Array.isArray(val)) ? val.join() : val
});
}
}
});
console.log(api);
//this.service.dsmAddApi(api, (data: void) => this.dialogRef.close(1), this.metadataForm);
}
onNoClick(): void {
this.dialogRef.close();
}
}