Merge branch 'DEVELOP' of ssh://git.eclipse.org:29418/openk-usermodules/org.eclipse.openk-usermodules.gridFailureInformation.frontend into DEVELOP
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.html b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.html
index 185818b..0ea8a36 100644
--- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.html
+++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.html
@@ -101,7 +101,7 @@
     <form *ngIf="gridFailureDetailsSandbox.gridFailureDetailsFormState$ | async as formState" [ngrxFormState]="formState">
       <div *ngIf="gridFailureDetailsSandbox.maxVersionNumber">
         <label> {{ 'GridFailure.Version' | translate }}</label>
-        <select class="version-select" (change)="gridFailureDetailsSandbox.loadGridFailureVersion($event.target.value)">
+        <select class="version-select" (change)="gridFailureDetailsSandbox.loadGridFailureVersion($event.target.value); changeMode()">
           <option *ngFor="let version of gridFailureVersions$ | async" [selected]="version.versionNumber == formState.value.versionNumber">{{
             version.versionNumber
           }}</option>
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.ts b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.ts
index 5874a40..431a507 100644
--- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.ts
+++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.component.ts
@@ -10,10 +10,10 @@
  *
  * SPDX-License-Identifier: EPL-2.0
  ********************************************************************************/
-import { combineLatest } from 'rxjs';
+import { combineLatest, Subject } from 'rxjs';
 import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
 import { GridFailureDetailsSandbox } from '@grid-failure-information-app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox';
-import { RolesEnum, StateEnum, VoltageLevelEnum } from '@grid-failure-information-app/shared/constants/enums';
+import { ModeEnum, RolesEnum, StateEnum, VoltageLevelEnum } from '@grid-failure-information-app/shared/constants/enums';
 import { Globals } from '@grid-failure-information-app/shared/constants/globals';
 import { GridFailure } from '@grid-failure-information-app/shared/models/grid-failure.model';
 import * as store from '@grid-failure-information-app/shared/store';
@@ -23,7 +23,7 @@
 import { BaseList } from '@grid-failure-information-app/shared/components/base-components/base.list';
 import { STATION_COLDEF } from './station-list-column-definition';
 import { SetFilterComponent } from '@grid-failure-information-app/shared/filters/ag-grid/set-filter/set-filter.component';
-import { take, skip, skipWhile } from 'rxjs/operators';
+import { take, skipWhile, takeUntil } from 'rxjs/operators';
 
 @Component({
   selector: 'app-grid-failure-details',
@@ -41,12 +41,13 @@
   public failureLocationView: string = this._initialLocationView;
   public mapOptions: MapOptions = new MapOptions();
   public get mapInteractionMode(): boolean {
-    return this.failureLocationView === Globals.FAILURE_LOCATION_MAP;
+    return this.failureLocationView === Globals.FAILURE_LOCATION_MAP && !this.gridFailureDetailsSandbox.disableStationAttributes;
   }
   public stationsColumnDefinition: any = STATION_COLDEF;
   public frameworkComponents: any;
 
   private _subscription: Subscription;
+  private _modeEnum = ModeEnum;
 
   constructor(public gridFailureDetailsSandbox: GridFailureDetailsSandbox, protected appState$: Store<store.State>) {
     super();
@@ -62,7 +63,7 @@
     });
     this.gridOptions.context = {
       ...this.gridOptions.context,
-      icons: { delete: true },
+      icons: { delete: !this.gridFailureDetailsSandbox.disableStationAttributes },
     };
     this._subscription = this.gridOptions.context.eventSubject.subscribe(event => {
       if (event.type === 'delete') {
@@ -70,6 +71,7 @@
       }
     });
   }
