70c2bd7b427f5fa70cc77371316d3cbfc63ad0de
[dcaegen2/services.git] /
1 /*
2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8             http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 import {Component, OnInit, ViewChild, ElementRef} from '@angular/core';
17 import {DashboardApiService} from "src/app/core/services/dashboard-api.service";
18 import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
19 import {Template,newTemplate} from "src/app/core/models/template.model";
20 // Loading spinner
21 import {NgxSpinnerService} from "ngx-spinner";
22
23 // modal
24 import {NewTemplateModalComponent} from "./new-template-modal/new-template-modal.component";
25 import {EditTemplateModalComponent} from "./edit-template-modal/edit-template-modal.component";
26 import {AlertComponent} from "src/app/core/alert/alert.component";
27 // notify
28 import {ToastrNotificationService} from "src/app/core/services/toastr-notification.service";
29
30 @Component({
31   selector: 'app-template-list',
32   templateUrl: './template-list.component.html',
33   styleUrls: ['./template-list.component.css']
34 })
35 export class TemplateListComponent {
36   template_list: any = [];
37   templates: Template[] = [];
38   temps: Template[] = [];
39   Template_New: Template;
40   Template_Newbody: newTemplate;
41   dashboardDeteleModelShow = true;
42   loadingIndicator: boolean = true;
43   mesgNoData = {
44     emptyMessage: `
45       <div class="d-flex justify-content-center">
46         <div class="p-2">
47           <label class="dl-nodata">No Data</label>
48         </div>
49       </div>
50     `
51   };
52   @ViewChild("searchText") searchText: ElementRef;
53   constructor(
54     private modalService: NgbModal,
55     private dashboardApiService: DashboardApiService,
56     private spinner: NgxSpinnerService,
57     private notificationService: ToastrNotificationService,
58   ) {
59     setTimeout(() => {
60       this.loadingIndicator = false;
61     }, 5000);
62
63     this.initData().then(data => {
64       this.initTemplateList(this.template_list).then(
65         data => {
66           // for cache of datatable
67           this.temps = [...data];
68           this.templates = data;
69           setTimeout(() => {
70             this.spinner.hide();
71           }, 500);
72         }
73       );
74     });
75   }
76
77   ngOnInit() {
78     this.spinner.show();
79   }
80
81   async initData() {
82     this.template_list = [];
83     this.template_list = await this.getTemplateList();
84     this.Template_New = new Template();
85     this.Template_Newbody = new newTemplate();
86     return true;
87   }
88
89
90   getTemplateList() {
91     return this.dashboardApiService.getTemplateAll().toPromise();
92   }
93
94   async initTemplateList(template_list: []) {
95     var t: Template[] = [];
96     for (var i = 0; i < template_list.length; i++) {
97       let data = template_list[i];
98       let feed = {
99         id: data["id"],
100         name: data["name"],
101         submitted: data["submitted"],
102         body: data["body"],
103         note: data["note"],
104         topic: data["topic"],
105         designType: data["designType"],
106         display: data["display"],
107       };
108       t.push(feed);
109     }
110     return t;
111   }
112
113   newTemplateModal() {
114     let tips = "";
115     const modalRef = this.modalService.open(NewTemplateModalComponent, {
116       windowClass: "dl-md-modal templatess",
117       centered: true
118     });
119     this.Template_New = new Template();
120     this.Template_Newbody = new newTemplate();
121     modalRef.componentInstance.template = this.Template_Newbody;
122     modalRef.componentInstance.templatelist_length = this.template_list.length;
123     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
124       this.Template_Newbody = receivedEntry;
125       let name = this.Template_Newbody.name;
126       this.dashboardApiService
127         .createNewTemplate(this.Template_Newbody)
128         .subscribe(
129           res => {
130             if (res.statusCode == 200) {
131               this.Template_New = res.returnBody;
132               console.log(this.Template_New,"this.Template_New add Success");
133               this.template_list.push(this.Template_New);
134               this.template_list = [...this.template_list];
135               console.log(this.template_list, "this.template_list,inserted");
136               this.notificationService.success("SUCCESSFULLY_CREARED");
137             } else {
138               this.notificationService.error("FAILED_CREARED");
139             }
140             modalRef.close();
141           },
142           err => {
143             this.notificationService.error(err);
144             modalRef.close();
145           }
146         );
147     });
148   }
149
150   onActivate(event) {
151     const emitType = event.type;
152     if(emitType == "dblclick"){
153       console.log('Activate Event', event);
154       let id = event.row.id;
155       this.editTemplateModal(id);
156     }
157
158   }
159
160   editTemplateModal(id) {
161     const index = this.template_list.findIndex(t => t.id === id);
162     // const name = this.template_list[index].name;
163     const modalRef = this.modalService.open(EditTemplateModalComponent, {
164       windowClass: "dl-md-modal templatess",
165       centered: true
166     });
167     modalRef.componentInstance.edittemplate = this.template_list[index];
168     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
169       const name = receivedEntry.name;
170       this.Template_New = receivedEntry;
171       this.dashboardApiService
172         .updateNewTemplate(this.Template_New)
173         .subscribe(
174           res => {
175
176             if (res.statusCode == 200) {
177               this.template_list[index] = this.Template_New;
178               this.template_list = [...this.template_list];
179               console.log(this.template_list, "this.template_list,update");
180               this.notificationService.success("SUCCESSFULLY_UPDATED");
181             } else {
182               this.notificationService.error("FAILED_UPDATED");
183             }
184             modalRef.close();
185           },
186           err => {
187             this.notificationService.error(err);
188             modalRef.close();
189           }
190         );
191     })
192   }
193
194   deleteTemplateModel(id: number) {
195     const index = this.template_list.findIndex(t => t.id === id);
196     const modalRef = this.modalService.open(AlertComponent, {
197       size: "sm",
198       centered: true
199     });
200     const name = this.template_list[index].name;
201     // modalRef.componentInstance.dashboardDeteleModelShow = this.dashboardDeteleModelShow;
202     modalRef.componentInstance.message = "ARE_YOU_SURE_DELETE";
203     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
204       // Delete database
205       this.dashboardApiService.DeleteTemplate(id).subscribe(
206         res => {
207           console.log(res,"template detele res");
208           console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
209           if (JSON.stringify(res).length<=2) {
210             console.log("Success deleted template");
211             this.template_list.splice(index, 1);
212             this.template_list = [...this.template_list];
213             this.notificationService.success("SUCCESSFULLY_DELETED");
214           } else {
215             console.log("Fail deleted template");
216             this.notificationService.error("FAILED_DELETED");
217           }
218
219           modalRef.close();
220         },
221         err => {
222           this.notificationService.error(err);
223           modalRef.close();
224         }
225       );
226     });
227
228   }
229
230   deployTemplate(id: number) {
231     const index = this.template_list.findIndex(t => t.id === id);
232     const name = this.template_list[index].name;
233     const body = this.template_list[index];
234     this.spinner.show();
235     this.dashboardApiService.deployTemplateKibana(id, body).subscribe(
236       res => {
237         this.spinner.hide();
238         console.log(res,"template deploy res");
239         console.log(JSON.stringify(res).length,"JSON.stringify(res).length");
240         if (JSON.stringify(res).length<=2) {
241           this.notificationService.success("Deploy_SUCCESSFULLY");
242         } else {
243           this.notificationService.error("Deploy_FAILED");
244         }
245       },
246       err => {
247         this.spinner.hide();
248         this.notificationService.error(err);
249       }
250     );
251   }
252
253   updateFilter(searchValue) {
254     const val = searchValue.toLowerCase();
255     // filter our data
256     const temps = this.temps.filter(function (d) {
257       return d.name.toLowerCase().indexOf(val) !== -1 || !val;
258     });
259     // update the rows
260     this.template_list = temps;
261   }
262
263
264 }