bugfix - rule update failure via GUI
[holmes/rule-management.git] / rulemgt-frontend / src / app / correlation-ruleInfo / ruleInfo.component.ts
1 /*
2  Copyright 2018 ZTE Corporation.
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 } from '@angular/core';
17 import { RuleModel } from '../correlation-ruleList/alarmRule';
18 import { ActivatedRoute, Params, Router } from '@angular/router';
19 import { AlarmRuleService } from '../correlation-ruleList/alarmRule.service';
20 import { ModalService } from '../correlation-modal/modal.service';
21 import { NgModel } from '@angular/forms';
22
23 declare var $: any;
24
25 @Component({
26   selector: 'RuleInfo',
27   templateUrl: './ruleInfo.component.html'
28 })
29 export class RuleInfo implements OnInit {
30   formModel: RuleModel;
31   queryRule: RuleModel;
32   id: number;
33   addBottonStatus: boolean;
34
35   constructor(private modalService: ModalService,
36     private route: ActivatedRoute,
37     private router: Router,
38     private alarmRuleService: AlarmRuleService) {
39   };
40
41
42
43   ngOnInit() {
44     this.formModel = {
45       ruleId: null,
46       ruleName: null,
47       description: null,
48       content: null,
49       createTime: null,
50       creator: null,
51       updateTime: null,
52       modifier: null,
53       enabled: null,
54       loopControlName: null
55     };
56     this.queryRule = {
57       ruleId: null,
58       ruleName: null,
59       description: null,
60       content: null,
61       createTime: null,
62       creator: null,
63       updateTime: null,
64       modifier: null,
65       enabled: 1,
66       loopControlName: ''
67     };
68
69     this.route.params.subscribe((params) => {
70
71       let id = params['id'];
72       if (typeof (id) == 'string') {
73         if (id.indexOf('&') == -1) {
74           $('#ruleNameInput').attr('disabled', true);
75           $('#saveLabel').hide();
76         } else {
77           this.addBottonStatus = false;
78           $('#saveLabel').hide();
79           $('#checkLabel').hide();
80           $('#updateLabel').hide();
81         }
82
83         let str = id.split('&');
84         this.getRuleInfo(str[0]);
85       } else if (typeof (id) != 'undefined') {
86         this.getRuleInfo(id);
87       } else {
88         $('#updateLabel').hide();
89       }
90       if (typeof (this.queryRule.enabled) === 'number') {
91         this.queryRule.enabled = '' + this.queryRule.enabled;
92       }
93     });
94
95     this.initImportDiv(this.queryRule);
96   }
97
98   update() {
99     this.queryRule.enabled = $('input:radio:checked').val();
100     if (!this.queryRule.content.startsWith('package ')) {
101       let msg = { title: 'exception_content_error', message: 'exception_package_error' };
102       this.modalService.announceOpenModal(msg);
103     } else {
104       this.alarmRuleService.checkContent(this.replacePkgName(this.queryRule.content))
105         .then(res => {
106           if (res.status == 200) {
107             this.alarmRuleService.updateRule(this.queryRule)
108               .then(res => {
109                 if (res.status == 200) {
110                   let msg = { title: 'modalTitleUpdate', message: 'message_update_rule_success' };
111                   this.modalService.announceOpenModal(msg);
112                   this.router.navigate(['alarmRule']);
113                 } else if (res.status == 499) {
114                   let msg = { title: 'modalTitleUpdate', message: 'message_exception_rule_fail' };
115                   this.modalService.announceOpenModal(msg);
116                 } else {
117                   let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
118                   this.modalService.announceOpenModal(msg);
119                 }
120               }).catch(
121               res => {
122                 let msg = { title: 'modalTitleUpdate', message: 'message_other_exception_rule_fail' };
123                 this.modalService.announceOpenModal(msg);
124               }
125               );
126           } else if (res.status == 499) {
127             let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
128             this.modalService.announceOpenModal(msg);
129             return false;
130           } else {
131             let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
132             this.modalService.announceOpenModal(msg);
133             return false;
134           }
135         })
136     }
137   }
138
139   onSubmit(obj: NgModel) {
140     this.save();
141   }
142
143   save() {
144     if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
145       let msg = { title: 'exception_content_error', message: 'exception_package_error' };
146       this.modalService.announceOpenModal(msg);
147     } else {
148       this.alarmRuleService.checkContent(this.queryRule.content).then(res => {
149         if (res.status == 200) {
150           this.alarmRuleService.save(this.queryRule).then(res => {
151             if (res.status == 200) {
152               let msg = { title: 'modalTitleDefault', message: 'message_add_rule_success' };
153               this.modalService.announceOpenModal(msg);
154               this.router.navigate(['alarmRule']);
155             } else if (res.status == 499) {
156               let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
157               this.modalService.announceOpenModal(msg);
158             } else {
159               let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
160               this.modalService.announceOpenModal(msg);
161             }
162
163           }).catch(error => {
164             let msg = { title: 'modalTitleDefault', message: 'message_rule_name_repeat_error' };
165             this.modalService.announceOpenModal(msg);
166           });
167           return true;
168         } else if (res.status == 499) {
169           let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
170           this.modalService.announceOpenModal(msg);
171           return false;
172         } else {
173           let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
174           this.modalService.announceOpenModal(msg);
175           return false;
176         }
177       })
178     }
179   }
180
181   getRuleInfo(id: string): void {
182     this.route.params.switchMap((params: Params) => this.alarmRuleService.search(id))
183       .subscribe(rule => {
184         rule[0].enabled = '' + rule[0].enabled;
185         this.queryRule = rule[0]
186       })
187   }
188
189   checkContent(judge: string): void {
190     if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
191       let msg = { title: 'exception_content_error', message: 'exception_package_error' };
192       this.modalService.announceOpenModal(msg);
193     } else {
194       this.alarmRuleService.checkContent(this.queryRule.content)
195         .then(res => {
196           if (res.status == 200) {
197             let msg = { 'title': 'modalTitleCheck', message: 'message_checkContent_rule_success' };
198             this.modalService.announceOpenModal(msg);
199           } else if (res.status == 499) {
200             let msg = { 'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error' };
201             this.modalService.announceOpenModal(msg);
202             return false;
203           } else {
204             let msg = { 'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail' };
205             this.modalService.announceOpenModal(msg);
206             return false;
207           }
208         });
209     }
210   }
211
212   switch(): void {
213     // alert(this.queryRule.enabled);
214     this.queryRule.enabled == '0' ? this.queryRule.enabled = '1' : this.queryRule.enabled = '0';
215   }
216
217
218   initUpload(queryRule: RuleModel): void {
219     $('#fileName').text('');
220     $('#importFailTip').addClass('hide_panel');
221     $('#fileupload').fileupload({
222
223       dropZone: $('#dropzone'),
224       maxNumberOfFiles: 1,
225       maxChunkSize: 20000000,
226       autoUpload: false,
227       add: function (e, data) {
228         let fileName = data.files[0].name;
229
230         $('#importFailTip').addClass('hide_panel');
231         let suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length)
232
233         if (suffix != '.txt') {
234           $('#importFailTip').removeClass('hide_panel');
235           return suffix;
236         }
237
238         $('#bar').css('width', '0%');
239         $('#persent').text('0%');
240         $('#fileName').text(fileName);
241         $('#fileremove').attr('disabled', false);
242         $('#filesubmit').attr('disabled', false);
243         $('#filesubmit').click(function () {
244           this.file = data.files[0]
245           var reader = new FileReader();
246           reader.readAsText(this.file);
247           reader.onload = function (data) {
248             queryRule.content = this.result;
249           }
250           $('#fileremove').click();
251           $('#importDiv').hide();
252           e.stopPropagation();
253         });
254
255         $('#fileremove').click(function () {
256           $('#bar').css('width', '0%');
257           $('#persent').text('0%');
258           $('#fileName').text('');
259           $('#filesubmit').attr('disabled', true);
260           $('#fileremove').attr('disabled', true);
261         });
262       },
263       done: function (e, data) {
264
265       },
266       fail: function (e, resp) {
267       },
268       always: function (e, data) {
269         $('.progress').removeClass('active');
270         $('#bar').css('width', '100%');
271         $('#persent').text('100%');
272       },
273       progressall: function (e, data) {
274       }
275     });
276   }
277
278   initImportDiv(queryRule: RuleModel): void {
279     this.initUpload(queryRule);
280     let importDiv = $('#importDiv');
281     $(function (arg) {
282       $('#importBtn').click(function (e) {
283         e.preventDefault();
284         $('[data-toggle="tooltip"]').tooltip();
285         $('#importFailTip').addClass('hide_panel');
286         showDiv();
287         $(document).one('click', function (e) {
288           $(importDiv).hide();
289         });
290         e.stopPropagation();
291       });
292       $(importDiv).click(function (e) {
293         e.stopPropagation();
294       });
295       $('#filesubmit').attr('disabled', true);
296       $('#fileremove').attr('disabled', true);
297     });
298
299     function showDiv() {
300       $('#bar').css('width', '0%');
301       $('#persent').text('0%');
302       $(importDiv).fadeIn();
303     }
304   }
305
306   replacePkgName (content: string): string {
307     let reg = /(package\s+[\w\d\.]+)(\s.|;).*/;
308     let groups = reg.exec(content);
309     let pkgName = groups[1];
310     if (pkgName == null) {
311       return content;
312     }
313     return content.replace(pkgName, pkgName + new Date().getTime());
314   }
315 }