blob: 16283b878dac198f95d48df5dd97dc06482200f1 [file] [log] [blame]
import { Component, Input, OnInit } from '@angular/core';
import { MdDialogRef } from '@angular/material';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { NotificationFileModel } from '../../model/file-model';
import { SortingState } from '../../model/sorting-state';
import { ImportService } from '../../services/import.service';
import { SessionContext } from '../../common/session-context';
import { BannerMessage } from '../../common/banner-message';
import { StatusEn, BannerMessageStatusEn } from '../../common/enums';
import { AbstractListComponent } from '../../lists/abstract-list/abstract-list.component';
import { SortingComponent } from '../../lists/sorting/sorting.component';
@Component({
selector: 'app-file-import',
templateUrl: './file-import.component.html',
styleUrls: ['./file-import.component.css', '../../lists/abstract-list/abstract-list.component.css']
})
export class FileImportComponent implements OnInit {
gridId = 'ImportNotificationDialog';
defaultList: NotificationFileModel[] = new Array<NotificationFileModel>();
defaultList1: NotificationFileModel[];
defaultList2: NotificationFileModel[];
fileModels: NotificationFileModel[];
importFileModel: NotificationFileModel;
isImportFileSelected = false;
sortingState: SortingState = new SortingState();
subscriptionImportFiles: Subscription;
subscriptionDeleteFiles: Subscription;
constructor(public dialogRef: MdDialogRef<FileImportComponent>,
private importService: ImportService,
public sessionContext: SessionContext) { }
ngOnInit() {
this.importFileModel = null;
this.isImportFileSelected = false;
this.setFiles();
}
setFiles() {
this.isImportFileSelected = false;
this.importService.getImportFiles().subscribe(files => {
this.fileModels = files;
this.defaultList1 = Object.assign(new Array<NotificationFileModel>(), files);
this.setFilesForBrachAndTer();
// if only one file is available, use direct import
if (this.fileModels.length === 1) {
this.importThisFile(this.defaultList[0]);
} else {
const sortingState = this.sessionContext.getSortingState(this.gridId);
if (!sortingState) {
return;
}
this.sortingState = sortingState;
this.sort(undefined);
}
},
error => {
this.sessionContext.setBannerMessage(
BannerMessageStatusEn.error,
'Fehler beim Lesen der Dateien von der Datenbank.',
true);
this.dialogRef.close();
});
}
setFilesForBrachAndTer() {
this.importService.importFile().subscribe(files => {
this.fileModels = files;
this.defaultList2 = Object.assign(new Array<NotificationFileModel>(), files);
this.mergeArrays();
// if only one file is available, use direct import
if (this.fileModels.length === 1) {
this.importThisFile(this.defaultList[0]);
} else {
const sortingState = this.sessionContext.getSortingState(this.gridId);
if (!sortingState) {
return;
}
this.sortingState = sortingState;
this.sort(undefined);
}
},
error => {
this.sessionContext.setBannerMessage(
BannerMessageStatusEn.error,
'Fehler beim Lesen der Dateien von der Datenbank.',
true);
this.dialogRef.close();
});
}
mergeArrays() {
for (let index = 0; index < this.defaultList1.length; index++) {
const item = new NotificationFileModel();
const objDateFileName = this.defaultList1[index];
const objBranchTerAndDescr = this.defaultList2[index];
item.creationDate = objDateFileName.creationDate;
item.fileName = objDateFileName.fileName;
item.branchName = objBranchTerAndDescr.branchName;
item.gridTerritoryName = objBranchTerAndDescr.gridTerritoryName;
item.notificationText = objBranchTerAndDescr.notificationText;
this.defaultList.push(item);
}
}
importThisFile(fileModel: NotificationFileModel) {
this.importFileModel = fileModel;
this.isImportFileSelected = true;
this.deleteFile(fileModel.fileName);
}
deleteFile(fileName: string) {
this.importService.deleteImportFile(fileName).subscribe(result => {
this.dialogRef.close();
},
error => {
this.sessionContext.setBannerMessage(
BannerMessageStatusEn.error,
'Fehler beim Löschen der Datei.',
true);
this.dialogRef.close();
}
);
}
cancel() {
this.dialogRef.close();
}
public sort(column: string) {
if (column) {
this.sortingState.isDesc = !this.sortingState.isDesc;
if (this.sortingState.column !== undefined && this.sortingState.column !== column) {
this.sortingState.counter = 1;
this.sortingState.isDesc = true;
} else {
this.sortingState.counter++;
}
this.sortingState.column = column;
}
const direction = this.sortingState.isDesc ? 1 : -1;
if (this.sortingState.defaultState && !column || this.sortingState.counter > 0 && this.sortingState.counter % 3 === 0) {
this.sortingState.counter = 0;
this.fileModels = Object.assign(new Array<NotificationFileModel>(), this.defaultList);
this.sortingState.defaultState = true;
this.sortingState.isDesc = false;
} else {
this.sortingState.defaultState = false;
this.fileModels.sort((a, b) => {
const a1 = this.getColumnValue(this.sortingState.column, a);
const b1 = this.getColumnValue(this.sortingState.column, b);
if (a1 == null) {
return 1 * direction;
}
if (b1 == null) {
return -1 * direction;
} else if (a1 < b1) {
return -1 * direction;
} else if (a1 > b1) {
return 1 * direction;
} else {
return 0;
}
});
}
this.sessionContext.setSortingState(this.gridId, this.sortingState);
};
private getColumnValue(columnName: string, notificationFileModel: NotificationFileModel) {
switch (columnName) {
case 'branchName':
return (notificationFileModel.branchName) ? notificationFileModel.branchName.toLowerCase() : null;
case 'gridTerritoryName':
return (notificationFileModel.gridTerritoryName) ? notificationFileModel.gridTerritoryName.toLowerCase() : null;
default:
return notificationFileModel[columnName] ? notificationFileModel[columnName].toLowerCase() : null;
}
}
}