Data exposure service management pages
[dcaegen2/services.git] / components / datalake-handler / admin / src / src / app / views / data-exposure / data-exposure.component.ts
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2020 QCT
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  *
23  * @author Ekko Chang
24  *
25  */
26
27 import { Component, ViewChild, ElementRef, OnInit } from "@angular/core";
28 import { RestApiService } from "src/app/core/services/rest-api.service";
29 import { AdminService } from "src/app/core/services/admin.service";
30 import { DataService } from "src/app/core/models/data-service.model";
31 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
32
33 // Notify
34 import { ToastrNotificationService } from "src/app/shared/components/toastr-notification/toastr-notification.service";
35
36 // Loading spinner
37 import { NgxSpinnerService } from "ngx-spinner";
38
39 import { AlertComponent } from "src/app/shared/components/alert/alert.component";
40 import { map, mergeMap } from "rxjs/operators";
41 import { forkJoin, from } from "rxjs";
42
43 // Modal
44 import { ModalComponent } from "src/app/shared/modules/modal/modal.component";
45 import { ModalContentData } from "src/app/shared/modules/modal/modal.data";
46 import { DeModalComponent } from "src/app/views/data-exposure/de-modal/de-modal.component";
47
48 @Component({
49   selector: "app-data-exposure",
50   templateUrl: "./data-exposure.component.html",
51   styleUrls: ["./data-exposure.component.css"]
52 })
53 export class DataExposureComponent implements OnInit {
54   datas: Array<DataService> = [];
55   t_temp: Array<DataService> = [];
56   columns: Array<any> = []; // column of table
57
58   @ViewChild("searchText") searchText: ElementRef;
59   constructor(
60     private restApiService: RestApiService,
61     private modalService: NgbModal,
62     private notificationService: ToastrNotificationService,
63     private spinner: NgxSpinnerService,
64     private adminService: AdminService
65   ) {
66     // Set page title
67     this.adminService.setTitle("SIDEBAR.DATAEXPOSURE");
68   }
69
70   ngOnInit() {
71     this.spinner.show();
72     let t_data: Array<DataService> = [];
73
74     const get_ds = this.restApiService.getAllDataService().pipe(
75       mergeMap(dss => from(dss)),
76       map(data => {
77         t_data.push(data);
78       })
79     );
80
81     forkJoin(get_ds).subscribe(data => {
82       this.columns = this.initColumn();
83       this.datas = t_data;
84       this.t_temp = [...this.datas];
85       this.updateFilter(this.searchText.nativeElement.value);
86       setTimeout(() => {
87         this.spinner.hide();
88       }, 500);
89     });
90   }
91
92   initColumn() {
93     let t_columns: Array<any> = [];
94
95     t_columns = [
96       {
97         headerName: "NAME",
98         width: "420",
99         sortable: true,
100         dataIndex: "id"
101       },
102       {
103         headerName: "DESCRIPTION",
104         width: "420",
105         sortable: true,
106         dataIndex: "note"
107       },
108       {
109         width: "2",
110         iconButton: "cog",
111         action: "edit"
112       },
113       {
114         width: "2",
115         iconButton: "trash",
116         action: "delete"
117       }
118     ];
119
120     return t_columns;
121   }
122
123   updateFilter(searchValue: string) {
124     const val = searchValue.toLowerCase();
125
126     // filter our data
127     const temp = this.t_temp.filter(data => {
128       return data.id.toLowerCase().indexOf(val) !== -1 || !val;
129     });
130
131     // update the rows
132     this.datas = temp;
133   }
134
135   btnTableAction(passValueArr: Array<any>) {
136     let action = passValueArr[0];
137     let id = passValueArr[1];
138
139     switch (action) {
140       case "edit":
141         this.openModal("edit", id);
142         break;
143       case "delete":
144         const modalRef = this.modalService.open(AlertComponent, {
145           size: "sm",
146           centered: true,
147           backdrop: "static"
148         });
149         modalRef.componentInstance.message = "ARE_YOU_SURE_DELETE";
150         modalRef.componentInstance.passEntry.subscribe(recevicedEntry => {
151           this.restApiService.deleteDataService(id).subscribe(
152             res => {
153               this.ngOnInit();
154               setTimeout(() => {
155                 this.notificationService.success("SUCCESSFULLY_DELETED");
156               }, 500);
157             },
158             err => {
159               this.notificationService.error(err);
160             }
161           );
162           modalRef.close();
163         });
164         break;
165     }
166   }
167
168   openModal(mode: string = "", id: number | string) {
169     const modalRef = this.modalService.open(ModalComponent, {
170       size: "lg",
171       centered: true,
172       backdrop: "static"
173     });
174
175     switch (mode) {
176       case "new":
177         let newDS: DataService;
178         let componentNew = new ModalContentData(DeModalComponent, newDS);
179
180         modalRef.componentInstance.title = "NEW_DATASERVICE";
181         modalRef.componentInstance.notice = "";
182         modalRef.componentInstance.mode = "new";
183         modalRef.componentInstance.component = componentNew;
184
185         modalRef.componentInstance.passEntry.subscribe((data: DataService) => {
186           newDS = Object.assign({}, data);
187           this.restApiService.addDataService(newDS).subscribe(
188             res => {
189               this.ngOnInit();
190               setTimeout(() => {
191                 this.notificationService.success("SUCCESSFULLY_CREARED");
192               }, 500);
193             },
194             err => {
195               this.notificationService.error(err);
196             }
197           );
198           modalRef.close();
199         });
200         break;
201       case "edit":
202         let index: number = this.datas.findIndex(data => data.id === id);
203         let editDS: DataService = this.datas[index];
204         let componentEdit = new ModalContentData(DeModalComponent, editDS);
205
206         modalRef.componentInstance.title = editDS.id;
207         modalRef.componentInstance.notice = "";
208         modalRef.componentInstance.mode = "edit";
209         modalRef.componentInstance.component = componentEdit;
210
211         modalRef.componentInstance.passEntry.subscribe((data: DataService) => {
212           editDS = Object.assign({}, data);
213           this.restApiService.updateDataService(editDS).subscribe(
214             res => {
215               this.ngOnInit();
216               setTimeout(() => {
217                 this.notificationService.success("SUCCESSFULLY_UPDATED");
218               }, 500);
219             },
220             err => {
221               this.notificationService.error(err);
222             }
223           );
224           modalRef.close();
225         });
226         break;
227     }
228   }
229 }