Changed the Style of 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.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
127           } else if (res.status == 499) {
128             let msg = {'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error'};
129             this.modalService.announceOpenModal(msg);
130             return false;
131           } else {
132             let msg = {'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail'};
133             this.modalService.announceOpenModal(msg);
134             return false;
135           }
136         })
137     }
138   }
139
140   onSubmit(obj: NgModel) {
141     this.save();
142   }
143
144   save() {
145     if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
146       let msg = {title: 'exception_content_error', message: 'exception_package_error'};
147       this.modalService.announceOpenModal(msg);
148     } else {
149       this.alarmRuleService.checkContent(this.queryRule.content).then(res => {
150         if (res.status == 200) {
151           this.alarmRuleService.save(this.queryRule).then(res => {
152             if (res.status == 200) {
153               let msg = {title: 'modalTitleDefault', message: 'message_add_rule_success'};
154               this.modalService.announceOpenModal(msg);
155               this.router.navigate(['alarmRule']);
156             } else if (res.status == 499) {
157               let msg = {title: 'modalTitleDefault', message: 'message_rule_name_repeat_error'};
158               this.modalService.announceOpenModal(msg);
159             } else {
160               let msg = {'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail'};
161               this.modalService.announceOpenModal(msg);
162             }
163
164           }).catch(error => {
165             let msg = {title: 'modalTitleDefault', message: 'message_rule_name_repeat_error'};
166             this.modalService.announceOpenModal(msg);
167           });
168           return true;
169         } else if (res.status == 499) {
170           let msg = {'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error'};
171           this.modalService.announceOpenModal(msg);
172           return false;
173         } else {
174           let msg = {'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail'};
175           this.modalService.announceOpenModal(msg);
176           return false;
177         }
178       })
179     }
180   }
181
182   getRuleInfo(id: string): void {
183     this.route.params.switchMap((params: Params) => this.alarmRuleService.search(id))
184       .subscribe(rule => {
185         rule[0].enabled = '' + rule[0].enabled;
186         this.queryRule = rule[0]
187       })
188   }
189
190   checkContent(judge: string): void {
191     if (!this.queryRule.content || !this.queryRule.content.startsWith('package ')) {
192       let msg = {title: 'exception_content_error', message: 'exception_package_error'};
193       this.modalService.announceOpenModal(msg);
194     } else {
195       this.alarmRuleService.checkContent(this.queryRule.content)
196         .then(res => {
197           if (res.status == 200) {
198             let msg = {'title': 'modalTitleCheck', message: 'message_checkContent_rule_success'};
199             this.modalService.announceOpenModal(msg);
200           } else if (res.status == 499) {
201             let msg = {'title': 'modalTitleCheck', message: 'message_rule_content_repeat_error'};
202             this.modalService.announceOpenModal(msg);
203             return false;
204           } else {
205             let msg = {'title': 'modalTitleCheck', message: 'message_other_exception_rule_fail'};
206             this.modalService.announceOpenModal(msg);
207             return false;
208           }
209         });
210     }
211   }
212
213   switch(): void {
214     // alert(this.queryRule.enabled);
215     this.queryRule.enabled == '0' ? this.queryRule.enabled = '1' : this.queryRule.enabled = '0';
216   }
217
218
219   initUpload(queryRule: RuleModel): void {
220     $('#fileName').text('');
221     $('#importFailTip').addClass('hide_panel');
222     $('#fileupload').fileupload({
223
224       dropZone: $('#dropzone'),
225       maxNumberOfFiles: 1,
226       maxChunkSize: 20000000,
227       autoUpload: false,
228       add: function (e, data) {
229         let fileName = data.files[0].name;
230
231         $('#importFailTip').addClass('hide_panel');
232         let suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length)
233
234         if (suffix != '.txt') {
235           $('#importFailTip').removeClass('hide_panel');
236           return suffix;
237         }
238
239         $('#bar').css('width', '0%');
240         $('#persent').text('0%');
241         $('#fileName').text(fileName);
242         $('#fileremove').attr('disabled', false);
243         $('#filesubmit').attr('disabled', false);
244         $('#filesubmit').click(function () {
245           this.file = data.files[0]
246           var reader = new FileReader();
247           reader.readAsText(this.file);
248           reader.onload = function (data) {
249             queryRule.content = this.result;
250           }
251           $('#fileremove').click();
252           $('#importDiv').hide();
253           e.stopPropagation();
254         });
255
256         $('#fileremove').click(function () {
257           $('#bar').css('width', '0%');
258           $('#persent').text('0%');
259           $('#fileName').text('');
260           $('#filesubmit').attr('disabled', true);
261           $('#fileremove').attr('disabled', true);
262         });
263       },
264       done: function (e, data) {
265
266       },
267       fail: function (e, resp) {
268       },
269       always: function (e, data) {
270         $('.progress').removeClass('active');
271         $('#bar').css('width', '100%');
272         $('#persent').text('100%');
273       },
274       progressall: function (e, data) {
275       }
276     });
277   }
278
279   initImportDiv(queryRule: RuleModel): void {
280     this.initUpload(queryRule);
281     let importDiv = $('#importDiv');
282     $(function (arg) {
283       $('#importBtn').click(function (e) {
284         e.preventDefault();
285         $('[data-toggle="tooltip"]').tooltip();
286         $('#importFailTip').addClass('hide_panel');
287         showDiv();
288         $(document).one('click', function (e) {
289           $(importDiv).hide();
290         });
291         e.stopPropagation();
292       });
293       $(importDiv).click(function (e) {
294         e.stopPropagation();
295       });
296       $('#filesubmit').attr('disabled', true);
297       $('#fileremove').attr('disabled', true);
298     });
299
300     function showDiv() {
301       $('#bar').css('width', '0%');
302       $('#persent').text('0%');
303       $(importDiv).fadeIn();
304     }
305   }
306
307 }