+
   public setLocation() {
     this.gridFailureDetailsSandbox.gridFailureDetailsFormState$.pipe(take(1)).subscribe(currentFormState => {
       if (currentFormState.value.branch === Globals.BUSINESS_RULE_FIELDS.branch.gas) {
@@ -110,6 +112,14 @@
     this.searchInput.nativeElement.value = '';
   }
 
+  public changeMode() {
+    this.gridFailureDetailsSandbox.gridFailureStations$.pipe(takeUntil(this._endSubscriptions$)).subscribe(() => {
+      this.gridFailureDetailsSandbox.disableStationAttributes
+        ? this.events$.next({ eventType: this._modeEnum.oldVersionMode })
+        : this.events$.next({ eventType: this._modeEnum.currentVersionMode });
+    });
+  }
+
   private _initialFailureLocationState() {
     combineLatest(this.gridFailureDetailsSandbox.gridFailureDetailsFormState$, this.gridFailureDetailsSandbox.gridFailureStations$)
       .pipe(
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts
index 1e354b8..168b70c 100644
--- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts
+++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.spec.ts
@@ -58,7 +58,7 @@
   });
 
   it('should loadStations via init()', () => {
-    const spy = spyOn(service as any, 'loadGridFailureStations');
+    const spy = spyOn(service as any, '_setBranchIds');
     service.gridFailureStations$ = of([new FailureStation()]);
     service.init();
     expect(spy).toHaveBeenCalled();
@@ -111,14 +111,7 @@
   it('should dispatch loadGridFailureVersion Action via loadGridFailureVersion(VsNo)', () => {
     service['gridFailureId'] = 'id';
     service.loadGridFailureVersion('1');
-    expect(appState.dispatch).toHaveBeenCalledTimes(3);
-  });
-
-  it('should dispatch loadGridFailureVersion Action via loadGridFailureVersion(VsNo)', () => {
-    service['gridFailureId'] = 'id';
-    service.maxVersionNumber = 2;
-    service.loadGridFailureVersion('1');
-    expect(appState.dispatch).toHaveBeenCalledTimes(3);
+    expect(appState.dispatch).toHaveBeenCalledTimes(4);
   });
 
   it('should clear form state when current change is canceled and form state is pristine', () => {
@@ -645,14 +638,12 @@
     expect((service as any)._selectedStation).toBe(null);
   });
 
-  // it('should delete station', () => {
-  //   const stationId = 'xx';
-  //   (service as any)._gridFailureDetailId = 'x';
-  //   service.deleteGridFailureStation(stationId);
-  //   expect(appState.dispatch).toHaveBeenCalledWith(
-  //     gridFailureActions.deleteGridFailureStation({ gridFailureDetailId: (service as any)._gridFailureDetailId, stationId: stationId })
-  //   );
-  // });
+  it('should delete station', () => {
+    const stationId = 'xx';
+    (service as any)._gridFailureDetailId = 'x';
+    service.deleteGridFailureStation(stationId);
+    expect(appState.dispatch).toHaveBeenCalled();
+  });
 
   it('should dispatch loadGridFailureDistributionGroups Action via loadGridFailureDistributionGroups(id)', () => {
     service.loadGridFailureDistributionGroups('id');
diff --git a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.ts b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.ts
index de18fc1..21d1936 100644
--- a/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.ts
+++ b/projects/grid-failure-information-app/src/app/pages/grid-failure/grid-failure-details/grid-failure-details.sandbox.ts
@@ -116,7 +116,6 @@
   public showCompleteButton: boolean = false;
   public gridFailureAddress: models.FailureAddress;
   public gridFailureStations: FailureStation[] = [];
-  private _stations: Array<models.FailureStation> = new Array<models.FailureStation>();
 
   public gridFailureDistributionGroups$: Observable<models.DistributionGroup[]> = this.appState$.select(store.getGridFailureDistributionGroupsData);
   public gridFailureDistributionGroupsLoading$: Observable<boolean> = this.appState$.select(store.getGridFailureDistributionGroupsLoading);
@@ -128,6 +127,7 @@
   private _gridFailureId: string;
   private _selectedStation: models.FailureStation;
   private _assignedDistributionGroups: Array<models.DistributionGroup>;
+  private _stations: Array<models.FailureStation> = new Array<models.FailureStation>();
 
   constructor(
     protected appState$: Store<store.State>,
@@ -146,11 +146,10 @@
       this.gridFailureStations = stations;
       // set coords if first element was inserted
       !!stations.length && this.latLonMapping({ longitude: stations[0].longitude, latitude: stations[0].latitude });
-
-      this._calculatePolygon(stations);
+      if (this.gridFailureStations && !!this.gridFailureStations.length) {
+        this._calculatePolygon(this.gridFailureStations.map(station => station.id));
+      }
     });
-
-    this.loadGridFailureStations(this._gridFailureId);
   }
 
   public loadGridFailure(gridFailureId: string): void {
@@ -203,13 +202,7 @@
   }
 
   public loadGridFailureVersion(versionNumber: string): void {
-    if (!!versionNumber && +versionNumber === this.maxVersionNumber) {
-      //necessary because of not-versioned publication properties
-      this.appState$.dispatch(gridFailureActions.loadGridFailureDetail({ payload: this._gridFailureId }));
-    } else {
-      this.appState$.dispatch(gridFailureActions.loadGridFailureVersion({ gridFailureId: this._gridFailureId, versionNumber: +versionNumber }));
-    }
-    this.loadGridFailureBranches();
+    this.appState$.dispatch(gridFailureActions.loadGridFailureStations({ payload: this._gridFailureId }));
     if (+versionNumber < this.maxVersionNumber) {
       this.appState$.dispatch(new DisableAction(FORM_ID));
       this.disableStationAttributes = true;
@@ -217,6 +210,14 @@
       this.appState$.dispatch(new EnableAction(FORM_ID));
       this.disableStationAttributes = false;
     }
+    if (!!versionNumber && +versionNumber === this.maxVersionNumber) {
+      //necessary because of not-versioned publication properties
+      this.appState$.dispatch(gridFailureActions.loadGridFailureDetail({ payload: this._gridFailureId }));
+    } else {
+      this.appState$.dispatch(gridFailureActions.loadGridFailureVersion({ gridFailureId: this._gridFailureId, versionNumber: +versionNumber }));
+    }
+    this.loadGridFailureBranches();
+    this._setGridFailureStations();
   }
   public setGridFailureId(gridFailureId: string) {
     this._gridFailureId = gridFailureId;
@@ -345,6 +346,7 @@
       if (!formState.value || !formState.controls) return;
 
       this.currentFormState = formState;
+      this.loadGridFailureStations(this._gridFailureId);
       this._showButtonsByState(formState.value.statusIntern);
       this._setDynamicRequired();
 
@@ -482,11 +484,7 @@
 
   public stationValueConverter: NgrxValueConverter<any | null, string | null> = stationToStationDescriptionConverter;
 
-  private _calculatePolygon(stations: models.FailureStation[]) {
-    let stationIds: string[] = [];
-    stations.forEach(station => {
-      stationIds.push(station.id);
-    });
+  private _calculatePolygon(stationIds: string[]) {
     this.appState$.dispatch(gridFailureActions.loadGridFailurePolygon({ payload: stationIds }));
   }
 
@@ -632,15 +630,14 @@
     }
 
     this.gridFailureStations = [...this.gridFailureStations, this._selectedStation];
-    !!this.gridFailureStations.length &&
+
+    if (this.gridFailureStations && !!this.gridFailureStations.length) {
       this.latLonMapping({ longitude: this.gridFailureStations[0].longitude, latitude: this.gridFailureStations[0].latitude });
 
-    this._calculatePolygon(this.gridFailureStations);
-    this.appState$.dispatch(new SetValueAction(this.currentFormState.controls.stationIds.id, box(this.gridFailureStations.map(station => station.id))));
-    this.appState$.dispatch(new MarkAsDirtyAction(this.currentFormState.id));
-
-    this.latLonMapping({ longitude: this.gridFailureStations[0].longitude, latitude: this.gridFailureStations[0].latitude });
-
+      this._calculatePolygon(this.gridFailureStations.map(station => station.id));
+      this.appState$.dispatch(new SetValueAction(this.currentFormState.controls.stationIds.id, box(this.gridFailureStations.map(station => station.id))));
+      this.appState$.dispatch(new MarkAsDirtyAction(this.currentFormState.id));
+    }
     this._selectedStation = null;
   }
 
@@ -654,9 +651,10 @@
 
     this.appState$.dispatch(new MarkAsDirtyAction(this.currentFormState.id));
 
-    !!this.gridFailureStations.length &&
+    if (this.gridFailureStations && !!this.gridFailureStations.length) {
       this.latLonMapping({ longitude: this.gridFailureStations[0].longitude, latitude: this.gridFailureStations[0].latitude });
-    this._calculatePolygon(this.gridFailureStations);
+      this._calculatePolygon(this.gridFailureStations.map(station => station.id));
+    }
   }
 
   private _disableAddressControls(): void {
@@ -759,6 +757,7 @@
     this.appState$.dispatch(new SetValueAction(FORM_ID, INITIAL_STATE.value));
     this.appState$.dispatch(new ResetAction(FORM_ID));
     this.appState$.dispatch(new EnableAction(FORM_ID));
+    this.disableStationAttributes = false;
     this._gridFailureId = null;
     this.maxVersionNumber = null;
     this._assignedDistributionGroups = null;
@@ -770,6 +769,15 @@
       !!this.currentFormState.controls && !!this.currentFormState.controls.branchId && this.currentFormState.controls.branchId.value !== this.noBranchId;
   }
 
+  private _setGridFailureStations() {
+    this.gridFailureStations$.pipe(take(1), takeUntil(this._endSubscriptions$)).subscribe(stations => {
+      this.gridFailureStations = stations;
+      if (this.gridFailureStations && !!this.gridFailureStations.length) {
+        this._calculatePolygon(this.gridFailureStations.map(station => station.id));
+      }
+    });
+  }
+
   private _showButtonsByState(state: string): void {
     this.showQualifyButton = false;
     this.showStornoButton = false;
diff --git a/projects/grid-failure-information-app/src/app/shared/components/cell-renderer/icon-cell-renderer/icon-cell-renderer.component.ts b/projects/grid-failure-information-app/src/app/shared/components/cell-renderer/icon-cell-renderer/icon-cell-renderer.component.ts
index 65bf1c0..0e0f6ac 100644
--- a/projects/grid-failure-information-app/src/app/shared/components/cell-renderer/icon-cell-renderer/icon-cell-renderer.component.ts
+++ b/projects/grid-failure-information-app/src/app/shared/components/cell-renderer/icon-cell-renderer/icon-cell-renderer.component.ts
@@ -78,6 +78,12 @@
           this.editIcon = false && !this.readonlyIcon;
           this.removeIcon = true;
           break;
+        case this._modeEnum.oldVersionMode:
+          this.deleteIcon = false;
+          break;
+        case this._modeEnum.currentVersionMode:
+          this.deleteIcon = true;
+          break;
         default:
           break;
       }
diff --git a/projects/grid-failure-information-app/src/app/shared/constants/enums.ts b/projects/grid-failure-information-app/src/app/shared/constants/enums.ts
index c9f138b..1e310b7 100644
--- a/projects/grid-failure-information-app/src/app/shared/constants/enums.ts
+++ b/projects/grid-failure-information-app/src/app/shared/constants/enums.ts
@@ -52,6 +52,8 @@
   InitialMode = 'initialMode',
   OverviewTableSelectionMode = 'overviewTableSelectionMode',
   CondensationTableSelectionMode = 'condensationTableSelectionMode',
+  oldVersionMode = 'oldVersionMode',
+  currentVersionMode = 'currentVersionMode',
 }
 
 export enum EventTypeEnum {
diff --git a/projects/grid-failure-information-app/src/app/shared/store/effects/grid-failures.effect.ts b/projects/grid-failure-information-app/src/app/shared/store/effects/grid-failures.effect.ts
index cbfb53e..5af4a14 100644
--- a/projects/grid-failure-information-app/src/app/shared/store/effects/grid-failures.effect.ts
+++ b/projects/grid-failure-information-app/src/app/shared/store/effects/grid-failures.effect.ts
@@ -97,7 +97,6 @@
         return this._apiClient
           .getGridFailureVersion(action.gridFailureId, action.versionNumber)
           .map((gridFailureVersion: GridFailure) => {
-            console.log(gridFailureVersion);
             return gridFailureActions.loadGridFailureVersionSuccess({ payload: gridFailureVersion });
           })