summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/searchMembersModal/members-table/members-table.service.ts
blob: 5b9cd39a24c278c068571462bc5c2a8b9a23fdd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {Injectable} from "@angular/core";
import {VnfMember} from "../../../models/VnfMember";
import {CustomTableColumnDefinition} from "./members-table.component";
import {NgRedux} from "@angular-redux/store";
import {AppState} from "../../../store/reducers";
import {createRelatedVnfMemberInstance} from "../../../storeUtil/utils/relatedVnfMember/relatedVnfMember.actions";
import * as _ from 'lodash';
import {DataFilterPipe} from "../../../pipes/dataFilter/data-filter.pipe";
import {MemberTableRowModel} from "./member-table-row.model";

@Injectable()
export class MembersTableService {
  allMemberStatusMap : { [key:string]: MemberTableRowModel; };
  filteredMembers :  VnfMember[];
  allCheckboxAreSelected : boolean;
  numberOfNotHideVnfMembers : number;
  numberOfSelectedVnfMembers : number;
  numberOfSelectedAndNotHideVnfMembers : number;

  constructor(private _store: NgRedux<AppState>, private dataFilter: DataFilterPipe){
    this.resetAll();
  }

   filterUsedVnfMembers = (serviceModelId: string, result: VnfMember[]): VnfMember[] => {
    const allMembersMap =  _.keyBy(result as VnfMember[], 'instanceId');
    const vnfGroupsData = this._store.getState().service.serviceInstance[serviceModelId].vnfGroups;
    const vnfMembersArr = _.flatMap(vnfGroupsData).map((vnfGroup) =>vnfGroup.vnfs );
    for( let vnf of vnfMembersArr ){
      for(let member in vnf){
        delete allMembersMap[member];
      }
    }
    return _.flatMap(allMembersMap);
  };

  updateAmountsAndCheckAll = () : void => {
    this.numberOfSelectedVnfMembers = this.calculateSelectedVnfMembers();
    this.numberOfNotHideVnfMembers = this.calculateNotHideVnfMembers();
    this.numberOfSelectedAndNotHideVnfMembers = this.calculateSelectedAndNotHide();
    this.allCheckboxAreSelected = this.numberOfNotHideVnfMembers > 0 && this.numberOfNotHideVnfMembers === this.numberOfSelectedAndNotHideVnfMembers;
  };

  resetAll = () : void => {
    this.allMemberStatusMap = {};
    this.filteredMembers = [];
    this.numberOfSelectedVnfMembers = 0;
    this.numberOfNotHideVnfMembers = 0;
    this.numberOfSelectedAndNotHideVnfMembers = 0;
    this.allCheckboxAreSelected = false;
  };

  changeAllCheckboxStatus = (status : boolean) : void =>{
    for(const member of this.filteredMembers){
        this.allMemberStatusMap[member.instanceId].isSelected = status;
      }
    this.updateAmountsAndCheckAll();
  };

  changeCheckboxStatus = (vnfInstanceId : string ) : void =>{
    this.allMemberStatusMap[vnfInstanceId].isSelected = !this.allMemberStatusMap[vnfInstanceId].isSelected;
    this.updateAmountsAndCheckAll();
  };

  /************************************************
   iterate over all current vnf members:
   1) if vnf member is selected then update REDUX store
   2) if vnf member is not selected then delete member
   @allMemberStatusMap: current vnf member status
   @vnfGroupStoreKey: vnf group store key
   @serviceId: service model id
   ************************************************/
  setMembers = (data : {serviceId : string, vnfGroupStoreKey : string}) : void =>{
    let tmpMembers = this.allMemberStatusMap;
    for(let key in tmpMembers){
      if(tmpMembers[key].isSelected){
        this._store.dispatch(createRelatedVnfMemberInstance( data.vnfGroupStoreKey, data.serviceId, tmpMembers[key]));
      }
    }
  };

  filterMembers(searchStr: string): void {
    const keys: string[][] =  MembersTableService.getDataKeys();
    this.filteredMembers = this.dataFilter.transform(_.values(this.allMemberStatusMap), searchStr || '', keys);
    this.updateAmountsAndCheckAll();
  }

  /************************************
   generate  vnf member data for select/ unselect rows
   ************************************/
  static generateAllMembersStatus(tableData : VnfMember[]) : { [key:string]: MemberTableRowModel; }{

    tableData.map((vnf) => {
      vnf['isSelected'] = false
    });
    return _.keyBy(tableData as MemberTableRowModel[], 'instanceId');
  }


  static sortVnfMembersByName(list : VnfMember[], keyName : string) :VnfMember[]{
    if(!_.isNil(list) && !_.isNil(keyName)) {
      return list.sort(function(itemA, itemB) { return itemA[keyName]- itemB[keyName];})
    }
    return [];

  }

  /********************************
   table columns headers and key's
   ********************************/
  static getHeaders() : CustomTableColumnDefinition[] {
    return  [
      {displayName: 'VNF instance name', key: ['instanceName']},
      {displayName: 'VNF version', key: ['modelInfo', 'modelVersion']},
      {displayName: 'VNF model name', key: ['modelInfo', 'modelName']},
      {displayName: 'Prov Status', key: ['provStatus']},
      {displayName: 'Service instance name', key: ['serviceInstanceName']},
      {displayName: 'Cloud Region', key: ['lcpCloudRegionId']},
      {displayName: 'Tenant Name', key: ['tenantName']}
    ];
  }

  static getDataKeys(): string[][]{
    const headers = MembersTableService.getHeaders();
    return headers.map((header)=> header.key).concat([['instanceId']],[['serviceInstanceId']]);
  }

  /*************************************************************************************
   calculate the number of selected vnf members - include not visible and visible rows
   @allMemberStatusMap: current vnf member status
   *************************************************************************************/
  calculateSelectedVnfMembers() : number {
    const flatObject = _.values(this.allMemberStatusMap);
    return  _.filter(flatObject, (item) => { if (item.isSelected) return item }).length;
  }

  /************************************************
   calculate the number of display vnf members
   @allMemberStatusMap: current vnf member status
   ************************************************/
  calculateNotHideVnfMembers() : number {
    return this.filteredMembers.length;
  }

  /************************************************
   calculate the number of display vnf members
   @allMemberStatusMap: current vnf member status
   ************************************************/
  calculateSelectedAndNotHide() : number {
    return  _.filter(this.filteredMembers, (item) => { if ( this.allMemberStatusMap[item.instanceId].isSelected) return item }).length;
  }


}