/** Angular Imports */ import { Component , OnInit , ViewChild } from '@angular/core' ; import { MatDialog } from '@angular/material/dialog' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatSort } from '@angular/material/sort' ; import { MatTableDataSource } from '@angular/material/table' ; import { ActivatedRoute } from '@angular/router' ; import { DatePipe } from '@angular/common' ; /** Custom Services */ import { SystemService } from 'app/system/system.service' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { SettingsService } from 'app/settings/settings.service' ; /** Custom Components */ import { DeleteDialogComponent } from '../../shared/delete-dialog/delete-dialog.component' ; import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base' ; import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base' ; import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base' ; import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; /**
 * Entity to Entity Mapping Component
 */ @ Component ( { selector : 'mifosx-entity-to-entity-mapping' , templateUrl : './entity-to-entity-mapping.component.html' , styleUrls : [ './entity-to-entity-mapping.component.scss' ] } ) export class EntityToEntityMappingComponent implements OnInit { /** Stores entity to entity mapping data */ entityMappings : string [ ] = [ ] ; /** Stores Id of selected mapping type */ selectedMappingType = 0 ; /** Stores Id of first Entity filter */ selectedFromId = 0 ; /** Stores Id of second Entity filter */ selectedToId = 0 ; /** Checks wheter the filter is default or customized */ hasClickedFilters = false ; /** Selected Entity Id */ retrieveById = 0 ; /** relative Id */ relId : number ; /** Stores filtered data */ filterPreference : any ; /** details of particular map id */ entityMap : any ; /** Map Id to ID */ mapIdToEdit : any ; /**
     * stores the data for clicked mapType
     */ firstEntityData : any = [ ] ; secondEntityData : any = [ ] ; firstMappingEntity : string ; secondMappingEntity : string ; /** Data source for entity table. */ datasource : MatTableDataSource < any > ; /** Data source for entity to entity list data. */ entityMappingsListData : MatTableDataSource < any > ; /** Filter Preference Form */ filterPreferenceForm : FormGroup ; /** List of Entity to Entity Mapping */ displayedColumns : string [ ] = [ 'entitymapping' ] ; /** Columns for details of a chosen mapping */ entityMappingListColumns : string [ ] = [ 'fromentity' , 'toentity' , 'startdate' , 'enddate' , 'edit' , 'delete' ] ; /** Paginator for entity table. */ @ ViewChild ( MatPaginator ) paginator : MatPaginator ; /** Sorter for entity table. */ @ ViewChild ( MatSort ) sort : MatSort ; param { ActivatedRoute } route constructor ( private route , private formBuilder , private systemService , private datePipe , private dialog , private settingsService ) { this . route . data . subscribe ( ( data ) => { this . entityMappings = data . entityMappings ; } ) ; } /**
     * Creates the filter preference form.
     */ createFilterPreferenceForm ( ) { this . filterPreferenceForm = this . formBuilder . group ( { 'mappingFirstParamId' : [ '' , Validators . required ] , 'mappingSecondParamId' : [ '' , Validators . required ] } ) ; } /**
     * Sets the mapping table.
     */ ngOnInit ( ) { this . setMapping ( ) ; } setMapping ( ) { this . datasource = new MatTableDataSource ( this . entityMappings ) ; } param id showFilters ( id ) { this . selectedMappingType = id ; this . hasClickedFilters = false ; this . fetchRelatedData ( this . selectedMappingType ) ; this . selectedFromId = 0 ; this . selectedToId = 0 ; } /** Fetches data based on the selected mapping type */ fetchRelatedData ( id ) { this . retrieveById = id ; this . createFilterPreferenceForm ( ) ; switch ( this . retrieveById ) { case 1 : this . systemService . getOffices ( ) . subscribe ( ( response ) => { this . firstEntityData = response ; this . firstMappingEntity = 'Office' ; } ) ; this . systemService . getLoanProducts ( ) . subscribe ( ( response ) => { this . secondEntityData = response ; this . secondMappingEntity = 'Loan Products' ; } ) ; break ; case 2 : this . systemService . getOffices ( ) . subscribe ( ( response ) => { this . firstEntityData = response ; this . firstMappingEntity = 'Office' ; } ) ; this . systemService . getSavingProducts ( ) . subscribe ( ( response ) => { this . secondEntityData = response ; this . secondMappingEntity = 'Saving Products' ; } ) ; break ; case 3 : this . systemService . getOffices ( ) . subscribe ( ( response ) => { this . firstEntityData = response ; this . firstMappingEntity = 'Office' ; } ) ; this . systemService . getCharges ( ) . subscribe ( ( response ) => { this . secondEntityData = response ; this . secondMappingEntity = 'Charges' ; } ) ; break ; case 4 : this . systemService . getRoles ( ) . subscribe ( ( response ) => { this . firstEntityData = response ; this . firstMappingEntity = 'Role' ; } ) ; this . systemService . getLoanProducts ( ) . subscribe ( ( response ) => { this . secondEntityData = response ; this . secondMappingEntity = 'Loan Products' ; } ) ; break ; case 5 : this . systemService . getRoles ( ) . subscribe ( ( response ) => { this . firstEntityData = response ; this . firstMappingEntity = 'Role' ; } ) ; this . systemService . getSavingProducts ( ) . subscribe ( ( response ) => { this . secondEntityData = response ; this . secondMappingEntity = 'Saving Products' ; } ) ; break ; } } /**
     * Submits the filter preference form and creates a entityMappingList data
     */ showFilteredData ( ) { this . filterPreference = this . filterPreferenceForm . value ; if ( this . filterPreference . mappingFirstParamId === '' ) { this . filterPreference . mappingFirstParamId = 0 ; } if ( this . filterPreference . mappingSecondParamId === '' ) { this . filterPreference . mappingSecondParamId = 0 ; } this . hasClickedFilters = true ; this . selectedFromId = this . filterPreference . mappingFirstParamId ; this . selectedToId = this . filterPreference . mappingSecondParamId ; this . systemService . getEntitytoEntityData ( this . retrieveById , this . selectedFromId , this . selectedToId ) . subscribe ( ( response ) => { this . entityMappingsListData = new MatTableDataSource ( response ) ; this . entityMappingsListData . paginator = this . paginator ; this . entityMappingsListData . sort = this . sort ; } ) ; } param selectedType showAddScreen ( selectedType ) { this . relId = selectedType ; this . fetchRelatedData ( this . relId ) ; const formfields = [ new SelectBase ( { controlName : 'fromId' , label : this . firstMappingEntity , options : { label : 'name' , value : 'id' , data : this . firstEntityData } , required : true } ) , new SelectBase ( { controlName : 'toId' , label : this . secondMappingEntity , options : { label : 'name' , value : 'id' , data : this . secondEntityData } , required : true } ) , new DatepickerBase ( { controlName : 'startDate' , label : 'Start Date' , type : 'date' , required : false } ) , new DatepickerBase ( { controlName : 'endDate' , label : 'End Date' , type : 'date' , required : false } ) ] ; const data = { title : 'Add Entity to Entity Mapping' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const addEntitytoEntityMappingDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; addEntitytoEntityMappingDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { this . submitNew ( response . data ) ; } } ) ; } param selectedMap param selectedType showEditScreen ( selectedMap , selectedType ) { this . relId = selectedType ; this . mapIdToEdit = selectedMap ; this . fetchRelatedData ( this . relId ) ; this . systemService . getMapIdData ( selectedMap ) . subscribe ( ( response ) => { this . entityMap = response ; } ) ; const formfields = [ new SelectBase ( { controlName : 'fromId' , label : this . firstMappingEntity , options : { label : 'name' , value : 'id' , data : this . firstEntityData } , required : true } ) , new SelectBase ( { controlName : 'toId' , label : this . secondMappingEntity , options : { label : 'name' , value : 'id' , data : this . secondEntityData } , required : true } ) , new DatepickerBase ( { controlName : 'startDate' , label : 'Start Date' , type : 'date' , required : false } ) , new DatepickerBase ( { controlName : 'endDate' , label : 'End Date' , type : 'date' , required : false } ) ] ; const data = { title : 'Edit Entity to Entity Mapping' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const editEntitytoEntityMappingDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editEntitytoEntityMappingDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { this . submitEdit ( response . data ) ; } } ) ; } param addMappingForm submitNew ( addMappingForm ) { if ( addMappingForm . value . fromId === '' ) { addMappingForm . value . fromId = undefined ; } if ( addMappingForm . value . toId === '' ) { addMappingForm . value . toId = undefined ; } const dateFormat = this . settingsService . dateFormat ; const startDate = addMappingForm . value . startDate ; const endDate = addMappingForm . value . endDate ; addMappingForm . patchValue ( { startDate : this . datePipe . transform ( startDate , dateFormat ) , endDate : this . datePipe . transform ( endDate , dateFormat ) } ) ; const newMappingData = addMappingForm . value ; newMappingData . dateFormat = dateFormat ; newMappingData . locale = this . settingsService . language . code ; this . systemService . createMapping ( this . relId , newMappingData ) . subscribe ( ( response ) => { this . showFilteredData ( ) ; } ) ; } param editMappingForm submitEdit ( editMappingForm ) { const dateFormat = this . settingsService . dateFormat ; const startDate = editMappingForm . value . startDate ; const endDate = editMappingForm . value . endDate ; editMappingForm . patchValue ( { startDate : this . datePipe . transform ( startDate , dateFormat ) , endDate : this . datePipe . transform ( endDate , dateFormat ) } ) ; const newMappingData = editMappingForm . value ; newMappingData . dateFormat = dateFormat ; newMappingData . locale = this . settingsService . language . code ; this . systemService . editMapping ( this . mapIdToEdit , newMappingData ) . subscribe ( ( response ) => { this . showFilteredData ( ) ; } ) ; } param id delete ( id ) { const deleteNoteDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `Mapping id: ${ id }` } } ) ; deleteNoteDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . systemService . deleteMapping ( id ) . subscribe ( ( ) => { this . showFilteredData ( ) ; } ) ; } } ) ; } }